Spaces:
Runtime error
Runtime error
fix app"
Browse files
app.py
DELETED
|
@@ -1,141 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python
|
| 2 |
-
# coding: utf-8
|
| 3 |
-
|
| 4 |
-
# In[3]:
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
import gradio
|
| 8 |
-
|
| 9 |
-
from fastai.vision.all import *
|
| 10 |
-
from fastai.data.all import *
|
| 11 |
-
from pathlib import Path
|
| 12 |
-
import pandas as pd
|
| 13 |
-
from matplotlib.pyplot import specgram
|
| 14 |
-
import librosa
|
| 15 |
-
import librosa.display
|
| 16 |
-
from huggingface_hub import hf_hub_download
|
| 17 |
-
from fastai.learner import load_learner
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# In[10]:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
ref_file = hf_hub_download("gputrain/UrbanSound8K", "UrbanSound8K.csv")
|
| 24 |
-
|
| 25 |
-
model_file = hf_hub_download(
|
| 26 |
-
hf_hub_download("gputrain/UrbanSound8K", "export.pkl")
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# In[3]:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Local test
|
| 34 |
-
# ref_file = "UrbanSound8K.csv"
|
| 35 |
-
# model_file = "export.pkl"
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# In[4]:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
df = pd.read_csv(ref_file)
|
| 42 |
-
df['fname'] = df[['slice_file_name','fold']].apply (lambda x: str(x['slice_file_name'][:-4])+'.png'.strip(),axis=1 )
|
| 43 |
-
my_dict = dict(zip(df.fname,df['class']))
|
| 44 |
-
def label_func(f_name):
|
| 45 |
-
f_name = str(f_name).split('/')[-1:][0]
|
| 46 |
-
return my_dict[f_name]
|
| 47 |
-
model = load_learner (model_file)
|
| 48 |
-
labels = model.dls.vocab
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# In[5]:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
with open("article.md") as f:
|
| 55 |
-
article = f.read()
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# In[6]:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
interface_options = {
|
| 62 |
-
"title": "Urban Sound 8K Classification",
|
| 63 |
-
"description": "A Fast AI example with ResNet34 image classification of a sound wav file transformed to a Mel Spectrogram ",
|
| 64 |
-
#"article": article,
|
| 65 |
-
"interpretation": "default",
|
| 66 |
-
"layout": "horizontal",
|
| 67 |
-
# Audio from validation file
|
| 68 |
-
"examples": ["dog_bark.wav", "children_playing.wav", "air_conditioner.wav", "street_music.wav", "engine_idling.wav",
|
| 69 |
-
"jackhammer.wav", "drilling.wav", "siren.wav","car_horn.wav","gun_shot.wav"],
|
| 70 |
-
"allow_flagging": "never"
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
# In[7]:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
def convert_sounds_melspectogram (audio_file):
|
| 78 |
-
|
| 79 |
-
samples, sample_rate = librosa.load(audio_file) #create onces with librosa
|
| 80 |
-
|
| 81 |
-
fig = plt.figure(figsize=[0.72,0.72])
|
| 82 |
-
ax = fig.add_subplot(111)
|
| 83 |
-
ax.axes.get_xaxis().set_visible(False)
|
| 84 |
-
ax.axes.get_yaxis().set_visible(False)
|
| 85 |
-
ax.set_frame_on(False)
|
| 86 |
-
melS = librosa.feature.melspectrogram(y=samples, sr=sample_rate)
|
| 87 |
-
librosa.display.specshow(librosa.power_to_db(melS, ref=np.max))
|
| 88 |
-
filename = 'temp.png'
|
| 89 |
-
plt.savefig(filename, dpi=400, bbox_inches='tight',pad_inches=0)
|
| 90 |
-
plt.close('all')
|
| 91 |
-
|
| 92 |
-
return None
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
# In[8]:
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
def predict():
|
| 99 |
-
img = PILImage.create('temp.png')
|
| 100 |
-
pred,pred_idx,probs = learn.predict(img)
|
| 101 |
-
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 102 |
-
return labels_probs
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
# In[9]:
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
def end2endpipeline(filename):
|
| 109 |
-
create_image(filename)
|
| 110 |
-
return predict("temp.png")
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
# In[10]:
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
demo = gradio.Interface(
|
| 117 |
-
fn=end2endpipeline,
|
| 118 |
-
inputs=gradio.inputs.Audio(source="upload", type="filepath"),
|
| 119 |
-
outputs=gradio.outputs.Label(num_top_classes=10),
|
| 120 |
-
**interface_options,
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
# In[ ]:
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
launch_options = {
|
| 128 |
-
"enable_queue": True,
|
| 129 |
-
"share": False,
|
| 130 |
-
# thanks Alex for pointing this option to cache examples
|
| 131 |
-
"cache_examples": True,
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
demo.launch(**launch_options)
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
# In[ ]:
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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