Blakus commited on
Commit
5c21323
·
verified ·
1 Parent(s): dab9ee4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -1
app.py CHANGED
@@ -49,4 +49,142 @@ def setup_mecab_and_unidic():
49
  print("Configurando MeCab y UniDic...")
50
  setup_mecab_and_unidic()
51
 
52
- # El resto del código permanece igual...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  print("Configurando MeCab y UniDic...")
50
  setup_mecab_and_unidic()
51
 
52
+ # Descargar y configurar el modelo
53
+ print("Descargando y configurando el modelo...")
54
+ repo_id = "Blakus/Pedro_Lab_XTTS"
55
+ local_dir = os.path.join(get_user_data_dir("tts"), "tts_models--multilingual--multi-dataset--xtts_v2")
56
+ os.makedirs(local_dir, exist_ok=True)
57
+ files_to_download = ["config.json", "model.pth", "vocab.json"]
58
+
59
+ for file_name in files_to_download:
60
+ print(f"Descargando {file_name} de {repo_id}")
61
+ hf_hub_download(repo_id=repo_id, filename=file_name, local_dir=local_dir)
62
+
63
+ config_path = os.path.join(local_dir, "config.json")
64
+ checkpoint_path = os.path.join(local_dir, "model.pth")
65
+ vocab_path = os.path.join(local_dir, "vocab.json")
66
+
67
+ config = XttsConfig()
68
+ config.load_json(config_path)
69
+
70
+ model = Xtts.init_from_config(config)
71
+ model.load_checkpoint(config, checkpoint_path=checkpoint_path, vocab_path=vocab_path, eval=True, use_deepspeed=False)
72
+
73
+ print("Modelo cargado en CPU")
74
+
75
+ # Funciones auxiliares
76
+ def split_text(text):
77
+ return re.split(r'(?<=[.!?])\s+', text)
78
+
79
+ def predict(prompt, language, reference_audio):
80
+ try:
81
+ if len(prompt) < 2 or len(prompt) > 600:
82
+ return None, "El texto debe tener entre 2 y 600 caracteres."
83
+
84
+ sentences = split_text(prompt)
85
+
86
+ temperature = config.inference.get("temperature", 0.75)
87
+ repetition_penalty = config.inference.get("repetition_penalty", 5.0)
88
+ gpt_cond_len = config.inference.get("gpt_cond_len", 30)
89
+ gpt_cond_chunk_len = config.inference.get("gpt_cond_chunk_len", 4)
90
+ max_ref_length = config.inference.get("max_ref_length", 60)
91
+
92
+ gpt_cond_latent, speaker_embedding = model.get_conditioning_latents(
93
+ audio_path=reference_audio,
94
+ gpt_cond_len=gpt_cond_len,
95
+ gpt_cond_chunk_len=gpt_cond_chunk_len,
96
+ max_ref_length=max_ref_length
97
+ )
98
+
99
+ start_time = time.time()
100
+ combined_audio = AudioSegment.empty()
101
+
102
+ for sentence in sentences:
103
+ out = model.inference(
104
+ sentence,
105
+ language,
106
+ gpt_cond_latent,
107
+ speaker_embedding,
108
+ temperature=temperature,
109
+ repetition_penalty=repetition_penalty,
110
+ )
111
+ audio_segment = AudioSegment(
112
+ out["wav"].tobytes(),
113
+ frame_rate=24000,
114
+ sample_width=2,
115
+ channels=1
116
+ )
117
+ combined_audio += audio_segment
118
+ combined_audio += AudioSegment.silent(duration=500) # 0.5 segundos de silencio
119
+
120
+ inference_time = time.time() - start_time
121
+
122
+ output_path = "output.wav"
123
+ combined_audio.export(output_path, format="wav")
124
+
125
+ audio_length = len(combined_audio) / 1000 # duración del audio en segundos
126
+ real_time_factor = inference_time / audio_length
127
+
128
+ metrics_text = f"Tiempo de generación: {inference_time:.2f} segundos\n"
129
+ metrics_text += f"Factor de tiempo real: {real_time_factor:.2f}"
130
+
131
+ return output_path, metrics_text
132
+
133
+ except Exception as e:
134
+ print(f"Error detallado: {str(e)}")
135
+ return None, f"Error: {str(e)}"
136
+
137
+ # Configuración de la interfaz de Gradio
138
+ supported_languages = ["es", "en"]
139
+ reference_audios = [
140
+ "serio.wav",
141
+ "neutral.wav",
142
+ "alegre.wav",
143
+ ]
144
+
145
+ theme = gr.themes.Soft(
146
+ primary_hue="blue",
147
+ secondary_hue="gray",
148
+ ).set(
149
+ body_background_fill='*neutral_100',
150
+ body_background_fill_dark='*neutral_900',
151
+ )
152
+
153
+ description = """
154
+ # Sintetizador de voz de Pedro Labattaglia 🎙️
155
+
156
+ Sintetizador de voz con la voz del locutor argentino Pedro Labattaglia.
157
+
158
+ ## Cómo usarlo:
159
+ - Elija el idioma (Español o Inglés)
160
+ - Elija un audio de referencia de la lista
161
+ - Escriba el texto que desea sintetizar
162
+ - Presione generar voz
163
+ """
164
+
165
+ # Interfaz de Gradio
166
+ with gr.Blocks(theme=theme) as demo:
167
+ gr.Markdown(description)
168
+
169
+ with gr.Row():
170
+ gr.Image("https://i1.sndcdn.com/artworks-000237574740-gwz61j-t500x500.jpg", label="", show_label=False, width=250, height=250)
171
+
172
+ with gr.Row():
173
+ with gr.Column(scale=2):
174
+ language_selector = gr.Dropdown(label="Idioma", choices=supported_languages)
175
+ reference_audio = gr.Dropdown(label="Audio de referencia", choices=reference_audios)
176
+ input_text = gr.Textbox(label="Texto a sintetizar", placeholder="Escribe aquí el texto que quieres convertir a voz...")
177
+ generate_button = gr.Button("Generar voz", variant="primary")
178
+
179
+ with gr.Column(scale=1):
180
+ generated_audio = gr.Audio(label="Audio generado", interactive=False)
181
+ metrics_output = gr.Textbox(label="Métricas", value="Tiempo de generación: -- segundos\nFactor de tiempo real: --")
182
+
183
+ generate_button.click(
184
+ predict,
185
+ inputs=[input_text, language_selector, reference_audio],
186
+ outputs=[generated_audio, metrics_output]
187
+ )
188
+
189
+ if __name__ == "__main__":
190
+ demo.launch()