-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
42 lines (29 loc) · 1023 Bytes
/
app.py
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
31
32
33
34
35
36
37
38
39
40
41
42
# Load the libraries
from fastapi import FastAPI, HTTPException
from joblib import load
# Load the model
spam_clf = load(open('./models/spam_detector_model.pkl','rb'))
# Load vectorizer
vectorizer = load(open('./vectors/vectorizer.pickle', 'rb'))
# Initialize an instance of FastAPI
app = FastAPI()
# Define the default route
@app.get("/")
def root():
return {"message": "Welcome to Your Sentiment Classification FastAPI"}
# Define the route to the sentiment predictor
@app.post("/predict_sentiment")
def predict_sentiment(text_message):
polarity = ""
if(not(text_message)):
raise HTTPException(status_code=400,
detail = "Please Provide a valid text message")
prediction = spam_clf.predict(vectorizer.transform([text_message]))
if(prediction[0] == 0):
polarity = "Ham"
elif(prediction[0] == 1):
polarity = "Spam"
return {
"text_message": text_message,
"sentiment_polarity": polarity
}