Darendra commited on
Commit
8f9241b
·
verified ·
1 Parent(s): 54584f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -166,30 +166,44 @@ def train_model_cloud(file_obj, sep, epochs, batch_size, lr, progress=gr.Progres
166
  # =========================================================
167
  # 5. LOAD & PREDIKSI
168
  # =========================================================
 
 
 
 
 
169
  def load_model_inference():
170
- global active_model_path
171
 
172
- # Prioritas 1: Model aktif (hasil upload/training barusan)
173
  if active_model_path and os.path.exists(active_model_path):
174
  target_path = active_model_path
175
-
176
- # Prioritas 2: Folder default (upload manual via Files HF)
177
  elif os.path.exists("model_default") and os.path.exists("model_default/config.json"):
178
  target_path = "model_default"
179
-
180
- # Prioritas 3: Download Base Model
181
  else:
182
- return AutoModelForSequenceClassification.from_pretrained("indobenchmark/indobert-base-p1", num_labels=8), \
183
- AutoTokenizer.from_pretrained("indobenchmark/indobert-base-p1")
 
 
184
 
185
  try:
 
186
  tokenizer = AutoTokenizer.from_pretrained(target_path)
187
  model = AutoModelForSequenceClassification.from_pretrained(target_path)
188
  model.eval()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  return model, tokenizer
190
- except:
191
- return AutoModelForSequenceClassification.from_pretrained("indobenchmark/indobert-base-p1", num_labels=8), \
192
- AutoTokenizer.from_pretrained("indobenchmark/indobert-base-p1")
193
 
194
  def predict_text(text):
195
  if not text: return None
@@ -285,4 +299,4 @@ with gr.Blocks(title="IndoBERT Emotion Cloud") as app:
285
  btn_batch.click(predict_csv, inputs=[in_csv_test, in_sep], outputs=out_json)
286
 
287
  if __name__ == "__main__":
288
- app.launch()
 
166
  # =========================================================
167
  # 5. LOAD & PREDIKSI
168
  # =========================================================
169
+
170
+ cached_model = None
171
+ cached_tokenizer = None
172
+ current_loaded_path = None
173
+
174
  def load_model_inference():
175
+ global active_model_path, cached_model, cached_tokenizer, current_loaded_path
176
 
 
177
  if active_model_path and os.path.exists(active_model_path):
178
  target_path = active_model_path
 
 
179
  elif os.path.exists("model_default") and os.path.exists("model_default/config.json"):
180
  target_path = "model_default"
 
 
181
  else:
182
+ target_path = "indobenchmark/indobert-base-p1"
183
+
184
+ if cached_model is not None and current_loaded_path == target_path:
185
+ return cached_model, cached_tokenizer
186
 
187
  try:
188
+ print(f"🔄 Memuat model ke memori dari: {target_path} ...")
189
  tokenizer = AutoTokenizer.from_pretrained(target_path)
190
  model = AutoModelForSequenceClassification.from_pretrained(target_path)
191
  model.eval()
192
+
193
+ cached_model = model
194
+ cached_tokenizer = tokenizer
195
+ current_loaded_path = target_path
196
+ return model, tokenizer
197
+ except Exception as e:
198
+ print(f"⚠️ Gagal memuat, fallback ke base model. Error: {e}")
199
+ tokenizer = AutoTokenizer.from_pretrained("indobenchmark/indobert-base-p1")
200
+ model = AutoModelForSequenceClassification.from_pretrained("indobenchmark/indobert-base-p1", num_labels=8)
201
+ model.eval()
202
+
203
+ cached_model = model
204
+ cached_tokenizer = tokenizer
205
+ current_loaded_path = "indobenchmark/indobert-base-p1"
206
  return model, tokenizer
 
 
 
207
 
208
  def predict_text(text):
209
  if not text: return None
 
299
  btn_batch.click(predict_csv, inputs=[in_csv_test, in_sep], outputs=out_json)
300
 
301
  if __name__ == "__main__":
302
+ app.launch(ssr_mode=False)