-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
275 lines (236 loc) · 9.91 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import streamlit as st
import pandas as pd
import joblib,os
import seaborn as sns
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_recall_fscore_support as score, mean_squared_error
from sklearn.metrics import confusion_matrix,accuracy_score
from nltk.tokenize import word_tokenize
from gensim.models.doc2vec import TaggedDocument
import nltk
from nltk.corpus import stopwords
from sklearn import preprocessing
from sklearn.feature_extraction.text import TfidfVectorizer
import matplotlib.pyplot as plt
import re
import warnings
import pickle
import webbrowser
from wordcloud import WordCloud
warnings.filterwarnings("ignore")
# Vectorizer
news_vectorizer = open("models\\Vectorizer", "rb")
news_cv = joblib.load(news_vectorizer)
#Loading Model
def load_prediction_model(model):
loaded_model = joblib.load(open(os.path.join(model), "rb"))
return loaded_model
# Get Category from Numeric Value
def get_category(val, dict):
for key, value in dict.items():
if val == value:
return key
def add_parameter_ui(clf_name):
params={}
st.sidebar.write("Select values: ")
if clf_name == "Logistic Regression":
R = st.sidebar.slider("Regularization",0.1,10.0,step=0.1)
MI = st.sidebar.slider("max_iter",50,400,step=50)
params["R"] = R
params["MI"] = MI
elif clf_name == "KNN":
K = st.sidebar.slider("n_neighbors",1,20)
params["K"] = K
elif clf_name == "SVM":
C = st.sidebar.slider("Regularization",0.01,10.0,step=0.01)
kernel = st.sidebar.selectbox("Kernel",("linear", "poly", "rbf", "sigmoid", "precomputed"))
params["C"] = C
params["kernel"] = kernel
elif clf_name == "Decision Tree":
M = st.sidebar.slider("max_depth", 2, 20)
C = st.sidebar.selectbox("Criterion", ("gini", "entropy"))
SS = st.sidebar.slider("min_samples_split",1,10)
params["M"] = M
params["C"] = C
params["SS"] = SS
return params
def get_classifier(clf_name,params):
global clf
if clf_name == "Logistic Regression":
clf = LogisticRegression(C=params["R"],max_iter=params["MI"])
elif clf_name == "KNN":
clf = KNeighborsClassifier(n_neighbors=params["K"])
elif clf_name == "SVM":
clf = SVC(kernel=params["kernel"],C=params["C"])
elif clf_name == "Decision Tree":
clf = DecisionTreeClassifier(max_depth=params["M"],criterion=params["C"])
elif clf_name == "Naive Bayes":
clf = MultinomialNB()
return clf
def process_text(text):
text = text.lower().replace('\n',' ').replace('\r','').strip()
text = re.sub(' +', ' ', text)
text = re.sub(r'[^\w\s]','',text)
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(text)
filtered_sentence = [w for w in word_tokens if not w in stop_words]
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
text = " ".join(filtered_sentence)
return text
def get_dataset():
data = pd.read_csv("data\BBC News Train.csv")
data['News_length'] = data['Text'].str.len()
data['Text_parsed'] = data['Text'].apply(process_text)
label_encoder = preprocessing.LabelEncoder()
data['Category_target']= label_encoder.fit_transform(data['Category'])
return data
#Plot Output
def compute(Y_pred,Y_test):
# c1, c2 = st.beta_columns((4,3))
#Confusion Matrix
st.set_option('deprecation.showPyplotGlobalUse', False)
cm=confusion_matrix(Y_test,Y_pred)
class_label = ["business", "tech", "politics", "sport","entertainment"]
df_cm = pd.DataFrame(cm, index=class_label,columns=class_label)
plt.figure(figsize=(12, 7.5))
sns.heatmap(df_cm,annot=True,cmap='Pastel1',linewidths=2,fmt='d')
plt.title("Confusion Matrix",fontsize=15)
plt.xlabel("Predicted")
plt.ylabel("True")
st.pyplot()
#Calculate Metrics
acc=accuracy_score(Y_test,Y_pred)
mse=mean_squared_error(Y_test,Y_pred)
precision, recall, fscore, train_support = score(Y_test, Y_pred, pos_label=1)
st.subheader("Metrics of the model: ")
st.text('Precision: {} \nRecall: {} \nF1-Score: {} \nAccuracy: {} %\nMean Squared Error: {}'.format(
precision,recall,fscore,acc*100, mse))
#Build Model
def model(clf):
X_train,X_test,Y_train,Y_test=train_test_split(data['Text_parsed'],
data['Category_target'],test_size=0.2,random_state=65)
ngram_range = (1,2)
min_df = 10
max_df = 1.
max_features = 300
tfidf = TfidfVectorizer(encoding='utf-8',
ngram_range=ngram_range,
stop_words=None,
lowercase=False,
max_df=max_df,
min_df=min_df,
max_features=max_features,
norm='l2',
sublinear_tf=True)
features_train = tfidf.fit_transform(X_train).toarray()
labels_train = Y_train
features_test = tfidf.transform(X_test).toarray()
labels_test = Y_test
clf.fit(features_train, labels_train)
Y_pred = clf.predict(features_test)
acc=accuracy_score(labels_test,Y_pred)
return clf, Y_test, Y_pred
#tokenize for nlp
def tokenize_text(text):
tokens = []
for sent in nltk.sent_tokenize(text):
for word in nltk.word_tokenize(sent):
if len(word) < 2:
continue
tokens.append(word.lower())
return tokens
def vec_for_learning(model_dbow, tagged_docs):
sents = tagged_docs.values
targets, regressors = zip(*[(doc.tags[0], model_dbow.infer_vector(doc.words, steps=20)) for doc in sents])
return targets, regressors
data = get_dataset()
X = data['Text_parsed']
Y = data['Category_target']
def main():
activities = ["About","Data", "Prediction","NLP"]
choice = st.sidebar.selectbox("Choose Activity", activities)
if choice=="Data":
st.title('Data')
st.write("The following is the DataFrame of the `BBC News` dataset.")
data = pd.read_csv("data\BBC News Train.csv")
st.write(data)
if choice=="About":
with st.container():
st.title("Welcome to News Classification ML App:wave:")
st.markdown("![Web Application](https://i.gifer.com/991p.gif)")
st.markdown("""
#### Built with Streamlit
## By
+ Devashree Pravakar
""")
st.markdown("""+ Arindam Rao""")
st.markdown("""+ Kintali Pardha Saradhi""")
url = 'https://github.com/devashree1923/News-Classification'
if st.button('Github'):
webbrowser.open_new_tab(url)
if choice=="Prediction":
st.info("Prediction with ML")
news_text = st.text_area("Enter Text", "Type Here")
all_ml_models = ["Logistic Regression", "Naive Bayes", "Decision Tree", "SVM", "KNN"]
model_choice = st.selectbox("Choose ML Model", all_ml_models)
prediction_labels = {'business':0, 'tech':1, 'politics':2, 'sport':3, 'entertainment':4}
params = add_parameter_ui(model_choice)
if st.button("Classify"):
st.text("Original text ::\n{}".format(news_text))
news_text = process_text(news_text)
vect_text = news_cv.transform([news_text]).toarray()
clf = get_classifier(model_choice,params)
predictor, Y_pred,Y_test = model(clf)
prediction = predictor.predict(vect_text)
result = get_category(prediction, prediction_labels)
st.success(result)
st.markdown("<hr>",unsafe_allow_html=True)
st.subheader(f"Classifier Used: {model_choice}")
compute(Y_pred,Y_test)
# if st.checkbox("WordCloud"):
st.subheader("WordCloud: ")
c_text = news_text
wordcloud = WordCloud().generate(c_text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
st.pyplot()
if choice=="NLP":
st.info("Natural Language Processing")
news_text = st.text_area("Enter Text", "Type Here")
c_text = news_text
df = pd.read_csv("data/BBC_News_Train_Processed.csv")
if st.button("Classify"):
prediction_labels = {0:'business', 1:'entertainment', 2:'politics', 3:'sport', 4:'tech'}
news_text = process_text(news_text)
news_text = pd.DataFrame({'Text':[news_text]})
train, test = train_test_split(df, test_size = 0.2, random_state=42)
news_text = news_text.apply(lambda r: TaggedDocument(words=tokenize_text(r['Text']), tags=[0]), axis=1)
test_tagged = test.apply(lambda r: TaggedDocument(words=tokenize_text(r['Text']), tags=[r.Category]), axis=1)
model_dbow = pickle.load(open('models\\nlp_model_dbow.sav', 'rb'))
model_logistic = pickle.load(open('models\\nlp_model.sav', 'rb'))
Y_text, X_text = vec_for_learning(model_dbow, news_text)
Y_test, X_test = vec_for_learning(model_dbow, test_tagged)
Y_pred = model_logistic.predict(X_test)
Y_text = model_logistic.predict(X_text)
result = prediction_labels[Y_text[0]]
st.success(result)
st.markdown("<hr>",unsafe_allow_html=True)
st.subheader("Classifier Used: NLP with logistic regression")
compute(Y_pred, Y_test)
st.subheader("WordCloud: ")
wordcloud = WordCloud().generate(c_text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
st.pyplot()
if __name__ == '__main__':
main()