CoolFace
Apppublic

anamikau/Neural_Network

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py154 linesDownload Raw Back to root
1import pandas as pd2import streamlit as st3import numpy as np4import tensorflow as tf5from PIL import Image6import pickle7 8 9 10st.header('Neural Networks Demo')11task = st.selectbox('Select Task', ["Select One",'Sentiment Classification', 'Tumor Detection'])12 13 14if task == "Tumor Detection":15        def cnn(img, model):16            img = Image.open(img)17            img = img.resize((128, 128))18            img = np.array(img)19            input_img = np.expand_dims(img, axis=0)20            res = model.predict(input_img)21            if res:22                return "Tumor Detected"23            else:24                return "No Tumor" 25            26        cnn_model = tf.keras.models.load_model("tumor_detection_model.h5")27        uploaded_file = st.file_uploader("Choose a file", type=["jpg", "jpeg", "png"])28        if uploaded_file is not None:29            st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)30            if st.button("Submit"):31                result=cnn(uploaded_file, cnn_model)32                st.write(result)33 34        35elif task == "Sentiment Classification":36        types = ["Perceptron","BackPropagation", "RNN","DNN", "LSTM"]37        input_text2 = st.radio("Select", types, horizontal=True)38 39        if input_text2 == "Perceptron":40                with open("ppn_model.pkl",'rb') as file:41                    perceptron = pickle.load(file)42                with open("ppn_tokeniser.pkl",'rb') as file:43                    ppn_tokeniser = pickle.load(file)44 45                def ppn_make_predictions(inp, model):46                    encoded_inp = ppn_tokeniser.texts_to_sequences([inp])47                    padded_inp = tf.keras.preprocessing.sequence.pad_sequences(encoded_inp, maxlen=500)48                    res = model.predict(padded_inp)49                    if res:50                        return "Negative"51                    else:52                        return "Positive"       53                54                st.subheader('Movie Review Classification using Perceptron')55                inp = st.text_area('Enter message')56                if st.button('Check'):57                    pred = ppn_make_predictions([inp], perceptron)58                    st.write(pred)59 60        if input_text2 == "BackPropagation":61                with open("bp_model.pkl",'rb') as file:62                    backprop = pickle.load(file)63                with open("bp_tokeniser.pkl",'rb') as file:64                    bp_tokeniser = pickle.load(file)65 66                def bp_make_predictions(inp, model):67                    encoded_inp = bp_tokeniser.texts_to_sequences([inp])68                    padded_inp = tf.keras.preprocessing.sequence.pad_sequences(encoded_inp, maxlen=500)69                    res = model.predict(padded_inp)70                    if res:71                        return "Negative"72                    else:73                        return "Positive"     74                       75                st.subheader('Movie Review Classification using BackPropagation')76                inp = st.text_area('Enter message')77                if st.button('Check'):78                    pred = bp_make_predictions([inp], backprop)79                    st.write(pred)80        81 82        elif input_text2 == "RNN":83                rnn_model=tf.keras.models.load_model("spam_model.h5")84                with open("spam_tokeniser.pkl", 'rb') as model_file:85                    rnn_tokeniser=pickle.load(model_file)86 87                def rnn_make_predictions(inp, model):88                    encoded_inp = rnn_tokeniser.texts_to_sequences(inp)89                    padded_inp = tf.keras.preprocessing.sequence.pad_sequences(encoded_inp, maxlen=10, padding='post')90                    res = (model.predict(padded_inp) > 0.5).astype("int32")91                    if res:92                        return "Spam"93                    else:94                        return "Ham"95 96                st.subheader('Spam message Classification using RNN')97                input = st.text_area("Give message")98                if st.button('Check'):99                    pred = rnn_make_predictions([input], rnn_model)100                    st.write(pred)101 102 103 104        elif input_text2 == "DNN":105                        dnn_model=tf.keras.models.load_model("dnn_model.h5")106                        with open("dnn_tokeniser.pkl",'rb') as file:107                            dnn_tokeniser = pickle.load(file)108 109                        def dnn_make_predictions(inp, model):110                            inp = dnn_tokeniser.texts_to_sequences(inp)111                            inp = tf.keras.preprocessing.sequence.pad_sequences(inp, maxlen=500)112                            res = (model.predict(inp) > 0.5).astype("int32")113                            if res:114                                return "Negative"115                            else:116                                return "Positive"       117                        118                        st.subheader('Movie Review Classification using DNN')119                        inp = st.text_area('Enter message')120                        if st.button('Check'):121                            pred = dnn_make_predictions([inp], dnn_model)122                            st.write(pred)123 124                            125 126        elif input_text2 == "LSTM":127                lstm_model=tf.keras.models.load_model("lstm_model.h5") 128 129                with open("lstm_tokeniser.pkl",'rb') as file:130                    lstm_tokeniser = pickle.load(file)131 132                def lstm_make_predictions(inp, model):133                    inp = lstm_tokeniser.texts_to_sequences(inp)134                    inp = tf.keras.preprocessing.sequence.pad_sequences(inp, maxlen=500)135                    res = (model.predict(inp) > 0.5).astype("int32")136                    if res:137                        return "Negative"138                    else:139                        return "Positive"140                st.subheader('Movie Review Classification using LSTM')141                inp = st.text_area('Enter message')142                if st.button('Check'):143                    pred = lstm_make_predictions([inp], lstm_model)144                    st.write(pred)  145 146 147 148 149                150 151 152 153 154