Vismayavs commited on
Commit
54f4801
1 Parent(s): 796b896

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +198 -0
app.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ from PIL import Image
4
+ from tensorflow.keras.models import load_model
5
+ 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
14
+ from sklearn.linear_model import Perceptron
15
+ import tensorflow as tf
16
+ import joblib
17
+ import pickle
18
+ from numpy import argmax
19
+
20
+
21
+ # Load the 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('backpropagation-model.pkl', 'rb') as file:
28
+ backprop_model = pickle.load(file)
29
+
30
+ with open('Perceptron_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):
111
+ image = image.resize((299, 299))
112
+ image_array = np.array(image)
113
+ preprocessed_image = preprocess_input(image_array)
114
+
115
+ return preprocessed_image
116
+ def make_prediction_cnn(image, image_model):
117
+ img = image.resize((128, 128))
118
+ img_array = np.array(img)
119
+ img_array = img_array.reshape((1, img_array.shape[0], img_array.shape[1], img_array.shape[2]))
120
+
121
+ preprocessed_image = preprocess_input(img_array)
122
+ prediction = image_model.predict(preprocessed_image)
123
+
124
+ if prediction > 0.5:
125
+ st.write("Tumor Detected")
126
+ else:
127
+ st.write("No Tumor")
128
+ if task == "Sentiment Classification":
129
+ st.subheader("Choose Model")
130
+ model_choice = st.radio("Select Model", ["DNN", "RNN", "Perceptron", "Backpropagation","LSTM"])
131
+
132
+ st.subheader("Text Input")
133
+
134
+
135
+ if model_choice=='DNN':
136
+ text_input = st.text_area("Enter Text")
137
+ if st.button("Predict"):
138
+ if text_input:
139
+ prediction_result = predictdnn_spam(text_input)
140
+ st.write(f"The review's class is: {prediction_result}")
141
+ else:
142
+ st.write("Enter a movie review")
143
+
144
+ elif model_choice == "RNN":
145
+ text_input = st.text_area("Enter Text")
146
+ if text_input:
147
+ prediction_result = predict_rnnspam(text_input,loaded_tokeniser,max_length=10)
148
+ if st.button("Predict"):
149
+ st.write(f"The message is classified as: {prediction_result}")
150
+ else:
151
+ st.write("Please enter some text for prediction")
152
+ elif model_choice == "Perceptron":
153
+ text_input = st.text_area("Enter Text" )
154
+ if st.button('Predict'):
155
+ processed_input = predict_sentiment_precep(text_input)
156
+ prediction = perceptron_model.predict(processed_input)[0]
157
+ sentiment = "Positive" if prediction == 1 else "Negative"
158
+ st.write(f"Predicted Sentiment: {sentiment}")
159
+ elif model_choice == "LSTM":
160
+
161
+ lstm_model = tf.keras.models.load_model(lstm_model_path)
162
+ text_input = st.text_area("Enter text for sentiment analysis:", "")
163
+ if st.button("Predict"):
164
+ tokenizer = Tokenizer(num_words=5000)
165
+ prediction = predict_sentiment_lstm(lstm_model, text_input, tokenizer)
166
+
167
+ if prediction[0][0]<0.5 :
168
+ result="Negative"
169
+ st.write(f"The message is classified as: {result}")
170
+ else:
171
+ result="Positive"
172
+ st.write(f"The message is classified as: {result}")
173
+
174
+ elif model_choice == "Backpropagation":
175
+ text_input = st.text_area("Enter Text" )
176
+ if st.button('Predict'):
177
+ processed_input = predict_sentiment_precep(text_input)
178
+ prediction = backprop_model.predict(processed_input)[0]
179
+ sentiment = "Positive" if prediction == 1 else "Negative"
180
+ st.write(f"Predicted Sentiment: {sentiment}")
181
+
182
+ else:
183
+ st.subheader("Choose Model")
184
+ model_choice = st.radio("Select Model", ["CNN"])
185
+
186
+ st.subheader("Image Input")
187
+ image_input = st.file_uploader("Choose an image...", type="jpg")
188
+
189
+ if image_input is not None:
190
+ image = Image.open(image_input)
191
+ st.image(image, caption="Uploaded Image.", use_column_width=True)
192
+
193
+ # Preprocess the image
194
+ preprocessed_image = preprocess_image(image)
195
+
196
+ if st.button("Predict"):
197
+ if model_choice == "CNN":
198
+ make_prediction_cnn(image, image_model)