Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
import torch
|
3 |
import tempfile
|
4 |
from scipy.io.wavfile import write
|
@@ -31,6 +32,16 @@ LLAMA_PIPELINES = {}
|
|
31 |
MUSICGEN_MODELS = {}
|
32 |
TTS_MODELS = {}
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
# ---------------------------------------------------------------------
|
35 |
# Helper Functions
|
36 |
# ---------------------------------------------------------------------
|
@@ -100,7 +111,7 @@ def generate_script(user_prompt: str, model_id: str, token: str, duration: int):
|
|
100 |
f"Based on the user's concept and the selected duration of {duration} seconds, produce the following: "
|
101 |
"1. A concise voice-over script. Prefix this section with 'Voice-Over Script:'.\n"
|
102 |
"2. Suggestions for sound design. Prefix this section with 'Sound Design Suggestions:'.\n"
|
103 |
-
"3. Music styles or track recommendations. Prefix this section with 'Music Suggestions:'."
|
104 |
)
|
105 |
combined_prompt = f"{system_prompt}\nUser concept: {user_prompt}\nOutput:"
|
106 |
|
@@ -163,11 +174,14 @@ def generate_voice(script: str, tts_model_name: str = "tts_models/en/ljspeech/ta
|
|
163 |
if not script.strip():
|
164 |
return "Error: No script provided."
|
165 |
|
|
|
|
|
|
|
166 |
tts_model = get_tts_model(tts_model_name)
|
167 |
|
168 |
# Generate and save voice
|
169 |
output_path = os.path.join(tempfile.gettempdir(), "voice_over.wav")
|
170 |
-
tts_model.tts_to_file(text=
|
171 |
return output_path
|
172 |
|
173 |
except Exception as e:
|
@@ -230,14 +244,14 @@ def blend_audio(voice_path: str, music_path: str, ducking: bool, duck_level: int
|
|
230 |
voice_len = len(voice) # in milliseconds
|
231 |
music_len = len(music) # in milliseconds
|
232 |
|
233 |
-
# Loop music if it's shorter than voice
|
234 |
if music_len < voice_len:
|
235 |
looped_music = AudioSegment.empty()
|
236 |
while len(looped_music) < voice_len:
|
237 |
looped_music += music
|
238 |
music = looped_music
|
239 |
|
240 |
-
# Trim music if it's longer than voice
|
241 |
if len(music) > voice_len:
|
242 |
music = music[:voice_len]
|
243 |
|
|
|
1 |
import os
|
2 |
+
import re
|
3 |
import torch
|
4 |
import tempfile
|
5 |
from scipy.io.wavfile import write
|
|
|
32 |
MUSICGEN_MODELS = {}
|
33 |
TTS_MODELS = {}
|
34 |
|
35 |
+
# ---------------------------------------------------------------------
|
36 |
+
# Utility Function: Clean Text
|
37 |
+
# ---------------------------------------------------------------------
|
38 |
+
def clean_text(text: str) -> str:
|
39 |
+
"""
|
40 |
+
Removes undesired characters (e.g., asterisks) that might not be recognized by the model's vocabulary.
|
41 |
+
"""
|
42 |
+
# Remove all asterisks. You can add more cleaning steps here as needed.
|
43 |
+
return re.sub(r'\*', '', text)
|
44 |
+
|
45 |
# ---------------------------------------------------------------------
|
46 |
# Helper Functions
|
47 |
# ---------------------------------------------------------------------
|
|
|
111 |
f"Based on the user's concept and the selected duration of {duration} seconds, produce the following: "
|
112 |
"1. A concise voice-over script. Prefix this section with 'Voice-Over Script:'.\n"
|
113 |
"2. Suggestions for sound design. Prefix this section with 'Sound Design Suggestions:'.\n"
|
114 |
+
"3. Music styles or track recommendations. Prefix this section with 'Music Suggestions:'."
|
115 |
)
|
116 |
combined_prompt = f"{system_prompt}\nUser concept: {user_prompt}\nOutput:"
|
117 |
|
|
|
174 |
if not script.strip():
|
175 |
return "Error: No script provided."
|
176 |
|
177 |
+
# Clean the script to remove special characters (e.g., asterisks) that may produce warnings
|
178 |
+
cleaned_script = clean_text(script)
|
179 |
+
|
180 |
tts_model = get_tts_model(tts_model_name)
|
181 |
|
182 |
# Generate and save voice
|
183 |
output_path = os.path.join(tempfile.gettempdir(), "voice_over.wav")
|
184 |
+
tts_model.tts_to_file(text=cleaned_script, file_path=output_path)
|
185 |
return output_path
|
186 |
|
187 |
except Exception as e:
|
|
|
244 |
voice_len = len(voice) # in milliseconds
|
245 |
music_len = len(music) # in milliseconds
|
246 |
|
247 |
+
# Loop music if it's shorter than the voice
|
248 |
if music_len < voice_len:
|
249 |
looped_music = AudioSegment.empty()
|
250 |
while len(looped_music) < voice_len:
|
251 |
looped_music += music
|
252 |
music = looped_music
|
253 |
|
254 |
+
# Trim music if it's longer than the voice
|
255 |
if len(music) > voice_len:
|
256 |
music = music[:voice_len]
|
257 |
|