anggars commited on
Commit
e25a431
Β·
verified Β·
1 Parent(s): 3abd738

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -19,21 +19,23 @@ print("Downloading model and extracting label encoders...")
19
  model_path = hf_hub_download(repo_id=REPO_ID, filename="model.pt")
20
  ckpt = torch.load(model_path, map_location=torch.device('cpu'), weights_only=False)
21
 
22
- # Petain manual sesuai nama key pas lu nge-save model di PyTorch
 
 
 
 
23
  label_encoders = {
24
  'mbti': ckpt['le_mbti'],
25
  'emotion': ckpt['le_emotion'],
26
- 'vibe': ckpt['le_vibe'],
27
- 'intensity': ckpt['le_intensity'],
28
- 'tempo': ckpt['le_tempo']
29
  }
30
  num_classes = {col: len(le.classes_) for col, le in label_encoders.items()}
31
 
32
  # ============================================================
33
  # ── ARCHITECTURE (WAJIB SESUAIKAN SAMA IPYNB TRAINING LU) ──
34
  # ============================================================
35
- # HAPUS DAN GANTI blok arsitektur ini pake class asli lu dari jupyter notebook.
36
-
37
  class AudioTransformer(nn.Module):
38
  def __init__(self):
39
  super().__init__()
@@ -75,7 +77,6 @@ class MultimodalMathRock(nn.Module):
75
  x_t = self.text(input_ids, attention_mask)
76
  fused = self.fusion(torch.cat([x_a, x_t], dim=1))
77
  return {col: self.heads[col](fused) for col in TARGET_COLS}
78
-
79
  # ============================================================
80
 
81
  print("Initializing architecture and loading weights...")
@@ -92,14 +93,14 @@ def predict_multimodal(audio_path, lyrics):
92
  return [None]*5
93
 
94
  try:
95
- # 1. Load audio mentah (otomatis jadi 16kHz mono)
96
  y, _ = librosa.load(audio_path, sr=SR, duration=DURATION, mono=True)
97
 
98
- # 2. Ekstraksi fitur pake WavLM processor
99
  inputs = audio_processor(y, sampling_rate=SR, return_tensors="pt")
100
  audio_tensor = inputs.input_values
101
 
102
- # 3. Proses Lirik
103
  if not lyrics or lyrics.strip() == "":
104
  lyrics = "instrumental"
105
 
@@ -108,7 +109,7 @@ def predict_multimodal(audio_path, lyrics):
108
  truncation=True, return_tensors='pt'
109
  )
110
 
111
- # 4. Forward pass
112
  with torch.no_grad():
113
  out = model(audio_tensor, enc['input_ids'], enc['attention_mask'])
114
 
@@ -128,7 +129,7 @@ def predict_multimodal(audio_path, lyrics):
128
 
129
  # ── UI ──
130
  with gr.Blocks() as demo:
131
- gr.Markdown("# Neural Math Rock Classifier")
132
  gr.Markdown("Multimodal WavLM & XLM-RoBERTa based MBTI and emotion classification system.")
133
 
134
  with gr.Row():
 
19
  model_path = hf_hub_download(repo_id=REPO_ID, filename="model.pt")
20
  ckpt = torch.load(model_path, map_location=torch.device('cpu'), weights_only=False)
21
 
22
+ # ── LABEL ENCODER PATCH ──
23
+ class DummyEncoder:
24
+ def __init__(self, classes_list):
25
+ self.classes_ = np.array(classes_list)
26
+
27
  label_encoders = {
28
  'mbti': ckpt['le_mbti'],
29
  'emotion': ckpt['le_emotion'],
30
+ 'vibe': DummyEncoder(['Aggressive', 'Atmospheric', 'Melancholic', 'Technical']),
31
+ 'intensity': DummyEncoder(['High', 'Low', 'Medium']),
32
+ 'tempo': DummyEncoder(['Fast', 'Moderate', 'Slow'])
33
  }
34
  num_classes = {col: len(le.classes_) for col, le in label_encoders.items()}
35
 
36
  # ============================================================
37
  # ── ARCHITECTURE (WAJIB SESUAIKAN SAMA IPYNB TRAINING LU) ──
38
  # ============================================================
 
 
39
  class AudioTransformer(nn.Module):
40
  def __init__(self):
41
  super().__init__()
 
77
  x_t = self.text(input_ids, attention_mask)
78
  fused = self.fusion(torch.cat([x_a, x_t], dim=1))
79
  return {col: self.heads[col](fused) for col in TARGET_COLS}
 
80
  # ============================================================
81
 
82
  print("Initializing architecture and loading weights...")
 
93
  return [None]*5
94
 
95
  try:
96
+ # Load audio mentah (otomatis jadi 16kHz mono)
97
  y, _ = librosa.load(audio_path, sr=SR, duration=DURATION, mono=True)
98
 
99
+ # Ekstraksi fitur pake WavLM processor
100
  inputs = audio_processor(y, sampling_rate=SR, return_tensors="pt")
101
  audio_tensor = inputs.input_values
102
 
103
+ # Proses Lirik
104
  if not lyrics or lyrics.strip() == "":
105
  lyrics = "instrumental"
106
 
 
109
  truncation=True, return_tensors='pt'
110
  )
111
 
112
+ # Forward pass
113
  with torch.no_grad():
114
  out = model(audio_tensor, enc['input_ids'], enc['attention_mask'])
115
 
 
129
 
130
  # ── UI ──
131
  with gr.Blocks() as demo:
132
+ gr.Markdown("# Neural Math Rock & Midwest Emo Classifier")
133
  gr.Markdown("Multimodal WavLM & XLM-RoBERTa based MBTI and emotion classification system.")
134
 
135
  with gr.Row():