MarcoM003 commited on
Commit
370a470
verified
1 Parent(s): 066c1e8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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()