Spaces:
Running
on
Zero
Running
on
Zero
Update hf_gradio_app.py
Browse files- hf_gradio_app.py +24 -22
hf_gradio_app.py
CHANGED
|
@@ -73,34 +73,31 @@ with torch.inference_mode():
|
|
| 73 |
pipeline = VideoPipeline(vae=vae, reference_net=reference_net, diffusion_net=diffusion_net, scheduler=noise_scheduler, image_proj=image_proj)
|
| 74 |
pipeline.to(device=device, dtype=weight_dtype)
|
| 75 |
|
| 76 |
-
def process_audio(file_path):
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
print(f"Processed audio saved at: {output_path}")
|
| 93 |
-
|
| 94 |
-
# Return the path for reference (optional)
|
| 95 |
-
return output_path
|
| 96 |
|
| 97 |
@torch.inference_mode()
|
| 98 |
def generate(input_video, input_audio, seed, progress=gr.Progress(track_tqdm=True)):
|
| 99 |
|
| 100 |
is_shared_ui = True if "fffiloni/MEMO" in os.environ['SPACE_ID'] else False
|
| 101 |
-
|
| 102 |
if is_shared_ui:
|
| 103 |
-
|
|
|
|
| 104 |
print(f"Processed file was stored temporarily at: {input_audio}")
|
| 105 |
|
| 106 |
resolution = 512
|
|
@@ -125,6 +122,11 @@ def generate(input_video, input_audio, seed, progress=gr.Progress(track_tqdm=Tru
|
|
| 125 |
os.makedirs(cache_dir, exist_ok=True)
|
| 126 |
input_audio = resample_audio(input_audio, os.path.join(cache_dir, f"{os.path.basename(input_audio).split('.')[0]}-16k.wav"))
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
audio_emb, audio_length = preprocess_audio(
|
| 129 |
wav_path=input_audio,
|
| 130 |
num_generated_frames_per_clip=num_generated_frames_per_clip,
|
|
|
|
| 73 |
pipeline = VideoPipeline(vae=vae, reference_net=reference_net, diffusion_net=diffusion_net, scheduler=noise_scheduler, image_proj=image_proj)
|
| 74 |
pipeline.to(device=device, dtype=weight_dtype)
|
| 75 |
|
| 76 |
+
def process_audio(file_path, temp_dir):
|
| 77 |
+
# Load the audio file
|
| 78 |
+
audio = AudioSegment.from_file(file_path)
|
| 79 |
+
|
| 80 |
+
# Check and cut the audio if longer than 4 seconds
|
| 81 |
+
max_duration = 4 * 1000 # 4 seconds in milliseconds
|
| 82 |
+
if len(audio) > max_duration:
|
| 83 |
+
audio = audio[:max_duration]
|
| 84 |
+
|
| 85 |
+
# Save the processed audio in the temporary directory
|
| 86 |
+
output_path = os.path.join(temp_dir, "trimmed_audio.wav")
|
| 87 |
+
audio.export(output_path, format="wav")
|
| 88 |
+
|
| 89 |
+
# Return the path to the trimmed file
|
| 90 |
+
print(f"Processed audio saved at: {output_path}")
|
| 91 |
+
return output_path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
@torch.inference_mode()
|
| 94 |
def generate(input_video, input_audio, seed, progress=gr.Progress(track_tqdm=True)):
|
| 95 |
|
| 96 |
is_shared_ui = True if "fffiloni/MEMO" in os.environ['SPACE_ID'] else False
|
| 97 |
+
temp_dir = None
|
| 98 |
if is_shared_ui:
|
| 99 |
+
temp_dir = tempfile.mkdtemp()
|
| 100 |
+
input_audio = process_audio(input_audio, temp_dir)
|
| 101 |
print(f"Processed file was stored temporarily at: {input_audio}")
|
| 102 |
|
| 103 |
resolution = 512
|
|
|
|
| 122 |
os.makedirs(cache_dir, exist_ok=True)
|
| 123 |
input_audio = resample_audio(input_audio, os.path.join(cache_dir, f"{os.path.basename(input_audio).split('.')[0]}-16k.wav"))
|
| 124 |
|
| 125 |
+
# Clean up the temporary directory
|
| 126 |
+
if os.path.exists(temp_dir):
|
| 127 |
+
shutil.rmtree(temp_dir)
|
| 128 |
+
print(f"Temporary directory {temp_dir} deleted.")
|
| 129 |
+
|
| 130 |
audio_emb, audio_length = preprocess_audio(
|
| 131 |
wav_path=input_audio,
|
| 132 |
num_generated_frames_per_clip=num_generated_frames_per_clip,
|