File size: 5,245 Bytes
e0e731e
 
 
 
 
 
f7d0739
e0e731e
 
0781598
e0e731e
 
 
 
 
 
 
 
 
 
 
 
 
bf42cab
e0e731e
f7d0739
e0e731e
6b9bc13
 
 
 
 
e0e731e
 
 
 
 
baddd3f
e0e731e
6b9bc13
 
 
 
 
 
e0e731e
6b9bc13
 
e0e731e
6b9bc13
 
 
b8acae3
6b9bc13
 
 
 
 
 
 
 
e0e731e
 
 
 
 
bf42cab
e0e731e
 
b8acae3
e0e731e
 
 
 
b8acae3
e0e731e
 
f7d0739
0781598
 
 
 
 
 
85e24ba
 
e0e731e
85e24ba
 
 
 
b8acae3
0781598
 
e0e731e
 
 
 
 
 
 
 
0781598
b8acae3
e0e731e
b8acae3
0781598
 
 
 
 
 
0b10e6e
e0e731e
b8acae3
 
85e24ba
e0e731e
bf42cab
0781598
e0e731e
 
 
 
bf42cab
e0e731e
f7d0739
e0e731e
 
 
 
 
 
 
 
 
 
 
 
 
 
6b9bc13
 
e0e731e
 
6b9bc13
 
e0e731e
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import sys
import io, os, stat
import subprocess
import random
from zipfile import ZipFile
import uuid
import time
import torch
import torchaudio
import time
# Mantenemos la descarga de MeCab
os.system('python -m unidic download')

# Mantenemos el acuerdo de CPML
os.environ["COQUI_TOS_AGREED"] = "1"

import langid
import base64
import csv
from io import StringIO
import datetime
import re

import gradio as gr
from scipy.io.wavfile import write
from pydub import AudioSegment

from TTS.api import TTS
from TTS.tts.configs.xtts_config import XttsConfig
from TTS.tts.models.xtts import Xtts
from TTS.utils.generic_utils import get_user_data_dir

HF_TOKEN = os.environ.get("HF_TOKEN")

from huggingface_hub import hf_hub_download
import os
from TTS.utils.manage import get_user_data_dir

# Mantenemos la autenticación y descarga del modelo
repo_id = "Blakus/Pedro_Lab_XTTS"
local_dir = os.path.join(get_user_data_dir("tts"), "tts_models--multilingual--multi-dataset--xtts_v2")
os.makedirs(local_dir, exist_ok=True)
files_to_download = ["config.json", "model.pth", "vocab.json"]
for file_name in files_to_download:
    print(f"Downloading {file_name} from {repo_id}")
    local_file_path = os.path.join(local_dir, file_name)
    hf_hub_download(repo_id=repo_id, filename=file_name, local_dir=local_dir)

# Cargamos configuración y modelo
config_path = os.path.join(local_dir, "config.json")
checkpoint_path = os.path.join(local_dir, "model.pth")
vocab_path = os.path.join(local_dir, "vocab.json")

config = XttsConfig()
config.load_json(config_path)

model = Xtts.init_from_config(config)
model.load_checkpoint(config, checkpoint_path=checkpoint_path, vocab_path=vocab_path, eval=True, use_deepspeed=False)

print("Modelo cargado en CPU")

# Mantenemos variables globales y funciones auxiliares
DEVICE_ASSERT_DETECTED = 0
DEVICE_ASSERT_PROMPT = None
DEVICE_ASSERT_LANG = None
supported_languages = config.languages

# Función de inferencia usando parámetros predeterminados del archivo de configuración
def predict(prompt, language, audio_file_pth, mic_file_path, use_mic):
    try:
        if use_mic:
            speaker_wav = mic_file_path
        else:
            speaker_wav = audio_file_pth

        if len(prompt) < 2 or len(prompt) > 200:
            return None, None, "El texto debe tener entre 2 y 200 caracteres."

        # Usamos los valores de la configuración directamente
        temperature = getattr(config, "temperature", 0.75)
        repetition_penalty = getattr(config, "repetition_penalty", 5.0)
        gpt_cond_len = getattr(config, "gpt_cond_len", 30)
        gpt_cond_chunk_len = getattr(config, "gpt_cond_chunk_len", 4)
        max_ref_length = getattr(config, "max_ref_len", 60)

        gpt_cond_latent, speaker_embedding = model.get_conditioning_latents(
            audio_path=speaker_wav,
            gpt_cond_len=gpt_cond_len,
            gpt_cond_chunk_len=gpt_cond_chunk_len,
            max_ref_length=max_ref_length
        )

        # Medimos el tiempo de inferencia manualmente
        start_time = time.time()
        out = model.inference(
            prompt,
            language,
            gpt_cond_latent,
            speaker_embedding,
            temperature=temperature,
            repetition_penalty=repetition_penalty,
        )
        inference_time = time.time() - start_time
        
        torchaudio.save("output.wav", torch.tensor(out["wav"]).unsqueeze(0), 24000)

        # Calculamos las métricas usando el tiempo medido manualmente
        audio_length = len(out["wav"]) / 24000  # duración del audio en segundos
        real_time_factor = inference_time / audio_length

        metrics_text = f"Tiempo de generación: {inference_time:.2f} segundos\n"
        metrics_text += f"Factor de tiempo real: {real_time_factor:.2f}"

        return gr.make_waveform("output.wav"), "output.wav", metrics_text

    except Exception as e:
        print(f"Error detallado: {str(e)}")
        return None, None, f"Error: {str(e)}"


# Interfaz de Gradio actualizada sin sliders
with gr.Blocks(theme=gr.themes.Base()) as demo:
    gr.Markdown("# Sintetizador de Voz XTTS")
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(label="Texto a sintetizar", placeholder="Escribe aquí el texto que quieres convertir a voz...")
            language = gr.Dropdown(label="Idioma", choices=supported_languages, value="es")
            audio_file = gr.Audio(label="Audio de referencia", type="filepath")
            use_mic = gr.Checkbox(label="Usar micrófono")
            mic_file = gr.Audio(label="Grabar con micrófono", source="microphone", type="filepath", visible=False)
            
            use_mic.change(fn=lambda x: gr.update(visible=x), inputs=[use_mic], outputs=[mic_file])
            
            generate_button = gr.Button("Generar voz")
        
        with gr.Column():
            output_audio = gr.Audio(label="Audio generado")
            waveform = gr.Image(label="Forma de onda")
            metrics = gr.Textbox(label="Métricas")
    
    generate_button.click(
        predict,
        inputs=[input_text, language, audio_file, mic_file, use_mic],
        outputs=[waveform, output_audio, metrics]
    )

demo.launch(debug=True)