deniltom commited on
Commit
de51eb0
·
1 Parent(s): 6564793

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -144
app.py CHANGED
@@ -1,154 +1,144 @@
 
1
  import streamlit as st
2
- import pickle
3
- import tensorflow as tf
4
- from tensorflow.keras.preprocessing import sequence
5
  import numpy as np
6
- import cv2
7
  from PIL import Image
 
8
 
9
 
10
- st.title('Classifier')
11
- task = st.selectbox('Select Task', ['Choose one','Sentiment Classification', 'Tumor Detection'])
12
-
13
- if task=='Tumor Detection':
14
- st.subheader('Tumor Detection with CNN')
15
- # CNN
16
- cnn_model = tf.keras.models.load_model("cnn_model.h5")
17
-
18
- def cnn_make_prediction(img,model):
19
- img=cv2.imread(img)
20
- img=Image.fromarray(img)
21
- img=img.resize((128,128))
22
- img=np.array(img)
23
- input_img = np.expand_dims(img, axis=0)
24
- res = model.predict(input_img)
25
- if res:
26
- return "Tumor Detected"
27
- else:
28
- return "No Tumor Detected"
29
-
30
- img = st.file_uploader('Upload image', type=['jpeg', 'jpg', 'png'])
31
- if img!=None:
32
- img_folder = "data/tumordata/pred/"
33
- img_path = img_folder+img.name
34
- st.image(img_path, caption = "Image preview")
35
- if st.button('Submit'):
36
- pred = cnn_make_prediction(img_path, cnn_model)
37
- st.write(pred)
38
-
39
-
40
- if task=='Sentiment Classification':
41
- arcs = ['Perceptron', 'Backpropagation', 'DNN', 'RNN', 'LSTM']
42
- arc = st.radio('Pick one:', arcs, horizontal=True)
43
-
44
- if arc == arcs[0]:
45
- # Perceptron
46
- with open("models/pickles/ppn_model.pkl",'rb') as file:
47
- perceptron = pickle.load(file)
48
- with open("models/pickles/ppn_tokeniser.pkl",'rb') as file:
49
- ppn_tokeniser = pickle.load(file)
50
-
51
- def ppn_make_predictions(inp, model):
52
- encoded_inp = ppn_tokeniser.texts_to_sequences([inp])
53
- padded_inp = sequence.pad_sequences(encoded_inp, maxlen=500)
54
- res = model.predict(padded_inp)
55
- if res:
56
- return "Negative"
57
- else:
58
- return "Positive"
59
-
60
- st.subheader('Movie Review Classification using Perceptron')
61
- inp = st.text_area('Enter message')
62
- if st.button('Check'):
63
- pred = ppn_make_predictions([inp], perceptron)
64
- st.write(pred)
65
-
66
- elif arc == arcs[1]:
67
- # BackPropogation
68
- with open("models/pickles/bp_model.pkl",'rb') as file:
69
- backprop = pickle.load(file)
70
- with open("models/pickles/bp_tokeniser.pkl",'rb') as file:
71
- bp_tokeniser = pickle.load(file)
72
-
73
- def bp_make_predictions(inp, model):
74
- encoded_inp = bp_tokeniser.texts_to_sequences([inp])
75
- padded_inp = sequence.pad_sequences(encoded_inp, maxlen=500)
76
- res = model.predict(padded_inp)
77
- if res:
78
- return "Negative"
79
- else:
80
- return "Positive"
81
-
82
- st.subheader('Movie Review Classification using Backpropagation')
83
- inp = st.text_area('Enter message')
84
- if st.button('Check'):
85
- pred = bp_make_predictions([inp], backprop)
86
- st.write(pred)
87
-
88
-
89
- elif arc == arcs[2]:
90
- # DNN
91
- dnn_model = tf.keras.models.load_model("dnn_model.h5")
92
- with open("models/pickles/dnn_tokeniser.pkl",'rb') as file:
93
- dnn_tokeniser = pickle.load(file)
94
-
95
- def dnn_make_predictions(inp, model):
96
- inp = dnn_tokeniser.texts_to_sequences(inp)
97
- inp = sequence.pad_sequences(inp, maxlen=500)
98
- res = (model.predict(inp) > 0.5).astype("int32")
99
- if res:
100
- return "Negative"
101
- else:
102
- return "Positive"
103
-
104
- st.subheader('Movie Review Classification using DNN')
105
- inp = st.text_area('Enter message')
106
- if st.button('Check'):
107
- pred = dnn_make_predictions([inp], dnn_model)
108
- st.write(pred)
109
-
110
-
111
- elif arc == arcs[3]:
112
- # RNN
113
- rnn_model = tf.keras.models.load_model("rnn_model.h5")
114
 
