KavyaK/DL-Models
0
1import streamlit as st2import numpy as np3from tensorflow.keras.models import load_model4from tokeniser import tokenize_and_pad_sequences5import pickle6from tensorflow.keras.datasets import imdb7from tensorflow.keras.preprocessing import sequence8from tensorflow.keras.preprocessing.sequence import pad_sequences9import tensorflow as tf10from PIL import Image11import cv212from keras.preprocessing import image as keras_image13from tensorflow.keras.applications.inception_v3 import preprocess_input14from sklearn.feature_extraction.text import CountVectorizer15 16 17 18# Load saved models19cnn_model = load_model('cnn.h5')20rnn_model = load_model('RNN.h5')21lstm_model = load_model('lstm.h5') 22dnn_model = load_model('DNN.h5') 23with open('perceptron_model.pkl', 'rb') as file:24 perceptron_model = pickle.load(file)25 26with open('backprop_model.pkl', 'rb') as file:27 backprop_model = pickle.load(file) 28 29with open('tokeniser.pkl', 'rb') as file:30 tokeniser = pickle.load(file)31 32# Streamlit App33st.title("Classification Task")34 35# Task selection36task = st.selectbox("Select Task:", ["Sentiment Classification", "Tumor Detection"])37 38# Algorithm selection39if task == "Sentiment Classification":40 algorithm_options = ["Perceptron", "Backpropagation", "RNN", "DNN", "LSTM"]41elif task == "Tumor Detection":42 algorithm_options = ["CNN"]43else:44 st.error("Invalid task selected!")45 46selected_algorithm = st.radio("Select Algorithm:", algorithm_options)47 48# Input and browse button for tumor detection49if task == "Tumor Detection" and selected_algorithm == "CNN":50 uploaded_file = st.file_uploader("Upload Tumor Image:", type=["jpg", "jpeg", "png"])51 52# Input and submit button for sentiment classification53elif task == "Sentiment Classification":54 input_text = st.text_area("Enter Text for Sentiment Analysis:")55 submit_button = st.button("Submit")56 57 if submit_button:58 # Perform sentiment classification using the selected algorithm59 if selected_algorithm == "Perceptron":60 # Tokenize and pad the input text using the same tokenizer61 max_length = 1062 input_sequence = tokeniser.texts_to_sequences([input_text])63 padded_input = tf.keras.preprocessing.sequence.pad_sequences(input_sequence, maxlen=max_length, padding='post')64 65 # Make prediction using perceptron_model on padded_input66 prediction = perceptron_model.predict(padded_input)67 68 prediction_value = prediction[0]69 70 # Display the result for the Backpropagation model71 result = "spam" if prediction_value > 0.5 else "ham"72 73 74 elif selected_algorithm == "Backpropagation":75 # Tokenize and pad the input text using the same tokenizer76 max_length = 1077 input_sequence = tokeniser.texts_to_sequences([input_text])78 padded_input = tf.keras.preprocessing.sequence.pad_sequences(input_sequence, maxlen=max_length, padding='post')79 80 # Make prediction using backprop_model on padded_input81 prediction = backprop_model.predict(padded_input)82 83 prediction_value = prediction[0]84 85 # Display the result for the Backpropagation model86 result = "spam" if prediction_value > 0.5 else "ham"87 88 89 elif selected_algorithm == "RNN":90 text_sequence = tokeniser.texts_to_sequences([input_text]) 91 padded_sequence = tf.keras.preprocessing.sequence.pad_sequences(text_sequence, maxlen=10, )92 #Make prediction using rnn_model on padded_sequence93 prediction = rnn_model.predict(padded_sequence)94 95 #Display the result for the RNN model96 result = "spam" if prediction >= 0.3 else "ham"97 98 99 elif selected_algorithm == "DNN":100 101 # Tokenize and pad the input text sequence102 text_sequence = tokeniser.texts_to_sequences([input_text]) 103 padded_sequence = tf.keras.preprocessing.sequence.pad_sequences(text_sequence, maxlen=10)104 105 # Make prediction using dnn_model on padded_sequence106 prediction = dnn_model.predict(padded_sequence)107 108 # Display the result for the DNN model109 result = "spam" if prediction >=0.5 else "ham"110 111 elif selected_algorithm == "LSTM":112 words = 5000113 max_review_length=500114 word_index = imdb.get_word_index()115 input_text = input_text.lower().split()116 input_text = [word_index[word] if word in word_index and word_index[word] < words else 0 for word in input_text]117 input_text = sequence.pad_sequences([input_text], maxlen=max_review_length)118 119 prediction = lstm_model.predict(input_text)120 print("Raw Prediction:", prediction)121 result = "positive" if prediction >= 0.5 else "negative"122 123 124 # Display the result125 st.write(f"Sentiment Analysis Result: {result}")126 127# Display tumor detection result128if task == "Tumor Detection" and selected_algorithm == "CNN" and uploaded_file is not None:129 # Preprocess the image130 image = Image.open(uploaded_file)131 # Perform any necessary preprocessing for your CNN model132 image = image.resize((128, 128)) # Adjust the size based on your model's requirements133 image_array = keras_image.img_to_array(image)134 135 image_array = np.expand_dims(image_array, axis=0) # Add batch dimension136 137 # Make predictions using cnn_model on the preprocessed image138 prediction = cnn_model.predict(image_array)139 140 # Make predictions using cnn_model on the preprocessed image141 result = "Tumor Detected" if prediction > 0.5 else "No Tumor"142 # Display the result as needed143 st.write(f"Tumor Detection Result: {result}")144 145 