create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import webbrowser
|
3 |
+
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
4 |
+
import nltk
|
5 |
+
import time
|
6 |
+
import plotly.graph_objects as go
|
7 |
+
|
8 |
+
# Set page config at the very beginning
|
9 |
+
st.set_page_config(page_title="Theaimart - Sentiment Analysis", layout="wide", initial_sidebar_state="auto")
|
10 |
+
|
11 |
+
# Download necessary NLTK data
|
12 |
+
@st.cache_resource
|
13 |
+
def download_nltk_data():
|
14 |
+
nltk.download('vader_lexicon', quiet=True)
|
15 |
+
|
16 |
+
download_nltk_data()
|
17 |
+
sid = SentimentIntensityAnalyzer()
|
18 |
+
|
19 |
+
def analyze_sentiment(sentence):
|
20 |
+
if sentence:
|
21 |
+
sentiment_scores = sid.polarity_scores(sentence)
|
22 |
+
highest_score = max(sentiment_scores, key=sentiment_scores.get)
|
23 |
+
sentiment_dict = {
|
24 |
+
'neg': 'Negative π',
|
25 |
+
'neu': 'Neutral π',
|
26 |
+
'pos': 'Positive π',
|
27 |
+
'compound': 'Mixed π€'
|
28 |
+
}
|
29 |
+
highest_sentiment = sentiment_dict[highest_score]
|
30 |
+
|
31 |
+
return sentiment_scores, highest_sentiment
|
32 |
+
return None, None
|
33 |
+
|
34 |
+
# Custom CSS to improve the app's appearance
|
35 |
+
st.markdown("""
|
36 |
+
<style>
|
37 |
+
.main {
|
38 |
+
background-color: #f0f2f6;
|
39 |
+
}
|
40 |
+
.stButton>button {
|
41 |
+
color: #ffffff;
|
42 |
+
background-color: #4CAF50;
|
43 |
+
border-radius: 5px;
|
44 |
+
border: none;
|
45 |
+
padding: 10px 24px;
|
46 |
+
transition: all 0.3s ease-in-out;
|
47 |
+
}
|
48 |
+
.stButton>button:hover {
|
49 |
+
background-color: #45a049;
|
50 |
+
transform: translateY(-2px);
|
51 |
+
}
|
52 |
+
.stTextInput>div>div>input {
|
53 |
+
border-radius: 5px;
|
54 |
+
}
|
55 |
+
.stTextArea textarea {
|
56 |
+
border: 2px solid #4CAF50;
|
57 |
+
border-radius: 5px;
|
58 |
+
}
|
59 |
+
.stTextArea textarea:focus {
|
60 |
+
box-shadow: 0 0 5px #4CAF50;
|
61 |
+
}
|
62 |
+
div.row-widget.stButton {
|
63 |
+
text-align: center;
|
64 |
+
}
|
65 |
+
</style>
|
66 |
+
""", unsafe_allow_html=True)
|
67 |
+
|
68 |
+
# App title with animation
|
69 |
+
st.markdown(
|
70 |
+
"""
|
71 |
+
<h1 style='text-align: center; color: #2E86C1; animation: fadeIn 1.5s;'>
|
72 |
+
Sentiment Analysis Tool
|
73 |
+
</h1>
|
74 |
+
""",
|
75 |
+
unsafe_allow_html=True
|
76 |
+
)
|
77 |
+
|
78 |
+
# Animated description
|
79 |
+
st.markdown(
|
80 |
+
"""
|
81 |
+
<p style='text-align: center; font-size: 18px; animation: slideIn 1.5s;'>
|
82 |
+
Analyze the sentiment of your text with our advanced AI-powered tool!
|
83 |
+
</p>
|
84 |
+
""",
|
85 |
+
unsafe_allow_html=True
|
86 |
+
)
|
87 |
+
|
88 |
+
input_text = st.text_area("Enter text for sentiment analysis", height=150)
|
89 |
+
|
90 |
+
# Center the Analyze button
|
91 |
+
col1, col2, col3 = st.columns([1,1,1])
|
92 |
+
with col2:
|
93 |
+
analyze_button = st.button("Analyze")
|
94 |
+
|
95 |
+
if analyze_button:
|
96 |
+
with st.spinner("Analyzing sentiment..."):
|
97 |
+
sentiment_scores, highest_sentiment = analyze_sentiment(input_text)
|
98 |
+
|
99 |
+
if sentiment_scores:
|
100 |
+
# Create a radar chart for sentiment scores
|
101 |
+
categories = ['Negative', 'Neutral', 'Positive', 'Compound']
|
102 |
+
values = [sentiment_scores['neg'], sentiment_scores['neu'], sentiment_scores['pos'], sentiment_scores['compound']]
|
103 |
+
|
104 |
+
fig = go.Figure(data=go.Scatterpolar(
|
105 |
+
r=values,
|
106 |
+
theta=categories,
|
107 |
+
fill='toself',
|
108 |
+
line=dict(color='#2E86C1')
|
109 |
+
))
|
110 |
+
|
111 |
+
fig.update_layout(
|
112 |
+
polar=dict(
|
113 |
+
radialaxis=dict(visible=True, range=[0, 1])
|
114 |
+
),
|
115 |
+
showlegend=False
|
116 |
+
)
|
117 |
+
|
118 |
+
st.plotly_chart(fig, use_container_width=True)
|
119 |
+
|
120 |
+
st.markdown(f"<h2 style='text-align: center; color: #2E86C1;'>Overall Sentiment: {highest_sentiment}</h2>", unsafe_allow_html=True)
|
121 |
+
|
122 |
+
# Display detailed scores
|
123 |
+
col1, col2, col3, col4 = st.columns(4)
|
124 |
+
col1.metric("Negative", f"{sentiment_scores['neg']:.2f}")
|
125 |
+
col2.metric("Neutral", f"{sentiment_scores['neu']:.2f}")
|
126 |
+
col3.metric("Positive", f"{sentiment_scores['pos']:.2f}")
|
127 |
+
col4.metric("Compound", f"{sentiment_scores['compound']:.2f}")
|
128 |
+
|
129 |
+
with st.spinner("Loading ad..."):
|
130 |
+
time.sleep(3) # Simulate a delay for loading
|
131 |
+
webbrowser.open("https://shaidraup.net/4/7529971")
|
132 |
+
|
133 |
+
# Footer
|
134 |
+
st.markdown(
|
135 |
+
"""
|
136 |
+
<div style='text-align: center; padding: 20px; animation: fadeIn 2s;'>
|
137 |
+
<p>Powered by Theaimart Β© 2024</p>
|
138 |
+
</div>
|
139 |
+
""",
|
140 |
+
unsafe_allow_html=True
|
141 |
+
)
|