-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
160 lines (141 loc) Β· 4.44 KB
/
app.py
File metadata and controls
160 lines (141 loc) Β· 4.44 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import streamlit as st
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing import sequence
import pickle
# Streamlit Page Config
st.set_page_config(
page_title="π¬ Sentiment Analyzer",
page_icon="π",
layout="centered"
)
# Load Custom CSS
def local_css():
st.markdown("""
<style>
.main-header {
color: #2c3e50;
text-align: center;
font-size: 2.5rem;
font-weight: bold;
margin-bottom: 20px;
background: linear-gradient(to right, #ff5733, #ffbd69);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.subheader {
color: #34495e;
text-align: center;
margin-bottom: 20px;
}
.stTextArea>div>div>textarea {
width: 100%;
height: 150px;
border-radius: 8px;
border: 2px solid #ff5733;
padding: 12px;
font-size: 1rem;
background-color: #f7f9fc;
color: #2c3e50;
caret-color: #ff5733; /* Ensures cursor is visible */
outline: none; /* Removes default focus outline */
}
.stTextArea>div>div>textarea::placeholder {
color: #95a5a6;
font-style: italic;
}
}
.stButton>button {
background-color: #ff5733;
color: white;
border: none;
padding: 12px 20px;
border-radius: 5px;
font-size: 1rem;
transition: all 0.3s ease;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
}
.stButton>button:hover {
background-color: #c0392b;
transform: scale(1.05);
box-shadow: 0px 6px 12px rgba(0, 0, 0, 0.15);
}
.result-box {
background-color: #f7f9fc;
border-radius: 12px;
padding: 20px;
border: 1px solid #e0e0e0;
margin-top: 20px;
text-align: center;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
}
.emoji {
font-size: 3rem;
text-align: center;
margin-top: 10px;
}
</style>
""", unsafe_allow_html=True)
# Load the Tokenizer
try:
with open('tokenizer.pkl', 'rb') as f:
word_index = pickle.load(f)
except Exception as e:
st.error(f"Error loading tokenizer: {e}")
st.stop()
# Load Model
try:
model = tf.keras.models.load_model('SimpleRnn_imdb.h5')
except Exception as e:
st.error(f"Error loading model: {e}")
st.stop()
# Function to preprocess text
def preprocess_text(text):
words = text.lower().split()
encoded_review = [word_index.get(word, 2) + 3 for word in words]
padded_review = sequence.pad_sequences([encoded_review], maxlen=500)
return padded_review
# Prediction Function
def predict_sentiment(review):
preprocessed_input = preprocess_text(review)
prediction = model.predict(preprocessed_input)[0][0]
if prediction >= 0.6:
sentiment = 'Positive'
emoji = "π"
elif prediction <= 0.4:
sentiment = 'Negative'
emoji = "π "
else:
sentiment = 'Neutral'
emoji = "π"
return sentiment, float(prediction), emoji
# Streamlit UI
def main():
local_css()
st.markdown('<div class="main-header">π¬ Sentiment Analyzer</div>', unsafe_allow_html=True)
st.markdown('<div class="subheader">Analyze the sentiment of your movie review!</div>', unsafe_allow_html=True)
# Improved Styled Text Area
st.markdown('<p style="font-size:18px; font-weight:bold; color:#2c3e50;">π Write Your Movie Review Below:</p>', unsafe_allow_html=True)
user_input = st.text_area(
"",
placeholder="Type your thoughts about the movie here...",
height=150,
max_chars=500,
key="review_input"
)
if st.button("Analyze Sentiment"):
if user_input:
sentiment, score, emoji = predict_sentiment(user_input)
# Display Results
st.markdown('<div class="result-box">', unsafe_allow_html=True)
st.subheader(f"π― Sentiment: {sentiment} {emoji}")
st.write(f"**Confidence Score:** `{score:.4f}`")
# Display Progress Bar
progress_val = score if sentiment == "Positive" else 1 - score
st.progress(progress_val)
st.markdown('</div>', unsafe_allow_html=True)
else:
st.warning("β οΈ Please enter a review before analyzing.")
# Run the App
if __name__ == "__main__":
main()