Spaces:
Runtime error
Runtime error
Commit
·
b340dfe
1
Parent(s):
2b52c26
Upload 2 files
Browse files- app.py +115 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
from tensorflow.keras.datasets import imdb
|
| 6 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 7 |
+
import pickle
|
| 8 |
+
|
| 9 |
+
# Load PERCEPTRON model
|
| 10 |
+
def load_perceptron_model():
|
| 11 |
+
with open('PP_model.pkl', 'rb') as file:
|
| 12 |
+
model = pickle.load(file)
|
| 13 |
+
return model
|
| 14 |
+
# Load BACKPROPAGATION model
|
| 15 |
+
def load_backpropagation_model():
|
| 16 |
+
with open('BP_model.pkl', 'rb') as file:
|
| 17 |
+
model = pickle.load(file)
|
| 18 |
+
return model
|
| 19 |
+
|
| 20 |
+
# Load DNN model
|
| 21 |
+
def load_dnn_model():
|
| 22 |
+
model_path = 'DNN_model.h5'
|
| 23 |
+
model = load_model(model_path)
|
| 24 |
+
return model
|
| 25 |
+
|
| 26 |
+
# Load RNN model
|
| 27 |
+
def load_rnn_model():
|
| 28 |
+
model_path = 'RNN_model.h5'
|
| 29 |
+
model = load_model(model_path)
|
| 30 |
+
return model
|
| 31 |
+
|
| 32 |
+
# Load LSTM model
|
| 33 |
+
def load_lstm_model():
|
| 34 |
+
model_path = 'LSTM_model.h5'
|
| 35 |
+
model = load_model(model_path)
|
| 36 |
+
return model
|
| 37 |
+
|
| 38 |
+
# Load CNN model
|
| 39 |
+
def load_cnn_model():
|
| 40 |
+
model_path = 'CNN_model.h5'
|
| 41 |
+
model = load_model(model_path)
|
| 42 |
+
return model
|
| 43 |
+
|
| 44 |
+
# Load word index for Sentiment Classification
|
| 45 |
+
word_to_index = imdb.get_word_index()
|
| 46 |
+
|
| 47 |
+
# Function to perform sentiment classification
|
| 48 |
+
def sentiment_classification(new_review_text, model):
|
| 49 |
+
max_review_length = 500
|
| 50 |
+
new_review_tokens = [word_to_index.get(word, 0) for word in new_review_text.split()]
|
| 51 |
+
new_review_tokens = pad_sequences([new_review_tokens], maxlen=max_review_length)
|
| 52 |
+
prediction = model.predict(new_review_tokens)
|
| 53 |
+
if type(prediction) == list:
|
| 54 |
+
prediction = prediction[0]
|
| 55 |
+
return "Positive" if prediction > 0.5 else "Negative"
|
| 56 |
+
|
| 57 |
+
# Function to perform tumor detection
|
| 58 |
+
def tumor_detection(img, model):
|
| 59 |
+
img = Image.open(img)
|
| 60 |
+
img=img.resize((128,128))
|
| 61 |
+
img=np.array(img)
|
| 62 |
+
input_img = np.expand_dims(img, axis=0)
|
| 63 |
+
res = model.predict(input_img)
|
| 64 |
+
return "Tumor Detected" if res else "No Tumor"
|
| 65 |
+
|
| 66 |
+
# Streamlit App
|
| 67 |
+
st.title("Sentimental Analysis and Tumor Detection")
|
| 68 |
+
|
| 69 |
+
# Choose between tasks
|
| 70 |
+
task = st.radio("Select Task", ("Sentiment Classification", "Tumor Detection"))
|
| 71 |
+
|
| 72 |
+
if task == "Sentiment Classification":
|
| 73 |
+
# Input box for new review
|
| 74 |
+
new_review_text = st.text_area("Enter a New Review:", value="")
|
| 75 |
+
if st.button("Submit") and not new_review_text.strip():
|
| 76 |
+
st.warning("Please enter a review.")
|
| 77 |
+
|
| 78 |
+
if new_review_text.strip():
|
| 79 |
+
st.subheader("Choose Model for Sentiment Classification")
|
| 80 |
+
model_option = st.selectbox("Select Model", ("Perceptron", "Backpropagation", "DNN", "RNN", "LSTM"))
|
| 81 |
+
|
| 82 |
+
# Load models dynamically based on the selected option
|
| 83 |
+
loaded_model = None # Initialize loaded_model variable
|
| 84 |
+
if model_option == "Perceptron":
|
| 85 |
+
loaded_model = load_perceptron_model()
|
| 86 |
+
elif model_option == "BackPropagation":
|
| 87 |
+
loaded_model = load_backpropagation_model()
|
| 88 |
+
elif model_option == "DNN":
|
| 89 |
+
loaded_model = load_dnn_model()
|
| 90 |
+
elif model_option == "RNN":
|
| 91 |
+
loaded_model = load_rnn_model()
|
| 92 |
+
elif model_option == "LSTM":
|
| 93 |
+
loaded_model = load_lstm_model()
|
| 94 |
+
|
| 95 |
+
if loaded_model is not None:
|
| 96 |
+
if st.button("Classify Sentiment"):
|
| 97 |
+
result = sentiment_classification(new_review_text, loaded_model)
|
| 98 |
+
st.subheader("Sentiment Classification Result")
|
| 99 |
+
st.write(f"**{result}**")
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
elif task == "Tumor Detection":
|
| 103 |
+
st.subheader("Tumor Detection")
|
| 104 |
+
uploaded_file = st.file_uploader("Choose a tumor image...", type=["jpg", "jpeg", "png"])
|
| 105 |
+
|
| 106 |
+
if uploaded_file is not None:
|
| 107 |
+
# Load the tumor detection model
|
| 108 |
+
model = load_cnn_model()
|
| 109 |
+
st.image(uploaded_file, caption="Uploaded Image.", use_column_width=False, width=200)
|
| 110 |
+
st.write("")
|
| 111 |
+
|
| 112 |
+
if st.button("Detect Tumor"):
|
| 113 |
+
result = tumor_detection(uploaded_file, model)
|
| 114 |
+
st.subheader("Tumor Detection Result")
|
| 115 |
+
st.write(f"**{result}**")
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
numpy
|
| 3 |
+
Pillow
|
| 4 |
+
tensorflow
|
| 5 |
+
tqdm
|
| 6 |
+
opencv-python
|
| 7 |
+
scikit-learn
|