CoolFace
Apppublic

Sreevidya25/Deep_Learning

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.py76 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 pickle8 9# Load word index for Sentiment Classification10word_to_index = imdb.get_word_index()11 12# Function to perform sentiment classification13def sentiment_classification(new_review_text, model):14    max_review_length = 50015    new_review_tokens = [word_to_index.get(word, 0) for word in new_review_text.split()]16    new_review_tokens = pad_sequences([new_review_tokens], maxlen=max_review_length)17    prediction = model.predict(new_review_tokens)18    if type(prediction) == list:19        prediction = prediction[0]20    return "Positive" if prediction > 0.5 else "Negative"21 22# Function to perform tumor detection23def tumor_detection(img, model):24    img = Image.open(img)25    img=img.resize((128,128))26    img=np.array(img)27    input_img = np.expand_dims(img, axis=0)28    res = model.predict(input_img)29    return "Tumor Detected" if res else "No Tumor"30 31st.title("Deep learning algorithms")32option = st.selectbox("Choose any classification task",("Movie review classification","Tumor classification"))33 34if option == "Movie review classification":35    # Input box for new review36    new_review_text = st.text_area("Enter a New Review:", value="")37    if st.button("Submit") and not new_review_text.strip():38        st.warning("Please enter a review.")39 40    if new_review_text.strip():41        st.subheader("Choose a Model for Classification")42        model_option = st.radio("Select Model", ("Perceptron", "Backpropagation", "DNN", "RNN", "LSTM"))43 44        # Load models dynamically based on the selected option45        if model_option == "Perceptron":46            with open('PERCEP_MODEL.pkl', 'rb') as file:47                model = pickle.load(file)48        elif model_option == "Backpropagation":49            with open('Back_Prop.pkl', 'rb') as file:50                model = pickle.load(file)51        elif model_option == "DNN":52            model = load_model('DNN_MODEL.keras')53        elif model_option == "RNN":54            model = load_model('RNN_MODEL.keras')55        elif model_option == "LSTM":56            model = load_model('LSTM_MODEL.keras')57 58        if st.button("Classify Sentiment"):59            result = sentiment_classification(new_review_text, model)60            61            st.write(f"The text is {result} ")62 63elif option == "Tumor classification":64    st.subheader("Tumor Detection")65    uploaded_file = st.file_uploader("Choose a tumor image...", type=["jpg", "jpeg", "png"])66 67    if uploaded_file is not None:68        # Load the tumor detection model69        model = load_model('CNN.keras')70        st.image(uploaded_file, caption="Uploaded Image.", use_column_width=False, width=200)71        st.write("")72 73        if st.button("Check for Tumor"):74            result = tumor_detection(uploaded_file, model)75            st.write(f" {result}**")76