alokvtk commited on
Commit
0bb40b4
·
1 Parent(s): c8bc0b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -123
app.py CHANGED
@@ -6,8 +6,6 @@ import joblib
6
  from tensorflow.keras.preprocessing.text import Tokenizer
7
  from tensorflow.keras.preprocessing.sequence import pad_sequences
8
  from tensorflow.keras.applications.inception_v3 import preprocess_input
9
- from tensorflow.keras.datasets import imdb
10
-
11
  import cv2
12
  from BackPropogation import BackPropogation
13
  from Perceptron import Perceptron
@@ -18,93 +16,66 @@ import pickle
18
  from numpy import argmax
19
 
20
 
 
 
 
 
21
  # Load saved models
22
  image_model = load_model('tumor_detection_model.h5')
23
- dnn_model = load_model('sms_spam_detection_dnnmodel.h5')
24
- rnn_model = load_model('spam_detection_rnn_model.h5')
25
-
26
- # Loading the model using pickle
27
- with open('Model_backprop.pkl', 'rb') as file:
28
- backprop_model = pickle.load(file)
29
-
30
- with open('Percep_model.pkl', 'rb') as file:
31
- perceptron_model = pickle.load(file)
32
 
33
- with open('tokeniser.pkl', 'rb') as file:
34
- loaded_tokeniser = pickle.load(file)
35
-
36
- lstm_model_path='Lstm_model.h5'
37
 
38
  # Streamlit app
39
  st.title("Classification")
40
 
41
  # Sidebar
42
- task = st.sidebar.selectbox("Select Task", ["Tumor Detection ", "Sentiment Classification"])
43
- tokeniser = tf.keras.preprocessing.text.Tokenizer()
44
- max_length=10
45
-
46
- def predictdnn_spam(text):
47
- sequence = loaded_tokeniser.texts_to_sequences([text])
48
- padded_sequence = pad_sequences(sequence, maxlen=10)
49
- prediction = dnn_model.predict(padded_sequence)[0][0]
50
- if prediction >= 0.5:
51
- return "not spam"
52
- else:
53
- return "spam"
54
- def preprocess_imdbtext(text, maxlen=200, num_words=10000):
55
- # Tokenizing the text
56
- tokenizer = Tokenizer(num_words=num_words)
57
- tokenizer.fit_on_texts(text)
58
-
59
- # Converting text to sequences
60
- sequences = tokenizer.texts_to_sequences(text)
61
-
62
- # Padding sequences to a fixed length
63
- padded_sequences = pad_sequences(sequences, maxlen=maxlen)
64
-
65
- return padded_sequences, tokenizer
66
 
67
- def predict_sentiment_backprop(text, model):
68
- preprocessed_text = preprocess_imdbtext(text, 200)
69
- prediction = backprop_model.predict(preprocessed_text)
70
- return prediction
71
-
72
- def preprocess_imdb_lstm(user_input, tokenizer, max_review_length=500):
73
- # Tokenize and pad the user input
74
- user_input_sequence = tokenizer.texts_to_sequences([user_input])
75
- user_input_padded = pad_sequences(user_input_sequence, maxlen=max_review_length)
76
- return user_input_padded
77
-
78
- def predict_sentiment_lstm(model, user_input, tokenizer):
79
- preprocessed_input = preprocess_imdb_lstm(user_input, tokenizer)
80
- prediction = model.predict(preprocessed_input)
81
- return prediction
82
-
83
- def predict_sentiment_precep(user_input, num_words=1000, max_len=200):
84
- word_index = imdb.get_word_index()
85
- input_sequence = [word_index[word] if word in word_index and word_index[word] < num_words else 0 for word in user_input.split()]
86
- padded_sequence = pad_sequences([input_sequence], maxlen=max_len)
87
- return padded_sequence
88
-
89
-
90
 
91
- def preprocess_message_dnn(message, tokeniser, max_length):
92
- # Tokenize and pad the input message
93
- encoded_message = tokeniser.texts_to_sequences([message])
94
- padded_message = tf.keras.preprocessing.sequence.pad_sequences(encoded_message, maxlen=max_length, padding='post')
95
- return padded_message
96
 
