File size: 599 Bytes
0a5bbcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from fastapi import FastAPI, Request
from pydantic import BaseModel
import joblib

app = FastAPI()

# Load the model
with open("roberta_model.pkl", "rb") as f:
    model = joblib.load(f)

class InputText(BaseModel):
    text: str

@app.post("/predict")
def predict(data: InputText):
    input_text = data.text
    inputs = model["tokenizer"](input_text, return_tensors="pt", padding=True, truncation=True)
    with model["torch"].no_grad():
        outputs = model["model"](**inputs)
    logits = outputs.logits
    predictions = logits.argmax(dim=1).tolist()
    return {"predictions": predictions}