File size: 5,935 Bytes
7967a16
 
 
 
 
 
 
 
 
 
 
f256088
7967a16
 
 
 
 
 
 
 
 
 
 
 
 
c72100d
 
 
7967a16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c72100d
 
a9512f0
d421498
 
 
 
 
 
7967a16
d421498
7967a16
d421498
 
 
 
7967a16
 
 
 
 
 
 
 
c72100d
 
 
7967a16
 
 
 
 
 
 
 
 
 
 
c72100d
7967a16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import streamlit as st
import pickle
import tensorflow as tf
from PIL import Image
import numpy as np
import cv2
import pandas as pd
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.text import Tokenizer
from BackPropogation import BackPropogation
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import save_model,load_model




def cnn_tumor(img):
    img=Image.open(img)
    img=np.array(img)
    img=cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
    img_array = cv2.medianBlur(img, 5)
    img = Image.fromarray(cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB))
    img=img.resize((128,128))
    input_img = np.expand_dims(img, axis=0)
    st.image(input_img, caption='Processed Image', use_column_width=True)
    loaded_model = load_model('cnn_model.h5')

    predictions = loaded_model.predict(input_img)    
    if predictions:
        st.write("Tumor Detected")
    else:
        st.write("No Tumor")

def perceptron():
    with open('imdb_perceptron.pkl', 'rb') as file:
        loaded_model = pickle.load(file)
    top_words = 5000
    (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words)
    word_index = imdb.get_word_index()
    reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])

    user_input = st.text_input("Enter your movie review:")

    if st.button("Predict"):

        if user_input:
            st.write("Review:", user_input)

            user_input_sequence = [word_index.get(word, 0) for word in user_input.split()]
            processed_input = tf.keras.preprocessing.sequence.pad_sequences([user_input_sequence], maxlen=500, padding='post', truncating='post')

            prediction = loaded_model.predict(processed_input)

            sentiment = 'Positive' if prediction[0] > 0.5 else 'Negative'
            st.write("Predicted Sentiment:", sentiment)
        else:
            st.warning("Please enter a movie review.")

def backprop():
    with open('BackP.pkl', 'rb') as file:
        loaded_model = pickle.load(file)
    top_words = 5000
    (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words)

    word_index = imdb.get_word_index()
    reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])

    user_input = st.text_input("Enter your movie review:")

    if st.button("Predict"):

        if user_input:
            st.write("Review:", user_input)

            user_input_sequence = [word_index.get(word, 0) for word in user_input.split()]
            processed_input = tf.keras.preprocessing.sequence.pad_sequences([user_input_sequence], maxlen=500, padding='post', truncating='post')

            prediction = loaded_model.predict(processed_input)

            sentiment = 'Positive' if prediction[0] > 0.5 else 'Negative'
            st.write("Predicted Sentiment:", sentiment)
        else:
            st.warning("Please enter a movie review.")

def rnn_model():
    loaded_model = load_model('rnn_model.h5')

    user_input_sequence = st.text_area("Enter your text message:")
    if st.button('Predict'):
        if user_input_sequence:
            tokenizer = Tokenizer(num_words=5000)
            tokenizer.fit_on_texts([user_input_sequence])
            sequences = tokenizer.texts_to_sequences([user_input_sequence])
            processed_input = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=10, padding='post', truncating='post')
        
            prediction = loaded_model.predict(np.array(processed_input))

            is_spam = 'Spam' if prediction[0] > 0.5 else 'Not Spam'
            st.write("Predicted Label:", is_spam)
    else:
        st.warning("Please enter a text message.")
                

def lstm_model():
    user_input = st.text_input('Enter a sentence:', 'I love this movie!')
    tokenizer = Tokenizer(num_words=10000, oov_token='<OOV>')
    tokenizer.fit_on_texts([user_input])
    sequences = tokenizer.texts_to_sequences([user_input])
    padded_sequence = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=500, padding='post', truncating='post')

    loaded_model = load_model('lstm_model.h5')

    if st.button('Predict Sentiment'):
            prediction = loaded_model.predict(np.array(padded_sequence))
            sentiment = 'Positive' if prediction > 0.5 else 'Negative'
            st.success(f'Sentiment: {sentiment}, Confidence: {prediction[0][0]:.4f}')  

def dnn_model():
    user_input = st.text_input('Enter a sentence:', 'I love this movie!')
    tokenizer = Tokenizer(num_words=10000, oov_token='<OOV>')
    tokenizer.fit_on_texts([user_input])
    sequences = tokenizer.texts_to_sequences([user_input])
    padded_sequence = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=500, padding='post', truncating='post')
    loaded_model = load_model('dnn_model.h5')
    if st.button('Predict Sentiment'):
            prediction = loaded_model.predict(np.array(padded_sequence))
            sentiment = 'Positive' if prediction > 0.5 else 'Negative'
            st.success(f'Sentiment: {sentiment}, Confidence: {prediction[0][0]:.4f}') 



st.title('Model Prediction')
option = st.selectbox("Choose One",['Tumor Detection','Sentiment Classification'])

if option=='Tumor Detection':
    st.title('CNN Tumor Detection Model')
    img=st.file_uploader("Upload your file here...", type=['png', 'jpeg', 'jpg'])
    cnn_tumor(img)
else:
    opt=st.radio("Select your prediction",key="visibility",options=["Perceptron",'Backpropogation','DNN','RNN','LSTM'])
    if opt=="Perceptron":
        st.title('Perceptron Model')
        perceptron()
    elif opt=="Backpropogation":
        st.title('Backpropogation Model')
        backprop()
    elif opt=='RNN':
        st.title('RNN Spam Detection')
        rnn_model()
    elif opt=='LSTM':
        st.title('LSTM Model')
        lstm_model()       
    else:
        st.title('DNN Model')
        dnn_model()