Spaces:
Sleeping
Sleeping
import gradio as gr | |
import whisper | |
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast | |
## Cargar modelos de Whisper | |
whisper_model = whisper.load_model("base.en") | |
## Cargar modelos de MBart | |
translation_model = MBartForConditionalGeneration.from_pretrained("SnypzZz/Llama2-13b-Language-translate") | |
tokenizer = MBart50TokenizerFast.from_pretrained("SnypzZz/Llama2-13b-Language-translate", src_lang="en_XX") | |
## Funci贸n para transcribir y traducir el audio | |
def transcribe_translate(audio_file, target_language): | |
# Transcribir audio con Whisper (aqu铆 se usa la variable whisper_model) | |
transcription = whisper_model.transcribe(audio_file, language="english")["text"] | |
# Traducir texto a idioma seleccionado (aqu铆 se usa translation_model y tokenizer) | |
model_inputs = tokenizer(transcription, return_tensors="pt") | |
generated_tokens = translation_model.generate( | |
**model_inputs, | |
forced_bos_token_id=tokenizer.lang_code_to_id[target_language] | |
) | |
translated_text = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0] | |
return translated_text.strip("[]' ") | |
## Interfaz de Gradio | |
# Est谩 creado en filas para "organizar" la distribuci贸n de cada caja | |
with gr.Blocks(theme="Nymbo/Nymbo_Theme") as app: | |
# T铆tulo | |
gr.Markdown("## Transcripci贸n y Traducci贸n de Audio") | |
# Primera fila -> Input de audio y elecci贸n del idioma | |
with gr.Row(): | |
# Audio | |
audio_input = gr.Audio(label="Subir o grabar audio en `ingl茅s` exclusivamente`", sources=["upload", "microphone"], type="filepath") | |
# Elecci贸n de idioma | |
language_dropdown = gr.Dropdown( | |
["de_DE", "es_XX", "fr_XX", "sv_SE", "ru_RU"], | |
label="Selecciona el idioma de traducci贸n", | |
value="es_XX" | |
) | |
# Segunda fila -> Bot贸n y salida de texto traducido | |
with gr.Row(): | |
# Boton | |
translate_button = gr.Button("Transcribir y Traducir") | |
# Caja de texto (output) | |
translation_output = gr.Textbox(label="Texto Traducido") | |
# Configuraci贸n bot贸n | |
translate_button.click( | |
transcribe_translate, | |
inputs=[audio_input, language_dropdown], | |
outputs=translation_output | |
) | |
##Iniciar aplicacion | |
app.queue().launch() |