Spaces:
Runtime error
Runtime error
Updated app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,21 @@
|
|
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 |
-
|
16 |
-
img = PILImage.resize((225,225))
|
17 |
pred, idx, prob = learn.predict(img)
|
18 |
return dict(zip(categories, map(float, prob)))
|
19 |
|
|
|
1 |
from fastai.vision.all import *
|
2 |
import librosa
|
3 |
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
|
6 |
learn = load_learner('audio_mnist_classifier_v1.pkl')
|
7 |
categories = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
|
8 |
|
9 |
def mel_spectrogram_tfm(file):
|
10 |
y, sr = librosa.load(file)
|
11 |
+
y, _ = librosa.effects.trim(y)
|
12 |
spec = librosa.feature.melspectrogram(y, sr=sr, n_fft=2048, hop_length=512)
|
13 |
spec_db = librosa.amplitude_to_db(spec, ref=np.max)
|
14 |
return spec_db
|
15 |
|
16 |
def classify(audio):
|
17 |
+
spec_db = mel_spectrogram_tfm(audio)
|
18 |
+
img = PILImage.create(spec_db).resize((225,225))
|
19 |
pred, idx, prob = learn.predict(img)
|
20 |
return dict(zip(categories, map(float, prob)))
|
21 |
|