Spaces:
Runtime error
Runtime error
111306047 commited on
Commit ·
bc6423e
1
Parent(s): 3466d4c
add /test route
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
|
@@ -9,7 +11,45 @@ app.add_middleware(
|
|
| 9 |
allow_methods=["*"],
|
| 10 |
allow_headers=["*"],
|
| 11 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@app.get("/")
|
| 14 |
def home():
|
| 15 |
return {"message": "FastAPI Space is running!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
| 11 |
allow_methods=["*"],
|
| 12 |
allow_headers=["*"],
|
| 13 |
)
|
| 14 |
+
# ===== 載入模型 =====
|
| 15 |
+
# 換成你自己的模型 repo 名稱
|
| 16 |
+
MODEL_REPO = "raxhel/Malvec"
|
| 17 |
+
|
| 18 |
+
# 如果是私有模型,會自動從 HF_TOKEN 抓取權限
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO, use_auth_token=True)
|
| 20 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_REPO, use_auth_token=True)
|
| 21 |
+
model.eval()
|
| 22 |
|
| 23 |
@app.get("/")
|
| 24 |
def home():
|
| 25 |
return {"message": "FastAPI Space is running!"}
|
| 26 |
+
|
| 27 |
+
@app.post("/test")
|
| 28 |
+
def test_model(data: dict):
|
| 29 |
+
text = data.get("text", "")
|
| 30 |
+
if not text:
|
| 31 |
+
return {"error": "請提供 text"}
|
| 32 |
+
|
| 33 |
+
# Tokenize
|
| 34 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 35 |
+
|
| 36 |
+
# 模型推論
|
| 37 |
+
with torch.no_grad():
|
| 38 |
+
outputs = model(**inputs)
|
| 39 |
+
|
| 40 |
+
# 取 logits
|
| 41 |
+
logits = outputs.logits
|
| 42 |
+
probs = torch.softmax(logits, dim=-1)
|
| 43 |
+
pred_label_id = torch.argmax(probs, dim=-1).item()
|
| 44 |
+
confidence = probs[0][pred_label_id].item()
|
| 45 |
+
|
| 46 |
+
# 若 config 裡有 id2label
|
| 47 |
+
label = model.config.id2label.get(pred_label_id, str(pred_label_id))
|
| 48 |
+
|
| 49 |
+
return {
|
| 50 |
+
"input_text": text,
|
| 51 |
+
"predicted_label": label,
|
| 52 |
+
"confidence": round(confidence, 4),
|
| 53 |
+
"logits": logits.tolist()
|
| 54 |
+
}
|
| 55 |
+
|