fffiloni commited on
Commit
7c33227
·
verified ·
1 Parent(s): f4d6d25

Update hf_gradio_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Create a temporary directory
78
- with tempfile.TemporaryDirectory() as temp_dir:
79
- # Load the audio file
80
- audio = AudioSegment.from_file(file_path)
81
-
82
- # Check and cut the audio if longer than 4 seconds
83
- max_duration = 4 * 1000 # 4 seconds in milliseconds
84
- if len(audio) > max_duration:
85
- audio = audio[:max_duration]
86
-
87
- # Save the processed audio in the temporary directory
88
- output_path = os.path.join(temp_dir, "trimmed_audio.wav")
89
- audio.export(output_path, format="wav")
90
-
91
- # Temporary file is available here for use
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
- input_audio = process_audio(input_audio)
 
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,