gnosticdev commited on
Commit
039f896
·
verified ·
1 Parent(s): c4c8284

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import asyncio
4
+ import torch
5
+ from transformers import pipeline
6
+ from pydub import AudioSegment
7
+ import io
8
+
9
+ # Configuración del modelo local (CPU)
10
+ pipe = pipeline(
11
+ "text-generation",
12
+ model="OpenAssistant/llama2-7b-orca-8k-3319",
13
+ device="cpu",
14
+ torch_dtype=torch.float32
15
+ )
16
+
17
+ # TTS básico (simulado - reemplázalo con Silero/Coqui si prefieres)
18
+ def text_to_speech(text, language="es"):
19
+ # Mock: Guarda un audio vacío (implementa TTS real aquí)
20
+ audio_path = "output.mp3"
21
+ AudioSegment.silent(duration=1000).export(audio_path, format="mp3")
22
+ return audio_path
23
+
24
+ async def synthesize(text_url, text_input, language="es"):
25
+ # Usa el texto directo o extrae de la URL
26
+ text = text_input if text_input else await fetch_text(text_url)
27
+
28
+ if not text:
29
+ return "Error: No hay texto para procesar", None
30
+
31
+ # Genera diálogo en el idioma del texto
32
+ prompt = f"Convierte esto en un diálogo de podcast (en {language}): {text}"
33
+ output = pipe(prompt, max_length=1000)
34
+ conversation = output[0]["generated_text"]
35
+
36
+ # Convierte a audio (simulado)
37
+ audio_path = text_to_speech(conversation, language)
38
+ return conversation, audio_path
39
+
40
+ def synthesize_sync(text_url, text_input, language):
41
+ return asyncio.run(synthesize(text_url, text_input, language))
42
+
43
+ # Interfaz
44
+ with gr.Blocks(theme='gstaff/sketch') as demo:
45
+ gr.Markdown("# 🎙 Convertir Texto/URL en Podcast")
46
+ gr.Markdown("### Soporta español/otros idiomas (sin GPU/API)")
47
+
48
+ with gr.Group():
49
+ text_url = gr.Textbox(label="URL del artículo (opcional)", placeholder="Ej: https://example.com")
50
+ text_input = gr.Textbox(label="O pega el texto aquí", lines=5, placeholder="Hola, esto es un ejemplo...")
51
+ language = gr.Dropdown(
52
+ label="Idioma",
53
+ choices=["es", "en", "fr", "de"],
54
+ value="es"
55
+ )
56
+ btn = gr.Button("Generar Podcast", variant="primary")
57
+
58
+ with gr.Row():
59
+ conv_display = gr.Textbox(label="Conversación generada", interactive=False)
60
+ aud = gr.Audio(label="Podcast", interactive=False)
61
+
62
+ btn.click(
63
+ synthesize_sync,
64
+ inputs=[text_url, text_input, language],
65
+ outputs=[conv_display, aud]
66
+ )
67
+
68
+ demo.launch()