Sibinraj commited on
Commit
9f47b4e
·
1 Parent(s): 8e32caf

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +164 -0
app.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ import numpy as np
3
+ import streamlit as st
4
+ import cv2
5
+ import tensorflow as tf
6
+ from tqdm import tqdm
7
+ from PIL import Image
8
+ import os
9
+ from tensorflow.keras.preprocessing import sequence
10
+ from tensorflow.keras.saving import load_model
11
+
12
+ st.title("DL-Classifier")
13
+
14
+ task1 = st.selectbox('Select One',("Choose any","Sentiment Classification", 'Tumor Detection'))
15
+
16
+
17
+ #choosing tumor detection
18
+ #CNN
19
+ if task1=="Tumor Detection":
20
+ st.subheader("Tumor Detection")
21
+ model_path = os.path.join(os.getcwd(), 'cnn_model.h5')
22
+ cnn_model = load_model(model_path)
23
+
24
+ img =st.file_uploader("choose the image",type=('jpg','jpeg','png'))
25
+ def cnn_make_prediction(img,cnn_model):
26
+ img=Image.open(img)
27
+ img=img.resize((128,128))
28
+ img=np.array(img)
29
+ input_img = np.expand_dims(img, axis=0)
30
+ res = cnn_model.predict(input_img)
31
+ if res:
32
+ return"Tumor"
33
+ else:
34
+ return"No Tumor"
35
+ if img != None:
36
+ img_f="D:/SEM 3/DL/DL-ALGORITHMS/CNN/tumor_detection/tumordata/pred/"
37
+ img_p=img_f + img.name
38
+ pred=cnn_make_prediction(img_p,cnn_model)
39
+ st.write(pred)
40
+
41
+
42
+ #choosing classification
43
+
44
+
45
+ if task1=="Sentiment Classification":
46
+ st.subheader("Sentiment Classification")
47
+ clss_model= st.radio("Select Classification Model:",("RNN","DNN","Backpropagation",'Perceptron','LSTM'))
48
+ select_model=None
49
+
50
+ if clss_model=="RNN":
51
+
52
+ model_path = os.path.join(os.getcwd(), 'rnn_model.h5')
53
+ rnn_model = load_model(model_path)
54
+ with open("rnn_tokeniser.pkl",'rb') as tokeniser_file:
55
+ rnn_tokeniser=pickle.load(tokeniser_file)
56
+
57
+ st.subheader('RNN Spam Classification')
58
+
59
+ input=st.text_input("Enter your message here:")
60
+ def rnn_pred(input):
61
+ max_length=10
62
+ encoded_test = rnn_tokeniser.texts_to_sequences(input)
63
+ padded_test = tf.keras.preprocessing.sequence.pad_sequences(encoded_test, maxlen=max_length, padding='post')
64
+ predict= (rnn_model.predict(padded_test) > 0.5).astype("int32")
65
+ if predict:
66
+ return "Spam "
67
+ else:
68
+ return "Not Spam"
69
+ if st.button('Check'):
70
+ pred=rnn_pred([input])
71
+ st.write(pred)
72
+
73
+ if clss_model=='Perceptron':
74
+ with open("perceptron_model_saved.pkl",'rb') as model_file:
75
+ percep_model=pickle.load(model_file)
76
+ with open('perceptron_tokeniser_saved.pkl','rb') as model_file:
77
+ percep_token=pickle.load(model_file)
78
+ st.subheader('Perceptron Spam Classification')
79
+ input= st.text_input("Enter your text here")
80
+
81
+ def percep_pred(input):
82
+ encoded_test_p = percep_token.texts_to_sequences([input])
83
+ padded_test_p = tf.keras.preprocessing.sequence.pad_sequences(encoded_test_p, maxlen=10)
84
+ predict_p= percep_model.predict(padded_test_p)
85
+ if predict_p:
86
+ return "Spam"
87
+ else:
88
+ return "Not Spam"
89
+ if st.button("Check"):
90
+ pred=percep_pred([input])
91
+ st.write(pred)
92
+
93
+
94
+ if clss_model=="Backpropagation":
95
+ with open('backprop_model.pkl','rb') as model_file:
96
+ bp_model=pickle.load(model_file)
97
+ with open('backrpop_tokeniser.pkl','rb') as model_file:
98
+ bp_tokeniser=pickle.load(model_file)
99
+ st.subheader('Backpropagation Spam Classification')
100
+ input= st.text_input("Enter your text here")
101
+
102
+
103
+ def back_pred(input):
104
+ encoded_test = bp_tokeniser.texts_to_sequences([input])
105
+ padded_test = tf.keras.preprocessing.sequence.pad_sequences(encoded_test, maxlen=10)
106
+ predict= bp_model.predict(padded_test)
107
+ if predict:
108
+ return "Spam"
109
+ else:
110
+ return "Not Spam"
111
+ if st.button("Check"):
112
+ pred=back_pred([input])
113
+ st.write(pred)
114
+
115
+ if clss_model=="DNN":
116
+
117
+ model_path = os.path.join(os.getcwd(), 'dnn_model.h5')
118
+ dnn_model = load_model(model_path)
119
+
120
+ with open("dnn_tokeniser.pkl",'rb') as file:
121
+ dnn_tokeniser=pickle.load(file)
122
+ st.subheader('DNN Spam Classification')
123
+ input= st.text_input("Enter your text here")
124
+
125
+ def dnn_pred(input):
126
+ encoded_test = dnn_tokeniser.texts_to_sequences([input])
127
+ padded_test = tf.keras.preprocessing.sequence.pad_sequences(encoded_test, maxlen=500)
128
+ predict= dnn_model.predict(padded_test)
129
+ if predict:
130
+ return "Spam"
131
+ else:
132
+ return "Not Spam"
133
+ if st.button('Check'):
134
+ pred=dnn_pred([input])
135
+ st.write(pred)
136
+
137
+
138
+ if clss_model=="LSTM":
139
+ model_path = os.path.join(os.getcwd(), 'lstm_model.h5')
140
+ lstm_model = load_model(model_path)
141
+
142
+ with open("lstm_tokeniser.pkl",'rb') as file:
143
+ lstm_tokeniser=pickle.load(file)
144
+ st.subheader('Movie Review Classification')
145
+ inp=st.text_area("Enter your review")
146
+ def lstm_make_predictions(inp, model):
147
+ inp = lstm_tokeniser.texts_to_sequences(inp)
148
+ inp = sequence.pad_sequences(inp, maxlen=500)
149
+ res = (model.predict(inp) > 0.5).astype("int32")
150
+ if res:
151
+ return "Negative"
152
+ else:
153
+ return "Positive"
154
+
155
+
156
+ if st.button('Check'):
157
+ pred = lstm_make_predictions([inp], lstm_model)
158
+ st.write(pred)
159
+
160
+
161
+
162
+
163
+
164
+