-
Notifications
You must be signed in to change notification settings - Fork 1
/
sent_webapp.py
432 lines (317 loc) · 15.7 KB
/
sent_webapp.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import streamlit as st
import pandas as pd
import numpy as np
from transformers import pipeline
import nltk
from nltk import word_tokenize, ne_chunk , pos_tag
#, download_dir='/path/to/nltk_data/' :: path for percentron
nltk.download('averaged_perceptron_tagger')
nltk.download("maxent_ne_chunker")
nltk.download('words')
nltk.download('punkt')
nltk.download('stopwords')
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lex_rank import LexRankSummarizer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.preprocessing import OneHotEncoder
from sklearn.metrics import accuracy_score,precision_score,recall_score,mean_squared_error,mean_absolute_error
#---------------------------------------------------------------------------------------------------------------
# all functions for app
#pipeline for sentiment analysis
@st.cache_resource
def pipe(text):
pipe_sentiment = pipeline("sentiment-analysis")
return pipe_sentiment(text)
# function to print summary
@st.cache_resource
def summer(text, sum_size):
summarizer = LexRankSummarizer()
parser = PlaintextParser.from_string(text, Tokenizer("english"))
summary_size = sum_size
summary_sentences = summarizer(parser.document, summary_size)
summary = ' '.join(str(sentence) for sentence in summary_sentences)
return summary
#Named Entity Recognition Function
@st.cache_resource
def ner(text):
text = word_tokenize(text)
pos_text = pos_tag(text)
ner = ne_chunk(pos_text)
named_entities = []
for chunk in ner:
if hasattr(chunk, 'label') and chunk.label() != 'O':
entity_label = chunk.label()
entity_words = ' '.join(c[0] for c in chunk)
named_entities.append(f'{entity_label}: {entity_words}')
return named_entities
#parts of speech Tagging function
@st.cache_resource
def postag(text):
text = word_tokenize(text)
pos_text = pos_tag(text)
l1=[]
for token, tag in pos_text:
l1.append(f"{token}: {tag}")
return l1
#--------------------------------------------------------------------------------------------------------
#app
st.set_page_config(layout="wide")
st.title("IceAge")
st.write("A Streamlit Application That Demonstrates Power of Machine Learing.")
with st.sidebar:
st.title("IceAge")
st.header("Content Navigation")
nav_selection = st.sidebar.radio('Navigation',("NLP","Classification","Regression","Description"))
#NLP TASK
if nav_selection == "NLP":
tab1,tab2,tab3,tab4 = st.tabs(['SENTIMENT ANALYSIS','SUMMERIZATION','NAMED ENTITY RECOGNITION','POS TAGGING'])
with tab1:
st.title("Sentiment Analysis")
T1_data = st.text_area("Enter Text For Analysis")
result_btn = st.button("Sentiment")
if(result_btn ):
sent_result = pipe(str(T1_data))
st.write(sent_result)
with tab2:
st.title("Text Summarization")
T2_data = st.text_area("Enter Text For Summerization")
sum_size = st.text_input("enter size of summary in no. of lines")
result_btn = st.button("Summery")
if(result_btn ):
summary = summer(str(T2_data),int(sum_size))
st.write(summary)
with tab3:
st.title("Named Entity Recognition")
T3_data = st.text_area("Enter Text For NER")
result_btn = st.button("NER Result")
if(result_btn ):
result_ner = ner(str(T3_data))
st.write(result_ner)
with tab4:
st.title("Part - Of - Speech Tagging")
T4_data = st.text_area("Enter Text For POS tag")
result_btn = st.button("POS TAGS")
if(result_btn ):
st.write(postag(str(T4_data)))
st.image("https://m-clark.github.io/text-analysis-with-R/img/POS-Tags.png")
pass
#CLASSIFICATION TASK
if nav_selection=="Classification":
class_tab1,class_tab2 = st.tabs(['LOGISTIC REGRESSION','NAIVE BAYES CLASSIFIER'])
with class_tab1:
df,df_cols = [],[]
st.title("Logistic Regression")
uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
df_cols = [i for i in df.columns]
st.dataframe(df)
col1,col2,col3,col4 = st.columns(4)
with col1:
btn = st.button("show cloumn name for DF")
if btn:
st.write(df.columns)
with col2:
btn = st.button("show all null values")
if btn:
st.write(df.isnull().sum())
with col3:
btn = st.button("Describe DF")
if btn:
st.write(df.describe())
with col4:
show_type_of_data = st.button("Show Datatypes")
if show_type_of_data:
st.write(df.dtypes)
#preprocessing
st.header("PREPROCESS , TRAIN AND PREDICT")
st.write("works for only numeric features")
class_form = st.form(key = 'classification')
select_feature = class_form.multiselect("select feature for Feature Engineering", df_cols)
target = class_form.selectbox("select Target ",df_cols)
prediction_data = class_form.text_input("enter prediction data (seperator = ',' and no gaps)")
train = class_form.form_submit_button(" Preprocess and train LR ")
if train:
if df is not None:
df_process = df
df_process = df_process.dropna()
if select_feature is not None:
df_process = df_process[select_feature]
else:
df_process = df_process
result_col1,result_col2,result_col3 = st.columns(3)
with result_col1:
st.write(df_process)
with result_col2:
st.write(f"null values:",df_process.isnull().sum())
y = df_process[[target]]
x = df_process.drop([target],axis =1)
with result_col3:
st.write("y columns: ",y.columns)
st.write(f"x columns",x.columns)
trainx , testx, trainy, testy = train_test_split(x,y,test_size=0.2,random_state=42)
regr = LogisticRegression()
lr_model = regr.fit(trainx,trainy)
pred = lr_model.predict(testx)
prediction_data = prediction_data.split(",")
prediction_data = [float(i) for i in prediction_data]
prediction_data_result = lr_model.predict([prediction_data])
st.title(f" predicted column: {target} : for data :{prediction_data} is {prediction_data_result}")
scores = {
'acc_score' : round(accuracy_score(pred,testy)*100,2),
'pre_score' : round(precision_score(testy,pred)*100,2),
'recall_score': round(recall_score(testy,pred)*100,2)
}
st.write(scores)
#show_score = st.button("show accuracy")
pass
pass
with class_tab2:
df,df_cols = [],[]
st.title("Naive Bayes Classifier")
uploaded_file_naive = st.file_uploader("Upload any CSV file", type=["csv"])
if uploaded_file_naive is not None:
df = pd.read_csv(uploaded_file_naive)
df_cols = [i for i in df.columns]
st.dataframe(df)
ncol1,ncol2,ncol3,ncol4 = st.columns(4)
with ncol1:
btn = st.button("show cloumn names for DF.")
if btn:
st.write(df.columns)
with ncol2:
btn = st.button("show all null value.")
if btn:
st.write(df.isnull().sum())
with ncol3:
btn = st.button("Describe DF.")
if btn:
st.write(df.describe())
with ncol4:
show_type_of_data = st.button("Show Datatype.")
if show_type_of_data:
st.write(df.dtypes)
#preprocessing
st.header("PREPROCESS , TRAIN AND PREDICT")
st.write("works for only numeric features")
nform = st.form(key="naive")
select_feature = nform.multiselect("select feature for Feature Engineering", df_cols)
target = nform.selectbox("select Target ",df_cols)
prediction_data = nform.text_input("enter prediction data (seperator = ',' and no gaps)")
train = nform.form_submit_button(" Preprocess and train NB ")
if train:
if df is not None:
df_process = df
df_process = df_process.dropna()
if select_feature is not None:
df_process = df_process[select_feature]
else:
df_process = df_process
result_col1,result_col2,result_col3 = st.columns(3)
with result_col1:
st.write(df_process)
with result_col2:
st.write(f"null values:",df_process.isnull().sum())
y = df_process[[target]]
x = df_process.drop([target],axis =1)
with result_col3:
st.write("y columns: ",y.columns)
st.write(f"x columns",x.columns)
trainx , testx, trainy, testy = train_test_split(x,y,test_size=0.2,random_state=42)
classifier = MultinomialNB()
nb_model = classifier.fit(trainx,trainy)
pred = nb_model.predict(testx)
prediction_data = prediction_data.split(",")
prediction_data = [float(i) for i in prediction_data]
prediction_data_result = nb_model.predict([prediction_data])
st.title(f" predicted column {target} : for data : {prediction_data} is {prediction_data_result}")
scores = {
'acc_score' : round(accuracy_score(pred,testy)*100,2),
'pre_score' : round(precision_score(testy,pred)*100,2),
'recall_score': round(recall_score(testy,pred)*100,2)
}
st.write(scores)
pass
if nav_selection=="Regression":
df,df_cols=[],[]
regr_tab1 = st.tabs(['LINEAR REGRESSION'])
st.title("Linear Regression")
if regr_tab1 :
uploaded_file_regr = st.file_uploader("Upload a CSV file", type=["csv"])
if uploaded_file_regr is not None:
df = pd.read_csv(uploaded_file_regr)
df_cols = [i for i in df.columns]
st.dataframe(df)
rcol1,rcol2,rcol3,rcol4 = st.columns(4)
with rcol1:
btn = st.button("show cloumn names for DF.")
if btn:
st.write(df.columns)
with rcol2:
btn = st.button("show all null value.")
if btn:
st.write(df.isnull().sum())
with rcol3:
btn = st.button("Describe DF.")
if btn:
st.write(df.describe())
with rcol4:
show_type_of_data = st.button("Show Datatype.")
if show_type_of_data:
st.write(df.dtypes)
#preprocessing
st.header("PREPROCESS , TRAIN AND PREDICT")
st.write("works for only numeric features")
rform = st.form(key='regression')
select_feature = rform.multiselect("select feature for Feature Engineering", df_cols)
target = rform.selectbox("select Target ",df_cols)
prediction_data = rform.text_input("enter prediction data (seperator = ',' and no gaps)")
train = rform.form_submit_button(" Preprocess and train regr ")
if train:
if df is not None:
df_process = df
df_process = df_process.dropna()
if select_feature is not None:
df_process = df_process[select_feature]
else:
df_process = df_process
result_col1,result_col2,result_col3 = st.columns(3)
with result_col1:
st.write(df_process)
with result_col2:
st.write(f"null values:",df_process.isnull().sum())
y = df_process[[target]]
x = df_process.drop([target],axis =1)
with result_col3:
st.write("y columns: ",y.columns)
st.write(f"x columns",x.columns)
trainx , testx, trainy, testy = train_test_split(x,y,test_size=0.2,random_state=42)
regr = LinearRegression()
lr_model = regr.fit(trainx,trainy)
pred = lr_model.predict(testx)
prediction_data = prediction_data.split(",")
prediction_data = [float(i) for i in prediction_data]
prediction_data_result = lr_model.predict([prediction_data])
st.title(f" predicted column {target} : for data : {prediction_data} is {prediction_data_result}")
scores = {
'MSE_error' : mean_squared_error(testy,pred),
'MAE_error' : mean_absolute_error(testy,pred),
}
st.write(scores)
pass
pass
if nav_selection=="Description":
st.header("Project Description")
st.write(f"""
Introducing IceAge: Your Ultimate NLP and Machine Learning Solution
IceAge is a revolutionary project that brings together the power of Natural Language Processing (NLP) and machine learning, allowing you to seamlessly perform classification and regression tasks on any dataset. Designed for data scientists, researchers, and professionals alike, IceAge empowers you to unlock valuable insights and make data-driven decisions with ease.
With IceAge's robust classification and regression capabilities, you can tackle a wide range of machine learning challenges. Whether you're predicting customer behavior, sentiment analysis, or financial trends, IceAge equips you with the tools to build accurate and reliable predictive models.
Gone are the days of grappling with complex data preprocessing and feature engineering. IceAge handles all the heavy lifting, automating data preprocessing, feature extraction, and model training. This allows you to focus on the most critical aspects of your analysis, interpretation, and decision-making.
But IceAge doesn't stop at machine learning. It seamlessly integrates powerful NLP functionalities such as sentiment analysis, text summarization, Named Entity Recognition (NER), and POS tagging. This comprehensive NLP toolkit provides you with unparalleled capabilities to extract meaningful insights from text data.
IceAge boasts a user-friendly interface built on the popular Streamlit framework, making it accessible and intuitive for users of all experience levels. Simply upload your dataset, select the desired classification or regression task, and let IceAge work its magic.
Discover the full potential of your data with IceAge. Harness the power of NLP and machine learning to solve complex problems, gain deeper understanding, and make informed decisions. Whether you're in academia, research, or industry, IceAge is your trusted companion on the journey to unlock the true value of your data.
""")
pass