Team Ai
Apppublic

anandhu-pk/Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py162 linesDownload Raw Back to root
1import streamlit as st2import pickle3import tensorflow as tf4from tensorflow.keras.preprocessing import sequence5from tensorflow.keras.saving import load_model6import numpy as np7from PIL import Image8 9 10st.title('Classifier System')11task = st.selectbox('Select Task', ['Choose one','Sentiment Classification', 'Tumor Detection'])12 13if task=='Tumor Detection':14    st.subheader('Tumor Detection with CNN')15    # CNN16    #with open(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\CNN\cnn_model.pkl",'rb') as file:17        #cnn_model = pickle.load(file)18    cnn_model = load_model("cnn_model1.h5")19 20    img = st.file_uploader('Upload image', type=['jpeg', 'jpg', 'png'])21 22    def cnn_make_prediction(img,model):23        img=Image.open(img)24        img=img.resize((128,128))25        img=np.array(img)26        input_img = np.expand_dims(img, axis=0)27        res = model.predict(input_img)28        if res:29            return "Tumor Detected"30        else:31            return "No Tumor Detected"32        33    34    if img is not None:35        st.image(img, caption = "Image preview")36        if st.button('Submit'):37            pred = cnn_make_prediction(img, cnn_model)38            st.write(pred)39 40 41if task=='Sentiment Classification':42    arcs = ['Perceptron', 'Backpropagation', 'DNN', 'RNN', 'LSTM']43    arc = st.radio('Pick one:', arcs, horizontal=True)44 45    if arc == arcs[0]:46        # Perceptron47        with open("pnn_model.pkl",'rb') as file:48            perceptron = pickle.load(file)49        with open("pnn_tokeniser.pkl",'rb') as file:50            ppn_tokeniser = pickle.load(file)51 52        def ppn_make_predictions(inp, model):53            encoded_inp = ppn_tokeniser.texts_to_sequences([inp])54            padded_inp = sequence.pad_sequences(encoded_inp, maxlen=500)55            res = model.predict(padded_inp)56            if res:57                return "Not spam"58            else:59                return "Spam"       60        61        st.subheader('SMS spam Classification using Perceptron')62        inp = st.text_area('Enter message')63        if st.button('Check'):64            pred = ppn_make_predictions([inp], perceptron)65            st.write(pred)66 67    elif arc == arcs[1]:68        # BackPropogation69        with open("bpn_model.pkl",'rb') as file:70            backprop = pickle.load(file)71        with open("bpn_tokeniser.pkl",'rb') as file:72            bp_tokeniser = pickle.load(file)73 74        def bp_make_predictions(inp, model):75            encoded_inp = bp_tokeniser.texts_to_sequences([inp])76            padded_inp = sequence.pad_sequences(encoded_inp, maxlen=500)77            res = model.predict(padded_inp)78            if res:79                return "Not spam"80            else:81                return "Spam"        82        83        st.subheader('SMS spam Classification using Backpropagation')84        inp = st.text_area('Enter message')85        if st.button('Check'):86            pred = bp_make_predictions([inp], backprop)87            st.write(pred)88 89 90    elif arc == arcs[2]:91        # DNN92        dnn_model = load_model("dnn_model1.h5")93 94        #with open("dnn_model.pkl",'rb') as file:95            #dnn_model = pickle.load(file)96        with open("dnn_tokeniser.pkl",'rb') as file:97            dnn_tokeniser = pickle.load(file)98 99        def dnn_make_predictions(inp, model):100            inp = dnn_tokeniser.texts_to_sequences(inp)101            inp = sequence.pad_sequences(inp, maxlen=500)102            res = (model.predict(inp) > 0.5).astype("int32")103            if res:104                return "Not spam"105            else:106                return "Spam"       107        108        st.subheader('SMS spam Classification using DNN')109        inp = st.text_area('Enter message')110        if st.button('Check'):111            pred = dnn_make_predictions([inp], dnn_model)112            st.write(pred)113 114 115    elif arc == arcs[3]:116        # RNN 117        #with open("rnn_model.pkl",'rb') as file:118            #rnn_model = pickle.load(file) 119        rnn_model = load_model("rnn_model1.h5")120 121        with open("rnn_tokeniser.pkl",'rb') as file:122            rnn_tokeniser = pickle.load(file)123 124        def rnn_make_predictions(inp, model):125            encoded_inp = rnn_tokeniser.texts_to_sequences(inp)126            padded_inp = sequence.pad_sequences(encoded_inp, maxlen=10, padding='post')127            res = (model.predict(padded_inp) > 0.5).astype("int32")128            if res:129                return "Spam"130            else:131                return "Not spam"132 133        st.subheader('SMS Spam Classification using RNN')134        inp = st.text_area('Enter message')135        if st.button('Check'):136            pred = rnn_make_predictions([inp], rnn_model)137            st.write(pred)138    139 140    elif arc == arcs[4]:141        # LSTM142        #with open("lstm_model.pkl",'rb') as file:143            #lstm_model = pickle.load(file) 144        lstm_model = load_model("lstm_model1.h5")145 146        with open("lstm_tokeniser.pkl",'rb') as file:147            lstm_tokeniser = pickle.load(file)148 149        def lstm_make_predictions(inp, model):150            inp = lstm_tokeniser.texts_to_sequences(inp)151            inp = sequence.pad_sequences(inp, maxlen=500)152            res = (model.predict(inp) > 0.5).astype("int32")153            if res:154                return "Not spam"155            else:156                return "Spam"157 158        st.subheader('SMS spam Classification using LSTM')159        inp = st.text_area('Enter message')160        if st.button('Check'):161            pred = lstm_make_predictions([inp], lstm_model)162            st.write(pred)