File size: 933 Bytes
54b76e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch

# Initialize FastAPI app
app = FastAPI(title="Stress Detection API", version="1.0")

model = AutoModelForSequenceClassification.from_pretrained("SrivarshiniGanesan/finetuned-stress-model")
tokenizer = AutoTokenizer.from_pretrained("SrivarshiniGanesan/finetuned-stress-model")

# Request format
class TextInput(BaseModel):
    text: str

@app.post("/predict")
def predict_stress(input_text: TextInput):
    inputs = tokenizer(input_text.text, return_tensors="pt", padding=True, truncation=True)
    with torch.no_grad():
        logits = model(**inputs).logits
        prediction = torch.argmax(logits, dim=-1).item()
    
    return {"text": input_text.text, "stress_prediction": "Stress" if prediction == 1 else "No Stress"}

@app.get("/")
def home():
    return {"message": "Welcome to the Stress Detection API!"}