115
- with open("models/pickles/rnn_tokeniser.pkl",'rb') as file:
116
- rnn_tokeniser = pickle.load(file)
117
 
118
- def rnn_make_predictions(inp, model):
119
- encoded_inp = rnn_tokeniser.texts_to_sequences(inp)
120
- padded_inp = sequence.pad_sequences(encoded_inp, maxlen=10, padding='post')
121
- res = (model.predict(padded_inp) > 0.5).astype("int32")
 
 
 
122
  if res:
123
- return "Spam"
124
  else:
125
- return "Ham"
126
-
127
- st.subheader('SMS Spam Classification using RNN')
128
- inp = st.text_area('Enter message')
129
- if st.button('Check'):
130
- pred = rnn_make_predictions([inp], rnn_model)
131
- st.write(pred)
132
-
133
-
134
- elif arc == arcs[4]:
135
- # LSTM
136
- lstm_model = tf.keras.models.load_model("lstm_model.h5")
137
-
138
- with open("models/pickles/lstm_tokeniser.pkl",'rb') as file:
139
- lstm_tokeniser = pickle.load(file)
140
-
141
- def lstm_make_predictions(inp, model):
142
- inp = lstm_tokeniser.texts_to_sequences(inp)
143
- inp = sequence.pad_sequences(inp, maxlen=500)
144
- res = (model.predict(inp) > 0.5).astype("int32")
145
- if res:
146
- return "Negative"
147
- else:
148
- return "Positive"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
- st.subheader('Movie Review Classification using LSTM')
151
- inp = st.text_area('Enter message')
152
- if st.button('Check'):
153
- pred = lstm_make_predictions([inp], lstm_model)
154
- st.write(pred)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
  import streamlit as st
 
 
 
3
  import numpy as np
4
+ import tensorflow as tf
5
  from PIL import Image
6
+ import pickle
7
 
8
 
