Update app.py
Browse files
app.py
CHANGED
|
@@ -2,64 +2,93 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import subprocess
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
model_path = "checkpoints/checkpoint.pt"
|
| 19 |
-
pads = "0,0,0,0"
|
| 20 |
-
|
| 21 |
if sample_mode == "reconstruction":
|
| 22 |
-
|
| 23 |
elif sample_mode == "cross":
|
| 24 |
-
|
| 25 |
else:
|
| 26 |
-
return
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
DATA_FLAGS = "--nframes 5 --nrefer 1 --image_size 128 --sampling_batch_size=32"
|
| 32 |
-
TFG_FLAGS =
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if os.path.exists(audio_path):
|
| 46 |
-
os.remove(audio_path)
|
| 47 |
-
if os.path.exists(video_path):
|
| 48 |
-
os.remove(video_path)
|
| 49 |
-
if os.path.exists(out_path):
|
| 50 |
-
os.remove(out_path)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
gr.Video(label="Video File"),
|
| 58 |
-
],
|
| 59 |
-
outputs="video",
|
| 60 |
-
description="Process Audio and Video with your Model",
|
| 61 |
-
allow_flagging=False # Disable flagging as output is a video
|
| 62 |
-
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 2 |
import os
|
| 3 |
import subprocess
|
| 4 |
|
| 5 |
+
# Define the paths where the input and output files will be stored
|
| 6 |
+
INPUT_AUDIO_PATH = "input_audio.wav"
|
| 7 |
+
INPUT_VIDEO_PATH = "input_video.mp4"
|
| 8 |
+
OUTPUT_VIDEO_PATH = "output_video.mp4"
|
| 9 |
+
MODEL_PATH = "checkpoints/checkpoint.pt"
|
| 10 |
+
|
| 11 |
+
# Sample mode configuration
|
| 12 |
+
SAMPLE_MODE = "cross" # Options: "cross" or "reconstruction"
|
| 13 |
+
PADS = "0,0,0,0"
|
| 14 |
+
GENERATE_FROM_FILELIST = 0
|
| 15 |
+
|
| 16 |
+
# Generate the appropriate flags based on the sample mode
|
| 17 |
+
def get_sample_flags(sample_mode):
|
|
|
|
|
|
|
|
|
|
| 18 |
if sample_mode == "reconstruction":
|
| 19 |
+
return "--sampling_input_type=first_frame --sampling_ref_type=first_frame"
|
| 20 |
elif sample_mode == "cross":
|
| 21 |
+
return "--sampling_input_type=gt --sampling_ref_type=gt"
|
| 22 |
else:
|
| 23 |
+
return None
|
| 24 |
+
|
| 25 |
+
# Function to run the model inference command
|
| 26 |
+
def generate_video(audio_file, video_file):
|
| 27 |
+
# Save uploaded files to disk
|
| 28 |
+
audio_file.save(INPUT_AUDIO_PATH)
|
| 29 |
+
video_file.save(INPUT_VIDEO_PATH)
|
| 30 |
+
|
| 31 |
+
sample_input_flags = get_sample_flags(SAMPLE_MODE)
|
| 32 |
+
if not sample_input_flags:
|
| 33 |
+
return "Error: sample_mode can only be 'cross' or 'reconstruction'"
|
| 34 |
+
|
| 35 |
+
# Build the command string
|
| 36 |
+
MODEL_FLAGS = (
|
| 37 |
+
"--attention_resolutions 32,16,8 --class_cond False --learn_sigma True "
|
| 38 |
+
"--num_channels 128 --num_head_channels 64 --num_res_blocks 2 "
|
| 39 |
+
"--resblock_updown True --use_fp16 True --use_scale_shift_norm False"
|
| 40 |
+
)
|
| 41 |
+
DIFFUSION_FLAGS = (
|
| 42 |
+
"--predict_xstart False --diffusion_steps 1000 --noise_schedule linear "
|
| 43 |
+
"--rescale_timesteps False"
|
| 44 |
+
)
|
| 45 |
+
SAMPLE_FLAGS = (
|
| 46 |
+
f"--sampling_seed=7 {sample_input_flags} --timestep_respacing ddim25 "
|
| 47 |
+
f"--use_ddim True --model_path={MODEL_PATH}"
|
| 48 |
+
)
|
| 49 |
DATA_FLAGS = "--nframes 5 --nrefer 1 --image_size 128 --sampling_batch_size=32"
|
| 50 |
+
TFG_FLAGS = (
|
| 51 |
+
"--face_hide_percentage 0.5 --use_ref=True --use_audio=True "
|
| 52 |
+
"--audio_as_style=True"
|
| 53 |
+
)
|
| 54 |
+
GEN_FLAGS = (
|
| 55 |
+
f"--generate_from_filelist {GENERATE_FROM_FILELIST} "
|
| 56 |
+
f"--video_path={INPUT_VIDEO_PATH} --audio_path={INPUT_AUDIO_PATH} "
|
| 57 |
+
f"--out_path={OUTPUT_VIDEO_PATH} --save_orig=False "
|
| 58 |
+
f"--face_det_batch_size 16 --pads {PADS} --is_voxceleb2=False"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
command = (
|
| 62 |
+
f"python your_model_script.py {MODEL_FLAGS} {DIFFUSION_FLAGS} "
|
| 63 |
+
f"{SAMPLE_FLAGS} {DATA_FLAGS} {TFG_FLAGS} {GEN_FLAGS}"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Run the command and wait for it to complete
|
| 67 |
+
process = subprocess.run(command, shell=True, capture_output=True, text=True)
|
| 68 |
+
|
| 69 |
+
if process.returncode != 0:
|
| 70 |
+
return f"Error: {process.stderr}"
|
| 71 |
+
|
| 72 |
+
# Return the generated video file
|
| 73 |
+
return OUTPUT_VIDEO_PATH
|
| 74 |
+
|
| 75 |
+
# Gradio interface
|
| 76 |
+
with gr.Blocks() as demo:
|
| 77 |
+
gr.Markdown("## Audio-Video Synthesis Model")
|
| 78 |
|
| 79 |
+
with gr.Row():
|
| 80 |
+
audio_input = gr.Audio(label="Upload Audio", type="file")
|
| 81 |
+
video_input = gr.Video(label="Upload Video", type="file")
|
| 82 |
+
|
| 83 |
+
output_video = gr.Video(label="Generated Video")
|
| 84 |
+
|
| 85 |
+
generate_button = gr.Button("Generate")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
generate_button.click(
|
| 88 |
+
fn=generate_video,
|
| 89 |
+
inputs=[audio_input, video_input],
|
| 90 |
+
outputs=output_video
|
| 91 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
demo.launch()
|