Skip to content

Commit

Permalink
added reponse model in predict router
Browse files Browse the repository at this point in the history
  • Loading branch information
LINSANITY03 committed Oct 7, 2024
1 parent f30adef commit 65d9305
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions routers/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from typing import List

from fastapi.responses import JSONResponse
import tensorflow as tf

from fastapi import APIRouter, Request, status, HTTPException
Expand All @@ -42,6 +43,15 @@ class TextStr(BaseModel):
"""
texts: str

class PredictResponse(BaseModel):
"""
A Pydantic Model representing the output response for sentiment prediction.
Attributes:
score (str): Prediction score for sentiment analysis.
"""
score: str

def pre_process_text(input_text: List[str]):
"""
Receives a plain text and convert into tf.data.Dataset
Expand Down Expand Up @@ -79,7 +89,7 @@ def prediction(dataset, sent_model, text_len):
# Return the prediction score
return predictions.item()

@router.post("/", status_code=status.HTTP_200_OK)
@router.post("/", status_code=status.HTTP_200_OK, response_model=PredictResponse)
async def predict_sentiment(request:Request, data: TextStr):
"""
Predict the sentiment of the input text using a pre-loaded machine learning model.
Expand Down Expand Up @@ -111,4 +121,5 @@ async def predict_sentiment(request:Request, data: TextStr):
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) from e
predictions = prediction(processed_text, ml_models, len(data.texts))
return {"score": predictions}
content = {"score": predictions}
return JSONResponse(content=content)

0 comments on commit 65d9305

Please sign in to comment.