Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,50 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def greet(name):
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
-
ds = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
|
| 6 |
iface = gr.Interface(
|
| 7 |
-
|
| 8 |
inputs=gr.inputs.Audio(),
|
| 9 |
-
outputs=
|
| 10 |
-
|
| 11 |
live=True
|
| 12 |
)
|
| 13 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import librosa
|
| 3 |
+
from tensorflow.keras.models import load_model
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# load model
|
| 7 |
+
model = load_model("BBBN_model.hdf5")
|
| 8 |
+
|
| 9 |
+
# basic variables for mel spectrogram
|
| 10 |
+
target_sr = 22050
|
| 11 |
+
frame_size = 2048
|
| 12 |
+
frame_shift_len = 1024
|
| 13 |
+
n_mels = 128
|
| 14 |
+
|
| 15 |
+
genre_classes = {
|
| 16 |
+
0: "Blues",
|
| 17 |
+
1: "Classical",
|
| 18 |
+
2: "Country",
|
| 19 |
+
3: "Disco",
|
| 20 |
+
4: "Hiphop",
|
| 21 |
+
5: "Jazz",
|
| 22 |
+
6: "Metal",
|
| 23 |
+
7: "Pop",
|
| 24 |
+
8: "Reggae",
|
| 25 |
+
9: "Rock"
|
| 26 |
+
}
|
| 27 |
+
def get_melspec_feature(X, target_sr, frame_size, frame_shift_len, n_mels):
|
| 28 |
+
melspec_feature = []
|
| 29 |
+
for audio in X:
|
| 30 |
+
audio_melspec = librosa.feature.melspectrogram(y=audio, sr=target_sr, n_fft=frame_size, hop_length=frame_shift_len)
|
| 31 |
+
audio_melspec = librosa.power_to_db(audio_melspec)
|
| 32 |
+
audio_melspec = audio_melspec.T
|
| 33 |
+
melspec_feature.append(audio_melspec)
|
| 34 |
+
return np.array(melspec_feature, dtype=np.float32)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def predict_genre(audio):
|
| 38 |
+
melspec = get_melspec_feature(audio, target_sr, frame_size, frame_shift_len, n_mels)
|
| 39 |
+
prediction = model.predict(melspec)[0]
|
| 40 |
+
return {genre_classes[i]: float(prediction[i]) for i in range(5)}
|
| 41 |
+
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
iface = gr.Interface(
|
| 44 |
+
predict_genre,
|
| 45 |
inputs=gr.inputs.Audio(),
|
| 46 |
+
outputs=gr.outputs.Label(num_top_classes=5),
|
| 47 |
+
title="Music Genre Classifier",
|
| 48 |
live=True
|
| 49 |
)
|
| 50 |
iface.launch()
|