Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import asyncio
|
| 4 |
-
import json
|
| 5 |
from conver import ConversationConfig, URLToAudioConverter
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from pydub import AudioSegment
|
|
@@ -25,7 +24,7 @@ def mezclar_musica_y_tags(audio_path: str, custom_music_path: str = None) -> str
|
|
| 25 |
musica_fondo_loop = musica_fondo_loop[:duracion_podcast]
|
| 26 |
|
| 27 |
mezcla = musica_fondo_loop.overlay(podcast_audio)
|
| 28 |
-
mezcla = mezcla + tag_outro
|
| 29 |
|
| 30 |
silent_ranges = []
|
| 31 |
for i in range(0, len(podcast_audio) - 500, 100):
|
|
@@ -40,41 +39,16 @@ def mezclar_musica_y_tags(audio_path: str, custom_music_path: str = None) -> str
|
|
| 40 |
mezcla.export(output_path, format="mp3")
|
| 41 |
return output_path
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
if not article_url and not text_input:
|
| 45 |
return "Error: Ingresa una URL o texto", None
|
| 46 |
|
| 47 |
try:
|
| 48 |
config = ConversationConfig(custom_prompt_template=custom_prompt)
|
| 49 |
converter = URLToAudioConverter(config, llm_api_key=os.environ.get("TOGETHER_API_KEY"))
|
| 50 |
-
|
| 51 |
-
if skip_llm and text_input:
|
| 52 |
-
dialogue = {"conversation": [{"speaker": "Anfitrión1", "text": text_input}]}
|
| 53 |
-
elif text_input:
|
| 54 |
-
dialogue = converter.extract_conversation(text_input)
|
| 55 |
-
else:
|
| 56 |
-
dialogue = converter.extract_conversation(await converter.fetch_text(article_url))
|
| 57 |
-
|
| 58 |
-
json_str = json.dumps(dialogue, indent=2, ensure_ascii=False)
|
| 59 |
-
print(f"JSON generado: {json_str[:500]}...") # Depuración
|
| 60 |
-
return json_str, dialogue
|
| 61 |
-
except Exception as e:
|
| 62 |
-
return f"Error: {str(e)}", None
|
| 63 |
-
|
| 64 |
-
async def generate_audio(dialogue_json, language, agregar_musica, custom_music):
|
| 65 |
-
try:
|
| 66 |
-
print(f"JSON recibido: {dialogue_json[:500]}...") # Depuración
|
| 67 |
-
json_str = dialogue_json.replace("'", '"')
|
| 68 |
-
try:
|
| 69 |
-
dialogue = json.loads(json_str)
|
| 70 |
-
except json.JSONDecodeError as e:
|
| 71 |
-
return f"Error: JSON inválido - {str(e)}", None
|
| 72 |
-
|
| 73 |
-
if not dialogue.get("conversation"):
|
| 74 |
-
return "Error: El JSON no contiene 'conversation'", None
|
| 75 |
-
|
| 76 |
-
config = ConversationConfig()
|
| 77 |
-
converter = URLToAudioConverter(config, llm_api_key=os.environ.get("TOGETHER_API_KEY"))
|
| 78 |
|
| 79 |
voices = {
|
| 80 |
"en": ("en-US-AvaMultilingualNeural", "en-US-AndrewMultilingualNeural"),
|
|
@@ -82,7 +56,12 @@ async def generate_audio(dialogue_json, language, agregar_musica, custom_music):
|
|
| 82 |
}
|
| 83 |
voice1, voice2 = voices.get(language, voices["en"])
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
if agregar_musica:
|
| 88 |
output_file = mezclar_musica_y_tags(output_file, custom_music)
|
|
@@ -91,12 +70,6 @@ async def generate_audio(dialogue_json, language, agregar_musica, custom_music):
|
|
| 91 |
except Exception as e:
|
| 92 |
return f"Error: {str(e)}", None
|
| 93 |
|
| 94 |
-
def synthesize_sync(article_url, text_input, language, skip_llm, custom_prompt):
|
| 95 |
-
return asyncio.run(generate_dialogue(article_url, text_input, language, skip_llm, custom_prompt))
|
| 96 |
-
|
| 97 |
-
def generate_audio_sync(dialogue_json, language, agregar_musica, custom_music):
|
| 98 |
-
return asyncio.run(generate_audio(dialogue_json, language, agregar_musica, custom_music))
|
| 99 |
-
|
| 100 |
with gr.Blocks(theme='gstaff/sketch') as demo:
|
| 101 |
gr.Markdown("# 🎙 Podcast Converter")
|
| 102 |
with gr.Group():
|
|
@@ -104,30 +77,21 @@ with gr.Blocks(theme='gstaff/sketch') as demo:
|
|
| 104 |
text_input = gr.Textbox(label="Texto manual", lines=5, placeholder="Pega tu texto aquí...")
|
| 105 |
language = gr.Dropdown(["en", "es"], label="Idioma", value="en")
|
| 106 |
skip_llm = gr.Checkbox(label="🔴 Modo libre (sin filtros LLM)", value=False)
|
|
|
|
|
|
|
| 107 |
custom_prompt = gr.Textbox(
|
| 108 |
label="Prompt personalizado (opcional)",
|
| 109 |
-
placeholder=
|
| 110 |
)
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
with gr.Group():
|
| 114 |
-
dialogue_json = gr.Textbox(label="Diálogo JSON (editable)", lines=10, interactive=True)
|
| 115 |
-
agregar_musica = gr.Checkbox(label="🎵 Agregar música de fondo y cortinillas", value=False)
|
| 116 |
-
custom_music = gr.File(label="Subir música de fondo (opcional)", file_types=[".mp3"])
|
| 117 |
-
btn_audio = gr.Button("Generar Audio", variant="primary")
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
conv_display = gr.Textbox(label="Conversación", interactive=False, lines=10)
|
| 121 |
aud = gr.Audio(label="Audio Generado", interactive=False)
|
| 122 |
|
| 123 |
-
|
| 124 |
synthesize_sync,
|
| 125 |
-
inputs=[text_url, text_input, language, skip_llm, custom_prompt],
|
| 126 |
-
outputs=[dialogue_json, dialogue_json]
|
| 127 |
-
)
|
| 128 |
-
btn_audio.click(
|
| 129 |
-
generate_audio_sync,
|
| 130 |
-
inputs=[dialogue_json, language, agregar_musica, custom_music],
|
| 131 |
outputs=[conv_display, aud]
|
| 132 |
)
|
| 133 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import asyncio
|
|
|
|
| 4 |
from conver import ConversationConfig, URLToAudioConverter
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from pydub import AudioSegment
|
|
|
|
| 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):
|
|
|
|
| 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 = {
|
| 54 |
"en": ("en-US-AvaMultilingualNeural", "en-US-AndrewMultilingualNeural"),
|
|
|
|
| 56 |
}
|
| 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)
|
|
|
|
| 70 |
except Exception as e:
|
| 71 |
return f"Error: {str(e)}", None
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
with gr.Blocks(theme='gstaff/sketch') as demo:
|
| 74 |
gr.Markdown("# 🎙 Podcast Converter")
|
| 75 |
with gr.Group():
|
|
|
|
| 77 |
text_input = gr.Textbox(label="Texto manual", lines=5, placeholder="Pega tu texto aquí...")
|
| 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():
|
| 89 |
conv_display = gr.Textbox(label="Conversación", interactive=False, lines=10)
|
| 90 |
aud = gr.Audio(label="Audio Generado", interactive=False)
|
| 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 |
|