| import streamlit as st |
| import numpy as np |
| from PIL import Image |
| from tensorflow.keras.models import load_model |
| import joblib |
| from tensorflow.keras.preprocessing.text import Tokenizer |
| from tensorflow.keras.preprocessing.sequence import pad_sequences |
| from tensorflow.keras.applications.inception_v3 import preprocess_input |
| import cv2 |
| from BackPropogation import BackPropogation |
| from Perceptron import Perceptron |
| from sklearn.linear_model import Perceptron |
| import tensorflow as tf |
| import joblib |
| import pickle |
| from numpy import argmax |
|
|
|
|
| |
| with open(r'tokeniser.pkl', 'rb') as handle: |
| loaded_tokenizer = pickle.load(handle) |
|
|
| |
| image_model = load_model('tumor_detection_model.h5') |
| dnn_model = load_model('imdb_model.h5') |
| loaded_model = tf.keras.models.load_model('sms_spam_detection_dnnmodel.h5') |
| perceptron_model = joblib.load('perceptron_model.joblib') |
| backprop_model = joblib.load('backprop_model.pkl') |
|
|
|
|
| |
| st.title("Classification") |
|
|
| |
| task = st.sidebar.selectbox("Select Task", ["Tumor Detection", "Sentiment Classification"]) |
|
|
| def preprocess_text(text): |
| tokenizer = Tokenizer() |
| tokenizer.fit_on_texts([text]) |
| sequences = tokenizer.texts_to_sequences([text]) |
| preprocessed_text = pad_sequences(sequences, maxlen=4) |
|
|
| return preprocessed_text |
|
|
|
|
|
|
| def predict_dnn(preprocessed_text): |
| preprocessed_text = preprocessed_text.reshape((1, 4)) |
|
|
| prediction = dnn_model.predict(preprocessed_text) |
| st.write("DNN Prediction:", prediction) |
| |
| |
| |
| def predict_rnn(input_text): |
| |
| encoded_input = loaded_tokenizer.texts_to_sequences([input_text]) |
| padded_input = tf.keras.preprocessing.sequence.pad_sequences(encoded_input, maxlen=10, padding='post') |
| prediction = loaded_model.predict(padded_input) |
| if prediction > 0.5: |
| return "spam" |
| else: |
| return "ham" |
|
|
|
|
| def predict_custom_perceptron(preprocessed_text): |
| perceptron = CustomPerceptron(epochs=10) |
| prediction = perceptron.predict(preprocessed_text) |
| st.write("Custom Perceptron Prediction:", prediction) |
| |
| def predict_sklearn_perceptron(preprocessed_text): |
| perceptron = SklearnPerceptron() |
| prediction = perceptron.predict(preprocessed_text) |
| st.write("Sklearn Perceptron Prediction:", prediction) |
| |
| def predict_backpropagation(preprocessed_text): |
| prediction = backprop_model.predict(preprocessed_text) |
| st.write("Backpropagation Prediction:", prediction) |
|
|
| |
| def preprocess_image(image): |
| image = image.resize((299, 299)) |
| image_array = np.array(image) |
| preprocessed_image = preprocess_input(image_array) |
|
|
| return preprocessed_image |
|
|
|
|
| def make_prediction_cnn(image, image_model): |
| img = image.resize((128, 128)) |
| img_array = np.array(img) |
| img_array = img_array.reshape((1, img_array.shape[0], img_array.shape[1], img_array.shape[2])) |
|
|
| preprocessed_image = preprocess_input(img_array) |
| prediction = image_model.predict(preprocessed_image) |
|
|
| if prediction > 0.5: |
| st.write("Tumor Detected") |
| else: |
| st.write("No Tumor") |
|
|
| if task == "Sentiment Classification": |
| st.subheader("Choose Model") |
| model_choice = st.radio("Select Model", ["DNN", "RNN", "Perceptron", "Backpropagation"]) |
|
|
| st.subheader("Text Input") |
| text_input = st.text_area("Enter Text") |
|
|
| if st.button("Predict"): |
| |
| preprocessed_text = preprocess_text(text_input) |
| if model_choice == "DNN": |
| predict_dnn(preprocessed_text) |
| elif model_choice == "RNN": |
| if text_input: |
| prediction_result = predict_rnn(text_input) |
| st.write(f"The message is classified as: {prediction_result}") |
| else: |
| st.write("Please enter some text for prediction") |
| elif model_choice == "Custom Perceptron": |
| predict_custom_perceptron(preprocessed_text) |
| elif model_choice == "Sklearn Perceptron": |
| predict_sklearn_perceptron(preprocessed_text) |
| elif model_choice == "Backpropagation": |
| predict_backpropagation(preprocessed_text) |
|
|
| else: |
| st.subheader("Choose Model") |
| model_choice = st.radio("Select Model", ["CNN"]) |
|
|
| st.subheader("Image Input") |
| image_input = st.file_uploader("Choose an image...", type="jpg") |
|
|
| if image_input is not None: |
| image = Image.open(image_input) |
| st.image(image, caption="Uploaded Image.", use_column_width=True) |
|
|
| |
| preprocessed_image = preprocess_image(image) |
|
|
| if st.button("Predict"): |
| if model_choice == "CNN": |
| make_prediction_cnn(image, image_model) |