Spaces:
Paused
Paused
Update chat_ai.py
Browse files- chat_ai.py +96 -27
chat_ai.py
CHANGED
@@ -3,6 +3,7 @@
|
|
3 |
# ruff: noqa: E402
|
4 |
# Above allows ruff to ignore E402: module level import not at top of file
|
5 |
|
|
|
6 |
import tempfile
|
7 |
import os
|
8 |
|
@@ -12,10 +13,7 @@ import gradio as gr
|
|
12 |
import soundfile as sf
|
13 |
import torchaudio
|
14 |
from cached_path import cached_path
|
15 |
-
from transformers import
|
16 |
-
WhisperProcessor,
|
17 |
-
WhisperForConditionalGeneration,
|
18 |
-
)
|
19 |
|
20 |
try:
|
21 |
import spaces
|
@@ -33,6 +31,7 @@ from f5_tts.model import DiT
|
|
33 |
from f5_tts.infer.utils_infer import (
|
34 |
load_vocoder,
|
35 |
load_model,
|
|
|
36 |
infer_process,
|
37 |
remove_silence_for_generated_wav,
|
38 |
save_spectrogram,
|
@@ -47,7 +46,7 @@ F5TTS_ema_model = load_model(
|
|
47 |
DiT, F5TTS_model_cfg, str(cached_path("hf://jpgallegoar/F5-Spanish/model_1200000.safetensors"))
|
48 |
)
|
49 |
|
50 |
-
# Cargar el modelo Whisper para transcripci贸n
|
51 |
whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-base")
|
52 |
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base")
|
53 |
whisper_model.eval()
|
@@ -56,21 +55,33 @@ if torch.cuda.is_available():
|
|
56 |
|
57 |
@gpu_decorator
|
58 |
def infer(
|
59 |
-
gen_text, model, remove_silence, cross_fade_duration=0.15, speed=1
|
60 |
):
|
61 |
-
"""Genera el audio sintetizado a partir del texto"""
|
62 |
try:
|
63 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
input_text = gen_text
|
65 |
|
66 |
print(f"Texto para generar audio: {input_text}") # Debug: Verificar el texto
|
67 |
|
|
|
68 |
final_wave, final_sample_rate, combined_spectrogram = infer_process(
|
69 |
-
|
70 |
-
ref_text
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
cross_fade_duration=cross_fade_duration,
|
75 |
speed=speed,
|
76 |
progress=gr.Progress(),
|
@@ -95,9 +106,50 @@ def infer(
|
|
95 |
print(f"Error en infer: {e}")
|
96 |
return None, None
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
@gpu_decorator
|
99 |
-
def generate_audio(text, model_choice, remove_silence):
|
100 |
-
"""Genera el audio a partir del texto ingresado"""
|
101 |
try:
|
102 |
if not text.strip():
|
103 |
return None, "Por favor, ingresa un texto para generar el audio."
|
@@ -105,27 +157,37 @@ def generate_audio(text, model_choice, remove_silence):
|
|
105 |
# Debug: Verificar el texto ingresado
|
106 |
print(f"Texto ingresado para TTS: {text}")
|
107 |
|
108 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
input_text = text
|
110 |
|
111 |
print(f"Texto final para inferencia: {input_text}") # Debug
|
112 |
|
|
|
113 |
audio_result, spectrogram_path = infer(
|
|
|
|
|
114 |
gen_text=input_text,
|
115 |
model=model_choice,
|
116 |
remove_silence=remove_silence,
|
117 |
cross_fade_duration=0.15,
|
118 |
speed=1.0,
|
119 |
)
|
120 |
-
|
121 |
if audio_result is None:
|
122 |
return None, "Error al generar el audio."
|
123 |
-
|
124 |
sample_rate, waveform = audio_result
|
125 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
126 |
sf.write(f.name, waveform, sample_rate)
|
127 |
audio_path = f.name
|
128 |
-
|
129 |
return audio_path, "Audio generado exitosamente."
|
130 |
except Exception as e:
|
131 |
print(f"Error en generate_audio: {e}")
|
@@ -134,12 +196,19 @@ def generate_audio(text, model_choice, remove_silence):
|
|
134 |
with gr.Blocks() as app:
|
135 |
gr.Markdown(
|
136 |
"""
|
137 |
-
# Conversor de Texto a Voz
|
138 |
-
|
139 |
"""
|
140 |
)
|
141 |
|
142 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
with gr.Column():
|
144 |
model_choice = gr.Radio(
|
145 |
choices=["F5-TTS"],
|
@@ -161,12 +230,12 @@ Escribe un texto y la IA lo convertir谩 a voz.
|
|
161 |
|
162 |
with gr.Row():
|
163 |
audio_output = gr.Audio(label="Audio Generado", autoplay=True)
|
164 |
-
|
165 |
status = gr.Textbox(label="Estado", interactive=False)
|
166 |
|
167 |
generate_btn.click(
|
168 |
generate_audio,
|
169 |
-
inputs=[text_input, model_choice, remove_silence],
|
170 |
outputs=[audio_output, status],
|
171 |
)
|
172 |
|
@@ -183,11 +252,11 @@ Escribe un texto y la IA lo convertir谩 a voz.
|
|
183 |
@click.option("--api", "-a", default=True, is_flag=True, help="Permitir acceso a la API")
|
184 |
def main(port, host, share, api):
|
185 |
"""Funci贸n principal para lanzar la aplicaci贸n Gradio de Texto a Voz."""
|
186 |
-
print("Iniciando la aplicaci贸n de Texto a Voz...")
|
187 |
app.queue(api_open=api).launch(
|
188 |
-
server_name=host,
|
189 |
-
server_port=port,
|
190 |
-
share=share,
|
191 |
show_api=api
|
192 |
)
|
193 |
|
|
|
3 |
# ruff: noqa: E402
|
4 |
# Above allows ruff to ignore E402: module level import not at top of file
|
5 |
|
6 |
+
import re
|
7 |
import tempfile
|
8 |
import os
|
9 |
|
|
|
13 |
import soundfile as sf
|
14 |
import torchaudio
|
15 |
from cached_path import cached_path
|
16 |
+
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
|
|
|
|
|
|
17 |
|
18 |
try:
|
19 |
import spaces
|
|
|
31 |
from f5_tts.infer.utils_infer import (
|
32 |
load_vocoder,
|
33 |
load_model,
|
34 |
+
preprocess_ref_audio_text,
|
35 |
infer_process,
|
36 |
remove_silence_for_generated_wav,
|
37 |
save_spectrogram,
|
|
|
46 |
DiT, F5TTS_model_cfg, str(cached_path("hf://jpgallegoar/F5-Spanish/model_1200000.safetensors"))
|
47 |
)
|
48 |
|
49 |
+
# Cargar el modelo Whisper para transcripci贸n
|
50 |
whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-base")
|
51 |
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base")
|
52 |
whisper_model.eval()
|
|
|
55 |
|
56 |
@gpu_decorator
|
57 |
def infer(
|
58 |
+
ref_audio_orig, ref_text, gen_text, model, remove_silence, cross_fade_duration=0.15, speed=1
|
59 |
):
|
60 |
+
"""Genera el audio sintetizado a partir del texto utilizando la voz de referencia."""
|
61 |
try:
|
62 |
+
# Preprocesar el audio de referencia y el texto de referencia
|
63 |
+
ref_audio, ref_text = preprocess_ref_audio_text(ref_audio_orig, ref_text)
|
64 |
+
|
65 |
+
ema_model = F5TTS_ema_model
|
66 |
+
|
67 |
+
# Asegurar que el texto a generar est茅 correctamente formateado
|
68 |
+
if not gen_text.startswith(" "):
|
69 |
+
gen_text = " " + gen_text
|
70 |
+
if not gen_text.endswith(". "):
|
71 |
+
gen_text += ". "
|
72 |
+
|
73 |
+
# El texto ingresado por el usuario se utiliza directamente sin modificaciones
|
74 |
input_text = gen_text
|
75 |
|
76 |
print(f"Texto para generar audio: {input_text}") # Debug: Verificar el texto
|
77 |
|
78 |
+
# Procesar la inferencia para generar el audio
|
79 |
final_wave, final_sample_rate, combined_spectrogram = infer_process(
|
80 |
+
ref_audio,
|
81 |
+
ref_text,
|
82 |
+
input_text,
|
83 |
+
ema_model,
|
84 |
+
vocoder,
|
85 |
cross_fade_duration=cross_fade_duration,
|
86 |
speed=speed,
|
87 |
progress=gr.Progress(),
|
|
|
106 |
print(f"Error en infer: {e}")
|
107 |
return None, None
|
108 |
|
109 |
+
def transcribe_audio(audio_path):
|
110 |
+
"""Transcribe el audio de referencia usando el modelo Whisper en espa帽ol."""
|
111 |
+
try:
|
112 |
+
if not os.path.exists(audio_path):
|
113 |
+
raise FileNotFoundError(f"Archivo de audio no encontrado: {audio_path}")
|
114 |
+
|
115 |
+
# Cargar el audio
|
116 |
+
audio, rate = torchaudio.load(audio_path)
|
117 |
+
|
118 |
+
# Resample si es necesario
|
119 |
+
if rate != 16000:
|
120 |
+
resampler = torchaudio.transforms.Resample(orig_freq=rate, new_freq=16000)
|
121 |
+
audio = resampler(audio)
|
122 |
+
|
123 |
+
# Asegurarse de que el audio tenga una sola dimensi贸n
|
124 |
+
if audio.ndim > 1:
|
125 |
+
audio = torch.mean(audio, dim=0)
|
126 |
+
|
127 |
+
# Procesar el audio con el procesador de Whisper
|
128 |
+
inputs = whisper_processor(audio.cpu().numpy(), sampling_rate=16000, return_tensors="pt")
|
129 |
+
|
130 |
+
if torch.cuda.is_available():
|
131 |
+
inputs = {k: v.to("cuda") for k, v in inputs.items()}
|
132 |
+
|
133 |
+
# Forzar el idioma a espa帽ol (usando el nombre en ingl茅s)
|
134 |
+
forced_decoder_ids = whisper_processor.get_decoder_prompt_ids(language="spanish", task="transcribe")
|
135 |
+
|
136 |
+
# Generar la transcripci贸n
|
137 |
+
predicted_ids = whisper_model.generate(
|
138 |
+
inputs["input_features"],
|
139 |
+
forced_decoder_ids=forced_decoder_ids
|
140 |
+
)
|
141 |
+
transcription = whisper_processor.decode(predicted_ids[0], skip_special_tokens=True)
|
142 |
+
|
143 |
+
print(f"Transcripci贸n: {transcription}") # Debug: Verificar la transcripci贸n
|
144 |
+
|
145 |
+
return transcription
|
146 |
+
except Exception as e:
|
147 |
+
print(f"Error en transcribe_audio: {e}")
|
148 |
+
return None
|
149 |
+
|
150 |
@gpu_decorator
|
151 |
+
def generate_audio(text, ref_audio, ref_text, model_choice, remove_silence):
|
152 |
+
"""Genera el audio a partir del texto ingresado utilizando la voz de referencia."""
|
153 |
try:
|
154 |
if not text.strip():
|
155 |
return None, "Por favor, ingresa un texto para generar el audio."
|
|
|
157 |
# Debug: Verificar el texto ingresado
|
158 |
print(f"Texto ingresado para TTS: {text}")
|
159 |
|
160 |
+
# Si se proporciona audio de referencia y no se proporciona texto de referencia, transcribir el audio
|
161 |
+
if ref_audio and not ref_text.strip():
|
162 |
+
ref_text = transcribe_audio(ref_audio)
|
163 |
+
if ref_text is None:
|
164 |
+
return None, "Error al transcribir el audio de referencia."
|
165 |
+
print(f"Texto de referencia transcrito: {ref_text}") # Debug
|
166 |
+
|
167 |
+
# Usar directamente el texto ingresado para generar el audio
|
168 |
input_text = text
|
169 |
|
170 |
print(f"Texto final para inferencia: {input_text}") # Debug
|
171 |
|
172 |
+
# Generar el audio utilizando la funci贸n infer
|
173 |
audio_result, spectrogram_path = infer(
|
174 |
+
ref_audio_orig=ref_audio,
|
175 |
+
ref_text=ref_text,
|
176 |
gen_text=input_text,
|
177 |
model=model_choice,
|
178 |
remove_silence=remove_silence,
|
179 |
cross_fade_duration=0.15,
|
180 |
speed=1.0,
|
181 |
)
|
182 |
+
|
183 |
if audio_result is None:
|
184 |
return None, "Error al generar el audio."
|
185 |
+
|
186 |
sample_rate, waveform = audio_result
|
187 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
188 |
sf.write(f.name, waveform, sample_rate)
|
189 |
audio_path = f.name
|
190 |
+
|
191 |
return audio_path, "Audio generado exitosamente."
|
192 |
except Exception as e:
|
193 |
print(f"Error en generate_audio: {e}")
|
|
|
196 |
with gr.Blocks() as app:
|
197 |
gr.Markdown(
|
198 |
"""
|
199 |
+
# Conversor de Texto a Voz con Clonaci贸n de Voz
|
200 |
+
Sube un audio de referencia para clonar la voz y luego escribe el texto que deseas convertir a voz.
|
201 |
"""
|
202 |
)
|
203 |
|
204 |
with gr.Row():
|
205 |
+
with gr.Column():
|
206 |
+
ref_audio = gr.Audio(label="Audio de Referencia (Clonaci贸n de Voz)", type="filepath")
|
207 |
+
ref_text = gr.Textbox(
|
208 |
+
label="Texto de Referencia (Opcional)",
|
209 |
+
info="Opcional: Deja en blanco para transcribir autom谩ticamente el audio de referencia",
|
210 |
+
lines=2,
|
211 |
+
)
|
212 |
with gr.Column():
|
213 |
model_choice = gr.Radio(
|
214 |
choices=["F5-TTS"],
|
|
|
230 |
|
231 |
with gr.Row():
|
232 |
audio_output = gr.Audio(label="Audio Generado", autoplay=True)
|
233 |
+
|
234 |
status = gr.Textbox(label="Estado", interactive=False)
|
235 |
|
236 |
generate_btn.click(
|
237 |
generate_audio,
|
238 |
+
inputs=[text_input, ref_audio, ref_text, model_choice, remove_silence],
|
239 |
outputs=[audio_output, status],
|
240 |
)
|
241 |
|
|
|
252 |
@click.option("--api", "-a", default=True, is_flag=True, help="Permitir acceso a la API")
|
253 |
def main(port, host, share, api):
|
254 |
"""Funci贸n principal para lanzar la aplicaci贸n Gradio de Texto a Voz."""
|
255 |
+
print("Iniciando la aplicaci贸n de Texto a Voz con Clonaci贸n de Voz...")
|
256 |
app.queue(api_open=api).launch(
|
257 |
+
server_name=host,
|
258 |
+
server_port=port,
|
259 |
+
share=share,
|
260 |
show_api=api
|
261 |
)
|
262 |
|