jarif commited on
Commit
a5999eb
1 Parent(s): c777b50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -50
app.py CHANGED
@@ -1,50 +1,44 @@
1
- # Step 1: Import Libraries and Load the Model
2
- import numpy as np
3
- import tensorflow as tf
4
- from tensorflow.keras.datasets import imdb
5
- from tensorflow.keras.preprocessing import sequence
6
- from tensorflow.keras.models import load_model
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
- # Step 2: Helper Functions
16
- # Function to decode reviews
17
- def decode_review(encoded_review):
18
- return ' '.join([reverse_word_index.get(i - 3, '?') for i in encoded_review])
19
-
20
- # Function to preprocess user input
21
- def preprocess_text(text):
22
- words = text.lower().split()
23
- encoded_review = [word_index.get(word, 2) + 3 for word in words]
24
- padded_review = sequence.pad_sequences([encoded_review], maxlen=500)
25
- return padded_review
26
-
27
-
28
- import streamlit as st
29
- ## streamlit app
30
- # Streamlit app
31
- st.title('IMDB Movie Review Sentiment Analysis')
32
- st.write('Enter a movie review to classify it as positive or negative.')
33
-
34
- # User input
35
- user_input = st.text_area('Movie Review')
36
-
37
- if st.button('Classify'):
38
-
39
- preprocessed_input=preprocess_text(user_input)
40
-
41
- ## MAke prediction
42
- prediction=model.predict(preprocessed_input)
43
- sentiment='Positive' if prediction[0][0] > 0.5 else 'Negative'
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.')