9
+ st.header('Demo')
10
+ task = st.selectbox('Select Task', ["Select One",'Sentiment Classification', 'Tumor Detection'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
 
 
12
 
13
+ if task == "Tumor Detection":
14
+ def cnn(img, model):
15
+ img = Image.open(img)
16
+ img = img.resize((128, 128))
17
+ img = np.array(img)
18
+ input_img = np.expand_dims(img, axis=0)
19
+ res = model.predict(input_img)
20
  if res:
21
+ return "Tumor Detected"
22
  else:
23
+ return "No Tumor"
24
+
25
+ cnn_model = tf.keras.models.load_model("cnn_model.h5")
26
+ uploaded_file = st.file_uploader("Choose a file", type=["jpg", "jpeg", "png"])
27
+ if uploaded_file is not None:
28
+ st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
29
+ if st.button("Submit"):
30
+ result=cnn(uploaded_file, cnn_model)
31
+ st.write(result)
32
+
33
+
34
+ elif task == "Sentiment Classification":
35
+ types = ["Perceptron","BackPropagation", "RNN","DNN", "LSTM"]
36
+ input_text2 = st.radio("Select", types, horizontal=True)
37
+
38
+ if input_text2 == "Perceptron":
39
+ with open("ppn_model.pkl",'rb') as file:
40
+ perceptron = pickle.load(file)
41
+ with open("ppn_tokeniser.pkl",'rb') as file:
42
+ ppn_tokeniser = pickle.load(file)
43
+
44
+ def ppn_make_predictions(inp, model):
45
+ encoded_inp = ppn_tokeniser.texts_to_sequences([inp])
46
+ padded_inp = tf.keras.preprocessing.sequence.pad_sequences(encoded_inp, maxlen=500)
47
+ res = model.predict(padded_inp)
48
+ if res:
49
+ return "Negative"
50
+ else:
51
+ return "Positive"
52
+
53
+ st.subheader('Movie Review Classification using Perceptron')
54
+ inp = st.text_area('Enter message')
55
+ if st.button('Check'):
56
+ pred = ppn_make_predictions([inp], perceptron)
57
+ st.write(pred)
58
+
59
+ if input_text2 == "BackPropagation":
60
+ with open("bp_model.pkl",'rb') as file:
61
+ backprop = pickle.load(file)
62
+ with open("bp_tokeniser.pkl",'rb') as file:
63
+ bp_tokeniser = pickle.load(file)
64
+
65
+ def bp_make_predictions(inp, model):
66
+ encoded_inp = bp_tokeniser.texts_to_sequences([inp])
67
+ padded_inp = tf.keras.preprocessing.sequence.pad_sequences(encoded_inp, maxlen=500)
68
+ res = model.predict(padded_inp)
69
+ if res:
70
+ return "Negative"
71
+ else:
72
+ return "Positive"
73
+
74
+ st.subheader('Movie Review Classification using BackPropagation')
75
+ inp = st.text_area('Enter message')
76
+ if st.button('Check'):
77
+ pred = bp_make_predictions([inp], backprop)
78
+ st.write(pred)
79
+
80
 
81
+ elif input_text2 == "RNN":
82
+ rnn_model=tf.keras.models.load_model("rnn_model.h5")
83
+ with open("rnn_tokeniser.pkl", 'rb') as model_file:
84
+ rnn_tokeniser=pickle.load(model_file)
85
+
86
+ def rnn_make_predictions(inp, model):
87
+ encoded_inp = rnn_tokeniser.texts_to_sequences([inp])
88
+ padded_inp = tf.keras.preprocessing.sequence.pad_sequences(encoded_inp, maxlen=10, padding='post')
89
+ res = (model.predict(padded_inp) > 0.5).astype("int32")
90
+ if res:
91
+ return "Spam"
92
+ else:
93
+ return "Ham"
94
+
95
+ st.subheader('Spam message Classification using RNN')
96
+ input = st.text_area("Give message")
97
+ if st.button('Check'):
98
+ pred = rnn_make_predictions([input], rnn_model)
99
+ st.write(pred)
100
+
101
+
102
+
103
+ elif input_text2 == "DNN":
104
+ dnn_model=tf.keras.models.load_model("dnn_model.h5")
105
+ with open("dnn_tokeniser.pkl",'rb') as file:
106
+ dnn_tokeniser = pickle.load(file)
107
+
108
+ def dnn_make_predictions(inp, model):
109
+
110
+ inp = dnn_tokeniser.texts_to_sequences([inp])
111
+ inp = tf.keras.preprocessing.sequence.pad_sequences(inp, maxlen=500)
112
+ res = model.predict([inp])
113
+ if res:
114
+ return "Negative"
115
+ else:
116
+ return "Positive"
117
+
118
+ st.subheader('Movie Review Classification using DNN')
119
+ inp = st.text_area('Enter message')
120
+ if st.button('Check'):
121
+ pred = dnn_make_predictions([inp], dnn_model)
122
+ st.write(pred)
123
+
124
+
125
+
126
+ elif input_text2 == "LSTM":
127
+ lstm_model=tf.keras.models.load_model("lstm_model.h5")
128
+
129
+ with open("lstm_tokeniser.pkl",'rb') as file:
130
+ lstm_tokeniser = pickle.load(file)
131
+
132
+ def lstm_make_predictions(inp, model):
133
+ inp = lstm_tokeniser.texts_to_sequences([inp])
134
+ inp = tf.keras.preprocessing.sequence.pad_sequences(inp, maxlen=500)
135
+ res = (model.predict(inp) > 0.5).astype("int32")
136
+ if res:
137
+ return "Negative"
138
+ else:
139
+ return "Positive"
140
+ st.subheader('Movie Review Classification using LSTM')
141
+ inp = st.text_area('Enter message')
142
+ if st.button('Check'):
143
+ pred = lstm_make_predictions([inp], lstm_model)
144
+ st.write(pred)