Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,8 @@ import spaces
|
|
8 |
from huggingface_hub import snapshot_download
|
9 |
from huggingface_hub.utils import GatedRepoError, RepositoryNotFoundError, RevisionNotFoundError
|
10 |
from pathlib import Path
|
|
|
|
|
11 |
|
12 |
# Add the src directory to the system path to allow for local imports
|
13 |
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'src')))
|
@@ -55,6 +57,45 @@ def download_weights():
|
|
55 |
else:
|
56 |
print(f"Found existing weights at '{WEIGHTS_DIR}'. Skipping download.")
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
# --- Initialization ---
|
60 |
# Create output directory if it doesn't exist
|
@@ -94,6 +135,10 @@ def generate_motion(source_image_path, driving_audio_path, emotion_name, cfg_sca
|
|
94 |
|
95 |
start_time = time.time()
|
96 |
|
|
|
|
|
|
|
|
|
97 |
# Create a unique subdirectory for this run
|
98 |
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
99 |
run_output_dir = os.path.join(OUTPUT_DIR, timestamp)
|
@@ -104,15 +149,16 @@ def generate_motion(source_image_path, driving_audio_path, emotion_name, cfg_sca
|
|
104 |
|
105 |
print(f"Starting generation with the following parameters:")
|
106 |
print(f" Source Image: {source_image_path}")
|
107 |
-
print(f" Driving Audio: {driving_audio_path}")
|
|
|
108 |
print(f" Emotion: {emotion_name} (ID: {emotion_id})")
|
109 |
print(f" CFG Scale: {cfg_scale}")
|
110 |
|
111 |
try:
|
112 |
-
# Call the pipeline's inference method
|
113 |
result_video_path = pipeline.driven_sample(
|
114 |
image_path=source_image_path,
|
115 |
-
audio_path=
|
116 |
cfg_scale=float(cfg_scale),
|
117 |
emo=emotion_id,
|
118 |
save_dir=".",
|
@@ -124,6 +170,14 @@ def generate_motion(source_image_path, driving_audio_path, emotion_name, cfg_sca
|
|
124 |
import traceback
|
125 |
traceback.print_exc()
|
126 |
raise gr.Error(f"An unexpected error occurred: {str(e)}. Please check the console for details.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
end_time = time.time()
|
129 |
|
@@ -150,6 +204,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 90% !i
|
|
150 |
</p>
|
151 |
<p>
|
152 |
This demo allows you to generate a talking head video from a source image and a driving audio file.
|
|
|
153 |
</p>
|
154 |
</div>
|
155 |
"""
|
@@ -161,7 +216,11 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 90% !i
|
|
161 |
source_image = gr.Image(label="Source Image", type="filepath", value="src/examples/reference_images/6.jpg")
|
162 |
|
163 |
with gr.Row():
|
164 |
-
driving_audio = gr.Audio(
|
|
|
|
|
|
|
|
|
165 |
|
166 |
with gr.Row():
|
167 |
emotion_dropdown = gr.Dropdown(
|
|
|
8 |
from huggingface_hub import snapshot_download
|
9 |
from huggingface_hub.utils import GatedRepoError, RepositoryNotFoundError, RevisionNotFoundError
|
10 |
from pathlib import Path
|
11 |
+
import tempfile
|
12 |
+
from pydub import AudioSegment
|
13 |
|
14 |
# Add the src directory to the system path to allow for local imports
|
15 |
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'src')))
|
|
|
57 |
else:
|
58 |
print(f"Found existing weights at '{WEIGHTS_DIR}'. Skipping download.")
|
59 |
|
60 |
+
# --- Audio Conversion Function ---
|
61 |
+
def ensure_wav_format(audio_path):
|
62 |
+
"""
|
63 |
+
Ensures the audio file is in WAV format. If not, converts it to WAV.
|
64 |
+
Returns the path to the WAV file (either original or converted).
|
65 |
+
"""
|
66 |
+
if audio_path is None:
|
67 |
+
return None
|
68 |
+
|
69 |
+
audio_path = Path(audio_path)
|
70 |
+
|
71 |
+
# Check if already WAV
|
72 |
+
if audio_path.suffix.lower() == '.wav':
|
73 |
+
print(f"Audio is already in WAV format: {audio_path}")
|
74 |
+
return str(audio_path)
|
75 |
+
|
76 |
+
# Convert to WAV
|
77 |
+
print(f"Converting audio from {audio_path.suffix} to WAV format...")
|
78 |
+
|
79 |
+
try:
|
80 |
+
# Load the audio file
|
81 |
+
audio = AudioSegment.from_file(audio_path)
|
82 |
+
|
83 |
+
# Create a temporary WAV file
|
84 |
+
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp_file:
|
85 |
+
wav_path = tmp_file.name
|
86 |
+
# Export as WAV with standard settings
|
87 |
+
audio.export(
|
88 |
+
wav_path,
|
89 |
+
format='wav',
|
90 |
+
parameters=["-ar", "16000", "-ac", "1"] # 16kHz, mono - adjust if your model needs different settings
|
91 |
+
)
|
92 |
+
|
93 |
+
print(f"Audio converted successfully to: {wav_path}")
|
94 |
+
return wav_path
|
95 |
+
|
96 |
+
except Exception as e:
|
97 |
+
print(f"Error converting audio: {e}")
|
98 |
+
raise gr.Error(f"Failed to convert audio file to WAV format. Error: {e}")
|
99 |
|
100 |
# --- Initialization ---
|
101 |
# Create output directory if it doesn't exist
|
|
|
135 |
|
136 |
start_time = time.time()
|
137 |
|
138 |
+
# Ensure audio is in WAV format
|
139 |
+
wav_audio_path = ensure_wav_format(driving_audio_path)
|
140 |
+
temp_wav_created = wav_audio_path != driving_audio_path
|
141 |
+
|
142 |
# Create a unique subdirectory for this run
|
143 |
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
144 |
run_output_dir = os.path.join(OUTPUT_DIR, timestamp)
|
|
|
149 |
|
150 |
print(f"Starting generation with the following parameters:")
|
151 |
print(f" Source Image: {source_image_path}")
|
152 |
+
print(f" Driving Audio (original): {driving_audio_path}")
|
153 |
+
print(f" Driving Audio (WAV): {wav_audio_path}")
|
154 |
print(f" Emotion: {emotion_name} (ID: {emotion_id})")
|
155 |
print(f" CFG Scale: {cfg_scale}")
|
156 |
|
157 |
try:
|
158 |
+
# Call the pipeline's inference method with the WAV audio
|
159 |
result_video_path = pipeline.driven_sample(
|
160 |
image_path=source_image_path,
|
161 |
+
audio_path=wav_audio_path,
|
162 |
cfg_scale=float(cfg_scale),
|
163 |
emo=emotion_id,
|
164 |
save_dir=".",
|
|
|
170 |
import traceback
|
171 |
traceback.print_exc()
|
172 |
raise gr.Error(f"An unexpected error occurred: {str(e)}. Please check the console for details.")
|
173 |
+
finally:
|
174 |
+
# Clean up temporary WAV file if created
|
175 |
+
if temp_wav_created and os.path.exists(wav_audio_path):
|
176 |
+
try:
|
177 |
+
os.remove(wav_audio_path)
|
178 |
+
print(f"Cleaned up temporary WAV file: {wav_audio_path}")
|
179 |
+
except Exception as e:
|
180 |
+
print(f"Warning: Could not delete temporary file {wav_audio_path}: {e}")
|
181 |
|
182 |
end_time = time.time()
|
183 |
|
|
|
204 |
</p>
|
205 |
<p>
|
206 |
This demo allows you to generate a talking head video from a source image and a driving audio file.
|
207 |
+
Audio files in any common format (MP3, WAV, M4A, etc.) are supported and will be automatically converted if needed.
|
208 |
</p>
|
209 |
</div>
|
210 |
"""
|
|
|
216 |
source_image = gr.Image(label="Source Image", type="filepath", value="src/examples/reference_images/6.jpg")
|
217 |
|
218 |
with gr.Row():
|
219 |
+
driving_audio = gr.Audio(
|
220 |
+
label="Driving Audio (any format - will be converted to WAV if needed)",
|
221 |
+
type="filepath",
|
222 |
+
value="src/examples/driving_audios/5.wav"
|
223 |
+
)
|
224 |
|
225 |
with gr.Row():
|
226 |
emotion_dropdown = gr.Dropdown(
|