File size: 3,935 Bytes
b340dfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
from PIL import Image
from tensorflow.keras.models import load_model
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.sequence import pad_sequences
import pickle

# Load PERCEPTRON model
def load_perceptron_model():
    with open('PP_model.pkl', 'rb') as file:
        model = pickle.load(file)
    return model
# Load BACKPROPAGATION model
def load_backpropagation_model():
    with open('BP_model.pkl', 'rb') as file:
        model = pickle.load(file)
    return model

# Load DNN model
def load_dnn_model():
    model_path = 'DNN_model.h5'
    model = load_model(model_path)
    return model

# Load RNN model
def load_rnn_model():
    model_path = 'RNN_model.h5'
    model = load_model(model_path)
    return model

# Load LSTM model
def load_lstm_model():
    model_path = 'LSTM_model.h5'
    model = load_model(model_path)
    return model

# Load CNN model
def load_cnn_model():
    model_path = 'CNN_model.h5'
    model = load_model(model_path)
    return model

# Load word index for Sentiment Classification
word_to_index = imdb.get_word_index()

# Function to perform sentiment classification
def sentiment_classification(new_review_text, model):
    max_review_length = 500
    new_review_tokens = [word_to_index.get(word, 0) for word in new_review_text.split()]
    new_review_tokens = pad_sequences([new_review_tokens], maxlen=max_review_length)
    prediction = model.predict(new_review_tokens)
    if type(prediction) == list:
        prediction = prediction[0]
    return "Positive" if prediction > 0.5 else "Negative"

# Function to perform tumor detection
def tumor_detection(img, model):
    img = Image.open(img)
    img=img.resize((128,128))
    img=np.array(img)
    input_img = np.expand_dims(img, axis=0)
    res = model.predict(input_img)
    return "Tumor Detected" if res else "No Tumor"

# Streamlit App
st.title("Sentimental Analysis and Tumor Detection")

# Choose between tasks
task = st.radio("Select Task", ("Sentiment Classification", "Tumor Detection"))

if task == "Sentiment Classification":
    # Input box for new review
    new_review_text = st.text_area("Enter a New Review:", value="")
    if st.button("Submit") and not new_review_text.strip():
        st.warning("Please enter a review.")

    if new_review_text.strip():
        st.subheader("Choose Model for Sentiment Classification")
        model_option = st.selectbox("Select Model", ("Perceptron", "Backpropagation", "DNN", "RNN", "LSTM"))

                # Load models dynamically based on the selected option
        loaded_model = None  # Initialize loaded_model variable
        if model_option == "Perceptron":
            loaded_model = load_perceptron_model()
        elif model_option == "BackPropagation":
            loaded_model = load_backpropagation_model()
        elif model_option == "DNN":
            loaded_model = load_dnn_model()
        elif model_option == "RNN":
            loaded_model = load_rnn_model()
        elif model_option == "LSTM":
            loaded_model = load_lstm_model()

        if loaded_model is not None:
            if st.button("Classify Sentiment"):
                result = sentiment_classification(new_review_text, loaded_model)
                st.subheader("Sentiment Classification Result")
                st.write(f"**{result}**")


elif task == "Tumor Detection":
    st.subheader("Tumor Detection")
    uploaded_file = st.file_uploader("Choose a tumor image...", type=["jpg", "jpeg", "png"])

    if uploaded_file is not None:
        # Load the tumor detection model
        model = load_cnn_model()
        st.image(uploaded_file, caption="Uploaded Image.", use_column_width=False, width=200)
        st.write("")

        if st.button("Detect Tumor"):
            result = tumor_detection(uploaded_file, model)
            st.subheader("Tumor Detection Result")
            st.write(f"**{result}**")