File size: 982 Bytes
ff0671e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6690995
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
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer

model_path = "./finetuned_model"
model = AutoModelForSequenceClassification.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path)

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

# 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!"}