File size: 1,319 Bytes
e9f4a14
e72f849
 
c3f8092
e72f849
 
 
 
9fe8d2b
 
 
e72f849
 
c3f8092
 
56f4c53
c3f8092
 
 
56f4c53
e72f849
 
1baa976
 
e72f849
1baa976
e72f849
 
 
 
0639973
 
 
 
e72f849
 
9fe8d2b
e72f849
 
 
 
 
 
 
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
44
45
import os
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import BertTokenizer, BertForSequenceClassification
from sklearn.preprocessing import LabelEncoder
import torch
import numpy as np

# Set cache
os.environ["TRANSFORMERS_CACHE"] = "/code/cache"

app = FastAPI()

model = BertForSequenceClassification.from_pretrained(
    "./bert-model"  # or adjust path based on your structure
)

tokenizer = BertTokenizer.from_pretrained(
    "./bert-model"
)
model.eval()

# Correct path to label_classes.npy
label_path = os.path.join(os.path.dirname(__file__), "label_classes.npy")
le = LabelEncoder()
le.classes_ = np.load(label_path, allow_pickle=True)

class TextInput(BaseModel):
    text: str

@app.get("/")
def read_root():
    return {"message": "FastAPI backend is live. Go to /docs to test."}

@app.post("/predict")
async def predict(data: TextInput):
    inputs = tokenizer(data.text, return_tensors="pt", truncation=True, padding=True)
    with torch.no_grad():
        outputs = model(**inputs)
        probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
        pred_class = torch.argmax(probs, dim=1).item()
        pred_label = le.classes_[pred_class]
        confidence = probs[0][pred_class].item()
    return {"predicted_category": pred_label, "confidence": confidence}