pksx01 commited on
Commit
6215757
·
1 Parent(s): 32e9ccb

Adding app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -0
app.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastai.vision.all import *
2
+ import librosa
3
+ import gradio as gr
4
+
5
+ learn = load_learner('audio_mnist_classifier_v1.pkl')
6
+ categories = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
7
+
8
+ def mel_spectrogram_tfm(file):
9
+ y, sr = librosa.load(file)
10
+ spec = librosa.feature.melspectrogram(y, sr=sr, n_fft=2048, hop_length=512)
11
+ spec_db = librosa.amplitude_to_db(spec, ref=np.max)
12
+ return spec_db
13
+
14
+ def classify(audio):
15
+ img = mel_spectrogram_tfm(audio)
16
+ img = PILImage.resize((225,225))
17
+ pred, idx, prob = learn.predict(img)
18
+ return dict(zip(categories, map(float, prob)))
19
+
20
+ gr.Interface(fn=classify, inputs=gr.Audio(source="microphone", type="filepath"), outputs=gr.outputs.Label()).launch()