97
- def predict_rnnspam(message, tokeniser, max_length):
98
- # Preprocess the message
99
- processed_message = preprocess_message_dnn(message, tokeniser, max_length)
 
 
 
 
100
 
101
- # Predict spam or ham
102
- prediction = rnn_model.predict(processed_message)
103
- if prediction >= 0.5:
104
- return "Spam"
 
 
 
 
105
  else:
106
- return "Ham"
 
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  # make a prediction for CNN
110
  def preprocess_image(image):
@@ -114,6 +85,7 @@ def preprocess_image(image):
114
 
115
  return preprocessed_image
116
 
 
117
  def make_prediction_cnn(image, image_model):
118
  img = image.resize((128, 128))
119
  img_array = np.array(img)
@@ -126,59 +98,31 @@ def make_prediction_cnn(image, image_model):
126
  st.write("Tumor Detected")
127
  else:
128
  st.write("No Tumor")
 
129
  if task == "Sentiment Classification":
130
  st.subheader("Choose Model")
131
- model_choice = st.radio("Select Model", ["DNN", "RNN", "Perceptron", "Backpropagation","LSTM"])
132
 
133
  st.subheader("Text Input")
134
-
135
-
136
- if model_choice=='DNN':
137
- text_input = st.text_area("Enter Text")
138
- if st.button("Predict"):
 
 
 
139
  if text_input:
140
- prediction_result = predictdnn_spam(text_input)
141
- st.write(f"The review's class is: {prediction_result}")
142
- else:
143
- st.write("Enter a movie review")
144
-
145
- elif model_choice == "RNN":
146
- text_input = st.text_area("Enter Text")
147
- if text_input:
148
- prediction_result = predict_rnnspam(text_input,loaded_tokeniser,max_length=10)
149
- if st.button("Predict"):
150
  st.write(f"The message is classified as: {prediction_result}")
151
  else:
152
  st.write("Please enter some text for prediction")
153
- elif model_choice == "Perceptron":
154
- text_input = st.text_area("Enter Text" )
155
- if st.button('Predict'):
156
- processed_input = predict_sentiment_precep(text_input)
157
- prediction = perceptron_model.predict(processed_input)[0]
158
- sentiment = "Positive" if prediction == 1 else "Negative"
159
- st.write(f"Predicted Sentiment: {sentiment}")
160
- elif model_choice == "LSTM":
161
-
162
- lstm_model = tf.keras.models.load_model(lstm_model_path)
163
- text_input = st.text_area("Enter text for sentiment analysis:", "")
164
- if st.button("Predict"):
165
- tokenizer = Tokenizer(num_words=5000)
166
- prediction = predict_sentiment_lstm(lstm_model, text_input, tokenizer)
167
-
168
- if prediction[0][0]<0.5 :
169
- result="Negative"
170
- st.write(f"The message is classified as: {result}")
171
- else:
172
- result="Positive"
173
- st.write(f"The message is classified as: {result}")
174
-
175
- elif model_choice == "Backpropagation":
176
- text_input = st.text_area("Enter Text" )
177
- if st.button('Predict'):
178
- processed_input = predict_sentiment_precep(text_input)
179
- prediction = backprop_model.predict(processed_input)[0]
180
- sentiment = "Positive" if prediction == 1 else "Negative"
181
- st.write(f"Predicted Sentiment: {sentiment}")
182
 
183
  else:
184
  st.subheader("Choose Model")
@@ -196,6 +140,4 @@ else:
196
 
197
  if st.button("Predict"):
198
  if model_choice == "CNN":
199
- make_prediction_cnn(image, image_model)
200
-
201
-
 
6
  from tensorflow.keras.preprocessing.text import Tokenizer
7
  from tensorflow.keras.preprocessing.sequence import pad_sequences
8
  from tensorflow.keras.applications.inception_v3 import preprocess_input
 
 
9
  import cv2
10
  from BackPropogation import BackPropogation
11
  from Perceptron import Perceptron
 
16
  from numpy import argmax
17
 
18
 
19
+ # Load the tokenizer using pickle
20
+ with open(r'tokeniser.pkl', 'rb') as handle:
21
+ loaded_tokenizer = pickle.load(handle)
22
+
23
  # Load saved models
24
  image_model = load_model('tumor_detection_model.h5')
