sanchit-gandhi HF staff commited on
Commit
4095019
1 Parent(s): 7ce7384

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+ import librosa
5
+ import soundfile
6
+
7
+ SAMPLE_RATE = 16000
8
+
9
+ pipe = pipeline(model="openai/whisper-small")
10
+
11
+
12
+ def transcribe(Microphone, File_Upload):
13
+ warn_output = ""
14
+ if (Microphone is not None) and (File_Upload is not None):
15
+ warn_output = "WARNING: You've uploaded an audio file and used the microphone. " \
16
+ "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
17
+ file = Microphone
18
+
19
+ elif (Microphone is None) and (File_Upload is None):
20
+ return "ERROR: You have to either use the microphone or upload an audio file"
21
+
22
+ elif Microphone is not None:
23
+ file = Microphone
24
+ else:
25
+ file = File_Upload
26
+
27
+ text = pipe(file)["text"]
28
+
29
+ return warn_output + text
30
+
31
+
32
+ iface = gr.Interface(
33
+ fn=transcribe,
34
+ inputs=[
35
+ gr.inputs.Audio(source="microphone", type='filepath', optional=True),
36
+ gr.inputs.Audio(source="upload", type='filepath', optional=True),
37
+ ],
38
+ outputs="text",
39
+ layout="horizontal",
40
+ theme="huggingface",
41
+ title="Whisper Small",
42
+ description="Demo for multilingual speech recognition using the official OpenAI [Whisper small checkpoint](https://huggingface.co/openai/whisper-small).",
43
+ allow_flagging='never',
44
+ )
45
+
46
+ iface.launch(enable_queue=True)