sanchit-gandhi HF staff commited on
Commit
62d3d54
1 Parent(s): d71ae8d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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="birgermoell/whisper-small-sv-bm") # change to "your-username/the-name-you-picked"
10
+
11
+
12
+ def process_audio_file(file):
13
+ data, sr = librosa.load(file)
14
+
15
+ if sr != SAMPLE_RATE:
16
+ data = librosa.resample(data, sr, SAMPLE_RATE)
17
+
18
+ # monochannel
19
+ data = librosa.to_mono(data)
20
+ return data
21
+
22
+
23
+ def transcribe(Microphone, File_Upload):
24
+ warn_output = ""
25
+ if (Microphone is not None) and (File_Upload is not None):
26
+ warn_output = "WARNING: You've uploaded an audio file and used the microphone. " \
27
+ "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
28
+ file = Microphone
29
+
30
+ elif (Microphone is None) and (File_Upload is None):
31
+ return "ERROR: You have to either use the microphone or upload an audio file"
32
+
33
+ elif Microphone is not None:
34
+ file = Microphone
35
+ else:
36
+ file = File_Upload
37
+
38
+ audio_data = process_audio_file(file)
39
+
40
+ text = pipe(audio_data)["text"]
41
+
42
+ return warn_output + text
43
+
44
+
45
+ iface = gr.Interface(
46
+ fn=transcribe,
47
+ inputs=[
48
+ gr.inputs.Audio(source="microphone", type='filepath', optional=True),
49
+ gr.inputs.Audio(source="upload", type='filepath', optional=True),
50
+ ],
51
+ outputs="text",
52
+ layout="horizontal",
53
+ theme="huggingface",
54
+ title="Whisper Small SV",
55
+ description="Demo for Swedish speech recognition using the [Whisper Small SV BM checkpoint](https://huggingface.co/birgermoell/whisper-small-sv-bm).",
56
+ allow_flagging='never',
57
+ )
58
+
59
+ iface.launch(enable_queue=True)