riyageorge commited on
Commit
1df5d20
·
1 Parent(s): fe6e4b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -290
app.py CHANGED
@@ -9,13 +9,8 @@ import pickle
9
 
10
 
11
 
12
- # Load your tumor classification model
13
- #cnn_model = tf.keras.models.load_model('cnn_tumor_model.h5')
14
- try:
15
- cnn_model = tf.keras.models.load_model('cnn_tumor_model.h5')
16
- except Exception as e:
17
- st.error(f"Error loading tumor model: {e}")
18
-
19
 
20
  # Function to perform image classification using CNN
21
  def classify_image(img, cnn_model):
@@ -28,23 +23,22 @@ def classify_image(img, cnn_model):
28
  return "Tumor Detected"
29
  else:
30
  return "No Tumor"
31
-
32
-
33
-
34
 
35
- # Load your SMS spam detection model
36
- spam_model = tf.keras.models.load_model('spammodel.h5')
 
 
37
  # Load the saved tokenizer
38
- with open('tokenizer.pickle', 'rb') as handle:
39
- tokeniser = pickle.load(handle)
40
 
41
- max_length=20
42
- def predict_message(input_text, tokeniser):
43
  # Process input text similarly to training data
44
- encoded_input = tokeniser.texts_to_sequences([input_text])
45
  padded_input = tf.keras.preprocessing.sequence.pad_sequences(encoded_input, maxlen=max_length, padding='post')
46
  # Get the probabilities of being classified as "Spam" for each input
47
- predictions = spam_model.predict(padded_input)
48
  # Define a threshold (e.g., 0.5) for classification
49
  threshold = 0.5
50
  # Make the predictions based on the threshold for each input
@@ -55,290 +49,52 @@ def predict_message(input_text, tokeniser):
55
  return "Not spam"
56
 
57
 
58
- # Load the saved model
59
- sms_sentiment_model=tf.keras.models.load_model('sms_sentiment_model.h5')
60
-
61
- # Load the saved tokenizer
62
- with open('tokenizer_smsglove.pickle', 'rb') as handle:
63
- smstokeniser = pickle.load(handle)
64
-
65
-
66
- def predict_sms_sentiment(message):
67
- maxlen=50
68
- sequence = smstokeniser.texts_to_sequences([message])
69
- sequence = tf.keras.preprocessing.sequence.pad_sequences(sequence, padding='post', maxlen=maxlen)
70
- prediction = sms_sentiment_model.predict(sequence)[0, 0]
71
- if prediction > 0.5:
72
- return 'Spam'
73
- else:
74
- return 'Not spam'
75
-
76
-
77
-
78
-
79
- # Load the saved model
80
- imdb_model = tf.keras.models.load_model('lstm_imdb_model.h5')
81
- top_words = 5000
82
- max_review_length = 500
83
-
84
- # Function to predict sentiment for a given review
85
- def predict_sentiment(review):
86
- # Process input text similarly to training data
87
- word_index = imdb.get_word_index()
88
- review = review.lower().split()
89
- review = [word_index[word] if word in word_index and word_index[word] < top_words else 0 for word in review]
90
- review = sequence.pad_sequences([review], maxlen=max_review_length)
91
- prediction = imdb_model.predict(review)
92
- if prediction > 0.5:
93
- return "Positive"
94
- else:
95
- return "Negative"
96
-
97
-
98
- # Load the saved model
99
- gru_movie_model = tf.keras.models.load_model('gru_movie_model.h5')
100
- with open('tokenizer_movie_gru.pickle', 'rb') as handle:
101
- lstm_movie_tokeniser = pickle.load(handle)
102
-
103
-
104
- # Function to predict sentiment for a given review
105
- def gru_predict_sentiment(review):
106
- maxlen = 100
107
- sequence = lstm_movie_tokeniser.texts_to_sequences([review])
108
- sequence = tf.keras.preprocessing.sequence.pad_sequences(sequence, padding='post', maxlen=maxlen)
109
- prediction = gru_movie_model.predict(sequence)
110
- if prediction > 0.5:
111
- return "Positive"
112
- else:
113
- return "Negative"
114
-
115
-
116
-
117
-
118
- # Load the saved model
119
- iris_dnn_model = tf.keras.models.load_model('iris_dnn_model.h5')
120
-
121
- def predict_iris_class(input_data):
122
- # Make predictions using the loaded model
123
- prediction = iris_dnn_model.predict(input_data)
124
- predicted_class = argmax(prediction)
125
-
126
- class_names = ['setosa', 'versicolor', 'virginica']
127
- predicted_class_name = class_names[predicted_class]
128
-
129
- return prediction, predicted_class_name
130
-
131
-
132
-
133
-
134
- # Load the saved model
135
- mnist_model = tf.keras.models.load_model('mnist_cnn_model.h5')
136
-
137
- def predict_digit(file_path):
138
- # Load the image using PIL
139
- image = Image.open(file_path)
140
- # Convert the image to grayscale
141
- image = image.convert('L')
142
- # Resize the image to 28x28 (same as MNIST dataset)
143
- image = image.resize((28, 28))
144
- # Convert image to array
145
- image_array = np.array(image)
146
- # Reshape and normalize the image (similar to training data)
147
- processed_image = image_array.reshape((1, 28, 28, 1))
148
- processed_image = processed_image.astype('float32') / 255.0
149
- # Make predictions using the loaded model
150
- prediction = mnist_model.predict(processed_image)
151
- predicted_class = np.argmax(prediction)
152
- return predicted_class
153
-
154
-
155
-
156
-
157
- # Load the model from the file using pickle
158
- with open('iris_perceptron_model.pkl', 'rb') as file:
159
- iris_perceptron_model = pickle.load(file)
160
- def predict_iris_species(input_data):
161
- # Make predictions using the loaded Perceptron model
162
- prediction = iris_perceptron_model.predict(input_data)
163
- predicted_class = prediction[0] # Assuming the prediction is a single class
164
- classes = {0: 'Setosa', 1: 'Not Setosa'} # Map prediction to class label
165
- predicted_class_name = classes[predicted_class]
166
-
167
- return predicted_class_name
168
-
169
- # Load the model from the file using pickle
170
- with open('iris_backprop_model.pkl', 'rb') as file:
171
- iris_backprop_model = pickle.load(file)
172
- def predict_iris_species_backprop(input_data):
173
- # Make predictions using the loaded Perceptron model
174
- prediction = iris_backprop_model.predict(input_data)
175
- predicted_class = prediction[0] # Assuming the prediction is a single class
176
- classes = {0: 'Setosa', 1: 'Not Setosa'} # Map prediction to class label
177
- predicted_class_name = classes[predicted_class]
178
-
179
- return predicted_class_name
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
 
