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