lyi029 commited on
Commit
e020aa8
·
1 Parent(s): 0b509ad
Files changed (1) hide show
  1. app.py +28 -0
app.py CHANGED
@@ -8,6 +8,7 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
8
  from typing import List
9
  from collections import Counter
10
  from fastapi.staticfiles import StaticFiles
 
11
  # ====== FastAPI 初始化 ======
12
  app = FastAPI(
13
  title="Malvec Prediction Space (A)",
@@ -89,6 +90,7 @@ def get_model_info():
89
 
90
  return {
91
  "model_name": MODEL_REPO,
 
92
  "num_labels": model.config.num_labels,
93
  "labels": list(model.config.id2label.values())
94
  if hasattr(model.config, 'id2label') else None,
@@ -220,6 +222,32 @@ async def trigger_train(background_tasks: BackgroundTasks):
220
  background_tasks.add_task(call_space_b)
221
  return {"status": "ok", "detail": f"Training triggered at {SPACE_B_URL}"}
222
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
 
224
  # ----------------------------------------------------------
225
  # 健康檢查
 
8
  from typing import List
9
  from collections import Counter
10
  from fastapi.staticfiles import StaticFiles
11
+ CURRENT_TAG = os.getenv("MODEL_TAG", "main")
12
  # ====== FastAPI 初始化 ======
13
  app = FastAPI(
14
  title="Malvec Prediction Space (A)",
 
90
 
91
  return {
92
  "model_name": MODEL_REPO,
93
+ "current_tag": CURRENT_TAG, # 👈 回報目前版本
94
  "num_labels": model.config.num_labels,
95
  "labels": list(model.config.id2label.values())
96
  if hasattr(model.config, 'id2label') else None,
 
222
  background_tasks.add_task(call_space_b)
223
  return {"status": "ok", "detail": f"Training triggered at {SPACE_B_URL}"}
224
 
225
+ def _reload_model_by_tag(tag: str):
226
+ global tokenizer, model, CURRENT_TAG
227
+ print(f"♻️ Reloading model: {MODEL_REPO}@{tag}")
228
+ tok = AutoTokenizer.from_pretrained(MODEL_REPO, token=HF_TOKEN, revision=tag)
229
+ mdl = AutoModelForSequenceClassification.from_pretrained(
230
+ MODEL_REPO, token=HF_TOKEN, revision=tag,
231
+ output_attentions=True, output_hidden_states=True
232
+ )
233
+ mdl.to(DEVICE).eval()
234
+ tokenizer = tok
235
+ model = mdl
236
+ CURRENT_TAG = tag
237
+ print("✅ Reloaded.")
238
+
239
+
240
+ @app.post("/api/update-model")
241
+ async def update_model(payload: dict):
242
+ tag = payload.get("tag")
243
+ if not tag:
244
+ raise HTTPException(400, "missing 'tag'")
245
+ try:
246
+ _reload_model_by_tag(tag)
247
+ return {"status":"ok", "tag": tag}
248
+ except Exception as e:
249
+ raise HTTPException(500, f"reload failed: {e}")
250
+
251
 
252
  # ----------------------------------------------------------
253
  # 健康檢查