Commit
·
1d0bd99
1
Parent(s):
83bc1c0
Upload api.py
Browse files- sentiment_analyzer/api.py +32 -0
sentiment_analyzer/api.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict
|
| 2 |
+
|
| 3 |
+
from fastapi import Depends, FastAPI, HTTPException, Query
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
|
| 6 |
+
from .classifier.model import Model, get_model
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class SentimentResponse(BaseModel):
|
| 12 |
+
probabilities: Dict[str, float]
|
| 13 |
+
sentiment: str
|
| 14 |
+
confidence: float
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def read_root():
|
| 19 |
+
return {"TrueFoundry": "Internship Project"}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@app.post("/predict", response_model=SentimentResponse)
|
| 23 |
+
def predict(review:str = Query(None, title="Airline Review", decription="Enter the review for airline"), model: Model = Depends(get_model)):
|
| 24 |
+
if type(review) != str:
|
| 25 |
+
raise HTTPException(status_code=404, detail="Bad request")
|
| 26 |
+
sentiment, confidence, probabilities = model.predict(review)
|
| 27 |
+
return SentimentResponse(
|
| 28 |
+
sentiment=sentiment, confidence=confidence, probabilities=probabilities
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|