Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import torch
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
model_name = "King-8/help-classifier"
|
| 10 |
+
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 13 |
+
|
| 14 |
+
labels = [
|
| 15 |
+
"learning_help",
|
| 16 |
+
"project_help",
|
| 17 |
+
"attendance_issue",
|
| 18 |
+
"technical_issue",
|
| 19 |
+
"general_guidance"
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
class InputText(BaseModel):
|
| 23 |
+
text: str
|
| 24 |
+
|
| 25 |
+
@app.post("/predict")
|
| 26 |
+
def predict(input: InputText):
|
| 27 |
+
inputs = tokenizer(input.text, return_tensors="pt", truncation=True, padding=True)
|
| 28 |
+
outputs = model(**inputs)
|
| 29 |
+
|
| 30 |
+
probs = F.softmax(outputs.logits, dim=1)
|
| 31 |
+
predicted_class = probs.argmax().item()
|
| 32 |
+
|
| 33 |
+
return {
|
| 34 |
+
"label": labels[predicted_class]
|
| 35 |
+
}
|
| 36 |
+
|