CoolFace
Apppublic

shamil-123/Text_Image_Classification

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py82 linesDownload Raw Back to root
1import streamlit as st2import numpy as np3from PIL import Image4from tensorflow.keras.models import load_model5from tensorflow.keras.datasets import imdb6from tensorflow.keras.preprocessing.sequence import pad_sequences7import pickle8import sys9 10sys.path.append(r"E:\3rd sem\Deep Learning\Streamlit\Perceptron")11sys.path.append(r"E:\3rd sem\Deep Learning\Streamlit\Back_propagatin")12# Load word index for Sentiment Classification13word_to_index = imdb.get_word_index()14 15# Function to perform sentiment classification16def sentiment_classification(new_review_text, model):17    max_review_length = 50018    new_review_tokens = [word_to_index.get(word, 0) for word in new_review_text.split()]19    new_review_tokens = pad_sequences([new_review_tokens], maxlen=max_review_length)20    prediction = model.predict(new_review_tokens)21    if type(prediction) == list:22        prediction = prediction[0]23    return "Positive" if prediction > 0.5 else "Negative"24 25# Function to perform tumor detection26def tumor_detection(img, model):27    img = Image.open(img)28    img=img.resize((128,128))29    img=np.array(img)30    input_img = np.expand_dims(img, axis=0)31    res = model.predict(input_img)32    return "Tumor Detected" if res else "No Tumor"33 34# Streamlit App35st.title("Deep Prediction")36 37# Choose between tasks38task = st.radio("Select Task", ("Sentiment Classification", "Tumor Detection"))39 40if task == "Sentiment Classification":41    # Input box for new review42    new_review_text = st.text_area("Enter a New Review:", value="")43    if st.button("Submit") and not new_review_text.strip():44        st.warning("Please enter a review.")45 46    if new_review_text.strip():47        st.subheader("Choose Model for Sentiment Classification")48        model_option = st.selectbox("Select Model", ("Perceptron", "Backpropagation", "DNN", "RNN", "LSTM"))49 50        # Load models dynamically based on the selected option51        if model_option == "Perceptron":52            with open('imdb_perceptron.pkl', 'rb') as file:53                model = pickle.load(file)54        elif model_option == "Backpropagation":55            with open('imdb_back_prop.pkl', 'rb') as file:56                model = pickle.load(file)57        elif model_option == "DNN":58            model = load_model('imdb_DNN.h5')59        elif model_option == "RNN":60            model = load_model('RNN_imdb.h5')61        elif model_option == "LSTM":62            model = load_model('lstm_model.h5')63 64        if st.button("Classify Sentiment"):65            result = sentiment_classification(new_review_text, model)66            st.subheader("Sentiment Classification Result")67            st.write(f"**{result}**")68 69elif task == "Tumor Detection":70    st.subheader("Tumor Detection")71    uploaded_file = st.file_uploader("Choose a tumor image...", type=["jpg", "jpeg", "png"])72 73    if uploaded_file is not None:74        # Load the tumor detection model75        model = load_model('CNN_imdb.h5')76        st.image(uploaded_file, caption="Uploaded Image.", use_column_width=False, width=200)77        st.write("")78 79        if st.button("Detect Tumor"):80            result = tumor_detection(uploaded_file, model)81            st.subheader("Tumor Detection Result")82            st.write(f"**{result}**")