-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_api.py
47 lines (37 loc) · 1.49 KB
/
model_api.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
import pickle
import re
from model.stops import stop_words
# import model (MultinomialNB)
with open('./model/model_pickle', 'rb') as f:
imported_model = pickle.load(f)
# import vectorizer (CountVectorizer)
with open('./model/vect_pickle', 'rb') as f:
imported_vect = pickle.load(f)
with open('./model/model_pickle_svm', 'rb') as f:
imported_model_svm = pickle.load (f)
with open('./model/vect_pickle_svm', 'rb') as f:
imported_vect_svm = pickle.load (f)
with open('./model/model_pickle_logistic_regression', 'rb') as f:
imported_model_logistic_regression = pickle.load (f)
with open('./model/vect_pickle_logistic_regression', 'rb') as f:
imported_vect_logistic_regression = pickle.load (f)
def predict(text):
# add text to an array
text_arr = []
text_arr.append(text)
# find number of words using regex
num_words = len(re.findall(r'\w+', text))
# res = 0 -> legitimate
# res = 1 -> fake
# res = 2 -> invalid
res = [2, 2, 2] # default is invalid
# if valid
if num_words >= 100:
# run input to the vectorizer and model
text_dtm_nb = imported_vect.transform(text_arr)
text_dtm_svm = imported_vect_svm.transform(text_arr)
text_dtm_logistic_regression = imported_vect_logistic_regression.transform(text_arr)
res = [int(imported_model.predict(text_dtm_nb)[0]),
int(imported_model.predict(text_dtm_svm)[0]),
int(imported_model.predict(text_dtm_logistic_regression)[0])]
return res