MarcoM003 commited on
Commit
0835bf1
verified
1 Parent(s): 6b7067b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -49
app.py CHANGED
@@ -2,56 +2,53 @@ import gradio as gr
2
  import whisper
3
  from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
4
 
5
- # Cargar modelo Whisper y modelo de traducci贸n MBart
6
- whisper_models = {
7
- "tiny.en": whisper.load_model("tiny.en"),
8
- "base.en": whisper.load_model("base.en"),
9
- "small.en": whisper.load_model("small.en"),
10
- "medium.en": whisper.load_model("medium.en"),
11
- }
12
-
13
- translation_model = MBartForConditionalGeneration.from_pretrained("SnypzZz/Llama2-13b-Language-translate")
14
- translation_tokenizer = MBart50TokenizerFast.from_pretrained("SnypzZz/Llama2-13b-Language-translate", src_lang="en_XX")
15
-
16
- # Funci贸n para transcribir el audio
17
- def whisper_transcript(model_size, audio_file):
18
- loaded_model = whisper_models[model_size]
19
- transcript = loaded_model.transcribe(audio_file, language="english")
20
- return transcript["text"]
21
 
22
- # Funci贸n para traducir el texto
23
- def translate_text(input_text, language_code):
24
- model_inputs = translation_tokenizer(input_text, return_tensors="pt")
25
- generated_tokens = translation_model.generate(
26
  **model_inputs,
27
- forced_bos_token_id=translation_tokenizer.lang_code_to_id[language_code]
28
  )
29
- output = translation_tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
30
- return output.strip()
31
-
32
- # Interfaz de Gradio combinada
33
- with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
34
- gr.Markdown("# Transcribe y Traduce Audios")
35
- 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.")
36
-
37
- # Selecci贸n de modelo y entrada de audio
38
- model_selector = gr.Dropdown(
39
- label="Selecciona el modelo Whisper",
40
- choices=["tiny.en", "base.en", "small.en", "medium.en"],
41
- value="base.en",
42
- )
43
- audio_input = gr.Audio(label="Sube o graba el audio", source=["upload", "microphone"], type="filepath")
44
-
45
- # Bot贸n para ejecutar transcripci贸n
46
- transcript_output = gr.Textbox(label="Texto transcrito (ingl茅s)")
47
- transcribe_button = gr.Button("Transcribir Audio")
48
- transcribe_button.click(whisper_transcript, inputs=[model_selector, audio_input], outputs=transcript_output)
49
 
50
- # Selecci贸n de idioma de traducci贸n y bot贸n de traducci贸n
51
- language_selector = gr.Dropdown(["de_DE", "es_XX", "fr_XX", "sv_SE", "ru_RU"], label="Elige el idioma de salida")
52
- translation_output = gr.Textbox(label="Texto traducido")
53
- translate_button = gr.Button("Traducir Texto")
54
- translate_button.click(translate_text, inputs=[transcript_output, language_selector], outputs=translation_output)
55
-
56
- # Lanzar la interfaz de Gradio
57
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import whisper
3
  from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
4
 
5
+ # funci贸n para transcribir el audio
6
+
7
+ 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
+ dropdown = gr.Dropdown(["de_DE", "es_XX", "fr_XX", "sv_SE", "ru_RU"], label="Choose Output Language")
 
 
 
 
 
 
 
 
 
 
11
 
12
+ def execute(input, dropdown_value):
13
+ model_inputs = tokenizer(input, return_tensors="pt")
14
+ generated_tokens = model.generate(
 
15
  **model_inputs,
16
+ forced_bos_token_id=tokenizer.lang_code_to_id[dropdown_value]
17
  )
18
+ output = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
19
+ output = output.strip("[]' ")
20
+ return output
21
+
22
+ def whisper_transcript(model_size, audio_file):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ source = audio_file
25
+ loaded_model = whisper.load_model(model_size)
26
+ transcript = loaded_model.transcribe(source, language="english")
27
+
28
+ return transcript["text"]
29
+
30
+
31
+ # interfaz gradio
32
+ gradio_ui = gr.Interface(
33
+ fn=whisper_transcript,
34
+ theme="Nymbo/Nymbo_Theme",
35
+ title="Transcribir audios en ingl茅s a texto",
36
+ description="**C贸mo usar**: Elegir uno de los 4 modelos, subir un audio o grabarlo y clicar el bot贸n de Submit.",
37
+ article="**Nota**: Exclusivo para audios en ingl茅s.",
38
+ inputs=[
39
+ gr.Dropdown(
40
+ label="Select Model",
41
+ choices=[
42
+ "tiny.en",
43
+ "base.en",
44
+ "small.en",
45
+ "medium.en",
46
+ ],
47
+ value="base",
48
+ ),
49
+ gr.Audio(label="Upload Audio File", sources=["upload", "microphone"], type="filepath"),
50
+ ],
51
+ outputs=gr.Textbox(label="Whisper Transcript"),
52
+ )
53
+
54
+ gradio_ui.queue().launch()