Spaces:
Runtime error
Runtime error
Commit
·
2b4f358
1
Parent(s):
925fdce
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from cProfile import label
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from fastai.vision.all import *
|
| 4 |
+
from fastai.vision.all import *
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from matplotlib.pyplot import specgram
|
| 7 |
+
import librosa
|
| 8 |
+
import numpy as np
|
| 9 |
+
import librosa.display
|
| 10 |
+
|
| 11 |
+
def is_cat(x): return x[0]=='c'
|
| 12 |
+
|
| 13 |
+
def audio_to_spectrogram(audio_file):
|
| 14 |
+
samples,sample_rate = librosa.load(audio_file)
|
| 15 |
+
fig = plt.figure(figsize=[0.72,0.72])
|
| 16 |
+
ax = fig.add_subplot(111)
|
| 17 |
+
ax.axes.get_xaxis().set_visible(False)
|
| 18 |
+
ax.axes.get_yaxis().set_visible(False)
|
| 19 |
+
ax.set_frame_on(False)
|
| 20 |
+
filename = Path(audio_file).name.replace('mp3','png')
|
| 21 |
+
S = librosa.feature.melspectrogram(y=samples,sr=sample_rate)
|
| 22 |
+
librosa.display.specshow(librosa.power_to_db(S,ref=np.max))
|
| 23 |
+
plt.savefig(filename,dpi=400,bbox_inches='tight',pad_inches=0)
|
| 24 |
+
plt.close('all')
|
| 25 |
+
return filename
|
| 26 |
+
|
| 27 |
+
categories = ('Dog','Cat')
|
| 28 |
+
def catvsdogsoundclassification(audio_file):
|
| 29 |
+
print('in the function')
|
| 30 |
+
filename = audio_to_spectrogram(audio_file)
|
| 31 |
+
learner = load_learner('model.pkl')
|
| 32 |
+
pred,pred_idx,probs = learner.predict(filename)
|
| 33 |
+
return dict(zip(categories,map(float,probs)))
|
| 34 |
+
|
| 35 |
+
# audio_file = gr.inputs.Audio()
|
| 36 |
+
labeel = gr.outputs.Label()
|
| 37 |
+
intf = gr.Interface(fn=catvsdogsoundclassification,inputs=gr.Audio(),outputs=labeel)
|
| 38 |
+
intf.launch(inline=False,debug=True)
|