Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import torch
|
|
|
|
|
4 |
from transformers import (
|
5 |
AutoTokenizer,
|
6 |
AutoModelForCausalLM,
|
@@ -13,378 +15,305 @@ from pydub import AudioSegment
|
|
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 |
-
#
|
22 |
-
#
|
23 |
load_dotenv()
|
24 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
return
|
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
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
83 |
|
|
|
84 |
|
85 |
-
#
|
86 |
-
#
|
87 |
-
#
|
88 |
-
@spaces.GPU
|
89 |
-
def generate_script(user_prompt
|
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 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
-
|
153 |
-
|
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 "
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
except Exception as e:
|
173 |
-
return f"Error
|
174 |
-
|
175 |
|
176 |
-
|
177 |
-
|
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 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
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
|
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 |
-
|
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 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
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 |
-
|
245 |
if ducking:
|
246 |
-
|
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
|
260 |
-
|
261 |
-
|
262 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
# Gradio Interface
|
264 |
-
#
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
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 |
-
|
282 |
-
with gr.Tab("Step 1: Generate Script"):
|
283 |
with gr.Row():
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
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 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
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 |
-
|
336 |
-
|
337 |
-
gr.
|
338 |
-
|
339 |
-
label="
|
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 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
|
|
|
|
|
|
354 |
|
355 |
-
|
356 |
-
|
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 |
-
<
|
378 |
-
|
379 |
-
|
380 |
-
</
|
381 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
382 |
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
389 |
|
390 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import torch
|
4 |
+
import numpy as np
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
from transformers import (
|
7 |
AutoTokenizer,
|
8 |
AutoModelForCausalLM,
|
|
|
15 |
from dotenv import load_dotenv
|
16 |
import tempfile
|
17 |
import spaces
|
|
|
|
|
18 |
from TTS.api import TTS
|
19 |
+
import psutil
|
20 |
+
import GPUtil
|
21 |
|
22 |
+
# -------------------------------
|
23 |
+
# Configuration
|
24 |
+
# -------------------------------
|
25 |
load_dotenv()
|
26 |
+
HF_TOKEN = os.getenv("HF_TOKEN", os.getenv("HF_TOKEN_SECRET"))
|
27 |
+
|
28 |
+
MODEL_CONFIG = {
|
29 |
+
"llama_models": {
|
30 |
+
"Meta-Llama-3-8B": "meta-llama/Meta-Llama-3-8B-Instruct",
|
31 |
+
"Mistral-7B": "mistralai/Mistral-7B-Instruct-v0.2",
|
32 |
+
},
|
33 |
+
"tts_models": {
|
34 |
+
"Standard English": "tts_models/en/ljspeech/tacotron2-DDC",
|
35 |
+
"High Quality": "tts_models/en/ljspeech/vits"
|
36 |
+
},
|
37 |
+
"musicgen_model": "facebook/musicgen-medium"
|
38 |
+
}
|
39 |
+
|
40 |
+
# -------------------------------
|
41 |
+
# Model Manager with Cache
|
42 |
+
# -------------------------------
|
43 |
+
class ModelManager:
|
44 |
+
def __init__(self):
|
45 |
+
self.llama_pipelines = {}
|
46 |
+
self.musicgen_model = None
|
47 |
+
self.tts_models = {}
|
48 |
+
|
49 |
+
def get_llama_pipeline(self, model_id, token):
|
50 |
+
if model_id not in self.llama_pipelines:
|
51 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=token)
|
52 |
+
model = AutoModelForCausalLM.from_pretrained(
|
53 |
+
model_id,
|
54 |
+
use_auth_token=token,
|
55 |
+
torch_dtype=torch.float16,
|
56 |
+
device_map="auto"
|
57 |
+
)
|
58 |
+
self.llama_pipelines[model_id] = pipeline(
|
59 |
+
"text-generation",
|
60 |
+
model=model,
|
61 |
+
tokenizer=tokenizer,
|
62 |
+
device_map="auto"
|
63 |
+
)
|
64 |
+
return self.llama_pipelines[model_id]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
def get_musicgen_model(self):
|
67 |
+
if not self.musicgen_model:
|
68 |
+
self.musicgen_model = MusicgenForConditionalGeneration.from_pretrained(
|
69 |
+
MODEL_CONFIG["musicgen_model"]
|
70 |
+
)
|
71 |
+
self.musicgen_model.to("cuda" if torch.cuda.is_available() else "cpu")
|
72 |
+
return self.musicgen_model
|
73 |
|
74 |
+
def get_tts_model(self, model_name):
|
75 |
+
if model_name not in self.tts_models:
|
76 |
+
self.tts_models[model_name] = TTS(model_name)
|
77 |
+
return self.tts_models[model_name]
|
78 |
|
79 |
+
model_manager = ModelManager()
|
80 |
|
81 |
+
# -------------------------------
|
82 |
+
# Core Functions with Enhanced Error Handling
|
83 |
+
# -------------------------------
|
84 |
+
@spaces.GPU
|
85 |
+
def generate_script(user_prompt, model_id, duration, progress=gr.Progress()):
|
|
|
|
|
|
|
|
|
86 |
try:
|
87 |
+
progress(0.1, "Initializing script generation...")
|
88 |
+
text_pipeline = model_manager.get_llama_pipeline(model_id, HF_TOKEN)
|
89 |
+
|
90 |
+
system_prompt = f"""Generate a {duration}-second radio promo with:
|
91 |
+
1. Voice Script: [Clear narration, 25-35 words]
|
92 |
+
2. Sound Design: [3-5 specific sound effects]
|
93 |
+
3. Music: [Genre, tempo, mood]
|
94 |
+
|
95 |
+
Format strictly as:
|
96 |
+
Voice Script: [content]
|
97 |
+
Sound Design: [effects]
|
98 |
+
Music: [description]"""
|
99 |
+
|
100 |
+
progress(0.3, "Generating content...")
|
101 |
+
response = text_pipeline(
|
102 |
+
f"{system_prompt}\nConcept: {user_prompt}",
|
103 |
+
max_new_tokens=300,
|
104 |
+
temperature=0.7,
|
105 |
+
do_sample=True,
|
106 |
+
top_p=0.95
|
107 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
+
progress(0.8, "Parsing results...")
|
110 |
+
return parse_generated_content(response[0]["generated_text"])
|
111 |
except Exception as e:
|
112 |
+
return [f"Error: {str(e)}"] * 3
|
113 |
|
114 |
+
def parse_generated_content(text):
|
115 |
+
sections = {"Voice Script": "", "Sound Design": "", "Music": ""}
|
116 |
+
current_section = None
|
117 |
+
|
118 |
+
for line in text.split('\n'):
|
119 |
+
line = line.strip()
|
120 |
+
for section in sections:
|
121 |
+
if line.startswith(section + ":"):
|
122 |
+
current_section = section
|
123 |
+
line = line.replace(section + ":", "").strip()
|
124 |
+
break
|
125 |
+
if current_section and line:
|
126 |
+
sections[current_section] += line + "\n"
|
127 |
+
|
128 |
+
return [sections[section].strip() for section in sections]
|
129 |
|
130 |
+
@spaces.GPU
|
131 |
+
def generate_voice(script, tts_model, speed=1.0, progress=gr.Progress()):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
try:
|
133 |
+
progress(0.2, "Initializing TTS...")
|
134 |
if not script.strip():
|
135 |
+
return None, "No script provided"
|
136 |
+
|
137 |
+
tts = model_manager.get_tts_model(tts_model)
|
138 |
+
output_path = os.path.join(tempfile.gettempdir(), "voice.wav")
|
139 |
+
|
140 |
+
progress(0.5, "Generating audio...")
|
141 |
+
tts.tts_to_file(text=script, file_path=output_path, speed=speed)
|
142 |
+
|
143 |
+
return output_path, None
|
144 |
except Exception as e:
|
145 |
+
return None, f"Voice Error: {str(e)}"
|
|
|
146 |
|
147 |
+
@spaces.GPU
|
148 |
+
def generate_music(prompt, duration_sec=30, progress=gr.Progress()):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
try:
|
150 |
+
progress(0.1, "Initializing MusicGen...")
|
151 |
+
model = model_manager.get_musicgen_model()
|
152 |
+
processor = AutoProcessor.from_pretrained(MODEL_CONFIG["musicgen_model"])
|
153 |
+
|
154 |
+
progress(0.4, "Processing input...")
|
155 |
+
inputs = processor(text=[prompt], padding=True, return_tensors="pt").to(model.device)
|
156 |
+
|
157 |
+
progress(0.6, "Generating music...")
|
158 |
+
audio_values = model.generate(**inputs, max_new_tokens=int(duration_sec * 50))
|
159 |
+
|
160 |
+
output_path = os.path.join(tempfile.gettempdir(), "music.wav")
|
161 |
+
write(output_path, 32000, audio_values[0, 0].cpu().numpy())
|
162 |
+
return output_path, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
except Exception as e:
|
164 |
+
return None, f"Music Error: {str(e)}"
|
165 |
|
166 |
+
def blend_audio(voice_path, music_path, ducking=True, progress=gr.Progress()):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
try:
|
168 |
+
progress(0.2, "Loading audio files...")
|
|
|
|
|
169 |
voice = AudioSegment.from_wav(voice_path)
|
170 |
music = AudioSegment.from_wav(music_path)
|
171 |
|
172 |
+
progress(0.4, "Aligning durations...")
|
173 |
+
if len(music) < len(voice):
|
174 |
+
music = music * (len(voice) // len(music) + 1)
|
175 |
+
music = music[:len(voice)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
+
progress(0.6, "Mixing audio...")
|
178 |
if ducking:
|
179 |
+
music = music - 10 # 10dB ducking
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
+
mixed = music.overlay(voice)
|
182 |
+
output_path = os.path.join(tempfile.gettempdir(), "final_mix.wav")
|
183 |
+
mixed.export(output_path, format="wav")
|
184 |
+
return output_path, None
|
185 |
except Exception as e:
|
186 |
+
return None, f"Mixing Error: {str(e)}"
|
187 |
+
|
188 |
+
# -------------------------------
|
189 |
+
# UI Components
|
190 |
+
# -------------------------------
|
191 |
+
def create_audio_visualization(audio_path):
|
192 |
+
if not audio_path:
|
193 |
+
return None
|
194 |
+
audio = AudioSegment.from_file(audio_path)
|
195 |
+
samples = np.array(audio.get_array_of_samples())
|
196 |
+
|
197 |
+
plt.figure(figsize=(10, 3))
|
198 |
+
plt.plot(samples)
|
199 |
+
plt.axis('off')
|
200 |
+
plt.tight_layout()
|
201 |
+
|
202 |
+
temp_file = os.path.join(tempfile.gettempdir(), "waveform.png")
|
203 |
+
plt.savefig(temp_file, bbox_inches='tight', pad_inches=0)
|
204 |
+
plt.close()
|
205 |
+
return temp_file
|
206 |
+
|
207 |
+
def system_monitor():
|
208 |
+
gpus = GPUtil.getGPUs()
|
209 |
+
return {
|
210 |
+
"CPU": f"{psutil.cpu_percent()}%",
|
211 |
+
"RAM": f"{psutil.virtual_memory().percent}%",
|
212 |
+
"GPU": f"{gpus[0].load*100 if gpus else 0:.1f}%" if gpus else "N/A"
|
213 |
+
}
|
214 |
+
|
215 |
+
# -------------------------------
|
216 |
# Gradio Interface
|
217 |
+
# -------------------------------
|
218 |
+
theme = gr.themes.Soft(
|
219 |
+
primary_hue="blue",
|
220 |
+
secondary_hue="teal",
|
221 |
+
).set(
|
222 |
+
body_text_color_dark='#FFFFFF',
|
223 |
+
background_fill_primary_dark='#1F1F1F'
|
224 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
+
with gr.Blocks(theme=theme, title="AI Radio Studio Pro") as demo:
|
227 |
+
gr.Markdown("# 🎙️ AI Radio Studio Pro")
|
228 |
+
|
229 |
+
with gr.Row():
|
230 |
+
with gr.Column(scale=3):
|
231 |
+
concept_input = gr.Textbox(
|
232 |
+
label="Concept Description",
|
233 |
+
placeholder="Describe your radio segment...",
|
234 |
+
lines=3
|
235 |
+
)
|
236 |
+
with gr.Accordion("Advanced Settings", open=False):
|
237 |
+
model_selector = gr.Dropdown(
|
238 |
+
list(MODEL_CONFIG["llama_models"].values()),
|
239 |
+
label="AI Model",
|
240 |
+
value=next(iter(MODEL_CONFIG["llama_models"].values()))
|
241 |
+
)
|
242 |
+
duration_selector = gr.Slider(15, 120, 30, step=15, label="Duration (seconds)")
|
243 |
+
|
244 |
+
generate_btn = gr.Button("Generate Script", variant="primary")
|
245 |
+
|
246 |
+
with gr.Column(scale=2):
|
247 |
+
script_output = gr.Textbox(label="Voice Script", interactive=True)
|
248 |
+
sound_output = gr.Textbox(label="Sound Design", interactive=True)
|
249 |
+
music_output = gr.Textbox(label="Music Style", interactive=True)
|
250 |
|
251 |
with gr.Tabs():
|
252 |
+
with gr.Tab("🎤 Voice Production"):
|
|
|
253 |
with gr.Row():
|
254 |
+
tts_selector = gr.Dropdown(
|
255 |
+
list(MODEL_CONFIG["tts_models"].values()),
|
256 |
+
label="Voice Model",
|
257 |
+
value=next(iter(MODEL_CONFIG["tts_models"].values()))
|
|
|
|
|
|
|
|
|
|
|
258 |
)
|
259 |
+
speed_selector = gr.Slider(0.5, 2.0, 1.0, step=0.1, label="Speaking Rate")
|
260 |
+
voice_btn = gr.Button("Generate Voiceover", variant="primary")
|
261 |
+
with gr.Row():
|
262 |
+
voice_audio = gr.Audio(label="Voice Preview", interactive=False)
|
263 |
+
voice_viz = gr.Image(label="Waveform", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
+
with gr.Tab("🎵 Music Production"):
|
266 |
+
music_btn = gr.Button("Generate Music Track", variant="primary")
|
267 |
+
with gr.Row():
|
268 |
+
music_audio = gr.Audio(label="Music Preview", interactive=False)
|
269 |
+
music_viz = gr.Image(label="Waveform", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
|
271 |
+
with gr.Tab("🔉 Final Mix"):
|
272 |
+
mix_btn = gr.Button("Create Final Mix", variant="primary")
|
273 |
+
with gr.Row():
|
274 |
+
final_mix_audio = gr.Audio(label="Final Mix", interactive=False)
|
275 |
+
final_mix_viz = gr.Image(label="Waveform", interactive=False)
|
276 |
+
with gr.Row():
|
277 |
+
download_btn = gr.Button("Download Mix")
|
278 |
+
play_btn = gr.Button("▶️ Play in Browser")
|
279 |
|
280 |
+
with gr.Accordion("📊 System Monitor", open=False):
|
281 |
+
monitor = gr.JSON(label="Resource Usage", value=lambda: system_monitor(), every=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
gr.Markdown("""
|
284 |
+
<div style="text-align: center; padding: 20px; border-top: 1px solid #444;">
|
285 |
+
<p>Created with ❤️ by <a href="https://bilsimaging.com">Bils Imaging</a></p>
|
286 |
+
<img src="https://api.visitorbadge.io/api/visitors?path=https://huggingface.co/spaces/Bils/radiogold&countColor=%23263759">
|
287 |
+
</div>
|
288 |
""")
|
289 |
+
|
290 |
+
# Event Handling
|
291 |
+
generate_btn.click(
|
292 |
+
generate_script,
|
293 |
+
[concept_input, model_selector, duration_selector],
|
294 |
+
[script_output, sound_output, music_output]
|
295 |
+
)
|
296 |
|
297 |
+
voice_btn.click(
|
298 |
+
generate_voice,
|
299 |
+
[script_output, tts_selector, speed_selector],
|
300 |
+
[voice_audio, voice_viz],
|
301 |
+
preprocess=create_audio_visualization
|
302 |
+
)
|
303 |
+
|
304 |
+
music_btn.click(
|
305 |
+
generate_music,
|
306 |
+
[music_output],
|
307 |
+
[music_audio, music_viz],
|
308 |
+
preprocess=create_audio_visualization
|
309 |
+
)
|
310 |
+
|
311 |
+
mix_btn.click(
|
312 |
+
blend_audio,
|
313 |
+
[voice_audio, music_audio],
|
314 |
+
[final_mix_audio, final_mix_viz],
|
315 |
+
preprocess=create_audio_visualization
|
316 |
+
)
|
317 |
|
318 |
+
if __name__ == "__main__":
|
319 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|