Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import whisper
|
3 |
+
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
4 |
+
|
5 |
+
## Cargar modelos de Whisper
|
6 |
+
whisper_model = whisper.load_model("base.en")
|
7 |
+
|
8 |
+
## Cargar modelos de MBart
|
9 |
+
translation_model = MBartForConditionalGeneration.from_pretrained("SnypzZz/Llama2-13b-Language-translate")
|
10 |
+
tokenizer = MBart50TokenizerFast.from_pretrained("SnypzZz/Llama2-13b-Language-translate", src_lang="en_XX")
|
11 |
+
|
12 |
+
## Funci贸n para transcribir y traducir el audio
|
13 |
+
def transcribe_translate(audio_file, target_language):
|
14 |
+
# Transcribir audio con Whisper (aqu铆 se usa la variable whisper_model)
|
15 |
+
transcription = whisper_model.transcribe(audio_file, language="english")["text"]
|
16 |
+
|
17 |
+
# Traducir texto a idioma seleccionado (aqu铆 se usa translation_model y tokenizer)
|
18 |
+
model_inputs = tokenizer(transcription, return_tensors="pt")
|
19 |
+
generated_tokens = translation_model.generate(
|
20 |
+
**model_inputs,
|
21 |
+
forced_bos_token_id=tokenizer.lang_code_to_id[target_language]
|
22 |
+
)
|
23 |
+
translated_text = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
|
24 |
+
return translated_text.strip("[]' ")
|
25 |
+
|
26 |
+
## Interfaz de Gradio
|
27 |
+
# Est谩 creado en filas para "organizar" la distribuci贸n de cada caja
|
28 |
+
with gr.Blocks(theme="Nymbo/Nymbo_Theme") as app:
|
29 |
+
# T铆tulo
|
30 |
+
gr.Markdown("## Transcripci贸n y Traducci贸n de Audio")
|
31 |
+
|
32 |
+
# Primera fila -> Input de audio y elecci贸n del idioma
|
33 |
+
with gr.Row():
|
34 |
+
# Audio
|
35 |
+
audio_input = gr.Audio(label="Subir o grabar audio en `ingl茅s` exclusivamente`", sources=["upload", "microphone"], type="filepath")
|
36 |
+
|
37 |
+
# Elecci贸n de idioma
|
38 |
+
language_dropdown = gr.Dropdown(
|
39 |
+
["de_DE", "es_XX", "fr_XX", "sv_SE", "ru_RU"],
|
40 |
+
label="Selecciona el idioma de traducci贸n",
|
41 |
+
value="es_XX"
|
42 |
+
)
|
43 |
+
|
44 |
+
# Segunda fila -> Bot贸n y salida de texto traducido
|
45 |
+
with gr.Row():
|
46 |
+
# Boton
|
47 |
+
translate_button = gr.Button("Transcribir y Traducir")
|
48 |
+
|
49 |
+
# Caja de texto (output)
|
50 |
+
translation_output = gr.Textbox(label="Texto Traducido")
|
51 |
+
|
52 |
+
# Configuraci贸n bot贸n
|
53 |
+
translate_button.click(
|
54 |
+
transcribe_translate,
|
55 |
+
inputs=[audio_input, language_dropdown],
|
56 |
+
outputs=translation_output
|
57 |
+
)
|
58 |
+
|
59 |
+
##Iniciar aplicacion
|
60 |
+
app.queue().launch()
|