Spaces:
Runtime error
Runtime error
File size: 1,773 Bytes
0875567 3466d4c bc6423e 3466d4c bc6423e 0875567 bc6423e 3466d4c bc6423e 0875567 bc6423e 0875567 bc6423e 0875567 bc6423e 0875567 bc6423e 0875567 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 57 58 59 60 61 62 63 64 | import os
import torch
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from transformers import AutoTokenizer, AutoModelForSequenceClassification
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# ===== 模型設定 =====
MODEL_REPO = "raxhel/Malvec" # 換成你的模型 repo
HF_TOKEN = os.environ.get("hf_token") # 從 Space Secrets 取得;若模型是公開,None 也可
# CPU 環境(Spaces CPU 機型)
DEVICE = torch.device("cpu")
# ===== 載入模型(啟動時載一次)=====
tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO, token=HF_TOKEN)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_REPO, token=HF_TOKEN)
model.to(DEVICE)
model.eval()
@app.get("/")
def home():
return {"message": "FastAPI Space is running!"}
# 簡單測試路由:確認 tokenizer + model 真的能跑
@app.post("/test")
def test_model(data: dict):
text = data.get("text", "")
if not text:
return {"error": "請提供 text"}
inputs = tokenizer(
text,
return_tensors="pt",
truncation=True,
padding=True,
max_length=512,
)
# 保證在 CPU
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits # [1, num_labels]
probs = torch.softmax(logits, dim=-1)
pred_id = int(torch.argmax(probs, dim=-1).item())
conf = float(probs[0, pred_id].item())
label = model.config.id2label.get(pred_id, str(pred_id))
return {
"input_text": text,
"predicted_label": label,
"confidence": round(conf, 4),
"logits": logits.cpu().tolist(),
}
|