189
  # Main function for Streamlit app
190
- def main():
191
- st.title("Multitasking App for Image, Text and Data Analysis")
192
- st.subheader("Task Selecetion")
193
-
194
- # Dropdown for task selection
195
- task = st.selectbox("Select Task", ["Tumor Detection-CNN", "Digit Recognition-CNN","SMS Spam Detection-RNN","SMS Spam Detection-LSTM", "IMDb Sentiment Analysis-LSTM","Movie Sentiment Analysis-GRU", "Iris Flower Classification-DNN","Iris Species Prediction-Perceptron","Iris Species Prediction-Backpropagation"])
196
-
197
- if task == "Tumor Detection-CNN":
198
- st.subheader("Tumor Detection-CNN")
199
- uploaded_file = st.file_uploader("Upload an image to check for tumor...", type=["jpg", "png", "jpeg"])
200
-
201
- if uploaded_file is not None:
202
- # Display the image
203
- image_display = Image.open(uploaded_file)
204
- st.image(image_display, caption="Uploaded Image", use_column_width=True)
205
-
206
- if st.button("Detect Tumor"):
207
- # Call the tumor detection function
208
- result = classify_image(uploaded_file, cnn_model)
209
- st.write("Tumor Detection Result:", result)
210
-
211
-
212
- elif task == "SMS Spam Detection-RNN":
213
- st.subheader("SMS Spam Detection-RNN")
214
- user_input = st.text_area("Enter a message to classify as 'Spam' or 'Not spam': ")
215
-
216
- if st.button("Predict"):
217
- if user_input:
218
- prediction_result = predict_message(user_input, tokeniser)
219
- st.write(f"The message is classified as: {prediction_result}")
220
- else:
221
- st.write("Please enter some text for prediction")
222
-
223
-
224
- elif task == "SMS Spam Detection-LSTM":
225
- st.subheader("SMS Spam Detection-LSTM")
226
- user_input = st.text_area("Enter a message to classify as 'Spam' or 'Not spam': ")
227
-
228
- if st.button("Predict"):
229
- if user_input:
230
- prediction_result = predict_sms_sentiment(user_input)
231
- st.write(f"The message is classified as: {prediction_result}")
232
- else:
233
- st.write("Please enter some text for prediction")
234
-
235
-
236
- elif task == "IMDb Sentiment Analysis-LSTM":
237
- st.subheader("IMDb Sentiment Analysis-LSTM")
238
- user_review = st.text_area("Enter a movie review: ")
239
 
