Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,11 +11,12 @@ MUSICA_FONDO = "musica.mp3"
|
|
11 |
TAG1 = "tag.mp3"
|
12 |
TAG2 = "tag2.mp3"
|
13 |
|
14 |
-
def mezclar_musica_y_tags(audio_path: str) -> str:
|
15 |
podcast_audio = AudioSegment.from_file(audio_path)
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
19 |
|
20 |
duracion_podcast = len(podcast_audio)
|
21 |
repeticiones = (duracion_podcast // len(musica_fondo)) + 1
|
@@ -23,21 +24,30 @@ def mezclar_musica_y_tags(audio_path: str) -> str:
|
|
23 |
musica_fondo_loop = musica_fondo_loop[:duracion_podcast]
|
24 |
|
25 |
mezcla = musica_fondo_loop.overlay(podcast_audio)
|
26 |
-
mezcla =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
output_path = audio_path.replace(".mp3", "_con_musica.mp3")
|
29 |
mezcla.export(output_path, format="mp3")
|
30 |
return output_path
|
31 |
|
32 |
-
def synthesize_sync(article_url, text_input, language, skip_llm, agregar_musica):
|
33 |
-
return asyncio.run(synthesize(article_url, text_input, language, skip_llm, agregar_musica))
|
34 |
|
35 |
-
async def synthesize(article_url, text_input, language="en", skip_llm=False, agregar_musica=False):
|
36 |
if not article_url and not text_input:
|
37 |
return "Error: Ingresa una URL o texto", None
|
38 |
|
39 |
try:
|
40 |
-
config = ConversationConfig()
|
41 |
converter = URLToAudioConverter(config, llm_api_key=os.environ.get("TOGETHER_API_KEY"))
|
42 |
|
43 |
voices = {
|
@@ -47,14 +57,14 @@ async def synthesize(article_url, text_input, language="en", skip_llm=False, agr
|
|
47 |
voice1, voice2 = voices.get(language, voices["en"])
|
48 |
|
49 |
if skip_llm and text_input:
|
50 |
-
output_file, conversation = await converter.raw_text_to_audio(text_input, voice1, voice2)
|
51 |
elif text_input:
|
52 |
-
output_file, conversation = await converter.text_to_audio(text_input, voice1, voice2)
|
53 |
else:
|
54 |
-
output_file, conversation = await converter.url_to_audio(article_url, voice1, voice2)
|
55 |
|
56 |
if agregar_musica:
|
57 |
-
output_file = mezclar_musica_y_tags(output_file)
|
58 |
|
59 |
return conversation, output_file
|
60 |
except Exception as e:
|
@@ -68,6 +78,11 @@ with gr.Blocks(theme='gstaff/sketch') as demo:
|
|
68 |
language = gr.Dropdown(["en", "es"], label="Idioma", value="en")
|
69 |
skip_llm = gr.Checkbox(label="🔴 Modo libre (sin filtros LLM)", value=False)
|
70 |
agregar_musica = gr.Checkbox(label="🎵 Agregar música de fondo y cortinillas", value=False)
|
|
|
|
|
|
|
|
|
|
|
71 |
btn = gr.Button("Generar Podcast", variant="primary")
|
72 |
|
73 |
with gr.Row():
|
@@ -76,8 +91,8 @@ with gr.Blocks(theme='gstaff/sketch') as demo:
|
|
76 |
|
77 |
btn.click(
|
78 |
synthesize_sync,
|
79 |
-
inputs=[text_url, text_input, language, skip_llm, agregar_musica],
|
80 |
outputs=[conv_display, aud]
|
81 |
)
|
82 |
|
83 |
-
demo.launch()
|
|
|
11 |
TAG1 = "tag.mp3"
|
12 |
TAG2 = "tag2.mp3"
|
13 |
|
14 |
+
def mezclar_musica_y_tags(audio_path: str, custom_music_path: str = None) -> str:
|
15 |
podcast_audio = AudioSegment.from_file(audio_path)
|
16 |
+
music_file = custom_music_path if custom_music_path and os.path.exists(custom_music_path) else MUSICA_FONDO
|
17 |
+
musica_fondo = AudioSegment.from_file(music_file).apply_gain(-15)
|
18 |
+
tag_outro = AudioSegment.from_file(TAG1).apply_gain(-5)
|
19 |
+
tag_trans = AudioSegment.from_file(TAG2).apply_gain(-5)
|
20 |
|
21 |
duracion_podcast = len(podcast_audio)
|
22 |
repeticiones = (duracion_podcast // len(musica_fondo)) + 1
|
|
|
24 |
musica_fondo_loop = musica_fondo_loop[:duracion_podcast]
|
25 |
|
26 |
mezcla = musica_fondo_loop.overlay(podcast_audio)
|
27 |
+
mezcla = mezcla + tag_outro # tag.mp3 como outro
|
28 |
+
|
29 |
+
silent_ranges = []
|
30 |
+
for i in range(0, len(podcast_audio) - 500, 100):
|
31 |
+
chunk = podcast_audio[i:i+500]
|
32 |
+
if chunk.dBFS < -40:
|
33 |
+
silent_ranges.append((i, i + 500))
|
34 |
+
for start, end in reversed(silent_ranges):
|
35 |
+
if (end - start) >= len(tag_trans):
|
36 |
+
mezcla = mezcla.overlay(tag_trans, position=start + 50)
|
37 |
|
38 |
output_path = audio_path.replace(".mp3", "_con_musica.mp3")
|
39 |
mezcla.export(output_path, format="mp3")
|
40 |
return output_path
|
41 |
|
42 |
+
def synthesize_sync(article_url, text_input, language, skip_llm, agregar_musica, custom_music, custom_prompt):
|
43 |
+
return asyncio.run(synthesize(article_url, text_input, language, skip_llm, agregar_musica, custom_music, custom_prompt))
|
44 |
|
45 |
+
async def synthesize(article_url, text_input, language="en", skip_llm=False, agregar_musica=False, custom_music=None, custom_prompt=None):
|
46 |
if not article_url and not text_input:
|
47 |
return "Error: Ingresa una URL o texto", None
|
48 |
|
49 |
try:
|
50 |
+
config = ConversationConfig(custom_prompt_template=custom_prompt)
|
51 |
converter = URLToAudioConverter(config, llm_api_key=os.environ.get("TOGETHER_API_KEY"))
|
52 |
|
53 |
voices = {
|
|
|
57 |
voice1, voice2 = voices.get(language, voices["en"])
|
58 |
|
59 |
if skip_llm and text_input:
|
60 |
+
output_file, conversation = await converter.raw_text_to_audio(text_input, voice1, voice2, custom_music)
|
61 |
elif text_input:
|
62 |
+
output_file, conversation = await converter.text_to_audio(text_input, voice1, voice2, custom_music)
|
63 |
else:
|
64 |
+
output_file, conversation = await converter.url_to_audio(article_url, voice1, voice2, custom_music)
|
65 |
|
66 |
if agregar_musica:
|
67 |
+
output_file = mezclar_musica_y_tags(output_file, custom_music)
|
68 |
|
69 |
return conversation, output_file
|
70 |
except Exception as e:
|
|
|
78 |
language = gr.Dropdown(["en", "es"], label="Idioma", value="en")
|
79 |
skip_llm = gr.Checkbox(label="🔴 Modo libre (sin filtros LLM)", value=False)
|
80 |
agregar_musica = gr.Checkbox(label="🎵 Agregar música de fondo y cortinillas", value=False)
|
81 |
+
custom_music = gr.File(label="Subir música de fondo (opcional)", file_types=[".mp3"])
|
82 |
+
custom_prompt = gr.Textbox(
|
83 |
+
label="Prompt personalizado (opcional)",
|
84 |
+
placeholder="{text}\nCrea un diálogo de podcast en español entre Anfitrión1 y Anfitrión2. Usa un tono informal y genera al menos 6 intercambios por hablante. Devuelve SOLO un objeto JSON: {\"conversation\": [{\"speaker\": \"Anfitrión1\", \"text\": \"...\"}, {\"speaker\": \"Anfitrión2\", \"text\": \"...\"}]}"
|
85 |
+
)
|
86 |
btn = gr.Button("Generar Podcast", variant="primary")
|
87 |
|
88 |
with gr.Row():
|
|
|
91 |
|
92 |
btn.click(
|
93 |
synthesize_sync,
|
94 |
+
inputs=[text_url, text_input, language, skip_llm, agregar_musica, custom_music, custom_prompt],
|
95 |
outputs=[conv_display, aud]
|
96 |
)
|
97 |
|
98 |
+
demo.launch()
|