Spaces:
Sleeping
Sleeping
File size: 2,323 Bytes
370a470 3d24aef a228f54 370a470 eb80a74 538404c eb80a74 538404c a228f54 eb80a74 538404c eb80a74 538404c a228f54 538404c a228f54 538404c 0835bf1 eb80a74 c9a8751 eb80a74 538404c eb80a74 538404c eb80a74 c9a8751 eb80a74 538404c a228f54 538404c a228f54 538404c eb80a74 538404c eb80a74 538404c eb80a74 538404c eb80a74 538404c eb80a74 538404c 3d24aef eb80a74 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
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() |