240
- if st.button("Analyze Sentiment"):
241
- if user_review:
242
- sentiment_result = predict_sentiment(user_review)
243
- st.write(f"The sentiment of the review is: {sentiment_result}")
244
- else:
245
- st.write("Please enter a movie review for sentiment analysis")
246
 
247
-
248
- elif task == "Movie Sentiment Analysis-GRU":
249
- st.subheader("Movie Sentiment Analysis-GRU")
250
- user_review = st.text_area("Enter a movie review: ")
251
 
252
- if st.button("Analyze Sentiment"):
253
- if user_review:
254
- sentiment_result = gru_predict_sentiment(user_review)
255
- st.write(f"The sentiment of the review is: {sentiment_result}")
256
- else:
257
- st.write("Please enter a movie review for sentiment analysis")
258
-
 
259
 
260
- elif task == "Iris Flower Classification-DNN":
261
- st.subheader("Iris Flower Classification-DNN")
 
 
262
 
263
- # Input fields for user to enter data
264
- sepal_length = st.number_input("Sepal Length", min_value=0.1, max_value=10.0, value=5.0)
265
- sepal_width = st.number_input("Sepal Width", min_value=0.1, max_value=10.0, value=3.5)
266
- petal_length = st.number_input("Petal Length", min_value=0.1, max_value=10.0, value=1.4)
267
- petal_width = st.number_input("Petal Width", min_value=0.1, max_value=10.0, value=0.2)
268
-
269
- if st.button("Predict Iris Class"):
270
- # Prepare input data for prediction
271
- input_row = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
272
-
273
- # Get prediction results
274
- probabilities, predicted_class = predict_iris_class(input_row)
275
-
276
- # Display prediction results
277
- st.subheader("Prediction Results")
278
- st.write('Predicted probabilities:', probabilities)
279
- st.write('Predicted class:', predicted_class)
280
-
281
-
282
- elif task == "Digit Recognition-CNN":
283
- st.subheader("Digit Recognition-CNN")
284
-
285
- uploaded_digit = st.file_uploader("Upload an image of a digit (0-9) to predict...", accept_multiple_files=True)
286
-
287
- if uploaded_digit is not None:
288
- # Display the uploaded digit image(s)
289
- for digit_image in uploaded_digit:
290
- img = Image.open(digit_image)
291
- st.image(img, caption="Uploaded Image", use_column_width=True)
292
-
293
- if st.button("Predict Digit"):
294
- # Call the digit prediction function
295
- digit_prediction = predict_digit(digit_image)
296
- st.write(f"The predicted digit is : {digit_prediction}")
297
-
298
-
299
-
300
-
301
- elif task == "Iris Species Prediction-Perceptron":
302
- st.subheader("Iris Species Prediction-Perceptron")
303
 
304
- # Input fields for user to enter data
305
- sepal_length = st.number_input("Sepal Length", min_value=0.1, max_value=10.0, value=5.0)
306
- sepal_width = st.number_input("Sepal Width", min_value=0.1, max_value=10.0, value=3.5)
307
-
308
- if st.button("Predict Iris Species"):
309
- # Prepare input data for prediction
310
- input_row = np.array([[sepal_length, sepal_width]])
311
-
312
- # Get prediction results using Perceptron model
313
- predicted_class_perceptron = predict_iris_species(input_row)
314
 
