sarmadhassan commited on
Commit
7988190
Β·
verified Β·
1 Parent(s): d25d261

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -24
app.py CHANGED
@@ -54,29 +54,58 @@ def analyze_sentiment_basic(text):
54
  return "Neutral", positive_count, negative_count
55
 
56
  # --- Streamlit UI ---
57
- st.title("✍️ Basic Python Sentiment Analyzer")
 
58
  st.markdown(
59
  """
60
- This app performs sentiment analysis using only pure Python, based on a simple keyword matching approach.
61
- It's a demonstration of a rule-based system without external NLP models.
 
 
 
 
 
 
 
 
 
62
  """
 
 
 
 
 
63
  )
64
 
65
  # Text input from the user
66
  user_input = st.text_area(
67
- "Enter your text here:",
68
- "This is a good example, but it could be even better.",
69
- height=150
 
70
  )
71
 
72
- # Analyze button
73
- if st.button("Analyze Sentiment"):
 
 
 
 
 
 
 
 
 
 
 
 
74
  if user_input:
75
  sentiment, pos_count, neg_count = analyze_sentiment_basic(user_input)
76
 
77
- st.subheader("Analysis Result:")
 
78
 
79
- # Display result with appropriate styling
80
  if sentiment == "Positive":
81
  st.success(f"**Sentiment:** Positive 😊")
82
  elif sentiment == "Negative":
@@ -84,40 +113,129 @@ if st.button("Analyze Sentiment"):
84
  else:
85
  st.warning(f"**Sentiment:** Neutral 😐")
86
 
87
- st.info(f"Positive keyword matches: {pos_count}")
88
- st.info(f"Negative keyword matches: {neg_count}")
89
 
90
  st.markdown("---")
91
  st.write(f"**Original Text:**")
92
- st.write(f"> {user_input}")
93
  else:
94
  st.warning("Please enter some text to analyze.")
95
 
96
- # Custom CSS for styling
97
  st.markdown(
98
  """
99
  <style>
 
 
 
 
 
 
 
 
100
  .stButton>button {
101
- background-color: #007bff; /* Blue color for the button */
102
  color: white;
103
- padding: 10px 20px;
104
- border-radius: 8px;
105
  border: none;
106
  cursor: pointer;
107
- font-size: 16px;
108
- box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
109
- transition: 0.3s;
 
 
110
  }
111
  .stButton>button:hover {
112
- background-color: #0056b3; /* Darker blue on hover */
113
- box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
 
114
  }
 
 
 
 
 
 
 
 
 
 
 
115
  .stTextArea>div>div>textarea {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  border-radius: 8px;
117
- border: 1px solid #ccc;
118
  padding: 10px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  }
120
  </style>
121
  """,
122
  unsafe_allow_html=True
123
- )
 
 
 
 
 
 
 
 
 
 
 
54
  return "Neutral", positive_count, negative_count
55
 
56
  # --- Streamlit UI ---
57
+
58
+ # Header Section
59
  st.markdown(
60
  """
61
+ <div style="text-align: center; padding: 20px; background-color: #f0f2f6; border-radius: 10px; margin-bottom: 30px;">
62
+ <h1 style="color: #333; font-size: 2.5em;">✍️ Sentiment Analyzer</h1>
63
+ <p style="color: #555; font-size: 1.1em;">
64
+ Discover the sentiment of your text with a simple keyword-based analysis.
65
+ </p>
66
+ </div>
67
+ """,
68
+ unsafe_allow_html=True
69
+ )
70
+
71
+ st.markdown(
72
  """
73
+ <p style="font-size: 1.1em; text-align: center; margin-bottom: 20px;">
74
+ Enter any text below, and I'll tell you if its sentiment is positive, negative, or neutral based on a predefined list of keywords.
75
+ </p>
76
+ """,
77
+ unsafe_allow_html=True
78
  )
79
 
80
  # Text input from the user
81
  user_input = st.text_area(
82
+ "πŸ“ Enter your text here:",
83
+ "This is a good example, but it could be even better. I really enjoy using Streamlit!",
84
+ height=180,
85
+ key="user_text_input" # Added a key for better control
86
  )
87
 
88
+ col1, col2 = st.columns([1, 1])
89
+
90
+ with col1:
91
+ analyze_button = st.button("✨ Analyze Sentiment", key="analyze_btn")
92
+ with col2:
93
+ clear_button = st.button("πŸ—‘οΈ Clear Text", key="clear_btn")
94
+
95
+ # Clear button functionality
96
+ if clear_button:
97
+ st.session_state.user_text_input = "" # Clear the text area
98
+ st.experimental_rerun() # Rerun to clear the output
99
+
100
+ # Analyze button logic
101
+ if analyze_button:
102
  if user_input:
103
  sentiment, pos_count, neg_count = analyze_sentiment_basic(user_input)
104
 
105
+ st.markdown("---")
106
+ st.subheader("πŸ“Š Analysis Result:")
107
 
108
+ # Display result with appropriate styling and icons
109
  if sentiment == "Positive":
