Download main.py from yazied49/disability-classifier: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/yazied49/disability-classifier/resolve/main/main.py
- Command line
-
hf download hf://yazied49/disability-classifier/main.py
-
curl -L -o main.py https://huggingface.co/yazied49/disability-classifier/resolve/main/main.py
1.18 kB
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| import torch | |
| from transformers import BertTokenizer, BertForSequenceClassification | |
| import json | |
| app = FastAPI() | |
| # تحميل الموديل والتوكنايزر | |
| model_path = "./needs_model" | |
| model = BertForSequenceClassification.from_pretrained(model_path) | |
| tokenizer = BertTokenizer.from_pretrained(model_path) | |
| # تحميل ماب الـ labels | |
| with open(f"{model_path}/id2label.json", "r", encoding="utf-8") as f: | |
| id2label = json.load(f) | |
| # نموذج البيانات اللي جايه من الباك إند | |
| class TextInput(BaseModel): | |
| text: str | |
| def predict(input: TextInput): | |
| inputs = tokenizer(input.text, return_tensors="pt", truncation=True, padding=True) | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| probs = torch.nn.functional.softmax(outputs.logits, dim=-1) | |
| predicted_id = torch.argmax(probs).item() | |
| label_info = id2label[str(predicted_id)] | |
| return { | |
| "category": label_info["category"], | |
| "sub_category": label_info["sub_category"], | |
| "confidence": float(probs[0][predicted_id]) | |
| } | |