Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,10 +18,9 @@ def predict_audio(audio_path):
|
|
| 18 |
global model
|
| 19 |
|
| 20 |
if audio_path is None:
|
| 21 |
-
return
|
| 22 |
|
| 23 |
try:
|
| 24 |
-
# Pindah semua import library berat ke dalem fungsi
|
| 25 |
import librosa
|
| 26 |
import cv2
|
| 27 |
import tensorflow as tf
|
|
@@ -29,8 +28,14 @@ def predict_audio(audio_path):
|
|
| 29 |
if model is None:
|
| 30 |
model = tf.keras.models.load_model(model_path)
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
y = librosa.util.fix_length(y, size=3*22050)
|
|
|
|
| 34 |
spec = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)
|
| 35 |
spec_db = librosa.power_to_db(spec, ref=np.max)
|
| 36 |
img = (spec_db - spec_db.min()) / (spec_db.max() - spec_db.min()) * 255.0
|
|
@@ -40,20 +45,29 @@ def predict_audio(audio_path):
|
|
| 40 |
|
| 41 |
preds = model.predict(input_tensor, verbose=0)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
return
|
| 52 |
|
|
|
|
| 53 |
demo = gr.Interface(
|
| 54 |
fn=predict_audio,
|
| 55 |
inputs=gr.Audio(type="filepath", label="Upload Math Rock / Midwest Emo Audio"),
|
| 56 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
title="Neural Mathrock Classifier"
|
| 58 |
)
|
| 59 |
|
|
|
|
| 18 |
global model
|
| 19 |
|
| 20 |
if audio_path is None:
|
| 21 |
+
return "Error: Upload file audio dulu.", "-", "-", "-", "-"
|
| 22 |
|
| 23 |
try:
|
|
|
|
| 24 |
import librosa
|
| 25 |
import cv2
|
| 26 |
import tensorflow as tf
|
|
|
|
| 28 |
if model is None:
|
| 29 |
model = tf.keras.models.load_model(model_path)
|
| 30 |
|
| 31 |
+
# Hitung durasi dan ambil tengahnya
|
| 32 |
+
total_duration = librosa.get_duration(path=audio_path)
|
| 33 |
+
offset_time = (total_duration / 2.0) - 1.5 if total_duration > 6.0 else 0.0
|
| 34 |
+
|
| 35 |
+
# Load 3 detik dari tengah
|
| 36 |
+
y, sr = librosa.load(audio_path, sr=22050, offset=offset_time, duration=3.0)
|
| 37 |
y = librosa.util.fix_length(y, size=3*22050)
|
| 38 |
+
|
| 39 |
spec = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)
|
| 40 |
spec_db = librosa.power_to_db(spec, ref=np.max)
|
| 41 |
img = (spec_db - spec_db.min()) / (spec_db.max() - spec_db.min()) * 255.0
|
|
|
|
| 45 |
|
| 46 |
preds = model.predict(input_tensor, verbose=0)
|
| 47 |
|
| 48 |
+
# Ekstrak hasil prediksi jadi variabel terpisah
|
| 49 |
+
mbti = str(labels['mbti'][np.argmax(preds[0])])
|
| 50 |
+
emotion = str(labels['emotion'][np.argmax(preds[1])])
|
| 51 |
+
vibe = str(labels['vibe'][np.argmax(preds[2])])
|
| 52 |
+
intensity = str(labels['intensity'][np.argmax(preds[3])])
|
| 53 |
+
tempo = str(labels['tempo'][np.argmax(preds[4])])
|
| 54 |
+
|
| 55 |
+
return mbti, emotion, vibe, intensity, tempo
|
| 56 |
+
|
| 57 |
except Exception as e:
|
| 58 |
+
return f"Error: {str(e)}", "-", "-", "-", "-"
|
| 59 |
|
| 60 |
+
# UI Gradio yang lebih rapi pake Textbox terpisah
|
| 61 |
demo = gr.Interface(
|
| 62 |
fn=predict_audio,
|
| 63 |
inputs=gr.Audio(type="filepath", label="Upload Math Rock / Midwest Emo Audio"),
|
| 64 |
+
outputs=[
|
| 65 |
+
gr.Textbox(label="MBTI Personality"),
|
| 66 |
+
gr.Textbox(label="Emotion"),
|
| 67 |
+
gr.Textbox(label="Audio Vibe"),
|
| 68 |
+
gr.Textbox(label="Intensity"),
|
| 69 |
+
gr.Textbox(label="Tempo")
|
| 70 |
+
],
|
| 71 |
title="Neural Mathrock Classifier"
|
| 72 |
)
|
| 73 |
|