File size: 833 Bytes
bc07c80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, HTTPException
from typing import List

app = FastAPI()

@app.get("/")
async def read_root():
    return {"message": "Welcome to the FastAPI application!"}

@app.post("/classify")
async def classify(labels: List[dict]):
    try:
        # if labels is empty return 400 error
        if not labels:
            raise HTTPException(status_code=400, detail="No labels provided")

        # Check if the highest score is lower than 0.6
        if labels[0]["score"] < 0.6:
            predicted_label = "OTHER"
        else:
            # Find the label with the highest score
            predicted_label = labels[0]["label"]

        # Return the predicted label
        return {"predicted_label": predicted_label}
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))