|
import gradio as gr |
|
import whisper |
|
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast |
|
|
|
|
|
whisper_model = whisper.load_model("base.en") |
|
|
|
|
|
translation_model = MBartForConditionalGeneration.from_pretrained("SnypzZz/Llama2-13b-Language-translate") |
|
tokenizer = MBart50TokenizerFast.from_pretrained("SnypzZz/Llama2-13b-Language-translate", src_lang="en_XX") |
|
|
|
|
|
def transcribe_translate(audio_file, target_language): |
|
|
|
transcription = whisper_model.transcribe(audio_file, language="english")["text"] |
|
|
|
|
|
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("[]' ") |
|
|
|
|
|
|
|
with gr.Blocks(theme="Nymbo/Nymbo_Theme") as app: |
|
|
|
gr.Markdown("## Transcripci贸n y Traducci贸n de Audio") |
|
|
|
|
|
with gr.Row(): |
|
|
|
audio_input = gr.Audio(label="Subir o grabar audio en `ingl茅s` exclusivamente`", sources=["upload", "microphone"], type="filepath") |
|
|
|
|
|
language_dropdown = gr.Dropdown( |
|
["de_DE", "es_XX", "fr_XX", "sv_SE", "ru_RU"], |
|
label="Selecciona el idioma de traducci贸n", |
|
value="es_XX" |
|
) |
|
|
|
|
|
with gr.Row(): |
|
|
|
translate_button = gr.Button("Transcribir y Traducir") |
|
|
|
|
|
translation_output = gr.Textbox(label="Texto Traducido") |
|
|
|
|
|
translate_button.click( |
|
transcribe_translate, |
|
inputs=[audio_input, language_dropdown], |
|
outputs=translation_output |
|
) |
|
|
|
|
|
app.queue().launch() |