Spaces:
Running
Running
GuilleMulasss
commited on
Commit
•
1d6d8f9
1
Parent(s):
9d74270
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,52 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
-
from
|
4 |
-
|
5 |
-
import
|
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 |
-
output_path =
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
gr.
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
interface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from datasets import load_dataset
|
4 |
+
import torchaudio
|
5 |
+
import torch
|
6 |
+
|
7 |
+
|
8 |
+
# Cargar el modelo de traducción
|
9 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-es-en")
|
10 |
+
synthesiser = pipeline("text-to-speech", "microsoft/speecht5_tts")
|
11 |
+
|
12 |
+
# Cargar voces preentrenadas
|
13 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
14 |
+
|
15 |
+
|
16 |
+
# Función principal
|
17 |
+
def translate_and_speak(text):
|
18 |
+
# Traducir de español a inglés
|
19 |
+
translated = translator(text, src_lang="es", tgt_lang="en")[0]["translation_text"]
|
20 |
+
|
21 |
+
# Usar una voz preentrenada
|
22 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
23 |
+
|
24 |
+
# Generar audio
|
25 |
+
speech = synthesiser(translated, forward_params={"speaker_embeddings": speaker_embeddings})
|
26 |
+
|
27 |
+
# Extraer waveform y sampling_rate del resultado
|
28 |
+
waveform = torch.tensor(speech["audio"]) # Convertir a tensor de PyTorch
|
29 |
+
sampling_rate = speech["sampling_rate"]
|
30 |
+
|
31 |
+
# Guardar el audio temporalmente
|
32 |
+
output_path = "output.wav"
|
33 |
+
torchaudio.save(output_path, waveform.unsqueeze(0), sample_rate=sampling_rate) # Unsqueeze para añadir batch dimension
|
34 |
+
|
35 |
+
return translated, output_path
|
36 |
+
|
37 |
+
|
38 |
+
# Crear la interfaz
|
39 |
+
interface = gr.Interface(
|
40 |
+
fn=translate_and_speak,
|
41 |
+
inputs=gr.Textbox(label="Texto en Español"),
|
42 |
+
outputs=[
|
43 |
+
gr.Textbox(label="Texto Traducido (Inglés)"),
|
44 |
+
gr.Audio(label="Audio Generado (Inglés)")
|
45 |
+
],
|
46 |
+
title="Traductor y Generador de Voz",
|
47 |
+
description="Escribe un texto en español y este será traducido al inglés y leído en voz alta usando IA."
|
48 |
+
)
|
49 |
+
|
50 |
+
# Lanzar la aplicación
|
51 |
+
if __name__ == "__main__":
|
52 |
+
interface.launch()
|
|