167AliRaza commited on
Commit
053ff25
Β·
verified Β·
1 Parent(s): ecd76b2

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +97 -35
src/streamlit_app.py CHANGED
@@ -1,40 +1,102 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
- import streamlit as st
5
 
6
- """
7
- # Welcome to Streamlit!
 
 
 
 
 
8
 
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
 
15
 
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
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()