Spaces:
Runtime error
Runtime error
Download app.py from malvec/codebert_Malvec: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/spaces/malvec/codebert_Malvec/resolve/bc6423ecf36d4d78c09440de2cc1ebd87b083fe6/app.py
- Command line
-
hf download hf://spaces/malvec/codebert_Malvec@bc6423ecf36d4d78c09440de2cc1ebd87b083fe6/app.py
-
curl -L -o app.py https://huggingface.co/spaces/malvec/codebert_Malvec/resolve/bc6423ecf36d4d78c09440de2cc1ebd87b083fe6/app.py
1.49 kB
| 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() | |
| def home(): | |
| return {"message": "FastAPI Space is running!"} | |
| 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() | |
| } | |