Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fastai.vision.all import *
|
| 3 |
+
import librosa
|
| 4 |
+
import numpy as np
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from pydub import AudioSegment
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
learn = load_learner('model.pkl')
|
| 10 |
+
labels = learn.dls.vocab
|
| 11 |
+
|
| 12 |
+
def audio_to_spectrogram(audio_file):
|
| 13 |
+
if audio_file.endswith('.mp3'):
|
| 14 |
+
with tempfile.NamedTemporaryFile(suffix='.wav') as temp_wav:
|
| 15 |
+
audio = AudioSegment.from_mp3(audio_file)
|
| 16 |
+
audio.export(temp_wav.name, format='wav')
|
| 17 |
+
y, sr = librosa.load(temp_wav.name, sr=None)
|
| 18 |
+
else:
|
| 19 |
+
y, sr = librosa.load(audio_file, sr=None)
|
| 20 |
+
|
| 21 |
+
S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=8000)
|
| 22 |
+
S_dB = librosa.power_to_db(S, ref=np.max)
|
| 23 |
+
fig, ax = plt.subplots()
|
| 24 |
+
img = librosa.display.specshow(S_dB, x_axis='time', y_axis='mel', sr=sr, fmax=8000, ax=ax)
|
| 25 |
+
fig.colorbar(img, ax=ax, format='%+2.0f dB')
|
| 26 |
+
ax.set(title='Mel-frequency spectrogram')
|
| 27 |
+
spectrogram_file = "spectrogram.png"
|
| 28 |
+
plt.savefig(spectrogram_file)
|
| 29 |
+
plt.close()
|
| 30 |
+
return spectrogram_file
|
| 31 |
+
|
| 32 |
+
def predict(audio):
|
| 33 |
+
spectrogram_file = audio_to_spectrogram(audio)
|
| 34 |
+
img = PILImage.create(spectrogram_file)
|
| 35 |
+
img = img.resize((512, 512))
|
| 36 |
+
pred, pred_idx, probs = learn.predict(img)
|
| 37 |
+
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 38 |
+
|
| 39 |
+
examples = ['example_audio.mp3']
|
| 40 |
+
|
| 41 |
+
gr.Interface(
|
| 42 |
+
fn=predict,
|
| 43 |
+
inputs=gr.inputs.Audio(source="upload", type="filepath", label="Upload audio (WAV or MP3)"),
|
| 44 |
+
outputs=gr.components.Label(num_top_classes=3),
|
| 45 |
+
examples=examples,
|
| 46 |
+
).launch()
|