Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,6 @@
|
|
|
|
1 |
import os
|
2 |
-
import uuid
|
3 |
import torch
|
4 |
-
import numpy as np
|
5 |
-
import gradio as gr
|
6 |
from transformers import (
|
7 |
AutoTokenizer,
|
8 |
AutoModelForCausalLM,
|
@@ -15,23 +13,30 @@ from pydub import AudioSegment
|
|
15 |
from dotenv import load_dotenv
|
16 |
import tempfile
|
17 |
import spaces
|
|
|
|
|
18 |
from TTS.api import TTS
|
19 |
|
20 |
-
#
|
21 |
-
#
|
22 |
-
#
|
23 |
load_dotenv()
|
24 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
25 |
|
26 |
-
#
|
27 |
-
# Model
|
28 |
-
#
|
29 |
LLAMA_PIPELINES = {}
|
30 |
MUSICGEN_MODELS = {}
|
31 |
TTS_MODELS = {}
|
32 |
|
|
|
|
|
|
|
33 |
def get_llama_pipeline(model_id: str, token: str):
|
34 |
-
"""
|
|
|
|
|
35 |
if model_id in LLAMA_PIPELINES:
|
36 |
return LLAMA_PIPELINES[model_id]
|
37 |
|
@@ -47,434 +52,339 @@ def get_llama_pipeline(model_id: str, token: str):
|
|
47 |
LLAMA_PIPELINES[model_id] = text_pipeline
|
48 |
return text_pipeline
|
49 |
|
|
|
50 |
def get_musicgen_model(model_key: str = "facebook/musicgen-large"):
|
51 |
-
"""
|
|
|
|
|
|
|
52 |
if model_key in MUSICGEN_MODELS:
|
53 |
return MUSICGEN_MODELS[model_key]
|
54 |
|
55 |
model = MusicgenForConditionalGeneration.from_pretrained(model_key)
|
56 |
processor = AutoProcessor.from_pretrained(model_key)
|
|
|
57 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
58 |
model.to(device)
|
59 |
MUSICGEN_MODELS[model_key] = (model, processor)
|
60 |
return model, processor
|
61 |
|
|
|
62 |
def get_tts_model(model_name: str = "tts_models/en/ljspeech/tacotron2-DDC"):
|
63 |
-
"""
|
|
|
|
|
64 |
if model_name in TTS_MODELS:
|
65 |
return TTS_MODELS[model_name]
|
|
|
66 |
tts_model = TTS(model_name)
|
67 |
TTS_MODELS[model_name] = tts_model
|
68 |
return tts_model
|
69 |
|
70 |
-
|
71 |
-
#
|
72 |
-
#
|
|
|
73 |
@spaces.GPU(duration=100)
|
74 |
def generate_script(user_prompt: str, model_id: str, token: str, duration: int):
|
75 |
"""
|
76 |
-
|
77 |
-
|
78 |
"""
|
79 |
try:
|
80 |
text_pipeline = get_llama_pipeline(model_id, token)
|
81 |
-
|
82 |
system_prompt = (
|
83 |
-
|
84 |
-
"
|
85 |
-
"
|
86 |
-
"
|
87 |
-
"Music
|
88 |
-
"Make sure each section starts with its header exactly."
|
89 |
)
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
with torch.inference_mode():
|
94 |
result = text_pipeline(
|
95 |
-
|
96 |
-
max_new_tokens=
|
97 |
do_sample=True,
|
98 |
-
temperature=0.
|
99 |
-
top_p=0.9
|
100 |
)
|
101 |
|
102 |
-
generated_text = result[0]["generated_text"]
|
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 |
except Exception as e:
|
129 |
-
return f"Error: {
|
130 |
|
|
|
|
|
|
|
|
|
131 |
@spaces.GPU(duration=100)
|
132 |
-
def generate_voice(script: str, tts_model_name: str):
|
133 |
"""
|
134 |
-
|
|
|
135 |
"""
|
136 |
try:
|
137 |
if not script.strip():
|
138 |
-
return
|
|
|
139 |
tts_model = get_tts_model(tts_model_name)
|
140 |
-
|
141 |
-
|
|
|
142 |
tts_model.tts_to_file(text=script, file_path=output_path)
|
143 |
return output_path
|
144 |
-
except Exception as e:
|
145 |
-
print(f"Voice generation error: {e}")
|
146 |
-
return None
|
147 |
|
148 |
-
@spaces.GPU(duration=100)
|
149 |
-
def generate_voice_preview(script: str, tts_model_name: str):
|
150 |
-
"""
|
151 |
-
Generate a short preview of the voice-over by taking the first 100 words.
|
152 |
-
"""
|
153 |
-
try:
|
154 |
-
if not script.strip():
|
155 |
-
return None
|
156 |
-
words = script.split()
|
157 |
-
preview_text = ' '.join(words[:100]) if len(words) > 100 else script
|
158 |
-
return generate_voice(preview_text, tts_model_name)
|
159 |
except Exception as e:
|
160 |
-
|
161 |
-
return None
|
162 |
|
|
|
|
|
|
|
|
|
163 |
@spaces.GPU(duration=100)
|
164 |
def generate_music(prompt: str, audio_length: int):
|
165 |
"""
|
166 |
-
|
|
|
167 |
"""
|
168 |
try:
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
170 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
171 |
-
inputs =
|
172 |
-
|
173 |
with torch.inference_mode():
|
174 |
-
outputs =
|
175 |
-
|
176 |
-
# Assuming outputs[0, 0] holds the generated audio waveform.
|
177 |
audio_data = outputs[0, 0].cpu().numpy()
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
normalized_audio = audio_data.astype("int16")
|
182 |
-
else:
|
183 |
-
normalized_audio = (audio_data / max_val * 32767).astype("int16")
|
184 |
-
output_path = os.path.join(tempfile.gettempdir(), f"music_{uuid.uuid4().hex}.wav")
|
185 |
write(output_path, 44100, normalized_audio)
|
|
|
186 |
return output_path
|
|
|
187 |
except Exception as e:
|
188 |
-
|
189 |
-
|
190 |
|
|
|
|
|
|
|
191 |
@spaces.GPU(duration=100)
|
192 |
-
def blend_audio(voice_path: str, music_path: str, ducking: bool, duck_level: int):
|
193 |
"""
|
194 |
-
|
195 |
-
If
|
|
|
|
|
|
|
196 |
"""
|
197 |
try:
|
|
|
|
|
|
|
198 |
voice = AudioSegment.from_wav(voice_path)
|
199 |
music = AudioSegment.from_wav(music_path)
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
if ducking:
|
|
|
208 |
ducked_music = music - duck_level
|
|
|
209 |
final_audio = ducked_music.overlay(voice)
|
210 |
else:
|
|
|
211 |
final_audio = music.overlay(voice)
|
212 |
-
|
213 |
-
output_path = os.path.join(tempfile.gettempdir(),
|
214 |
final_audio.export(output_path, format="wav")
|
215 |
return output_path
|
|
|
216 |
except Exception as e:
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
#
|
221 |
-
#
|
222 |
-
#
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
.
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
.
|
303 |
-
|
304 |
-
|
305 |
-
"""
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
maximum=120,
|
333 |
-
step=15,
|
334 |
-
value=30,
|
335 |
-
interactive=True
|
336 |
-
)
|
337 |
-
llama_model_id = gr.Dropdown(
|
338 |
-
label="AI Model",
|
339 |
-
choices=["meta-llama/Meta-Llama-3-8B-Instruct"],
|
340 |
-
value="meta-llama/Meta-Llama-3-8B-Instruct",
|
341 |
-
interactive=True
|
342 |
-
)
|
343 |
-
generate_btn = gr.Button("Generate Script 🚀", elem_classes="dark-btn")
|
344 |
-
|
345 |
-
with gr.Column(scale=1, elem_classes="output-card"):
|
346 |
-
gr.Markdown("### 📄 Generated Content")
|
347 |
-
script_output = gr.Textbox(label="Voice Script", lines=6)
|
348 |
-
sound_design_output = gr.Textbox(label="Sound Design", lines=3)
|
349 |
-
music_suggestion_output = gr.Textbox(label="Music Style", lines=3)
|
350 |
-
|
351 |
-
# Voice Production Tab
|
352 |
-
with gr.Tab("🎙️ Voice Production", elem_classes="tab-button"):
|
353 |
-
with gr.Row():
|
354 |
-
with gr.Column(scale=1):
|
355 |
-
gr.Markdown("### 🔊 Voice Settings")
|
356 |
-
tts_model = gr.Dropdown(
|
357 |
-
label="Voice Model",
|
358 |
-
choices=[
|
359 |
-
"tts_models/en/ljspeech/tacotron2-DDC",
|
360 |
-
"tts_models/en/ljspeech/vits",
|
361 |
-
"tts_models/en/sam/tacotron-DDC"
|
362 |
-
],
|
363 |
-
value="tts_models/en/ljspeech/tacotron2-DDC",
|
364 |
-
interactive=True
|
365 |
-
)
|
366 |
-
with gr.Row():
|
367 |
-
voice_preview_btn = gr.Button("Preview Sample", elem_classes="dark-btn")
|
368 |
-
voice_generate_btn = gr.Button("Generate Full Voiceover", elem_classes="dark-btn")
|
369 |
-
with gr.Column(scale=1, elem_classes="output-card"):
|
370 |
-
gr.Markdown("### 🎧 Voice Preview")
|
371 |
-
voice_audio = gr.Audio(
|
372 |
-
label="Generated Voice",
|
373 |
-
interactive=False,
|
374 |
-
waveform_options={"show_controls": True}
|
375 |
-
)
|
376 |
-
|
377 |
-
# Music Production Tab
|
378 |
-
with gr.Tab("🎵 Music Design", elem_classes="tab-button"):
|
379 |
-
with gr.Row():
|
380 |
-
with gr.Column(scale=1):
|
381 |
-
gr.Markdown("### 🎹 Music Parameters")
|
382 |
-
audio_length = gr.Slider(
|
383 |
-
label="Generation Length",
|
384 |
-
minimum=256,
|
385 |
-
maximum=1024,
|
386 |
-
step=64,
|
387 |
-
value=512,
|
388 |
-
info="Higher values = longer generation time"
|
389 |
-
)
|
390 |
-
music_generate_btn = gr.Button("Generate Music Track", elem_classes="dark-btn")
|
391 |
-
with gr.Column(scale=1, elem_classes="output-card"):
|
392 |
-
gr.Markdown("### 🎶 Music Preview")
|
393 |
-
music_output = gr.Audio(
|
394 |
-
label="Generated Music",
|
395 |
-
interactive=False,
|
396 |
-
waveform_options={"show_controls": True}
|
397 |
-
)
|
398 |
-
|
399 |
-
# Final Mix Tab
|
400 |
-
with gr.Tab("🔊 Final Mix", elem_classes="tab-button"):
|
401 |
-
with gr.Row():
|
402 |
-
with gr.Column(scale=1):
|
403 |
-
gr.Markdown("### 🎚️ Mixing Console")
|
404 |
-
ducking_enabled = gr.Checkbox(
|
405 |
-
label="Enable Voice Ducking",
|
406 |
-
value=True,
|
407 |
-
info="Automatically lower music during voice segments"
|
408 |
-
)
|
409 |
-
duck_level = gr.Slider(
|
410 |
-
label="Ducking Intensity (dB)",
|
411 |
-
minimum=3,
|
412 |
-
maximum=20,
|
413 |
-
step=1,
|
414 |
-
value=10
|
415 |
-
)
|
416 |
-
mix_btn = gr.Button("Generate Final Mix", elem_classes="dark-btn")
|
417 |
-
with gr.Column(scale=1, elem_classes="output-card"):
|
418 |
-
gr.Markdown("### 🎧 Final Production")
|
419 |
-
final_mix = gr.Audio(
|
420 |
-
label="Mixed Output",
|
421 |
-
interactive=False,
|
422 |
-
waveform_options={"show_controls": True}
|
423 |
-
)
|
424 |
-
|
425 |
-
# Footer Section
|
426 |
-
with gr.Column(elem_classes="output-card"):
|
427 |
-
gr.Markdown("""
|
428 |
-
<div style="text-align: center; padding: 1.5em 0;">
|
429 |
-
<a href="https://bilsimaging.com" target="_blank">
|
430 |
-
<img src="https://bilsimaging.com/logo.png" alt="Bils Imaging" style="height: 35px; margin-right: 15px;">
|
431 |
-
</a>
|
432 |
-
<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FBils%2Fradiogold">
|
433 |
-
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FBils%2Fradiogold&countColor=%23263759" />
|
434 |
-
</a>
|
435 |
-
</div>
|
436 |
-
<p style="text-align: center; color: #666; font-size: 0.9em;">
|
437 |
-
Professional Audio Production Suite v2.1 © 2024 | Bils Imaging
|
438 |
-
</p>
|
439 |
-
""")
|
440 |
-
|
441 |
-
# -----------------------------------------------------------
|
442 |
-
# Event Handling
|
443 |
-
# -----------------------------------------------------------
|
444 |
-
# Hidden textbox for HF_TOKEN (its value is set via the environment variable).
|
445 |
-
hf_token_hidden = gr.Textbox(value=HF_TOKEN, visible=False)
|
446 |
-
|
447 |
-
generate_btn.click(
|
448 |
-
generate_script,
|
449 |
-
inputs=[user_prompt, llama_model_id, hf_token_hidden, duration],
|
450 |
-
outputs=[script_output, sound_design_output, music_suggestion_output]
|
451 |
-
)
|
452 |
-
|
453 |
-
# Voice preview: generates a trimmed version of the script.
|
454 |
-
voice_preview_btn.click(
|
455 |
-
generate_voice_preview,
|
456 |
-
inputs=[script_output, tts_model],
|
457 |
-
outputs=voice_audio
|
458 |
-
)
|
459 |
-
|
460 |
-
# Full voice generation using the complete script.
|
461 |
-
voice_generate_btn.click(
|
462 |
-
generate_voice,
|
463 |
-
inputs=[script_output, tts_model],
|
464 |
-
outputs=voice_audio
|
465 |
-
)
|
466 |
-
|
467 |
-
music_generate_btn.click(
|
468 |
-
generate_music,
|
469 |
-
inputs=[music_suggestion_output, audio_length],
|
470 |
-
outputs=music_output
|
471 |
-
)
|
472 |
-
|
473 |
-
mix_btn.click(
|
474 |
-
blend_audio,
|
475 |
-
inputs=[voice_audio, music_output, ducking_enabled, duck_level],
|
476 |
-
outputs=final_mix
|
477 |
-
)
|
478 |
|
479 |
-
|
480 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
import os
|
|
|
3 |
import torch
|
|
|
|
|
4 |
from transformers import (
|
5 |
AutoTokenizer,
|
6 |
AutoModelForCausalLM,
|
|
|
13 |
from dotenv import load_dotenv
|
14 |
import tempfile
|
15 |
import spaces
|
16 |
+
|
17 |
+
# Coqui TTS
|
18 |
from TTS.api import TTS
|
19 |
|
20 |
+
# ---------------------------------------------------------------------
|
21 |
+
# Load Environment Variables
|
22 |
+
# ---------------------------------------------------------------------
|
23 |
load_dotenv()
|
24 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
25 |
|
26 |
+
# ---------------------------------------------------------------------
|
27 |
+
# Global Model Caches
|
28 |
+
# ---------------------------------------------------------------------
|
29 |
LLAMA_PIPELINES = {}
|
30 |
MUSICGEN_MODELS = {}
|
31 |
TTS_MODELS = {}
|
32 |
|
33 |
+
# ---------------------------------------------------------------------
|
34 |
+
# Helper Functions
|
35 |
+
# ---------------------------------------------------------------------
|
36 |
def get_llama_pipeline(model_id: str, token: str):
|
37 |
+
"""
|
38 |
+
Returns a cached LLaMA pipeline if available; otherwise, loads it.
|
39 |
+
"""
|
40 |
if model_id in LLAMA_PIPELINES:
|
41 |
return LLAMA_PIPELINES[model_id]
|
42 |
|
|
|
52 |
LLAMA_PIPELINES[model_id] = text_pipeline
|
53 |
return text_pipeline
|
54 |
|
55 |
+
|
56 |
def get_musicgen_model(model_key: str = "facebook/musicgen-large"):
|
57 |
+
"""
|
58 |
+
Returns a cached MusicGen model if available; otherwise, loads it.
|
59 |
+
Uses the 'large' variant for higher quality outputs.
|
60 |
+
"""
|
61 |
if model_key in MUSICGEN_MODELS:
|
62 |
return MUSICGEN_MODELS[model_key]
|
63 |
|
64 |
model = MusicgenForConditionalGeneration.from_pretrained(model_key)
|
65 |
processor = AutoProcessor.from_pretrained(model_key)
|
66 |
+
|
67 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
68 |
model.to(device)
|
69 |
MUSICGEN_MODELS[model_key] = (model, processor)
|
70 |
return model, processor
|
71 |
|
72 |
+
|
73 |
def get_tts_model(model_name: str = "tts_models/en/ljspeech/tacotron2-DDC"):
|
74 |
+
"""
|
75 |
+
Returns a cached TTS model if available; otherwise, loads it.
|
76 |
+
"""
|
77 |
if model_name in TTS_MODELS:
|
78 |
return TTS_MODELS[model_name]
|
79 |
+
|
80 |
tts_model = TTS(model_name)
|
81 |
TTS_MODELS[model_name] = tts_model
|
82 |
return tts_model
|
83 |
|
84 |
+
|
85 |
+
# ---------------------------------------------------------------------
|
86 |
+
# Script Generation Function
|
87 |
+
# ---------------------------------------------------------------------
|
88 |
@spaces.GPU(duration=100)
|
89 |
def generate_script(user_prompt: str, model_id: str, token: str, duration: int):
|
90 |
"""
|
91 |
+
Generates a script, sound design suggestions, and music ideas from a user prompt.
|
92 |
+
Returns a tuple of strings: (voice_script, sound_design, music_suggestions).
|
93 |
"""
|
94 |
try:
|
95 |
text_pipeline = get_llama_pipeline(model_id, token)
|
96 |
+
|
97 |
system_prompt = (
|
98 |
+
"You are an expert radio imaging producer specializing in sound design and music. "
|
99 |
+
f"Based on the user's concept and the selected duration of {duration} seconds, produce the following: "
|
100 |
+
"1. A concise voice-over script. Prefix this section with 'Voice-Over Script:'.\n"
|
101 |
+
"2. Suggestions for sound design. Prefix this section with 'Sound Design Suggestions:'.\n"
|
102 |
+
"3. Music styles or track recommendations. Prefix this section with 'Music Suggestions:'."
|
|
|
103 |
)
|
104 |
+
combined_prompt = f"{system_prompt}\nUser concept: {user_prompt}\nOutput:"
|
105 |
+
|
|
|
106 |
with torch.inference_mode():
|
107 |
result = text_pipeline(
|
108 |
+
combined_prompt,
|
109 |
+
max_new_tokens=300,
|
110 |
do_sample=True,
|
111 |
+
temperature=0.8
|
|
|
112 |
)
|
113 |
|
114 |
+
generated_text = result[0]["generated_text"]
|
115 |
+
if "Output:" in generated_text:
|
116 |
+
generated_text = generated_text.split("Output:")[-1].strip()
|
117 |
+
|
118 |
+
# Default placeholders
|
119 |
+
voice_script = "No voice-over script found."
|
120 |
+
sound_design = "No sound design suggestions found."
|
121 |
+
music_suggestions = "No music suggestions found."
|
122 |
+
|
123 |
+
# Voice-Over Script
|
124 |
+
if "Voice-Over Script:" in generated_text:
|
125 |
+
parts = generated_text.split("Voice-Over Script:")
|
126 |
+
voice_script_part = parts[1]
|
127 |
+
if "Sound Design Suggestions:" in voice_script_part:
|
128 |
+
voice_script = voice_script_part.split("Sound Design Suggestions:")[0].strip()
|
129 |
+
else:
|
130 |
+
voice_script = voice_script_part.strip()
|
131 |
+
|
132 |
+
# Sound Design
|
133 |
+
if "Sound Design Suggestions:" in generated_text:
|
134 |
+
parts = generated_text.split("Sound Design Suggestions:")
|
135 |
+
sound_design_part = parts[1]
|
136 |
+
if "Music Suggestions:" in sound_design_part:
|
137 |
+
sound_design = sound_design_part.split("Music Suggestions:")[0].strip()
|
138 |
+
else:
|
139 |
+
sound_design = sound_design_part.strip()
|
140 |
+
|
141 |
+
# Music Suggestions
|
142 |
+
if "Music Suggestions:" in generated_text:
|
143 |
+
parts = generated_text.split("Music Suggestions:")
|
144 |
+
music_suggestions = parts[1].strip()
|
145 |
+
|
146 |
+
return voice_script, sound_design, music_suggestions
|
147 |
|
148 |
except Exception as e:
|
149 |
+
return f"Error generating script: {e}", "", ""
|
150 |
|
151 |
+
|
152 |
+
# ---------------------------------------------------------------------
|
153 |
+
# Voice-Over Generation Function
|
154 |
+
# ---------------------------------------------------------------------
|
155 |
@spaces.GPU(duration=100)
|
156 |
+
def generate_voice(script: str, tts_model_name: str = "tts_models/en/ljspeech/tacotron2-DDC"):
|
157 |
"""
|
158 |
+
Generates a voice-over from the provided script using the Coqui TTS model.
|
159 |
+
Returns the file path to the generated .wav file.
|
160 |
"""
|
161 |
try:
|
162 |
if not script.strip():
|
163 |
+
return "Error: No script provided."
|
164 |
+
|
165 |
tts_model = get_tts_model(tts_model_name)
|
166 |
+
|
167 |
+
# Generate and save voice
|
168 |
+
output_path = os.path.join(tempfile.gettempdir(), "voice_over.wav")
|
169 |
tts_model.tts_to_file(text=script, file_path=output_path)
|
170 |
return output_path
|
|
|
|
|
|
|
171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
except Exception as e:
|
173 |
+
return f"Error generating voice: {e}"
|
|
|
174 |
|
175 |
+
|
176 |
+
# ---------------------------------------------------------------------
|
177 |
+
# Music Generation Function
|
178 |
+
# ---------------------------------------------------------------------
|
179 |
@spaces.GPU(duration=100)
|
180 |
def generate_music(prompt: str, audio_length: int):
|
181 |
"""
|
182 |
+
Generates music from the 'facebook/musicgen-large' model based on the prompt.
|
183 |
+
Returns the file path to the generated .wav file.
|
184 |
"""
|
185 |
try:
|
186 |
+
if not prompt.strip():
|
187 |
+
return "Error: No music suggestion provided."
|
188 |
+
|
189 |
+
model_key = "facebook/musicgen-large"
|
190 |
+
musicgen_model, musicgen_processor = get_musicgen_model(model_key)
|
191 |
+
|
192 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
193 |
+
inputs = musicgen_processor(text=[prompt], padding=True, return_tensors="pt").to(device)
|
194 |
+
|
195 |
with torch.inference_mode():
|
196 |
+
outputs = musicgen_model.generate(**inputs, max_new_tokens=audio_length)
|
197 |
+
|
|
|
198 |
audio_data = outputs[0, 0].cpu().numpy()
|
199 |
+
normalized_audio = (audio_data / max(abs(audio_data)) * 32767).astype("int16")
|
200 |
+
|
201 |
+
output_path = f"{tempfile.gettempdir()}/musicgen_large_generated_music.wav"
|
|
|
|
|
|
|
|
|
202 |
write(output_path, 44100, normalized_audio)
|
203 |
+
|
204 |
return output_path
|
205 |
+
|
206 |
except Exception as e:
|
207 |
+
return f"Error generating music: {e}"
|
208 |
+
|
209 |
|
210 |
+
# ---------------------------------------------------------------------
|
211 |
+
# Audio Blending with Duration Sync & Ducking
|
212 |
+
# ---------------------------------------------------------------------
|
213 |
@spaces.GPU(duration=100)
|
214 |
+
def blend_audio(voice_path: str, music_path: str, ducking: bool, duck_level: int = 10):
|
215 |
"""
|
216 |
+
Blends two audio files (voice and music).
|
217 |
+
1. If music < voice, loops the music until it meets/exceeds the voice duration.
|
218 |
+
2. If music > voice, trims music to the voice duration.
|
219 |
+
3. If ducking=True, the music is attenuated by 'duck_level' dB while the voice is playing.
|
220 |
+
Returns the file path to the blended .wav file.
|
221 |
"""
|
222 |
try:
|
223 |
+
if not os.path.isfile(voice_path) or not os.path.isfile(music_path):
|
224 |
+
return "Error: Missing audio files for blending."
|
225 |
+
|
226 |
voice = AudioSegment.from_wav(voice_path)
|
227 |
music = AudioSegment.from_wav(music_path)
|
228 |
+
|
229 |
+
voice_len = len(voice) # in milliseconds
|
230 |
+
music_len = len(music) # in milliseconds
|
231 |
+
|
232 |
+
# 1) If the music is shorter than the voice, loop it:
|
233 |
+
if music_len < voice_len:
|
234 |
+
looped_music = AudioSegment.empty()
|
235 |
+
# Keep appending until we exceed voice length
|
236 |
+
while len(looped_music) < voice_len:
|
237 |
+
looped_music += music
|
238 |
+
music = looped_music
|
239 |
+
|
240 |
+
# 2) If the music is longer than the voice, truncate it:
|
241 |
+
if len(music) > voice_len:
|
242 |
+
music = music[:voice_len]
|
243 |
+
|
244 |
+
# Now music and voice are the same length
|
245 |
if ducking:
|
246 |
+
# Step 1: Reduce music dB while voice is playing
|
247 |
ducked_music = music - duck_level
|
248 |
+
# Step 2: Overlay voice on top of ducked music
|
249 |
final_audio = ducked_music.overlay(voice)
|
250 |
else:
|
251 |
+
# No ducking, just overlay
|
252 |
final_audio = music.overlay(voice)
|
253 |
+
|
254 |
+
output_path = os.path.join(tempfile.gettempdir(), "blended_output.wav")
|
255 |
final_audio.export(output_path, format="wav")
|
256 |
return output_path
|
257 |
+
|
258 |
except Exception as e:
|
259 |
+
return f"Error blending audio: {e}"
|
260 |
+
|
261 |
+
|
262 |
+
# ---------------------------------------------------------------------
|
263 |
+
# Gradio Interface
|
264 |
+
# ---------------------------------------------------------------------
|
265 |
+
with gr.Blocks() as demo:
|
266 |
+
gr.Markdown("""
|
267 |
+
# 🎧 AI Promo Studio
|
268 |
+
Welcome to **AI Promo Studio**, your all-in-one solution for creating professional, engaging audio promos with minimal effort!
|
269 |
+
|
270 |
+
This next-generation platform uses powerful AI models to handle:
|
271 |
+
- **Script Generation**: Craft concise and impactful copy with LLaMA.
|
272 |
+
- **Voice Synthesis**: Convert text into natural-sounding voice-overs using Coqui TTS.
|
273 |
+
- **Music Production**: Generate custom music tracks with MusicGen Large for sound bed.
|
274 |
+
- **Seamless Blending**: Easily combine voice and music—loop or trim tracks to match your desired promo length, with optional ducking to keep the voice front and center.
|
275 |
+
|
276 |
+
Whether you’re a radio producer, podcaster, or content creator, **AI Promo Studio** streamlines your entire production pipeline—cutting hours of manual editing down to a few clicks.
|
277 |
+
""")
|
278 |
+
|
279 |
+
|
280 |
+
with gr.Tabs():
|
281 |
+
# Step 1: Generate Script
|
282 |
+
with gr.Tab("Step 1: Generate Script"):
|
283 |
+
with gr.Row():
|
284 |
+
user_prompt = gr.Textbox(
|
285 |
+
label="Promo Idea",
|
286 |
+
placeholder="E.g., A 30-second promo for a morning show...",
|
287 |
+
lines=2
|
288 |
+
)
|
289 |
+
llama_model_id = gr.Textbox(
|
290 |
+
label="LLaMA Model ID",
|
291 |
+
value="meta-llama/Meta-Llama-3-8B-Instruct",
|
292 |
+
placeholder="Enter a valid Hugging Face model ID"
|
293 |
+
)
|
294 |
+
duration = gr.Slider(
|
295 |
+
label="Desired Promo Duration (seconds)",
|
296 |
+
minimum=15,
|
297 |
+
maximum=60,
|
298 |
+
step=15,
|
299 |
+
value=30
|
300 |
+
)
|
301 |
+
|
302 |
+
generate_script_button = gr.Button("Generate Script")
|
303 |
+
script_output = gr.Textbox(label="Generated Voice-Over Script", lines=5, interactive=False)
|
304 |
+
sound_design_output = gr.Textbox(label="Sound Design Suggestions", lines=3, interactive=False)
|
305 |
+
music_suggestion_output = gr.Textbox(label="Music Suggestions", lines=3, interactive=False)
|
306 |
+
|
307 |
+
generate_script_button.click(
|
308 |
+
fn=lambda user_prompt, model_id, dur: generate_script(user_prompt, model_id, HF_TOKEN, dur),
|
309 |
+
inputs=[user_prompt, llama_model_id, duration],
|
310 |
+
outputs=[script_output, sound_design_output, music_suggestion_output],
|
311 |
+
)
|
312 |
+
|
313 |
+
# Step 2: Generate Voice
|
314 |
+
with gr.Tab("Step 2: Generate Voice"):
|
315 |
+
gr.Markdown("Generate the voice-over using a Coqui TTS model.")
|
316 |
+
selected_tts_model = gr.Dropdown(
|
317 |
+
label="TTS Model",
|
318 |
+
choices=[
|
319 |
+
"tts_models/en/ljspeech/tacotron2-DDC",
|
320 |
+
"tts_models/en/ljspeech/vits",
|
321 |
+
"tts_models/en/sam/tacotron-DDC",
|
322 |
+
],
|
323 |
+
value="tts_models/en/ljspeech/tacotron2-DDC",
|
324 |
+
multiselect=False
|
325 |
+
)
|
326 |
+
generate_voice_button = gr.Button("Generate Voice-Over")
|
327 |
+
voice_audio_output = gr.Audio(label="Voice-Over (WAV)", type="filepath")
|
328 |
+
|
329 |
+
generate_voice_button.click(
|
330 |
+
fn=lambda script, tts_model: generate_voice(script, tts_model),
|
331 |
+
inputs=[script_output, selected_tts_model],
|
332 |
+
outputs=voice_audio_output,
|
333 |
+
)
|
334 |
+
|
335 |
+
# Step 3: Generate Music (MusicGen Large)
|
336 |
+
with gr.Tab("Step 3: Generate Music"):
|
337 |
+
gr.Markdown("Generate a music track with the **MusicGen Large** model.")
|
338 |
+
audio_length = gr.Slider(
|
339 |
+
label="Music Length (tokens)",
|
340 |
+
minimum=128,
|
341 |
+
maximum=1024,
|
342 |
+
step=64,
|
343 |
+
value=512,
|
344 |
+
info="Increase tokens for longer audio, but be mindful of inference time."
|
345 |
+
)
|
346 |
+
generate_music_button = gr.Button("Generate Music")
|
347 |
+
music_output = gr.Audio(label="Generated Music (WAV)", type="filepath")
|
348 |
+
|
349 |
+
generate_music_button.click(
|
350 |
+
fn=lambda music_suggestion, length: generate_music(music_suggestion, length),
|
351 |
+
inputs=[music_suggestion_output, audio_length],
|
352 |
+
outputs=[music_output],
|
353 |
+
)
|
354 |
+
|
355 |
+
# Step 4: Blend Audio (Loop/Trim + Ducking)
|
356 |
+
with gr.Tab("Step 4: Blend Audio"):
|
357 |
+
gr.Markdown("**Music** will be looped or trimmed to match **Voice** duration, then optionally ducked.")
|
358 |
+
ducking_checkbox = gr.Checkbox(label="Enable Ducking?", value=True)
|
359 |
+
duck_level_slider = gr.Slider(
|
360 |
+
label="Ducking Level (dB attenuation)",
|
361 |
+
minimum=0,
|
362 |
+
maximum=20,
|
363 |
+
step=1,
|
364 |
+
value=10
|
365 |
+
)
|
366 |
+
blend_button = gr.Button("Blend Voice + Music")
|
367 |
+
blended_output = gr.Audio(label="Final Blended Output (WAV)", type="filepath")
|
368 |
+
|
369 |
+
blend_button.click(
|
370 |
+
fn=blend_audio,
|
371 |
+
inputs=[voice_audio_output, music_output, ducking_checkbox, duck_level_slider],
|
372 |
+
outputs=blended_output
|
373 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
374 |
|
375 |
+
# Footer
|
376 |
+
gr.Markdown("""
|
377 |
+
<hr>
|
378 |
+
<p style="text-align: center; font-size: 0.9em;">
|
379 |
+
Created with ❤️ by <a href="https://bilsimaging.com" target="_blank">bilsimaging.com</a>
|
380 |
+
</p>
|
381 |
+
""")
|
382 |
+
|
383 |
+
# Visitor Badge
|
384 |
+
gr.HTML("""
|
385 |
+
<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FBils%2Fradiogold">
|
386 |
+
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FBils%2Fradiogold&countColor=%23263759" />
|
387 |
+
</a>
|
388 |
+
""")
|
389 |
+
|
390 |
+
demo.launch(debug=True)
|