Spaces:
Runtime error
Runtime error
Download app.py from malvec/codebert_Malvec: direct link, hf CLI and curl.
- Browser
- Download file 1.77 kB
-
https://huggingface.co/spaces/malvec/codebert_Malvec/resolve/08755675832305be463fdac1c2b14ee8c64a910a/app.py
- Command line
-
hf download hf://spaces/malvec/codebert_Malvec@08755675832305be463fdac1c2b14ee8c64a910a/app.py
-
curl -L -o app.py https://huggingface.co/spaces/malvec/codebert_Malvec/resolve/08755675832305be463fdac1c2b14ee8c64a910a/app.py
1.77 kB
| 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() | |
| def home(): | |
| return {"message": "FastAPI Space is running!"} | |
| # 簡單測試路由:確認 tokenizer + model 真的能跑 | |
| 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(), | |
| } | |