110
  st.success(f"**Sentiment:** Positive 😊")
111
  elif sentiment == "Negative":
 
113
  else:
114
  st.warning(f"**Sentiment:** Neutral 😐")
115
 
116
+ st.markdown(f"<p style='font-size: 1.05em;'>Positive keyword matches: <strong>{pos_count}</strong></p>", unsafe_allow_html=True)
117
+ st.markdown(f"<p style='font-size: 1.05em;'>Negative keyword matches: <strong>{neg_count}</strong></p>", unsafe_allow_html=True)
118
 
119
  st.markdown("---")
120
  st.write(f"**Original Text:**")
121
+ st.markdown(f"<div style='background-color: #e9ecef; padding: 15px; border-radius: 8px; border-left: 5px solid #007bff;'><em>{user_input}</em></div>", unsafe_allow_html=True)
122
  else:
123
  st.warning("Please enter some text to analyze.")
124
 
125
+ # Custom CSS for enhanced styling
126
  st.markdown(
127
  """
128
  <style>
129
+ /* General body and font */
130
+ body {
131
+ font-family: 'Inter', sans-serif;
132
+ background-color: #f8f9fa;
133
+ color: #343a40;
134
+ }
135
+
136
+ /* Streamlit widgets styling */
137
  .stButton>button {
138
+ background-color: #28a745; /* Green for analyze */
139
  color: white;
140
+ padding: 12px 25px;
141
+ border-radius: 10px;
142
  border: none;
143
  cursor: pointer;
144
+ font-size: 1.1em;
145
+ font-weight: bold;
146
+ box-shadow: 0 5px 15px rgba(40, 167, 69, 0.3);
147
+ transition: all 0.3s ease-in-out;
148
+ width: 100%; /* Make buttons full width in columns */
149
  }
150
  .stButton>button:hover {
151
+ background-color: #218838;
152
+ box-shadow: 0 8px 20px rgba(40, 167, 69, 0.4);
153
+ transform: translateY(-2px);
154
  }
155
+
156
+ /* Clear button specific style */
157
+ .stButton[key="clear_btn"] > button {
158
+ background-color: #dc3545; /* Red for clear */
159
+ box-shadow: 0 5px 15px rgba(220, 53, 69, 0.3);
160
+ }
161
+ .stButton[key="clear_btn"] > button:hover {
162
+ background-color: #c82333;
163
+ box-shadow: 0 8px 20px rgba(220, 53, 69, 0.4);
164
+ }
165
+
166
  .stTextArea>div>div>textarea {
167
+ border-radius: 10px;
168
+ border: 1px solid #ced4da;
169
+ padding: 15px;
170
+ font-size: 1.05em;
171
+ box-shadow: inset 0 1px 3px rgba(0,0,0,0.1);
172
+ transition: border-color 0.3s ease-in-out;
173
+ }
174
+ .stTextArea>div>div>textarea:focus {
175
+ border-color: #007bff;
176
+ outline: none;
177
+ }
178
+
179
+ /* Streamlit message boxes */
180
+ .stSuccess {
181
+ background-color: #d4edda;
182
+ color: #155724;
183
+ border-radius: 8px;
184
+ padding: 15px;
185
+ border: 1px solid #c3e6cb;
186
+ font-weight: bold;
187
+ }
188
+ .stError {
189
+ background-color: #f8d7da;
190
+ color: #721c24;
191
+ border-radius: 8px;
192
+ padding: 15px;
193
+ border: 1px solid #f5c6cb;
194
+ font-weight: bold;
195
+ }
196
+ .stWarning {
197
+ background-color: #fff3cd;
198
+ color: #856404;
199
+ border-radius: 8px;
200
+ padding: 15px;
201
+ border: 1px solid #ffeeba;
202
+ font-weight: bold;
203
+ }
204
+ .stInfo {
205
+ background-color: #d1ecf1;
206
+ color: #0c5460;
207
  border-radius: 8px;
 
208
  padding: 10px;
209
+ border: 1px solid #bee5eb;
210
+ margin-top: 10px;
211
+ }
212
+
213
+ /* Markdown styling for titles and text */
214
+ h1 {
215
+ color: #007bff;
216
+ text-align: center;
217
+ font-weight: 700;
218
+ margin-bottom: 20px;
219
+ }
220
+ h2, h3, h4, h5, h6 {
221
+ color: #343a40;
222
+ margin-top: 25px;
223
+ margin-bottom: 15px;
224
+ }
225
+ p {
226
+ line-height: 1.6;
227
  }
228
  </style>
229
  """,
230
  unsafe_allow_html=True
231
+ )
232
+
233
+ # Footer
234
+ st.markdown(
235
+ """
236
+ <div style="text-align: center; margin-top: 50px; padding: 20px; border-top: 1px solid #eee; color: #6c757d;">
237
+ <p>Built with ❀️ using Streamlit and pure Python.</p>
238
+ </div>
239
+ """,
240
+ unsafe_allow_html=True
241
+ )