riyageorge/Multitasking_App
0
1import streamlit as st2from PIL import Image3import tensorflow as tf4import numpy as np5from tensorflow.keras.datasets import imdb6from tensorflow.keras.preprocessing import sequence7from numpy import argmax8import pickle9 10 11 12# Load your CNN tumor classification model13cnn_model = tf.keras.models.load_model('cnn_tumor_model.h5')14 15# Function to perform image classification using CNN16def classify_image(img, cnn_model):17 img = Image.open(img)18 img = img.resize((128, 128))19 img = np.array(img)20 input_img = np.expand_dims(img, axis=0)21 res = cnn_model.predict(input_img)22 if res > 0.5:23 return "Tumor Detected"24 else:25 return "No Tumor"26 27 28# Load your DNN SMS spam detection model29dnn_smsspam_model = tf.keras.models.load_model('dnn_smsspam_model.h5')30# Load the saved tokenizer31with open('dnn_smsspam_tokenizer.pickle', 'rb') as handle:32 dnn_smsspam_tokenizer = pickle.load(handle)33 34def dnn_predict_message(input_text):35 max_length=2036 # Process input text similarly to training data37 encoded_input = dnn_smsspam_tokenizer.texts_to_sequences([input_text])38 padded_input = tf.keras.preprocessing.sequence.pad_sequences(encoded_input, maxlen=max_length, padding='post')39 # Get the probabilities of being classified as "Spam" for each input40 predictions = dnn_smsspam_model.predict(padded_input)41 # Define a threshold (e.g., 0.5) for classification42 threshold = 0.543 # Make the predictions based on the threshold for each input44 for prediction in predictions:45 if prediction > threshold:46 return "Spam"47 else:48 return "Not spam"49 50 51# Load your RNN SMS spam detection model52rnn_smsspam_model = tf.keras.models.load_model('rnn_smsspam_model.h5')53# Load the saved tokenizer54with open('rnn_smsspam_tokenizer.pickle', 'rb') as handle:55 rnn_smsspam_tokenizer = pickle.load(handle)56 57def rnn_predict_message(input_text):58 max_length=2059 # Process input text similarly to training data60 encoded_input = rnn_smsspam_tokenizer.texts_to_sequences([input_text])61 padded_input = tf.keras.preprocessing.sequence.pad_sequences(encoded_input, maxlen=max_length, padding='post')62 # Get the probabilities of being classified as "Spam" for each input63 predictions = rnn_smsspam_model.predict(padded_input)64 # Define a threshold (e.g., 0.5) for classification65 threshold = 0.566 # Make the predictions based on the threshold for each input67 for prediction in predictions:68 if prediction > threshold:69 return "Spam"70 else:71 return "Not spam"72 73 74# Load the saved LSTM model75lstm_smsspam_model=tf.keras.models.load_model('lstm_smsspam_model.h5')76# Load the saved tokenizer77with open('lstm_smsspam_tokenizer.pickle', 'rb') as handle:78 lstm_smsspam_tokenizer = pickle.load(handle)79 80def lstm_predict_message(message):81 maxlen=5082 sequence = lstm_smsspam_tokenizer.texts_to_sequences([message])83 sequence = tf.keras.preprocessing.sequence.pad_sequences(sequence, padding='post', maxlen=maxlen)84 prediction = lstm_smsspam_model.predict(sequence)[0, 0]85 if prediction > 0.5:86 return 'Spam'87 else:88 return 'Not spam'89 90 91# Load the saved model92gru_movie_model = tf.keras.models.load_model('gru_movie_model.h5')93with open('tokenizer_movie_gru.pickle', 'rb') as handle:94 lstm_movie_tokeniser = pickle.load(handle)95 96# Function to predict sentiment for a given review97def gru_predict_movie_sentiment(review):98 maxlen = 10099 sequence = lstm_movie_tokeniser.texts_to_sequences([review])100 sequence = tf.keras.preprocessing.sequence.pad_sequences(sequence, padding='post', maxlen=maxlen)101 prediction = gru_movie_model.predict(sequence)102 if prediction > 0.5:103 return "Positive"104 else:105 return "Negative"106 107 108with open('perceptron_movie_model.pkl', 'rb') as file:109 perceptron_movie_model = pickle.load(file)110 111def predict_movie_sentiment_perceptron(review):112 max_review_length = 500113 top_words = 5000114 word_index = imdb.get_word_index()115 review = review.lower().split()116 review = [word_index[word] if (word in word_index and word_index[word] < top_words) else 0 for word in review]117 review_bin = np.where(np.array(review) > 0, 1, 0)118 # Padding or truncating the review to match the perceptron's input size119 review_bin_padded = np.pad(review_bin, (0, max_review_length - len(review_bin)), 'constant')120 prediction = perceptron_movie_model.predict([review_bin_padded])121 if prediction[0] == 1:122 return "Positive"123 else:124 return "Negative"125 126 127# Load the saved instance of the Perceptron class128with open('backprop_movie_model.pkl', 'rb') as file:129 backprop_movie_model = pickle.load(file)130 131def predict_movie_sentiment_backprop(review):132 max_review_length = 500133 top_words = 5000134 word_index = imdb.get_word_index()135 review = review.lower().split()136 review = [word_index[word] if (word in word_index and word_index[word] < top_words) else 0 for word in review]137 review_bin = np.where(np.array(review) > 0, 1, 0)138 # Padding or truncating the review to match the perceptron's input size139 review_bin_padded = np.pad(review_bin, (0, max_review_length - len(review_bin)), 'constant')140 prediction = backprop_movie_model.predict([review_bin_padded])141 if prediction[0] == 1:142 return "Positive"143 else:144 return "Negative"145 146 147 148# Main function for Streamlit app149def main(): 150 st.title("Multitasking App") 151 152 # Sidebar dropdown for selecting tasks153 task = st.sidebar.radio("Select Task", (["Tumor Detection", "Sentiment Classification"]))154 155 # Depending on the selected task, provide model options156 if task == "Tumor Detection":157 model = st.sidebar.radio("Select Model", (["CNN"]))158 159 if model == "CNN":160 st.subheader("Tumor Detection")161 uploaded_file = st.file_uploader("Upload an image to check for tumor...", type=["jpg", "png", "jpeg"])162 163 if uploaded_file is not None:164 # Display the image165 image_display = Image.open(uploaded_file)166 st.image(image_display, caption="Uploaded Image", use_column_width=True)167 168 if st.button("Detect Tumor"):169 # Call the tumor detection function170 result = classify_image(uploaded_file, cnn_model)171 st.write("Tumor Detection Result:", result)172 173 174 175 elif task == "Sentiment Classification":176 model = st.sidebar.radio("Select Model", (["DNN", "RNN", "LSTM", "GRU", "Perceptron", "Backpropagation"]))177 178 179 180 if model == "DNN":181 st.subheader("SMS Spam Detection")182 user_input = st.text_area("Enter a message to classify as 'Spam' or 'Not spam': ")183 184 if st.button("Predict"):185 if user_input:186 prediction_result = dnn_predict_message(user_input)187 st.write(f"The message is classified as: {prediction_result}")188 else:189 st.write("Please enter some text for prediction")190 191 elif model == "RNN":192 st.subheader("SMS Spam Detection")193 user_input = st.text_area("Enter a message to classify as 'Spam' or 'Not spam': ")194 195 if st.button("Predict"):196 if user_input:197 prediction_result = rnn_predict_message(user_input)198 st.write(f"The message is classified as: {prediction_result}")199 else:200 st.write("Please enter some text for prediction")201 202 elif model == "LSTM":203 st.subheader("SMS Spam Detection")204 user_input = st.text_area("Enter a message to classify as 'Spam' or 'Not spam': ")205 206 if st.button("Predict"):207 if user_input:208 prediction_result = lstm_predict_message(user_input)209 st.write(f"The message is classified as: {prediction_result}")210 else:211 st.write("Please enter some text for prediction")212 213 elif model == "GRU":214 st.subheader("Movie Sentiment Analysis")215 user_review = st.text_area("Enter a movie review: ")216 217 if st.button("Analyze Sentiment"):218 if user_review:219 sentiment_result = gru_predict_movie_sentiment(user_review)220 st.write(f"The sentiment of the review is: {sentiment_result}")221 else:222 st.write("Please enter a movie review for sentiment analysis")223 224 elif model == "Perceptron":225 st.subheader("Movie Sentiment Analysis")226 user_review = st.text_area("Enter a movie review: ")227 228 if st.button("Analyze Sentiment"):229 if user_review:230 sentiment_result = predict_movie_sentiment_perceptron(user_review)231 st.write(f"The sentiment of the review is: {sentiment_result}")232 else:233 st.write("Please enter a movie review for sentiment analysis")234 235 elif model == "Backpropagation":236 st.subheader("Movie Sentiment Analysis")237 user_review = st.text_area("Enter a movie review: ")238 239 if st.button("Analyze Sentiment"):240 if user_review:241 sentiment_result = predict_movie_sentiment_backprop(user_review)242 st.write(f"The sentiment of the review is: {sentiment_result}")243 else:244 st.write("Please enter a movie review for sentiment analysis")245 246 247if __name__ == "__main__":248 main()249 