315
- # Display prediction results
316
- st.subheader("Prediction Results")
317
- st.write('Predicted class:', predicted_class_perceptron)
318
-
319
-
320
- elif task == "Iris Species Prediction-Backpropagation":
321
- st.subheader("Iris Species Prediction-Backpropagation")
322
-
323
- # Input fields for user to enter data
324
- sepal_length = st.number_input("Sepal Length", min_value=0.1, max_value=10.0, value=5.0)
325
- sepal_width = st.number_input("Sepal Width", min_value=0.1, max_value=10.0, value=2.5)
326
-
327
- if st.button("Predict Iris Species"):
328
- # Prepare input data for prediction
329
- input_row = np.array([[sepal_length, sepal_width]])
330
-
331
- # Get prediction results using Perceptron model
332
- predicted_class = predict_iris_species_backprop(input_row)
333
-
334
- # Display prediction results
335
- st.subheader("Prediction Results")
336
- st.write('Predicted class:', predicted_class)
337
 
 
 
 
 
 
 
 
 
 
 
338
 
339
 
340
-
 
 
341
 
342
  if __name__ == "__main__":
343
  main()
344
-
 
9
 
10
 
11
 
12
+ # Load your CNN tumor classification model
13
+ cnn_model = tf.keras.models.load_model('APP\cnn_tumor_model.h5')
 
 
 
 
 
14
 
15
  # Function to perform image classification using CNN
16
  def classify_image(img, cnn_model):
 
23
  return "Tumor Detected"
24
  else:
25
  return "No Tumor"
 
 
 
26
 
27
+
28
+
29
+ # Load your RNN SMS spam detection model
30
+ rnn_smsspam_model = tf.keras.models.load_model('APP/rnn_smsspam_model.h5')
31
  # Load the saved tokenizer
32
+ with open('APP/rnn_smsspam_tokenizer.pickle', 'rb') as handle:
33
+ rnn_smsspam_tokenizer = pickle.load(handle)
34
 
35
+ def rnn_predict_message(input_text):
36
+ max_length=20
37
  # Process input text similarly to training data
38
+ encoded_input = rnn_smsspam_tokenizer.texts_to_sequences([input_text])
39
  padded_input = tf.keras.preprocessing.sequence.pad_sequences(encoded_input, maxlen=max_length, padding='post')
40
  # Get the probabilities of being classified as "Spam" for each input
41
+ predictions = rnn_smsspam_model.predict(padded_input)
42
  # Define a threshold (e.g., 0.5) for classification
43
  threshold = 0.5
44
  # Make the predictions based on the threshold for each input
 
49
  return "Not spam"
50
 
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  # Main function for Streamlit app
54
+ def main():
55
+ st.title("Multitasking App")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+ # Sidebar dropdown for selecting tasks
58
+ task = st.sidebar.radio("Select Task", (["Tumor Detection", "SMS Spam Detection", "Movie Sentiment Analysis"]))
 
 
 
 
59
 
60
+ # Depending on the selected task, provide model options
61
+ if task == "Tumor Detection":
62
+ model = st.sidebar.radio("Select Model", (["CNN"]))
 
63
 
64
+ if model == "CNN":
65
+ st.subheader("Tumor Detection")
66
+ uploaded_file = st.file_uploader("Upload an image to check for tumor...", type=["jpg", "png", "jpeg"])
67
+
68
+ if uploaded_file is not None:
69
+ # Display the image
70
+ image_display = Image.open(uploaded_file)
71
+ st.image(image_display, caption="Uploaded Image", use_column_width=True)
72
 
73
+ if st.button("Detect Tumor"):
74
+ # Call the tumor detection function
75
+ result = classify_image(uploaded_file, cnn_model)
76
+ st.write("Tumor Detection Result:", result)
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
 
 
 
 
 
 
 
 
 
 
79
 
80
+ elif task == "SMS Spam Detection":
81
+ model = st.sidebar.radio("Select Model", (["Perceptron", "Backpropagation", "DNN", "RNN", "LSTM"]))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
+ if model == "RNN":
84
+ st.subheader("SMS Spam Detection")
85
+ user_input = st.text_area("Enter a message to classify as 'Spam' or 'Not spam': ")
86
+
87
+ if st.button("Predict"):
88
+ if user_input:
89
+ prediction_result = rnn_predict_message(user_input)
90
+ st.write(f"The message is classified as: {prediction_result}")
91
+ else:
92
+ st.write("Please enter some text for prediction")
93
 
94
 
95
+ elif task == "Movie Sentiment Analysis":
96
+ model = st.sidebar.radio("Select Model", (["Perceptron", "Backpropagation", "DNN", "RNN", "LSTM", "GRU"]))
97
+
98
 
99
  if __name__ == "__main__":
100
  main()