Spaces:
Sleeping
Sleeping
File size: 1,077 Bytes
b270bdd 2e88ae2 b270bdd 2e88ae2 b270bdd d7dbe04 b270bdd a471ca8 b270bdd a471ca8 b270bdd | 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 | from fastapi import FastAPI
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import torch.nn.functional as F
app = FastAPI()
# Using v2 model (1000 examples, improved performance)
model_name = "King-8/help-classifier-v2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
labels = [
"learning_help",
"project_help",
"technical_issue",
"attendance_issue",
"general_guidance"
]
class InputText(BaseModel):
text: str
@app.get("/")
def root():
return {"message": "Help Classifier API is running"}
@app.post("/predict")
def predict(input: InputText):
inputs = tokenizer(input.text, return_tensors="pt", truncation=True, padding=True)
outputs = model(**inputs)
probs = F.softmax(outputs.logits, dim=1)
predicted_class = probs.argmax().item()
confidence = probs.max().item()
return {
"label": labels[predicted_class],
"confidence": round(confidence, 4)
}
|