Spaces:
Sleeping
Sleeping
....
Browse files
app.py
CHANGED
|
@@ -6,16 +6,37 @@ import torch
|
|
| 6 |
from bark import generate_audio
|
| 7 |
from scipy.io.wavfile import write
|
| 8 |
import tempfile
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
transcribir = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Funci贸n para transcribir el audio y traducir el audio de entrada
|
| 14 |
def transcribir_audio(audio):
|
| 15 |
-
#
|
| 16 |
-
result =
|
| 17 |
return result["text"]
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Funci贸n para generar el audio
|
| 20 |
def generar_audio(text):
|
| 21 |
if not isinstance(text, str):
|
|
@@ -26,12 +47,17 @@ def generar_audio(text):
|
|
| 26 |
write(temp_wav.name, 24000, (audio_array * 32767).astype(np.int16))
|
| 27 |
return temp_wav.name
|
| 28 |
|
|
|
|
|
|
|
| 29 |
def process_audio(audio_file):
|
| 30 |
try:
|
| 31 |
# Paso 1: Transcripci贸n con Whisper
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
# Paso
|
| 35 |
audio_sintetizado = generar_audio(transcripcion_traducida)
|
| 36 |
|
| 37 |
return transcripcion_traducida, audio_sintetizado
|
|
@@ -51,4 +77,4 @@ with gr.Blocks() as demo:
|
|
| 51 |
process_button.click(process_audio, inputs=input_audio, outputs=[transcription_output, output_audio])
|
| 52 |
|
| 53 |
# Lanzar la app
|
| 54 |
-
demo.launch(share=True)
|
|
|
|
| 6 |
from bark import generate_audio
|
| 7 |
from scipy.io.wavfile import write
|
| 8 |
import tempfile
|
| 9 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 10 |
|
| 11 |
+
|
| 12 |
+
# Cargar el modelo Whisper-small y bark
|
| 13 |
transcribir = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
| 14 |
+
"""bark = pipeline("text-to-speech", model="suno/bark")"""
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Cargar el tokenizador y el modelo para espa帽ol a ingl茅s
|
| 18 |
+
model_name = "Helsinki-NLP/opus-mt-es-en"
|
| 19 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 20 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 21 |
+
|
| 22 |
|
| 23 |
# Funci贸n para transcribir el audio y traducir el audio de entrada
|
| 24 |
def transcribir_audio(audio):
|
| 25 |
+
# Usamos el pipeline de Hugging Face para la transcripci贸n
|
| 26 |
+
result = transcribir_audio(audio_file, task="translate")
|
| 27 |
return result["text"]
|
| 28 |
|
| 29 |
+
|
| 30 |
+
def traducir_texto(texto):
|
| 31 |
+
# Tokenizar el texto
|
| 32 |
+
inputs = tokenizer(texto, return_tensors="pt", padding=True, truncation=True)
|
| 33 |
+
# Generar la traducci贸n
|
| 34 |
+
translated = model.generate(**inputs)
|
| 35 |
+
# Decodificar la traducci贸n
|
| 36 |
+
traduccion = tokenizer.batch_decode(translated, skip_special_tokens=True)[0]
|
| 37 |
+
return traduccion
|
| 38 |
+
|
| 39 |
+
|
| 40 |
# Funci贸n para generar el audio
|
| 41 |
def generar_audio(text):
|
| 42 |
if not isinstance(text, str):
|
|
|
|
| 47 |
write(temp_wav.name, 24000, (audio_array * 32767).astype(np.int16))
|
| 48 |
return temp_wav.name
|
| 49 |
|
| 50 |
+
|
| 51 |
+
|
| 52 |
def process_audio(audio_file):
|
| 53 |
try:
|
| 54 |
# Paso 1: Transcripci贸n con Whisper
|
| 55 |
+
transcripcion = transcribir_audio(audio_file)
|
| 56 |
+
|
| 57 |
+
# Paso 2: Traducci贸n con MarianMT
|
| 58 |
+
transcripcion_traducida = traducir_texto(transcripcion)
|
| 59 |
|
| 60 |
+
# Paso 3: Generaci贸n de audio con Bark
|
| 61 |
audio_sintetizado = generar_audio(transcripcion_traducida)
|
| 62 |
|
| 63 |
return transcripcion_traducida, audio_sintetizado
|
|
|
|
| 77 |
process_button.click(process_audio, inputs=input_audio, outputs=[transcription_output, output_audio])
|
| 78 |
|
| 79 |
# Lanzar la app
|
| 80 |
+
demo.launch(share=True)
|