Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,39 +24,31 @@ genre_classes = {
|
|
| 24 |
9: "Rock"
|
| 25 |
}
|
| 26 |
def get_melspec_feature(X, target_sr, frame_size, frame_shift_len, n_mels):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
return np.array(melspec_feature, dtype=np.float32)
|
| 34 |
-
|
| 35 |
-
def process_data_for_conv2D(X, resize_shape=None):
|
| 36 |
-
X_conv2D = []
|
| 37 |
-
for sample in X:
|
| 38 |
-
sample = np.reshape(sample, newshape=(sample.shape[0], sample.shape[1], 1))
|
| 39 |
-
if resize_shape:
|
| 40 |
-
sample = resize(sample, output_shape=resize_shape)
|
| 41 |
-
X_conv2D.append(sample)
|
| 42 |
-
return np.array(X_conv2D, dtype=np.float32)
|
| 43 |
|
| 44 |
|
| 45 |
def predict_genre(audio):
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
prediction = model.predict(melspec)[0]
|
| 53 |
-
return {genre_classes[i]: float(prediction[i]) for i in range(
|
| 54 |
|
| 55 |
|
| 56 |
iface = gr.Interface(
|
| 57 |
predict_genre,
|
| 58 |
-
inputs=gr.inputs.Audio(type="
|
| 59 |
-
outputs=gr.outputs.Label(num_top_classes=
|
| 60 |
title="Music Genre Classifier",
|
| 61 |
live=True
|
| 62 |
)
|
|
|
|
| 24 |
9: "Rock"
|
| 25 |
}
|
| 26 |
def get_melspec_feature(X, target_sr, frame_size, frame_shift_len, n_mels):
|
| 27 |
+
|
| 28 |
+
audio_melspec = librosa.feature.melspectrogram(y=X, sr=target_sr, n_fft=frame_size, hop_length=frame_shift_len)
|
| 29 |
+
audio_melspec = librosa.power_to_db(audio_melspec)
|
| 30 |
+
audio_melspec = audio_melspec.T
|
| 31 |
+
print(audio_melspec.shape)
|
| 32 |
+
return np.array(audio_melspec, dtype=np.float32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
def predict_genre(audio):
|
| 36 |
+
# print(type(audio[1]))
|
| 37 |
+
print(audio)
|
| 38 |
+
start = random.randint(1, 15)
|
| 39 |
+
x, sr = librosa.load(audio, offset=start, duration=10.0)
|
| 40 |
+
# # print(audio)
|
| 41 |
+
# print(type(x))
|
| 42 |
+
melspec = get_melspec_feature(x, target_sr, frame_size, frame_shift_len, 128)
|
| 43 |
+
melspec = np.reshape(melspec, (-1, 431, 128, 1))
|
| 44 |
prediction = model.predict(melspec)[0]
|
| 45 |
+
return {genre_classes[i]: float(prediction[i]) for i in range(10)}
|
| 46 |
|
| 47 |
|
| 48 |
iface = gr.Interface(
|
| 49 |
predict_genre,
|
| 50 |
+
inputs=gr.inputs.Audio(type="filepath", label="Upload Music file"),
|
| 51 |
+
outputs=gr.outputs.Label(num_top_classes=10),
|
| 52 |
title="Music Genre Classifier",
|
| 53 |
live=True
|
| 54 |
)
|