Spaces:
Runtime error
Runtime error
Vaibhav Srivastav
commited on
Commit
·
8163f25
1
Parent(s):
9b0c2e3
up
Browse files- app.py +59 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
MODEL_NAME = "openai/whisper-small" #this always needs to stay in line 8 :D sorry for the hackiness
|
7 |
+
lang = "en"
|
8 |
+
|
9 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
10 |
+
pipe = pipeline(
|
11 |
+
task="automatic-speech-recognition",
|
12 |
+
model=MODEL_NAME,
|
13 |
+
chunk_length_s=30,
|
14 |
+
device=device,
|
15 |
+
)
|
16 |
+
|
17 |
+
def transcribe(microphone, file_upload):
|
18 |
+
warn_output = ""
|
19 |
+
if (microphone is not None) and (file_upload is not None):
|
20 |
+
warn_output = (
|
21 |
+
"WARNING: You've uploaded an audio file and used the microphone. "
|
22 |
+
"The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
|
23 |
+
)
|
24 |
+
|
25 |
+
elif (microphone is None) and (file_upload is None):
|
26 |
+
return "ERROR: You have to either use the microphone or upload an audio file"
|
27 |
+
|
28 |
+
file = microphone if microphone is not None else file_upload
|
29 |
+
|
30 |
+
text = pipe(file)["text"]
|
31 |
+
|
32 |
+
return warn_output + text
|
33 |
+
|
34 |
+
|
35 |
+
demo = gr.Blocks()
|
36 |
+
|
37 |
+
mf_transcribe = gr.Interface(
|
38 |
+
fn=transcribe,
|
39 |
+
inputs=[
|
40 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True),
|
41 |
+
gr.inputs.Audio(source="upload", type="filepath", optional=True),
|
42 |
+
],
|
43 |
+
outputs="text",
|
44 |
+
layout="horizontal",
|
45 |
+
theme="huggingface",
|
46 |
+
title="Whisper Demo: Transcribe Audio",
|
47 |
+
description=(
|
48 |
+
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the the fine-tuned"
|
49 |
+
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
50 |
+
" of arbitrary length."
|
51 |
+
),
|
52 |
+
allow_flagging="never",
|
53 |
+
)
|
54 |
+
|
55 |
+
|
56 |
+
with demo:
|
57 |
+
gr.TabbedInterface([mf_transcribe], ["Transcribe Audio"])
|
58 |
+
|
59 |
+
demo.launch(enable_queue=True)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
transformers
|