FaizaRiaz commited on
Commit
acfdc08
Β·
verified Β·
1 Parent(s): 6473b64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -15
app.py CHANGED
@@ -1,33 +1,64 @@
1
  import streamlit as st
2
 
3
- # Set up page
4
- st.title("πŸ’¬ Sentiment Analysis App (Rule-based)")
5
- st.write("This app predicts the sentiment of your text using simple Python logic (no ML model).")
 
 
 
6
 
7
- # Input text
8
- user_input = st.text_area("Enter your text here:")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Basic keyword lists
 
 
 
 
 
 
 
 
11
  positive_words = ["good", "great", "happy", "excellent", "amazing", "love", "awesome", "fantastic", "positive", "nice"]
12
  negative_words = ["bad", "sad", "terrible", "horrible", "hate", "awful", "worst", "angry", "negative", "poor"]
13
 
 
14
  def analyze_sentiment(text):
15
  text = text.lower()
16
  pos_count = sum(word in text for word in positive_words)
17
  neg_count = sum(word in text for word in negative_words)
18
 
19
  if pos_count > neg_count:
20
- return "😊 Positive"
21
  elif neg_count > pos_count:
22
- return "☹️ Negative"
23
  else:
24
- return "😐 Neutral"
25
 
26
- # Analyze button
27
- if st.button("Analyze"):
28
  if user_input.strip():
29
- result = analyze_sentiment(user_input)
30
- st.subheader("Sentiment Result:")
31
- st.success(result)
32
  else:
33
- st.warning("Please enter some text.")
 
 
 
 
 
1
  import streamlit as st
2
 
3
+ # Page config
4
+ st.set_page_config(
5
+ page_title="Sentiment Analysis",
6
+ page_icon="πŸ’¬",
7
+ layout="centered"
8
+ )
9
 
10
+ # Custom styles
11
+ st.markdown("""
12
+ <style>
13
+ .stTextArea textarea {
14
+ height: 150px;
15
+ }
16
+ .result-box {
17
+ padding: 1rem;
18
+ border-radius: 0.5rem;
19
+ margin-top: 1rem;
20
+ text-align: center;
21
+ font-size: 1.3rem;
22
+ }
23
+ .positive { background-color: #d4edda; color: #155724; }
24
+ .negative { background-color: #f8d7da; color: #721c24; }
25
+ .neutral { background-color: #fff3cd; color: #856404; }
26
+ </style>
27
+ """, unsafe_allow_html=True)
28
 
29
+ # App title and intro
30
+ st.title("πŸ’¬ Sentiment Analysis App")
31
+ st.markdown("Predict sentiment using simple Python logic β€” no ML, just rule-based 😎")
32
+
33
+ # Input section
34
+ st.subheader("πŸ“ Enter your text")
35
+ user_input = st.text_area("", placeholder="Type or paste your sentence here...")
36
+
37
+ # Keywords
38
  positive_words = ["good", "great", "happy", "excellent", "amazing", "love", "awesome", "fantastic", "positive", "nice"]
39
  negative_words = ["bad", "sad", "terrible", "horrible", "hate", "awful", "worst", "angry", "negative", "poor"]
40
 
41
+ # Sentiment analysis function
42
  def analyze_sentiment(text):
43
  text = text.lower()
44
  pos_count = sum(word in text for word in positive_words)
45
  neg_count = sum(word in text for word in negative_words)
46
 
47
  if pos_count > neg_count:
48
+ return "😊 Positive", "positive"
49
  elif neg_count > pos_count:
50
+ return "☹️ Negative", "negative"
51
  else:
52
+ return "😐 Neutral", "neutral"
53
 
54
+ # Button & result
55
+ if st.button("πŸ” Analyze Sentiment"):
56
  if user_input.strip():
57
+ sentiment, sentiment_class = analyze_sentiment(user_input)
58
+ st.markdown(f'<div class="result-box {sentiment_class}">{sentiment}</div>', unsafe_allow_html=True)
 
59
  else:
60
+ st.warning("⚠️ Please enter some text before analyzing.")
61
+
62
+ # Footer
63
+ st.markdown("---")
64
+ st.markdown("Made with ❀️ using Streamlit | Rule-based approach")