Spaces:
Sleeping
Sleeping
Commit
·
f8dc74c
1
Parent(s):
95d4d6b
Update app.py
Browse files
app.py
CHANGED
@@ -105,6 +105,45 @@ def gru_predict_movie_sentiment(review):
|
|
105 |
return "Negative"
|
106 |
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
# Main function for Streamlit app
|
110 |
def main():
|
@@ -182,7 +221,27 @@ def main():
|
|
182 |
else:
|
183 |
st.write("Please enter a movie review for sentiment analysis")
|
184 |
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
|
188 |
if __name__ == "__main__":
|
|
|
105 |
return "Negative"
|
106 |
|
107 |
|
108 |
+
with open('perceptron_movie_model.pkl', 'rb') as file:
|
109 |
+
perceptron_movie_model = pickle.load(file)
|
110 |
+
|
111 |
+
def predict_movie_sentiment_perceptron(review):
|
112 |
+
max_review_length = 500
|
113 |
+
top_words = 5000
|
114 |
+
word_index = imdb.get_word_index()
|
115 |
+
review = review.lower().split()
|
116 |
+
review = [word_index[word] if (word in word_index and word_index[word] < top_words) else 0 for word in review]
|
117 |
+
review_bin = np.where(np.array(review) > 0, 1, 0)
|
118 |
+
# Padding or truncating the review to match the perceptron's input size
|
119 |
+
review_bin_padded = np.pad(review_bin, (0, max_review_length - len(review_bin)), 'constant')
|
120 |
+
prediction = perceptron_movie_model.predict([review_bin_padded])
|
121 |
+
if prediction[0] == 1:
|
122 |
+
return "Positive"
|
123 |
+
else:
|
124 |
+
return "Negative"
|
125 |
+
|
126 |
+
|
127 |
+
# Load the saved instance of the Perceptron class
|
128 |
+
with open('backprop_movie_model.pkl', 'rb') as file:
|
129 |
+
backprop_movie_model = pickle.load(file)
|
130 |
+
|
131 |
+
def predict_movie_sentiment_backprop(review):
|
132 |
+
max_review_length = 500
|
133 |
+
top_words = 5000
|
134 |
+
word_index = imdb.get_word_index()
|
135 |
+
review = review.lower().split()
|
136 |
+
review = [word_index[word] if (word in word_index and word_index[word] < top_words) else 0 for word in review]
|
137 |
+
review_bin = np.where(np.array(review) > 0, 1, 0)
|
138 |
+
# Padding or truncating the review to match the perceptron's input size
|
139 |
+
review_bin_padded = np.pad(review_bin, (0, max_review_length - len(review_bin)), 'constant')
|
140 |
+
prediction = backprop_movie_model.predict([review_bin_padded])
|
141 |
+
if prediction[0] == 1:
|
142 |
+
return "Positive"
|
143 |
+
else:
|
144 |
+
return "Negative"
|
145 |
+
|
146 |
+
|
147 |
|
148 |
# Main function for Streamlit app
|
149 |
def main():
|
|
|
221 |
else:
|
222 |
st.write("Please enter a movie review for sentiment analysis")
|
223 |
|
224 |
+
elif model == "Perceptron":
|
225 |
+
st.subheader("Movie Sentiment Analysis")
|
226 |
+
user_review = st.text_area("Enter a movie review: ")
|
227 |
+
|
228 |
+
if st.button("Analyze Sentiment"):
|
229 |
+
if user_review:
|
230 |
+
sentiment_result = predict_movie_sentiment_perceptron(user_review)
|
231 |
+
st.write(f"The sentiment of the review is: {sentiment_result}")
|
232 |
+
else:
|
233 |
+
st.write("Please enter a movie review for sentiment analysis")
|
234 |
+
|
235 |
+
elif model == "Backpropagation":
|
236 |
+
st.subheader("Movie Sentiment Analysis")
|
237 |
+
user_review = st.text_area("Enter a movie review: ")
|
238 |
+
|
239 |
+
if st.button("Analyze Sentiment"):
|
240 |
+
if user_review:
|
241 |
+
sentiment_result = predict_movie_sentiment_backprop(user_review)
|
242 |
+
st.write(f"The sentiment of the review is: {sentiment_result}")
|
243 |
+
else:
|
244 |
+
st.write("Please enter a movie review for sentiment analysis")
|
245 |
|
246 |
|
247 |
if __name__ == "__main__":
|