Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,56 +2,53 @@ import gradio as gr
|
|
2 |
import whisper
|
3 |
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
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 |
-
|
23 |
-
|
24 |
-
|
25 |
-
generated_tokens = translation_model.generate(
|
26 |
**model_inputs,
|
27 |
-
forced_bos_token_id=
|
28 |
)
|
29 |
-
output =
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
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 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|