Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,8 @@
|
|
| 1 |
-
import os, tempfile, wave, uuid
|
| 2 |
-
from datetime import datetime
|
| 3 |
-
|
| 4 |
import gradio as gr
|
| 5 |
-
from google import genai
|
| 6 |
-
from google.genai import types
|
| 7 |
-
|
| 8 |
-
# ---------- Helpers ----------
|
| 9 |
-
def save_wav_bytes(pcm: bytes, rate_hz: int = 24000, channels: int = 1, sample_width: int = 2) -> str:
|
| 10 |
-
name = f"tts-{datetime.now().strftime('%Y%m%d-%H%M%S')}-{uuid.uuid4().hex[:6]}.wav"
|
| 11 |
-
path = os.path.join(tempfile.gettempdir(), name)
|
| 12 |
-
with wave.open(path, "wb") as wf:
|
| 13 |
-
wf.setnchannels(channels) # mono
|
| 14 |
-
wf.setsampwidth(sample_width) # 16-bit
|
| 15 |
-
wf.setframerate(rate_hz) # 24 kHz
|
| 16 |
-
wf.writeframes(pcm)
|
| 17 |
-
return path
|
| 18 |
-
|
| 19 |
-
def wav_to_mp3(wav_path: str, bitrate: str = "192k") -> str:
|
| 20 |
-
# Requiere ffmpeg (instalado vía apt.txt) y pydub
|
| 21 |
-
from pydub import AudioSegment
|
| 22 |
-
audio = AudioSegment.from_wav(wav_path)
|
| 23 |
-
mp3_path = wav_path.replace(".wav", ".mp3")
|
| 24 |
-
audio.export(mp3_path, format="mp3", bitrate=bitrate)
|
| 25 |
-
return mp3_path
|
| 26 |
-
|
| 27 |
-
def synthesize(api_key: str, text: str, model: str, voice_name: str, make_mp3: bool):
|
| 28 |
-
if not api_key or not api_key.strip():
|
| 29 |
-
raise gr.Error("Falta GOOGLE_API_KEY (cárgala a la izquierda o como Secret).")
|
| 30 |
-
if not text or not text.strip():
|
| 31 |
-
raise gr.Error("El texto está vacío.")
|
| 32 |
-
|
| 33 |
-
client = genai.Client(api_key=api_key.strip())
|
| 34 |
-
resp = client.models.generate_content(
|
| 35 |
-
model=model,
|
| 36 |
-
contents=text,
|
| 37 |
-
config=types.GenerateContentConfig(
|
| 38 |
-
response_modalities=["AUDIO"],
|
| 39 |
-
speech_config=types.SpeechConfig(
|
| 40 |
-
voice_config=types.VoiceConfig(
|
| 41 |
-
prebuilt_voice_config=types.PrebuiltVoiceConfig(voice_name=voice_name)
|
| 42 |
-
)
|
| 43 |
-
),
|
| 44 |
-
),
|
| 45 |
-
)
|
| 46 |
-
pcm = resp.candidates[0].content.parts[0].inline_data.data
|
| 47 |
-
wav_path = save_wav_bytes(pcm)
|
| 48 |
-
mp3_path = wav_path
|
| 49 |
-
if make_mp3:
|
| 50 |
-
mp3_path = wav_to_mp3(wav_path)
|
| 51 |
-
return wav_path, (mp3_path if make_mp3 else None), f"✅ Audio generado ({voice_name} · {model})"
|
| 52 |
-
|
| 53 |
-
# ---------- UI ----------
|
| 54 |
-
DEFAULT_MODEL = "gemini-2.5-flash-preview-tts"
|
| 55 |
-
VOICES = ["Kore","Zephyr","Puck","Charon","Fenrir","Leda","Aoede"]
|
| 56 |
-
|
| 57 |
-
with gr.Blocks(title="Gemini 2.5 · Texto a Voz") as demo:
|
| 58 |
-
gr.Markdown("# Gemini 2.5 · Texto a Voz (TTS)")
|
| 59 |
-
gr.Markdown("Convierte texto → **audio** con Gemini 2.5. Ahora también exporta **MP3**.")
|
| 60 |
-
|
| 61 |
-
with gr.Row():
|
| 62 |
-
api_key = gr.Textbox(label="GOOGLE_API_KEY", type="password", placeholder="Pega tu clave (si no está como Secret)")
|
| 63 |
-
model = gr.Textbox(label="Modelo", value=DEFAULT_MODEL, placeholder="gemini-2.5-flash-preview-tts")
|
| 64 |
-
voice = gr.Dropdown(label="Voz", choices=VOICES, value="Kore")
|
| 65 |
-
text = gr.Textbox(label="Texto", lines=5, placeholder="Escribe el texto a sintetizar…")
|
| 66 |
-
make_mp3 = gr.Checkbox(label="Exportar también MP3", value=True)
|
| 67 |
-
|
| 68 |
-
with gr.Row():
|
| 69 |
-
btn = gr.Button("Generar audio", variant="primary")
|
| 70 |
-
status = gr.Markdown("")
|
| 71 |
-
|
| 72 |
-
audio_wav = gr.Audio(label="Reproducción (WAV)", type="filepath", interactive=False)
|
| 73 |
-
dl_wav = gr.File(label="Descargar WAV")
|
| 74 |
-
dl_mp3 = gr.File(label="Descargar MP3 (si se eligió)")
|
| 75 |
-
|
| 76 |
-
def do_tts(api_key, text, model, voice, make_mp3):
|
| 77 |
-
wav_path, mp3_path, msg = synthesize(api_key, text, model, voice, make_mp3)
|
| 78 |
-
return msg, wav_path, wav_path, (mp3_path if make_mp3 else None)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
inputs=[api_key, text, model, voice, make_mp3],
|
| 83 |
-
outputs=[status, audio_wav, dl_wav, dl_mp3]
|
| 84 |
-
)
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
app =
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
def echo(txt):
|
| 4 |
+
return f"OK: {txt}"
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Nota: definimos el objeto a NIVEL MÓDULO
|
| 7 |
+
app = gr.Interface(fn=echo, inputs="text", outputs="text", title="Smoke test")
|
| 8 |
|