"""Voz de los tutoriales de FacturaSeguraSV, en una página web (Hugging Face Space). Usa exactamente lo mismo que el grabador de los videos (video-pipeline/proxy/tts_proxy.py): Kokoro-82M v1.0 en ONNX, kokoro-onnx 0.6.1, voz ef_dora y acento latinoamericano (espeak-ng "es-419"). Al arrancar descarga el modelo y comprueba su huella SHA-256: si no coincide con el de los videos, no arranca. """ from __future__ import annotations import hashlib import re import urllib.request from pathlib import Path import gradio as gr import numpy as np from kokoro_onnx import Kokoro URL_MODELOS = "https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files-v1.0/" ARCHIVOS = { # nombre → SHA-256 de los archivos que usan los videos "kokoro-v1.0.onnx": "7d5df8ecf7d4b1878015a32686053fd0eebe2bc377234608764cc0ef3636a6c5", "voices-v1.0.bin": "bca610b8308e8d99f32e6fe4197e7ec01679264efed0cac9140fe9c29f1fbf7d", } CARPETA = Path(__file__).resolve().parent / "modelo" ACENTO = "es-419" # español latinoamericano: la "c" y la "z" suenan como "s" VOCES = { "Dora (femenina) · la de los tutoriales": "ef_dora", "Alex (masculina)": "em_alex", "Santa (masculina)": "em_santa", } # Cómo se pronuncia en voz (igual que en los videos). Solo cambia el audio. PRONUNCIACION = {"FacturaSeguraSV": "Factura Segura ese ve"} MAX_CARACTERES = 5000 def huella(ruta: Path) -> str: h = hashlib.sha256() with ruta.open("rb") as f: for bloque in iter(lambda: f.read(1 << 20), b""): h.update(bloque) return h.hexdigest() def preparar_modelo() -> Kokoro: CARPETA.mkdir(exist_ok=True) for nombre, esperada in ARCHIVOS.items(): ruta = CARPETA / nombre if not ruta.exists() or huella(ruta) != esperada: print(f"Descargando {nombre}…", flush=True) urllib.request.urlretrieve(URL_MODELOS + nombre, ruta) if huella(ruta) != esperada: raise RuntimeError(f"{nombre} no es el mismo modelo de los videos (huella distinta).") return Kokoro(str(CARPETA / "kokoro-v1.0.onnx"), str(CARPETA / "voices-v1.0.bin")) KOKORO = preparar_modelo() def generar(texto: str, voz: str, velocidad: float, pronunciar_marca: bool): texto = re.sub(r"\s+", " ", texto or "").strip() if not texto: raise gr.Error("Escribe el texto que quieres convertir en voz.") if len(texto) > MAX_CARACTERES: raise gr.Error(f"El texto tiene {len(texto)} caracteres; el máximo es {MAX_CARACTERES}. Divídelo en partes.") if pronunciar_marca: for escrito, hablado in PRONUNCIACION.items(): texto = texto.replace(escrito, hablado) audio, sr = KOKORO.create(texto, voice=VOCES[voz], speed=float(velocidad), lang=ACENTO) return sr, (np.clip(audio, -1, 1) * 32767).astype(np.int16) with gr.Blocks(title="Voz de los tutoriales · FacturaSeguraSV") as demo: gr.Markdown( "# Voz de los tutoriales de FacturaSeguraSV\n" "Pega el texto, pulsa **Generar** y descarga el audio con el botón de descarga del reproductor. " "Es la misma voz y el mismo acento de los videos." ) with gr.Row(): with gr.Column(): texto = gr.Textbox(label="Texto", lines=8, max_length=MAX_CARACTERES, placeholder="Hola, bienvenido a FacturaSeguraSV…") voz = gr.Dropdown(list(VOCES), value=list(VOCES)[0], label="Voz") velocidad = gr.Slider(0.7, 1.3, value=1.0, step=0.05, label="Velocidad (los videos usan 1.0)") marca = gr.Checkbox(value=True, label='Leer "FacturaSeguraSV" como "Factura Segura ese ve"') boton = gr.Button("Generar", variant="primary") with gr.Column(): salida = gr.Audio(label="Audio", type="numpy", format="mp3", interactive=False) gr.Examples( [["Hola, bienvenido a FacturaSeguraSV. En este video vas a aprender a emitir una factura."]], inputs=[texto], ) boton.click(generar, inputs=[texto, voz, velocidad, marca], outputs=salida) if __name__ == "__main__": demo.queue(default_concurrency_limit=1).launch()