-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
126 lines (77 loc) · 3.09 KB
/
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from fastapi import FastAPI
from pydantic import BaseModel
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
import string
import pickle
app = FastAPI()
model_kmeans = pickle.load(open('modelkmeans.pkl', 'rb'))
model_hierarchy = pickle.load(open('modelhierarchy.pkl', 'rb'))
vectorizer = pickle.load(open('vectorizer.pkl', 'rb'))
pca = pickle.load(open('pca.pkl', 'rb'))
nltk.download('stopwords')
nltk.download('punkt')
ps = PorterStemmer()
def preprocess_text(text):
text = text.lower()
text = nltk.word_tokenize(text)
y = []
for i in text:
if i.isalnum():
y.append(i)
text = y[:]
y.clear()
for i in text:
if i not in stopwords.words('english') and i not in string.punctuation:
y.append(i)
text = y[:]
y.clear()
for i in text:
y.append(ps.stem(i))
return " ".join(y)
class inputModelSingleNewsText(BaseModel):
inputTextSingle: str
class inputModelMultipleNewsText(BaseModel):
input_text_one: str
input_text_two: str
input_text_three: str
@app.get('/')
def welcome():
return {
'success': True,
'message': 'server of "fake news clustering is up and running successfully"'
}
@app.post('/predict-single-news-cluster')
def predict_single_news(inputFromUser: inputModelSingleNewsText):
userInput = inputFromUser.inputTextSingle
preproccessed_input = preprocess_text(userInput)
input_text_vectorized = vectorizer.transform([preproccessed_input]).toarray()
input_text_vectorized_pca = pca.transform(input_text_vectorized)
prediction = model_kmeans.predict(input_text_vectorized_pca)
pred_message = f'The news provided belongs to Cluster Number: {prediction[0]}'
return {
'success': True,
'pred_result': pred_message
}
@app.post('/predict-multiple-news-cluster')
def predict_multiple_news(inputsFromUser: inputModelMultipleNewsText):
input_text_input_one = inputsFromUser.input_text_one
input_text_input_two = inputsFromUser.input_text_two
input_text_input_three = inputsFromUser.input_text_three
input_text_one_preproccessed = preprocess_text(input_text_input_one)
input_text_two_preproccessed = preprocess_text(input_text_input_two)
input_text_three_preproccessed = preprocess_text(input_text_input_three)
input_texts_final = [input_text_one_preproccessed, input_text_two_preproccessed, input_text_three_preproccessed]
inputs_texts_final_vectorized = vectorizer.transform(input_texts_final).toarray()
input_texts_final_pca = pca.transform(inputs_texts_final_vectorized)
prediction = model_hierarchy.fit_predict(input_texts_final_pca)
pred_result_one = prediction[0]
pred_result_two = prediction[1]
pred_result_three = prediction[2]
pred_message = f'The first news text belongs to Cluster Number: {pred_result_one}. The second news belongs to Cluster Number: {pred_result_two} and the third news belongs to Cluster Number: {pred_result_three}.'
print(prediction)
return {
'success': True,
'pred_result': pred_message
}