-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastapi_app.py
112 lines (97 loc) · 3.15 KB
/
fastapi_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from fastapi import FastAPI, BackgroundTasks
from pydantic import BaseModel
from uvicorn import run
from nltk.stem import PorterStemmer
import pandas as pd
import re
import json
import joblib
# create an object of FastAPI
app = FastAPI()
if __name__ == "__main__":
run(app, ws_ping_timeout = 60)
# create objects to receive input from client
class Input(BaseModel):
reviews : str = None
# create route for index page
@app.get("/")
def homepage():
# print("Hi this is index page")
return {'a':'b'}
# create another route for calculation
def load_models():
tfidf, lr, xgb, cnb = None, None, None, None
try:
tfidf = joblib.load('Models/tfidf.joblib')
print(tfidf)
except Exception as exp:
print(f"problem in loading tfidf - {str(exp)}")
try:
lr = joblib.load('Models/lr.joblib')
print(lr)
except Exception as exp:
print(f"problem in loading lr - {str(exp)}")
try:
xgb = joblib.load('Models/xgb.joblib')
print(xgb)
except Exception as exp:
print(f"problem in loading xgb - {str(exp)}")
try:
cnb = joblib.load('Models/cnb.joblib')
print(cnb)
except Exception as exp:
print(f"problem in loading cnb - {str(exp)}")
return tfidf, lr, xgb, cnb
def preprocessing_task(review):
ps = PorterStemmer()
review = re.sub(r'[^A-Za-z\s]', '', review)
print("Special Characters removed from review")
review = re.sub(r' +', ' ', review)
print("extra spaces removed")
review = review.lower()
print("Word Stemming stated...")
review = " ".join([ps.stem(word) for word in review.split()])
print("Stemming Completed")
return review
@app.post("/predict")
async def prediction(data:Input):
data = dict(data)
print(data)
tfidf, lr, xgb, cnb = load_models()
print("models loaded")
try:
review = preprocessing_task(data['reviews'])
except Exception as exp:
print(f"problem in preprocessing - {str(exp)}")
try:
X = tfidf.transform(pd.Series(review))
print("vector created")
except Exception as exp:
print(f"problem in transforming to tfidif vectors - {str(exp)}")
# print("vector created")
try:
lr_prob = lr.predict_proba(X)[0]
print("lr prediction completed")
except Exception as exp:
print(f"problem lr prediction - {str(exp)}")
try:
cnb_prob = cnb.predict_proba(X)[0]
print("cnb prediction completed")
except Exception as exp:
print(f"problem cnb prediction - {str(exp)}")
try:
xgb_prob = xgb.predict_proba(X)[0]
print("xgb prediction completed")
except Exception as exp:
print(f"problem xgb prediction - {str(exp)}")
try:
prob = {
'lr':[float(lr_prob[0]), float(lr_prob[1])],
'xgb':[float(xgb_prob[0]), float(xgb_prob[1])],
'cnb':[float(cnb_prob[0]), float(cnb_prob[1])]
}
print(prob)
except Exception as exp:
print(f"problem in forming return response message - {str(exp)}")
# return {'success':'True'}
return json.dumps(prob)