Spaces:
Paused
Paused
Update chat_ai.py
Browse files- chat_ai.py +115 -269
chat_ai.py
CHANGED
@@ -1,28 +1,12 @@
|
|
1 |
-
# text_to_speech_ai.py
|
2 |
-
|
3 |
import re
|
4 |
import tempfile
|
5 |
-
import os
|
6 |
|
7 |
-
import torch
|
8 |
-
import click
|
9 |
import gradio as gr
|
|
|
10 |
import soundfile as sf
|
11 |
import torchaudio
|
12 |
-
from
|
13 |
-
from transformers import
|
14 |
-
|
15 |
-
try:
|
16 |
-
import spaces
|
17 |
-
USING_SPACES = True
|
18 |
-
except ImportError:
|
19 |
-
USING_SPACES = False
|
20 |
-
|
21 |
-
def gpu_decorator(func):
|
22 |
-
if USING_SPACES:
|
23 |
-
return spaces.GPU(func)
|
24 |
-
else:
|
25 |
-
return func
|
26 |
|
27 |
from f5_tts.model import DiT
|
28 |
from f5_tts.infer.utils_infer import (
|
@@ -34,267 +18,129 @@ from f5_tts.infer.utils_infer import (
|
|
34 |
save_spectrogram,
|
35 |
)
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
return torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
40 |
-
|
41 |
-
@gpu_decorator
|
42 |
-
def load_models():
|
43 |
-
"""Carga y devuelve los modelos necesarios."""
|
44 |
-
device = get_device()
|
45 |
-
|
46 |
-
# Cargar el vocoder y moverlo al dispositivo
|
47 |
-
vocoder = load_vocoder().to(device)
|
48 |
-
|
49 |
-
# Configuración y carga del modelo F5-TTS
|
50 |
-
F5TTS_model_cfg = dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4)
|
51 |
-
F5TTS_ema_model = load_model(
|
52 |
-
DiT, F5TTS_model_cfg, str(cached_path("hf://jpgallegoar/F5-Spanish/model_1200000.safetensors"))
|
53 |
-
).to(device)
|
54 |
-
|
55 |
-
# Cargar el modelo Whisper para transcripción
|
56 |
-
whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-base")
|
57 |
-
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base").to(device)
|
58 |
-
whisper_model.eval()
|
59 |
-
|
60 |
-
return vocoder, F5TTS_ema_model, whisper_processor, whisper_model, device
|
61 |
-
|
62 |
-
# Cargar modelos una sola vez y almacenarlos en variables globales dentro de la función
|
63 |
-
def get_models():
|
64 |
-
if not hasattr(get_models, "vocoder"):
|
65 |
-
get_models.vocoder, get_models.F5TTS_ema_model, get_models.whisper_processor, get_models.whisper_model, get_models.device = load_models()
|
66 |
-
return get_models.vocoder, get_models.F5TTS_ema_model, get_models.whisper_processor, get_models.whisper_model, get_models.device
|
67 |
-
|
68 |
-
@gpu_decorator
|
69 |
-
def infer(
|
70 |
-
ref_audio_orig, ref_text, gen_text, model, remove_silence, cross_fade_duration=0.15, speed=1
|
71 |
-
):
|
72 |
-
"""Genera el audio sintetizado a partir del texto utilizando la voz de referencia."""
|
73 |
-
try:
|
74 |
-
with torch.no_grad():
|
75 |
-
vocoder, F5TTS_ema_model, _, _, device = get_models()
|
76 |
-
|
77 |
-
# Preprocesar el audio de referencia y el texto de referencia
|
78 |
-
ref_audio, ref_text = preprocess_ref_audio_text(ref_audio_orig, ref_text)
|
79 |
-
|
80 |
-
# Mover solo ref_audio al dispositivo
|
81 |
-
ref_audio = ref_audio.to(device)
|
82 |
-
|
83 |
-
# Asegurar que el texto a generar esté correctamente formateado
|
84 |
-
if not gen_text.startswith(" "):
|
85 |
-
gen_text = " " + gen_text
|
86 |
-
if not gen_text.endswith(". "):
|
87 |
-
gen_text += ". "
|
88 |
-
|
89 |
-
# El texto ingresado por el usuario se utiliza directamente sin modificaciones
|
90 |
-
input_text = gen_text
|
91 |
-
|
92 |
-
print(f"Texto para generar audio: {input_text}") # Debug: Verificar el texto
|
93 |
-
|
94 |
-
# Procesar la inferencia para generar el audio
|
95 |
-
final_wave, final_sample_rate, combined_spectrogram = infer_process(
|
96 |
-
ref_audio,
|
97 |
-
ref_text,
|
98 |
-
input_text,
|
99 |
-
F5TTS_ema_model,
|
100 |
-
vocoder,
|
101 |
-
cross_fade_duration=cross_fade_duration,
|
102 |
-
speed=speed,
|
103 |
-
progress=gr.Progress(),
|
104 |
-
)
|
105 |
-
|
106 |
-
# Eliminar silencios si está activado
|
107 |
-
if remove_silence:
|
108 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
109 |
-
sf.write(f.name, final_wave.cpu().numpy(), final_sample_rate)
|
110 |
-
remove_silence_for_generated_wav(f.name)
|
111 |
-
final_wave, _ = torchaudio.load(f.name)
|
112 |
-
final_wave = final_wave.squeeze().cpu().numpy()
|
113 |
-
|
114 |
-
# Guardar el espectrograma (opcional)
|
115 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_spectrogram:
|
116 |
-
spectrogram_path = tmp_spectrogram.name
|
117 |
-
save_spectrogram(combined_spectrogram, spectrogram_path)
|
118 |
-
|
119 |
-
return (final_sample_rate, final_wave), spectrogram_path
|
120 |
-
except Exception as e:
|
121 |
-
# Log del error para depuración
|
122 |
-
print(f"Error en infer: {e}")
|
123 |
-
return None, None
|
124 |
-
|
125 |
-
@gpu_decorator
|
126 |
-
def transcribe_audio(audio_path):
|
127 |
-
"""Transcribe el audio de referencia usando el modelo Whisper en español."""
|
128 |
-
try:
|
129 |
-
vocoder, F5TTS_ema_model, whisper_processor, whisper_model, device = get_models()
|
130 |
-
|
131 |
-
if not os.path.exists(audio_path):
|
132 |
-
raise FileNotFoundError(f"Archivo de audio no encontrado: {audio_path}")
|
133 |
-
|
134 |
-
# Cargar el audio
|
135 |
-
audio, rate = torchaudio.load(audio_path)
|
136 |
-
|
137 |
-
# Resample si es necesario
|
138 |
-
if rate != 16000:
|
139 |
-
resampler = torchaudio.transforms.Resample(orig_freq=rate, new_freq=16000)
|
140 |
-
audio = resampler(audio)
|
141 |
-
|
142 |
-
# Asegurarse de que el audio tenga una sola dimensión
|
143 |
-
if audio.ndim > 1:
|
144 |
-
audio = torch.mean(audio, dim=0)
|
145 |
-
|
146 |
-
# Procesar el audio con el procesador de Whisper
|
147 |
-
inputs = whisper_processor(audio.cpu().numpy(), sampling_rate=16000, return_tensors="pt")
|
148 |
|
149 |
-
|
|
|
|
|
150 |
|
151 |
-
|
152 |
-
|
153 |
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
)
|
160 |
-
transcription = whisper_processor.decode(predicted_ids[0], skip_special_tokens=True)
|
161 |
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
-
|
165 |
-
except Exception as e:
|
166 |
-
print(f"Error en transcribe_audio: {e}")
|
167 |
-
return None
|
168 |
|
169 |
-
|
170 |
-
"""Transcribe el audio de referencia y devuelve el texto transcrito."""
|
171 |
-
transcription = transcribe_audio(audio_path)
|
172 |
-
if transcription is None:
|
173 |
-
return "Error al transcribir el audio de referencia."
|
174 |
-
return transcription
|
175 |
|
176 |
@gpu_decorator
|
177 |
-
def
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
gen_text=input_text,
|
203 |
-
model=model_choice,
|
204 |
-
remove_silence=remove_silence,
|
205 |
-
cross_fade_duration=0.15,
|
206 |
-
speed=1.0,
|
207 |
-
)
|
208 |
-
|
209 |
-
if audio_result is None:
|
210 |
-
return None, "Error al generar el audio."
|
211 |
-
|
212 |
-
sample_rate, waveform = audio_result
|
213 |
-
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
214 |
-
sf.write(f.name, waveform, sample_rate)
|
215 |
-
audio_path = f.name
|
216 |
-
|
217 |
-
return audio_path, "Audio generado exitosamente."
|
218 |
-
except Exception as e:
|
219 |
-
print(f"Error en generate_audio: {e}")
|
220 |
-
return None, "Ocurrió un error al generar el audio."
|
221 |
-
|
222 |
-
@click.command()
|
223 |
-
@click.option("--port", "-p", default=None, type=int, help="Puerto para ejecutar la aplicación")
|
224 |
-
@click.option("--host", "-H", default=None, help="Host para ejecutar la aplicación")
|
225 |
-
@click.option(
|
226 |
-
"--share",
|
227 |
-
"-s",
|
228 |
-
default=False,
|
229 |
-
is_flag=True,
|
230 |
-
help="Compartir la aplicación a través de un enlace compartido de Gradio",
|
231 |
-
)
|
232 |
-
@click.option("--api", "-a", default=True, is_flag=True, help="Permitir acceso a la API")
|
233 |
-
def main(port, host, share, api):
|
234 |
-
"""Función principal para lanzar la aplicación Gradio de Texto a Voz."""
|
235 |
-
print("Iniciando la aplicación de Texto a Voz con Clonación de Voz...")
|
236 |
-
app.queue(api_open=api).launch(
|
237 |
-
server_name=host,
|
238 |
-
server_port=port,
|
239 |
-
share=share,
|
240 |
-
show_api=api
|
241 |
)
|
242 |
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
)
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
ref_text = gr.Textbox(
|
256 |
-
label="Texto de Referencia (Opcional)",
|
257 |
-
info="Opcional: Deja en blanco para transcribir automáticamente el audio de referencia",
|
258 |
-
lines=2,
|
259 |
-
)
|
260 |
-
with gr.Column():
|
261 |
-
model_choice = gr.Radio(
|
262 |
-
choices=["F5-TTS"],
|
263 |
-
label="Modelo TTS",
|
264 |
-
value="F5-TTS",
|
265 |
-
)
|
266 |
-
remove_silence = gr.Checkbox(
|
267 |
-
label="Eliminar Silencios",
|
268 |
-
value=True,
|
269 |
-
)
|
270 |
-
|
271 |
-
with gr.Row():
|
272 |
-
text_input = gr.Textbox(
|
273 |
-
label="Escribe tu texto",
|
274 |
-
placeholder="Ingresa el texto que deseas convertir a voz...",
|
275 |
-
lines=3,
|
276 |
-
)
|
277 |
-
generate_btn = gr.Button("Generar Audio")
|
278 |
-
|
279 |
-
with gr.Row():
|
280 |
-
audio_output = gr.Audio(label="Audio Generado", autoplay=True)
|
281 |
-
|
282 |
-
status = gr.Textbox(label="Estado", interactive=False)
|
283 |
-
|
284 |
-
# Conectar la función de transcripción al evento de cambio del audio de referencia
|
285 |
-
ref_audio.change(
|
286 |
-
fn=transcribe_and_update,
|
287 |
-
inputs=ref_audio,
|
288 |
-
outputs=ref_text,
|
289 |
)
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
295 |
)
|
296 |
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import re
|
2 |
import tempfile
|
|
|
3 |
|
|
|
|
|
4 |
import gradio as gr
|
5 |
+
import numpy as np
|
6 |
import soundfile as sf
|
7 |
import torchaudio
|
8 |
+
from num2words import num2words
|
9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
from f5_tts.model import DiT
|
12 |
from f5_tts.infer.utils_infer import (
|
|
|
18 |
save_spectrogram,
|
19 |
)
|
20 |
|
21 |
+
# Implementación de cached_path (si es necesario, dependiendo de tu configuración)
|
22 |
+
from cached_path import cached_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
# Decorador GPU para Spaces o local
|
25 |
+
def gpu_decorator(func):
|
26 |
+
return func # Simplemente devuelve la función, ajusta según tu entorno si usas HF中国镜像站 Spaces
|
27 |
|
28 |
+
# Cargar el vocoder
|
29 |
+
vocoder = load_vocoder()
|
30 |
|
31 |
+
# Configuración y carga del modelo F5TTS
|
32 |
+
F5TTS_model_cfg = dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4)
|
33 |
+
F5TTS_ema_model = load_model(
|
34 |
+
DiT, F5TTS_model_cfg, str(cached_path("hf://jpgallegoar/F5-Spanish/model_1200000.safetensors"))
|
35 |
+
)
|
|
|
|
|
36 |
|
37 |
+
def traducir_numero_a_texto(texto):
|
38 |
+
texto_separado = re.sub(r'([A-Za-z])(\d)', r'\1 \2', texto)
|
39 |
+
texto_separado = re.sub(r'(\d)([A-Za-z])', r'\1 \2', texto_separado)
|
40 |
+
|
41 |
+
def reemplazar_numero(match):
|
42 |
+
numero = match.group()
|
43 |
+
return num2words(int(numero), lang='es')
|
44 |
|
45 |
+
texto_traducido = re.sub(r'\b\d+\b', reemplazar_numero, texto_separado)
|
|
|
|
|
|
|
46 |
|
47 |
+
return texto_traducido
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
@gpu_decorator
|
50 |
+
def infer(
|
51 |
+
ref_audio_orig, ref_text, gen_text, model, remove_silence, cross_fade_duration=0.15, speed=1, show_info=gr.Info
|
52 |
+
):
|
53 |
+
ref_audio, ref_text = preprocess_ref_audio_text(ref_audio_orig, ref_text, show_info=show_info)
|
54 |
+
|
55 |
+
ema_model = F5TTS_ema_model
|
56 |
+
|
57 |
+
if not gen_text.startswith(" "):
|
58 |
+
gen_text = " " + gen_text
|
59 |
+
if not gen_text.endswith(". "):
|
60 |
+
gen_text += ". "
|
61 |
+
|
62 |
+
gen_text = gen_text.lower()
|
63 |
+
gen_text = traducir_numero_a_texto(gen_text)
|
64 |
+
|
65 |
+
final_wave, final_sample_rate, combined_spectrogram = infer_process(
|
66 |
+
ref_audio,
|
67 |
+
ref_text,
|
68 |
+
gen_text,
|
69 |
+
ema_model,
|
70 |
+
vocoder,
|
71 |
+
cross_fade_duration=cross_fade_duration,
|
72 |
+
speed=speed,
|
73 |
+
show_info=show_info,
|
74 |
+
progress=gr.Progress(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
)
|
76 |
|
77 |
+
# Eliminar silencios
|
78 |
+
if remove_silence:
|
79 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
80 |
+
sf.write(f.name, final_wave, final_sample_rate)
|
81 |
+
remove_silence_for_generated_wav(f.name)
|
82 |
+
final_wave, _ = torchaudio.load(f.name)
|
83 |
+
final_wave = final_wave.squeeze().cpu().numpy()
|
84 |
+
|
85 |
+
# Guardar el espectrograma
|
86 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_spectrogram:
|
87 |
+
spectrogram_path = tmp_spectrogram.name
|
88 |
+
save_spectrogram(combined_spectrogram, spectrogram_path)
|
89 |
+
|
90 |
+
return (final_sample_rate, final_wave), spectrogram_path
|
91 |
+
|
92 |
+
# Interfaz Gradio
|
93 |
+
with gr.Blocks() as app_tts:
|
94 |
+
gr.Markdown("# TTS por Lotes")
|
95 |
+
ref_audio_input = gr.Audio(label="Audio de Referencia", type="filepath")
|
96 |
+
gen_text_input = gr.Textbox(label="Texto para Generar", lines=10)
|
97 |
+
model_choice = gr.Radio(choices=["F5-TTS"], label="Seleccionar Modelo TTS", value="F5-TTS")
|
98 |
+
generate_btn = gr.Button("Sintetizar", variant="primary")
|
99 |
+
with gr.Accordion("Configuraciones Avanzadas", open=False):
|
100 |
+
ref_text_input = gr.Textbox(
|
101 |
+
label="Texto de Referencia",
|
102 |
+
info="Deja en blanco para transcribir automáticamente el audio de referencia. Si ingresas texto, sobrescribirá la transcripción automática.",
|
103 |
+
lines=2,
|
104 |
)
|
105 |
+
remove_silence = gr.Checkbox(
|
106 |
+
label="Eliminar Silencios",
|
107 |
+
info="El modelo tiende a producir silencios, especialmente en audios más largos. Podemos eliminar manualmente los silencios si es necesario. Ten en cuenta que esta es una característica experimental y puede producir resultados extraños. Esto también aumentará el tiempo de generación.",
|
108 |
+
value=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
)
|
110 |
+
speed_slider = gr.Slider(
|
111 |
+
label="Velocidad",
|
112 |
+
minimum=0.3,
|
113 |
+
maximum=2.0,
|
114 |
+
value=1.0,
|
115 |
+
step=0.1,
|
116 |
+
info="Ajusta la velocidad del audio.",
|
117 |
+
)
|
118 |
+
cross_fade_duration_slider = gr.Slider(
|
119 |
+
label="Duración del Cross-Fade (s)",
|
120 |
+
minimum=0.0,
|
121 |
+
maximum=1.0,
|
122 |
+
value=0.15,
|
123 |
+
step=0.01,
|
124 |
+
info="Establece la duración del cross-fade entre clips de audio.",
|
125 |
)
|
126 |
|
127 |
+
audio_output = gr.Audio(label="Audio Sintetizado")
|
128 |
+
spectrogram_output = gr.Image(label="Espectrograma")
|
129 |
+
|
130 |
+
generate_btn.click(
|
131 |
+
infer,
|
132 |
+
inputs=[
|
133 |
+
ref_audio_input,
|
134 |
+
ref_text_input,
|
135 |
+
gen_text_input,
|
136 |
+
model_choice,
|
137 |
+
remove_silence,
|
138 |
+
cross_fade_duration_slider,
|
139 |
+
speed_slider,
|
140 |
+
],
|
141 |
+
outputs=[audio_output, spectrogram_output],
|
142 |
+
)
|
143 |
+
|
144 |
+
# Ejecutar la aplicación
|
145 |
+
if __name__ == "__main__":
|
146 |
+
app_tts.launch()
|