File size: 6,143 Bytes
206c178
 
 
 
 
 
55f2e5a
98c7ca6
206c178
 
 
 
cd8c6a9
206c178
 
 
 
 
 
 
45378e6
21c3587
206c178
 
 
 
 
45378e6
 
21c3587
 
 
45378e6
206c178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21c3587
206c178
 
 
 
 
 
 
 
 
 
 
 
 
 
21c3587
 
206c178
 
 
 
 
 
 
 
21c3587
206c178
 
 
 
45378e6
206c178
 
 
 
 
 
 
 
 
 
21c3587
 
206c178
 
 
45378e6
 
229302a
45378e6
 
 
206c178
21c3587
 
 
 
 
 
 
206c178
4717688
206c178
 
4717688
 
 
206c178
 
 
 
 
21c3587
45378e6
 
 
 
 
21c3587
 
 
 
 
206c178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03b0850
 
 
 
 
 
 
 
206c178
 
 
 
 
 
 
 
 
45378e6
21c3587
206c178
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import gradio as gr
from pathlib import Path
from scripts.inference import main
from omegaconf import OmegaConf
import argparse
from datetime import datetime
import subprocess
import os

CONFIG_PATH = Path("configs/unet/second_stage.yaml")
CHECKPOINT_PATH = Path("checkpoints/latentsync_unet.pt")

subprocess.run(["huggingface-cli", "download", "Hyathi/LatentSync", "--local-dir", "checkpoints", "--exclude", "*.git*", "README.md", "--token", os.environ["HF_TOKEN"]])

def process_video(
    video_path,
    audio_path,
    guidance_scale,
    inference_steps,
    seed,
    checkpoint_file,
    mask_file,
):
    # Create the temp directory if it doesn't exist
    output_dir = Path("./temp")
    output_dir.mkdir(parents=True, exist_ok=True)

    # Use selected checkpoint or fall back to default
    checkpoint_path = Path("checkpoints/unetFiles") / checkpoint_file if checkpoint_file else CHECKPOINT_PATH
    
    # Get mask path
    mask_path = Path("masks") / mask_file if mask_file else None

    # Convert paths to absolute Path objects and normalize them
    video_file_path = Path(video_path)
    video_path = video_file_path.absolute().as_posix()
    audio_path = Path(audio_path).absolute().as_posix()

    current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
    # Set the output path for the processed video
    output_path = str(
        output_dir / f"{video_file_path.stem}_{current_time}.mp4"
    )  # Change the filename as needed

    config = OmegaConf.load(CONFIG_PATH)

    config["run"].update(
        {
            "guidance_scale": guidance_scale,
            "inference_steps": inference_steps,
        }
    )

    # Parse the arguments
    args = create_args(video_path, audio_path, output_path, guidance_scale, seed, checkpoint_path, mask_path)

    try:
        result = main(
            config=config,
            args=args,
        )
        print("Processing completed successfully.")
        return output_path  # Ensure the output path is returned
    except Exception as e:
        print(f"Error during processing: {str(e)}")
        raise gr.Error(f"Error during processing: {str(e)}")


def create_args(
    video_path: str, audio_path: str, output_path: str, guidance_scale: float, seed: int, 
    checkpoint_path: Path, mask_path: Path
) -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument("--inference_ckpt_path", type=str, required=True)
    parser.add_argument("--video_path", type=str, required=True)
    parser.add_argument("--audio_path", type=str, required=True)
    parser.add_argument("--video_out_path", type=str, required=True)
    parser.add_argument("--guidance_scale", type=float, default=1.0)
    parser.add_argument("--seed", type=int, default=1247)
    parser.add_argument("--mask_path", type=str, required=False)

    return parser.parse_args(
        [
            "--inference_ckpt_path",
            checkpoint_path.absolute().as_posix(),
            "--video_path",
            video_path,
            "--audio_path",
            audio_path,
            "--video_out_path",
            output_path,
            "--guidance_scale",
            str(guidance_scale),
            "--seed",
            str(seed),
            "--mask_path",
            mask_path.absolute().as_posix() if mask_path else "",
        ]
    )

# Add this function to get checkpoint files
def get_checkpoint_files():
    unet_files_dir = Path("checkpoints/unetFiles")
    if not unet_files_dir.exists():
        return []
    return [f.name for f in unet_files_dir.glob("*.pt")]

# Add this function to get mask files
def get_mask_files():
    masks_dir = Path("masks")
    if not masks_dir.exists():
        return []
    return [f.name for f in masks_dir.glob("*.png")]  # Assuming masks are PNG files

# Create Gradio interface
with gr.Blocks(title="SoundImage") as demo:
    gr.Markdown(
        """
    # SoundImage: Audio Conditioned Video Generation
    Upload a video and audio file to process with SoundImage model.
    
    """
    )

    with gr.Row():
        with gr.Column():
            # Add checkpoint and mask selectors
            checkpoint_dropdown = gr.Dropdown(
                choices=get_checkpoint_files(),
                label="Select Checkpoint",
                value=get_checkpoint_files()[0] if get_checkpoint_files() else None
            )
            mask_dropdown = gr.Dropdown(  # New dropdown for masks
                choices=get_mask_files(),
                label="Select Mask",
                value=get_mask_files()[0] if get_mask_files() else None
            )
            video_input = gr.Video(label="Input Video")
            audio_input = gr.Audio(label="Input Audio", type="filepath")

            with gr.Row():
                guidance_scale = gr.Slider(
                    minimum=0.1,
                    maximum=3.0,
                    value=1.0,
                    step=0.1,
                    label="Guidance Scale",
                )
                inference_steps = gr.Slider(
                    minimum=1, maximum=50, value=20, step=1, label="Inference Steps"
                )

            with gr.Row():
                seed = gr.Number(value=1247, label="Random Seed", precision=0)

            process_btn = gr.Button("Process Video")

        with gr.Column():
            video_output = gr.Video(label="Output Video")

            # gr.Examples(
            #     examples=[
            #         ["assets/demo1_video.mp4", "assets/demo1_audio.wav"],
            #         ["assets/demo2_video.mp4", "assets/demo2_audio.wav"],
            #         ["assets/demo3_video.mp4", "assets/demo3_audio.wav"],
            #     ],
            #     inputs=[video_input, audio_input],
            # )

    process_btn.click(
        fn=process_video,
        inputs=[
            video_input,
            audio_input,
            guidance_scale,
            inference_steps,
            seed,
            checkpoint_dropdown,
            mask_dropdown,  # Add mask_dropdown to inputs
        ],
        outputs=video_output,
    )

if __name__ == "__main__":
    demo.launch(inbrowser=True, share=True)