167AliRaza commited on
Commit
ecd76b2
Β·
verified Β·
1 Parent(s): 5bae463

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from textblob import TextBlob
3
+
4
+ def main():
5
+ # Page configuration
6
+ st.set_page_config(
7
+ page_title="Basic Sentiment Analysis",
8
+ page_icon="πŸ”„",
9
+ layout="centered"
10
+ )
11
+
12
+ # Custom CSS for styling
13
+ st.markdown("""
14
+ <style>
15
+ .main {
16
+ background-color: #f8f9fa;
17
+ }
18
+ .stTextInput > div > div > input {
19
+ border-radius: 15px;
20
+ padding: 10px;
21
+ }
22
+ .reportview-container .main .block-container {
23
+ padding: 2rem;
24
+ }
25
+ .result-box {
26
+ padding: 20px;
27
+ border-radius: 10px;
28
+ margin-top: 20px;
29
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
30
+ }
31
+ .positive {
32
+ background-color: #d4edda;
33
+ color: #155724;
34
+ }
35
+ .neutral {
36
+ background-color: #e2e3e5;
37
+ color: #383d41;
38
+ }
39
+ .negative {
40
+ background-color: #f8d7da;
41
+ color: #721c24;
42
+ }
43
+ </style>
44
+ """, unsafe_allow_html=True)
45
+
46
+ # Header
47
+ st.title("Basic Sentiment Analysis")
48
+ st.markdown("Enter text below to analyze its sentiment (without using pre-trained models).")
49
+
50
+ # Text input
51
+ user_input = st.text_area("Enter your text:", height=100,
52
+ placeholder="Type something like 'I love this!' or 'This is terrible.'")
53
+
54
+ if st.button("Analyze Sentiment"):
55
+ if user_input:
56
+ # Analyze sentiment using TextBlob
57
+ analysis = TextBlob(user_input)
58
+ polarity = analysis.sentiment.polarity
59
+
60
+ # Determine sentiment category
61
+ if polarity > 0.2:
62
+ sentiment = "Positive 😊"
63
+ emotion_class = "positive"
64
+ elif polarity < -0.2:
65
+ sentiment = "Negative 😞"
66
+ emotion_class = "negative"
67
+ else:
68
+ sentiment = "Neutral 😐"
69
+ emotion_class = "neutral"
70
+
71
+ # Display results
72
+ st.markdown(f"<div class='result-box {emotion_class}'>", unsafe_allow_html=True)
73
+
74
+ st.subheader("Sentiment Analysis Results:")
75
+ col1, col2 = st.columns(2)
76
+
77
+ with col1:
78
+ st.metric("Sentiment", sentiment)
79
+
80
+ with col2:
81
+ st.metric("Polarity Score", round(polarity, 3))
82
+
83
+ st.progress((polarity + 1) / 2)
84
+
85
+ st.markdown("""
86
+ **Polarity Scale:**
87
+ -1.0 (Very Negative) β€”β€” 0.0 (Neutral) β€”β€” +1.0 (Very Positive)
88
+ """)
89
+
90
+ st.markdown("</div>", unsafe_allow_html=True)
91
+
92
+ # Additional analysis
93
+ with st.expander("Detailed Analysis:"):
94
+ st.write(f"- **Subjectivity:** {'Subjective' if analysis.sentiment.subjectivity > 0.5 else 'Objective'} "
95
+ f"(Score: {round(analysis.sentiment.subjectivity, 3)})")
96
+ st.write("- **Word Count:**", len(analysis.words))
97
+
98
+ else:
99
+ st.warning("Please enter some text to analyze.")
100
+
101
+ if __name__ == "__main__":
102
+ main()