25
+ dnn_model = load_model('imdb_model.h5')
26
+ loaded_model = tf.keras.models.load_model('sms_spam_detection_dnnmodel.h5')
27
+ perceptron_model = joblib.load('perceptron_model.joblib')
28
+ backprop_model = joblib.load('backprop_model.pkl')
 
 
 
 
 
29
 
 
 
 
 
30
 
31
  # Streamlit app
32
  st.title("Classification")
33
 
34
  # Sidebar
35
+ task = st.sidebar.selectbox("Select Task", ["Tumor Detection", "Sentiment Classification"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ def preprocess_text(text):
38
+ tokenizer = Tokenizer()
39
+ tokenizer.fit_on_texts([text])
40
+ sequences = tokenizer.texts_to_sequences([text])
41
+ preprocessed_text = pad_sequences(sequences, maxlen=4)
42
+
43
+ return preprocessed_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
 
 
 
 
 
45
 
46
+
47
+ def predict_dnn(preprocessed_text):
48
+ preprocessed_text = preprocessed_text.reshape((1, 4)) # Adjust the shape according to your model's input shape
49
+
50
+ prediction = dnn_model.predict(preprocessed_text)
51
+ st.write("DNN Prediction:", prediction)
52
+
53
 
54
+
55
+ def predict_rnn(input_text):
56
+ # Process input text similarly to training data
57
+ encoded_input = loaded_tokenizer.texts_to_sequences([input_text])
58
+ padded_input = tf.keras.preprocessing.sequence.pad_sequences(encoded_input, maxlen=10, padding='post')
59
+ prediction = loaded_model.predict(padded_input)
60
+ if prediction > 0.5:
61
+ return "spam"
62
  else:
63
+ return "ham"
64
+
65
 
66
+ def predict_custom_perceptron(preprocessed_text):
67
+ perceptron = CustomPerceptron(epochs=10) # Using the custom Perceptron
68
+ prediction = perceptron.predict(preprocessed_text)
69
+ st.write("Custom Perceptron Prediction:", prediction)
70
+
71
+ def predict_sklearn_perceptron(preprocessed_text):
72
+ perceptron = SklearnPerceptron() # Using the sklearn Perceptron
73
+ prediction = perceptron.predict(preprocessed_text)
74
+ st.write("Sklearn Perceptron Prediction:", prediction)
75
+
76
+ def predict_backpropagation(preprocessed_text):
77
+ prediction = backprop_model.predict(preprocessed_text)
78
+ st.write("Backpropagation Prediction:", prediction)
79
 
80
  # make a prediction for CNN
81
  def preprocess_image(image):
 
85
 
86
  return preprocessed_image
87
 
88
+
89
  def make_prediction_cnn(image, image_model):
90
  img = image.resize((128, 128))
91
  img_array = np.array(img)
 
98
  st.write("Tumor Detected")
99
  else:
100
  st.write("No Tumor")
101
+
102
  if task == "Sentiment Classification":
103
  st.subheader("Choose Model")
104
+ model_choice = st.radio("Select Model", ["DNN", "RNN", "Perceptron", "Backpropagation"])
105
 
106
  st.subheader("Text Input")
107
+ text_input = st.text_area("Enter Text")
108
+
109
+ if st.button("Predict"):
110
+ # Preprocess the text
111
+ preprocessed_text = preprocess_text(text_input)
112
+ if model_choice == "DNN":
113
+ predict_dnn(preprocessed_text)
114
+ elif model_choice == "RNN":
115
  if text_input:
116
+ prediction_result = predict_rnn(text_input)
 
 
 
 
 
 
 
 
 
117
  st.write(f"The message is classified as: {prediction_result}")
118
  else:
119
  st.write("Please enter some text for prediction")
120
+ elif model_choice == "Custom Perceptron":
121
+ predict_custom_perceptron(preprocessed_text)
122
+ elif model_choice == "Sklearn Perceptron":
123
+ predict_sklearn_perceptron(preprocessed_text)
124
+ elif model_choice == "Backpropagation":
125
+ predict_backpropagation(preprocessed_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  else:
128
  st.subheader("Choose Model")
 
140
 
141
  if st.button("Predict"):
142
  if model_choice == "CNN":
143
+ make_prediction_cnn(image, image_model)