Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,44 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
|
4 |
-
from tensorflow.keras.
|
5 |
-
from tensorflow.keras.
|
6 |
-
|
7 |
-
|
8 |
-
# Load the IMDB dataset word index
|
9 |
-
word_index = imdb.get_word_index()
|
10 |
-
reverse_word_index = {value: key for key, value in word_index.items()}
|
11 |
-
|
12 |
-
# Load the pre-trained model with ReLU activation
|
13 |
-
model = load_model('simple_rnn_imdb.h5')
|
14 |
-
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
padded_review
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
#
|
31 |
-
st.
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
# Display the result
|
46 |
-
st.write(f'Sentiment: {sentiment}')
|
47 |
-
st.write(f'Prediction Score: {prediction[0][0]}')
|
48 |
-
else:
|
49 |
-
st.write('Please enter a movie review.')
|
50 |
-
|
|
|
1 |
+
import numpy as np
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow.keras.datasets import imdb
|
4 |
+
from tensorflow.keras.preprocessing import sequence
|
5 |
+
from tensorflow.keras.models import load_model
|
6 |
+
import streamlit as st
|
7 |
+
|
8 |
+
# Load the IMDB dataset word index
|
9 |
+
word_index = imdb.get_word_index()
|
10 |
+
reverse_word_index = {value: key for key, value in word_index.items()}
|
11 |
+
|
12 |
+
# Load the pre-trained model with ReLU activation
|
13 |
+
model = load_model('simple_rnn_imdb.h5')
|
14 |
+
|
15 |
+
# Helper function to decode reviews
|
16 |
+
def decode_review(encoded_review):
|
17 |
+
return ' '.join([reverse_word_index.get(i - 3, '?') for i in encoded_review])
|
18 |
+
|
19 |
+
# Function to preprocess user input
|
20 |
+
def preprocess_text(text, max_word_index=10000, maxlen=500):
|
21 |
+
words = text.lower().split()
|
22 |
+
encoded_review = [min(word_index.get(word, 2) + 3, max_word_index - 1) for word in words]
|
23 |
+
padded_review = sequence.pad_sequences([encoded_review], maxlen=maxlen)
|
24 |
+
return padded_review
|
25 |
+
|
26 |
+
# Streamlit app
|
27 |
+
st.title('IMDB Movie Review Sentiment Analysis')
|
28 |
+
st.write('Enter a movie review to classify it as positive or negative.')
|
29 |
+
|
30 |
+
# User input
|
31 |
+
user_input = st.text_area('Movie Review')
|
32 |
+
|
33 |
+
if st.button('Classify'):
|
34 |
+
preprocessed_input = preprocess_text(user_input)
|
35 |
+
|
36 |
+
# Make prediction
|
37 |
+
prediction = model.predict(preprocessed_input)
|
38 |
+
sentiment = 'Positive' if prediction[0][0] > 0.5 else 'Negative'
|
39 |
+
|
40 |
+
# Display the result
|
41 |
+
st.write(f'Sentiment: {sentiment}')
|
42 |
+
st.write(f'Prediction Score: {prediction[0][0]}')
|
43 |
+
else:
|
44 |
+
st.write('Please enter a movie review.')
|
|
|
|
|
|
|
|
|
|
|
|