Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,22 +9,19 @@ from huggingface_hub import hf_hub_download
|
|
| 9 |
|
| 10 |
REPO_ID = "anggars/neural-mathrock"
|
| 11 |
|
| 12 |
-
#
|
| 13 |
model_path = hf_hub_download(repo_id=REPO_ID, filename="neural_mathrock_model.keras")
|
| 14 |
label_path = hf_hub_download(repo_id=REPO_ID, filename="neural_mathrock_labels.pkl")
|
| 15 |
|
| 16 |
-
# 2. Load model dan kamus
|
| 17 |
model = tf.keras.models.load_model(model_path)
|
| 18 |
with open(label_path, 'rb') as f:
|
| 19 |
labels = pickle.load(f)
|
| 20 |
|
| 21 |
-
# 3. Fungsi prediksi
|
| 22 |
def predict_audio(audio_path):
|
| 23 |
if audio_path is None:
|
| 24 |
-
return {"error": "
|
| 25 |
|
| 26 |
try:
|
| 27 |
-
# Preprocessing audio ke spectrogram
|
| 28 |
y, sr = librosa.load(audio_path, sr=22050, duration=3.0)
|
| 29 |
y = librosa.util.fix_length(y, size=3*22050)
|
| 30 |
spec = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)
|
|
@@ -34,10 +31,8 @@ def predict_audio(audio_path):
|
|
| 34 |
img_rgb = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
|
| 35 |
input_tensor = np.expand_dims(img_rgb.astype(np.float32) / 255.0, axis=0)
|
| 36 |
|
| 37 |
-
# Prediksi
|
| 38 |
preds = model.predict(input_tensor, verbose=0)
|
| 39 |
|
| 40 |
-
# Mapping hasil ke teks pake kamus
|
| 41 |
return {
|
| 42 |
"mbti": str(labels['mbti'][np.argmax(preds[0])]),
|
| 43 |
"emotion": str(labels['emotion'][np.argmax(preds[1])]),
|
|
@@ -48,13 +43,12 @@ def predict_audio(audio_path):
|
|
| 48 |
except Exception as e:
|
| 49 |
return {"error": str(e)}
|
| 50 |
|
| 51 |
-
# 4. UI Gradio
|
| 52 |
demo = gr.Interface(
|
| 53 |
fn=predict_audio,
|
| 54 |
-
inputs=gr.Audio(type="filepath", label="Upload
|
| 55 |
-
outputs=gr.JSON(label="
|
| 56 |
title="Neural Mathrock Classifier"
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
-
demo.launch()
|
|
|
|
| 9 |
|
| 10 |
REPO_ID = "anggars/neural-mathrock"
|
| 11 |
|
| 12 |
+
# Load model and labels
|
| 13 |
model_path = hf_hub_download(repo_id=REPO_ID, filename="neural_mathrock_model.keras")
|
| 14 |
label_path = hf_hub_download(repo_id=REPO_ID, filename="neural_mathrock_labels.pkl")
|
| 15 |
|
|
|
|
| 16 |
model = tf.keras.models.load_model(model_path)
|
| 17 |
with open(label_path, 'rb') as f:
|
| 18 |
labels = pickle.load(f)
|
| 19 |
|
|
|
|
| 20 |
def predict_audio(audio_path):
|
| 21 |
if audio_path is None:
|
| 22 |
+
return {"error": "Please upload an audio file first."}
|
| 23 |
|
| 24 |
try:
|
|
|
|
| 25 |
y, sr = librosa.load(audio_path, sr=22050, duration=3.0)
|
| 26 |
y = librosa.util.fix_length(y, size=3*22050)
|
| 27 |
spec = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)
|
|
|
|
| 31 |
img_rgb = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
|
| 32 |
input_tensor = np.expand_dims(img_rgb.astype(np.float32) / 255.0, axis=0)
|
| 33 |
|
|
|
|
| 34 |
preds = model.predict(input_tensor, verbose=0)
|
| 35 |
|
|
|
|
| 36 |
return {
|
| 37 |
"mbti": str(labels['mbti'][np.argmax(preds[0])]),
|
| 38 |
"emotion": str(labels['emotion'][np.argmax(preds[1])]),
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
return {"error": str(e)}
|
| 45 |
|
|
|
|
| 46 |
demo = gr.Interface(
|
| 47 |
fn=predict_audio,
|
| 48 |
+
inputs=gr.Audio(type="filepath", label="Upload Math Rock / Midwest Emo Audio"),
|
| 49 |
+
outputs=gr.JSON(label="Classification Results"),
|
| 50 |
title="Neural Mathrock Classifier"
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
+
demo.launch(ssr_mode=False)
|