File size: 4,340 Bytes
d4be823
83b64d5
d4be823
83b64d5
b3ce194
fb669e4
b3ce194
f7f4451
 
b3ce194
6c641c6
d4be823
b3ce194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83b64d5
f7f4451
d289451
 
f7f4451
b3ce194
 
 
 
 
 
 
 
 
 
 
f7f4451
 
 
 
83b64d5
f7f4451
 
 
b3ce194
f7f4451
1acc93a
f7f4451
9e404ee
f7f4451
1acc93a
f7f4451
83b64d5
b3ce194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d289451
 
b3ce194
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import gradio as gr
from kokoro import KPipeline
import numpy as np
import torch
import os

# Detectar hardware
device = "cuda" if torch.cuda.is_available() else "cpu"

# Inicializar pipeline - 'e' para español
pipeline = KPipeline(lang_code='e', device=device)

# Lista COMPLETA de voces del Space original
VOICES = [
    "af_alloy.pt", "af_aoede.pt", "af_bella.pt", "af_heart.pt", "af_jessica.pt", 
    "af_kore.pt", "af_nicole.pt", "af_nova.pt", "af_river.pt", "af_sarah.pt", 
    "af_sky.pt", "am_adam.pt", "am_echo.pt", "am_eric.pt", "am_fenrir.pt", 
    "am_liam.pt", "am_michael.pt", "am_onyx.pt", "am_puck.pt", "bf_alice.pt", 
    "bf_isabella.pt", "bf_lily.pt", "bm_daniel.pt", "bm_fable.pt", "bm_george.pt", 
    "bm_lewis.pt", "ef_dora.pt", "em_alex.pt", "em_santa.pt", "ff_siwis.pt", 
    "hf_alpha.pt", "hf_beta.pt", "hm_omega.pt", "hm_psi.pt", "if_sara.pt", 
    "im_nicola.pt", "jf_alpha.pt", "jf_gongitsune.pt", "jf_nezumi.pt", 
    "jf_tebukuro.pt", "jm_kumo.pt", "pf_dora.pt", "pm_alex.pt", "pm_santa.pt", 
    "zf_xiaobei.pt", "zf_xiaoni.pt", "zf_xiaoyi.pt", "zm_yunjian.pt", 
    "zm_yunxi.pt", "zm_yunxia.pt", "zm_yunyang.pt"
]

def tts_pro(text, voice_name):
    if not text or not text.strip():
        return None
    
    try:
        # Verificar si el archivo de voz existe
        voice_path = f"voices/{voice_name}"
        
        # Si no está en voices/, buscar en el directorio actual
        if not os.path.exists(voice_path):
            voice_path = voice_name
        
        print(f"Usando voz: {voice_path}")
        
        # Generar audio - pasar la ruta del archivo .pt como voz
        generator = pipeline(text, voice=voice_path, speed=1.0)
        
        audio_segments = []
        for _, _, audio in generator:
            audio_segments.append(audio)
        
        if not audio_segments:
            return None
            
        # Concatenar audio
        final_audio = np.concatenate(audio_segments)
        
        return (24000, final_audio)
        
    except Exception as e:
        print(f"Error en la generación: {e}")
        return None

# Crear la interfaz
with gr.Blocks(title="Kokoro TTS - Aliah Plus", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🎤 Kokoro TTS v1.0")
    gr.Markdown("### Texto a Voz con múltiples voces")
    gr.Markdown("Escribe el texto y selecciona una voz para generar audio.")
    
    with gr.Row():
        with gr.Column():
            texto_input = gr.Textbox(
                label="Texto a convertir",
                placeholder="Escribe aquí el texto en español...",
                lines=4,
                max_lines=10
            )
            
            voz_seleccionada = gr.Dropdown(
                choices=VOICES,
                label="Selecciona una voz",
                value="af_bella.pt",
                info="Elige entre más de 50 voces diferentes"
            )
            
            generar_btn = gr.Button("🎵 Generar Audio", variant="primary")
        
        with gr.Column():
            audio_output = gr.Audio(
                label="Audio Generado",
                type="numpy",
                interactive=False
            )
    
    # Ejemplos
    gr.Markdown("### Ejemplos rápidos:")
    ejemplos = gr.Examples(
        examples=[
            ["Hola, ¿cómo estás? Soy una voz generada por inteligencia artificial.", "af_bella.pt"],
            ["Bienvenido al sistema de texto a voz más avanzado.", "af_nova.pt"],
            ["La tecnología de síntesis de voz ha avanzado mucho.", "am_adam.pt"]
        ],
        inputs=[texto_input, voz_seleccionada],
        outputs=audio_output,
        fn=tts_pro,
        cache_examples=False
    )
    
    # Conectar el botón
    generar_btn.click(
        fn=tts_pro,
        inputs=[texto_input, voz_seleccionada],
        outputs=audio_output
    )
    
    # Información adicional
    gr.Markdown("---")
    gr.Markdown("""
    ### 📝 Notas:
    - Las voces están en formato `.pt` y se cargan desde la carpeta `voices/`
    - Tiempo de generación: ~5-10 segundos dependiendo del texto
    - Formato de audio: 24kHz, mono
    - Modelo: Kokoro TTS v1.0
    """)

if __name__ == "__main__":
    demo.launch(
        share=False,
        server_name="0.0.0.0" if os.getenv('SPACE_ID') else None,
        server_port=7860
    )