MarcoM003 commited on
Commit
eb80a74
verified
1 Parent(s): 538404c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -12
app.py CHANGED
@@ -2,17 +2,19 @@ import gradio as gr
2
  import whisper
3
  from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
4
 
5
- # Cargar modelos de Whisper y MBart
6
  whisper_model = whisper.load_model("base.en")
 
 
7
  translation_model = MBartForConditionalGeneration.from_pretrained("SnypzZz/Llama2-13b-Language-translate")
8
  tokenizer = MBart50TokenizerFast.from_pretrained("SnypzZz/Llama2-13b-Language-translate", src_lang="en_XX")
9
 
10
- # Funci贸n para transcribir y traducir el audio
11
- def transcribe_and_translate(audio_file, target_language):
12
- # Transcribir el audio con Whisper
13
  transcription = whisper_model.transcribe(audio_file, language="english")["text"]
14
 
15
- # Traducir el texto al idioma seleccionado
16
  model_inputs = tokenizer(transcription, return_tensors="pt")
17
  generated_tokens = translation_model.generate(
18
  **model_inputs,
@@ -21,29 +23,38 @@ def transcribe_and_translate(audio_file, target_language):
21
  translated_text = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
22
  return translated_text.strip("[]' ")
23
 
24
- # Interfaz de Gradio
 
25
  with gr.Blocks() as app:
 
26
  gr.Markdown("## Transcripci贸n y Traducci贸n de Audio")
27
 
28
- # Entrada de audio y selecci贸n de idioma
29
  with gr.Row():
30
- audio_input = gr.Audio(label="Sube o graba un audio en ingl茅s", sources=["upload", "microphone"], type="filepath")
 
 
 
31
  language_dropdown = gr.Dropdown(
32
  ["de_DE", "es_XX", "fr_XX", "sv_SE", "ru_RU"],
33
  label="Selecciona el idioma de traducci贸n",
34
  value="es_XX"
35
  )
36
 
37
- # Bot贸n y salida de texto traducido
38
  with gr.Row():
 
39
  translate_button = gr.Button("Transcribir y Traducir")
 
 
40
  translation_output = gr.Textbox(label="Texto Traducido")
41
 
42
- # Configuraci贸n del bot贸n
43
  translate_button.click(
44
- transcribe_and_translate,
45
  inputs=[audio_input, language_dropdown],
46
  outputs=translation_output
47
  )
48
 
49
- app.queue().launch()
 
 
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,
 
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() 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**", 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()