Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +68 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
asr = pipeline(task="automatic-speech-recognition",
|
| 5 |
+
model="openai/whisper-medium")
|
| 6 |
+
|
| 7 |
+
# Especificar el idioma de salida en espa帽ol
|
| 8 |
+
asr.model.config.forced_decoder_ids = asr.tokenizer.get_decoder_prompt_ids(language="spanish", task="transcribe")
|
| 9 |
+
|
| 10 |
+
demo = gr.Blocks()
|
| 11 |
+
|
| 12 |
+
def transcribe_long_form(filepath):
|
| 13 |
+
if filepath is None:
|
| 14 |
+
gr.Warning("No audio found, please retry.")
|
| 15 |
+
return ""
|
| 16 |
+
output = asr(
|
| 17 |
+
filepath,
|
| 18 |
+
max_new_tokens=256,
|
| 19 |
+
chunk_length_s=30,
|
| 20 |
+
batch_size=8,
|
| 21 |
+
)
|
| 22 |
+
return output["text"]
|
| 23 |
+
|
| 24 |
+
ner = pipeline("ner",
|
| 25 |
+
model="mrm8488/bert-spanish-cased-finetuned-ner",
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
def get_ner(input_text):
|
| 29 |
+
if input_text is None:
|
| 30 |
+
gr.Warning("No transcription found, please retry.")
|
| 31 |
+
return {"text": "", "entities": ""}
|
| 32 |
+
output = ner(input_text)
|
| 33 |
+
return {"text": input_text, "entities": output}
|
| 34 |
+
|
| 35 |
+
def main(filepath):
|
| 36 |
+
transcription = transcribe_long_form(filepath)
|
| 37 |
+
ner = get_ner(transcription)
|
| 38 |
+
return transcription, ner
|
| 39 |
+
|
| 40 |
+
mic_transcribe = gr.Interface(
|
| 41 |
+
fn=main,
|
| 42 |
+
inputs=gr.Audio(sources="microphone",
|
| 43 |
+
type="filepath"),
|
| 44 |
+
outputs=[gr.Textbox(label="Transcription", lines=3),
|
| 45 |
+
gr.HighlightedText(label="Text with entities")],
|
| 46 |
+
title="Transcribir audio desde grabaci贸n",
|
| 47 |
+
description="Transcripci贸n de audio grabado desde micr贸fono.",
|
| 48 |
+
allow_flagging="never")
|
| 49 |
+
|
| 50 |
+
file_transcribe = gr.Interface(
|
| 51 |
+
fn=main,
|
| 52 |
+
inputs=gr.Audio(sources="upload",
|
| 53 |
+
type="filepath"),
|
| 54 |
+
outputs=[gr.Textbox(label="Transcription", lines=3),
|
| 55 |
+
gr.HighlightedText(label="Text with entities")],
|
| 56 |
+
title="Transcribir audio desde archivo",
|
| 57 |
+
description="Transcripci贸n a partir de un archivo de audio.",
|
| 58 |
+
allow_flagging="never",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
with demo:
|
| 62 |
+
gr.TabbedInterface(
|
| 63 |
+
[mic_transcribe,
|
| 64 |
+
file_transcribe],
|
| 65 |
+
["Transcribe Microphone",
|
| 66 |
+
"Transcribe Audio File"],
|
| 67 |
+
)
|
| 68 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers==4.37.2
|
| 2 |
+
torch==2.2.0
|
| 3 |
+
soundfile==0.12.1
|
| 4 |
+
librosa==0.10.1
|
| 5 |
+
gradio==4.16.0
|