Nabeelp/Image_Text_Classifier
0
1import streamlit as st2import numpy as np3from PIL import Image4import tensorflow5from tensorflow.keras.models import load_model6from tensorflow.keras.datasets import imdb7from tensorflow.keras.preprocessing.sequence import pad_sequences8import pickle9import sys10 11sys.path.append(r"E:\np\IMPORTANT\Stremlit(DL)\perceptron")12sys.path.append(r"E:\np\IMPORTANT\Stremlit(DL)\Back_propagation")13 14# Load word index for Sentiment Classification15word_to_index = imdb.get_word_index()16 17# Function to perform sentiment classification18def sentiment_classification(new_review_text, model):19 max_review_length = 50020 new_review_tokens = [word_to_index.get(word, 0) for word in new_review_text.split()]21 new_review_tokens = pad_sequences([new_review_tokens], maxlen=max_review_length)22 prediction = model.predict(new_review_tokens)23 if type(prediction) == list:24 prediction = prediction[0]25 return "Positive" if prediction > 0.5 else "Negative"26 27# Function to perform tumor detection28def tumor_detection(img, model):29 img = Image.open(img)30 img=img.resize((128,128))31 img=np.array(img)32 input_img = np.expand_dims(img, axis=0)33 res = model.predict(input_img)34 return "Tumor Detected" if res else "No Tumor"35 36# Streamlit App37st.title("Deep Prediction Hub")38 39# Choose between tasks40task = st.radio("Select Task", ("Sentiment Classification", "Tumor Detection"))41 42if task == "Sentiment Classification":43 # Input box for new review44 new_review_text = st.text_area("Enter a New Review:", value="")45 if st.button("Submit") and not new_review_text.strip():46 st.warning("Please enter a review.")47 48 if new_review_text.strip():49 st.subheader("Choose Model for Sentiment Classification")50 model_option = st.selectbox("Select Model", ("Perceptron", "Backpropagation", "DNN", "RNN", "LSTM"))51 52 # Load models dynamically based on the selected option53 if model_option == "Perceptron":54 with open('imdb_perceptron.pkl', 'rb') as file:55 model = pickle.load(file)56 elif model_option == "Backpropagation":57 with open('imdb_back_prop.pkl', 'rb') as file:58 model = pickle.load(file)59 elif model_option == "DNN":60 model = load_model('imdb_DNN.h5')61 elif model_option == "RNN":62 model = load_model('imdb_RNN.h5')63 elif model_option == "LSTM":64 model = load_model('lstm_model.h5')65 66 if st.button("Classify Sentiment"):67 result = sentiment_classification(new_review_text, model)68 st.subheader("Sentiment Classification Result")69 st.write(f"**{result}**")70 71elif task == "Tumor Detection":72 st.subheader("Tumor Detection")73 uploaded_file = st.file_uploader("Choose a tumor image...", type=["jpg", "jpeg", "png"])74 75 if uploaded_file is not None:76 # Load the tumor detection model77 model = load_model('CNN_.h5')78 st.image(uploaded_file, caption="Uploaded Image.", use_column_width=False, width=200)79 st.write("")80 81 if st.button("Detect Tumor"):82 result = tumor_detection(uploaded_file, model)83 st.subheader("Tumor Detection Result")84 st.write(f"**{result}**")