MarcoM003's picture
Create app.py
370a470 verified
raw
history blame
2.67 kB
import gradio as gr
import whisper
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
# Cargar modelo Whisper y modelo de traducci贸n MBart
whisper_models = {
"tiny.en": whisper.load_model("tiny.en"),
"base.en": whisper.load_model("base.en"),
"small.en": whisper.load_model("small.en"),
"medium.en": whisper.load_model("medium.en"),
}
translation_model = MBartForConditionalGeneration.from_pretrained("SnypzZz/Llama2-13b-Language-translate")
translation_tokenizer = MBart50TokenizerFast.from_pretrained("SnypzZz/Llama2-13b-Language-translate", src_lang="en_XX")
# Funci贸n para transcribir el audio
def whisper_transcript(model_size, audio_file):
loaded_model = whisper_models[model_size]
transcript = loaded_model.transcribe(audio_file, language="english")
return transcript["text"]
# Funci贸n para traducir el texto
def translate_text(input_text, language_code):
model_inputs = translation_tokenizer(input_text, return_tensors="pt")
generated_tokens = translation_model.generate(
**model_inputs,
forced_bos_token_id=translation_tokenizer.lang_code_to_id[language_code]
)
output = translation_tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
return output.strip()
# Interfaz de Gradio combinada
with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
gr.Markdown("# Transcribe y Traduce Audios")
gr.Markdown("**C贸mo usar**: Selecciona un modelo de transcripci贸n, graba o sube un audio en ingl茅s y clica en transcribir. Luego, elige un idioma y traduce el texto.")
# Selecci贸n de modelo y entrada de audio
model_selector = gr.Dropdown(
label="Selecciona el modelo Whisper",
choices=["tiny.en", "base.en", "small.en", "medium.en"],
value="base.en",
)
audio_input = gr.Audio(label="Sube o graba el audio", source=["upload", "microphone"], type="filepath")
# Bot贸n para ejecutar transcripci贸n
transcript_output = gr.Textbox(label="Texto transcrito (ingl茅s)")
transcribe_button = gr.Button("Transcribir Audio")
transcribe_button.click(whisper_transcript, inputs=[model_selector, audio_input], outputs=transcript_output)
# Selecci贸n de idioma de traducci贸n y bot贸n de traducci贸n
language_selector = gr.Dropdown(["de_DE", "es_XX", "fr_XX", "sv_SE", "ru_RU"], label="Elige el idioma de salida")
translation_output = gr.Textbox(label="Texto traducido")
translate_button = gr.Button("Traducir Texto")
translate_button.click(translate_text, inputs=[transcript_output, language_selector], outputs=translation_output)
# Lanzar la interfaz de Gradio
demo.launch()