File size: 4,239 Bytes
e534dc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05c10ae
e534dc1
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import streamlit as st
import webbrowser
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import nltk
import time
import plotly.graph_objects as go

# Set page config at the very beginning
st.set_page_config(page_title="Theaimart - Sentiment Analysis", layout="wide", initial_sidebar_state="auto")

# Download necessary NLTK data
@st.cache_resource
def download_nltk_data():
    nltk.download('vader_lexicon', quiet=True)

download_nltk_data()
sid = SentimentIntensityAnalyzer()

def analyze_sentiment(sentence):
    if sentence:
        sentiment_scores = sid.polarity_scores(sentence)
        highest_score = max(sentiment_scores, key=sentiment_scores.get)
        sentiment_dict = {
            'neg': 'Negative πŸ˜”',
            'neu': 'Neutral 😐',
            'pos': 'Positive 😊',
            'compound': 'Mixed πŸ€”'
        }
        highest_sentiment = sentiment_dict[highest_score]
        
        return sentiment_scores, highest_sentiment
    return None, None

# Custom CSS to improve the app's appearance
st.markdown("""
    <style>
    .main {
        background-color: #f0f2f6;
    }
    .stButton>button {
        color: #ffffff;
        background-color: #4CAF50;
        border-radius: 5px;
        border: none;
        padding: 10px 24px;
        transition: all 0.3s ease-in-out;
    }
    .stButton>button:hover {
        background-color: #45a049;
        transform: translateY(-2px);
    }
    .stTextInput>div>div>input {
        border-radius: 5px;
    }
    .stTextArea textarea {
        border: 2px solid #4CAF50;
        border-radius: 5px;
    }
    .stTextArea textarea:focus {
        box-shadow: 0 0 5px #4CAF50;
    }
    div.row-widget.stButton {
        text-align: center;
    }
    </style>
    """, unsafe_allow_html=True)

# App title with animation
st.markdown(
    """
    <h1 style='text-align: center; color: #2E86C1; animation: fadeIn 1.5s;'>
        Sentiment Analysis Tool
    </h1>
    """,
    unsafe_allow_html=True
)

# Animated description
st.markdown(
    """
    <p style='text-align: center; font-size: 18px; animation: slideIn 1.5s;'>
        Analyze the sentiment of your text with our advanced AI-powered tool!
    </p>
    """,
    unsafe_allow_html=True
)

input_text = st.text_area("Enter text for sentiment analysis", height=150)

# Center the Analyze button
col1, col2, col3 = st.columns([1,1,1])
with col2:
    analyze_button = st.button("Analyze")

if analyze_button:
    with st.spinner("Analyzing sentiment..."):
        sentiment_scores, highest_sentiment = analyze_sentiment(input_text)
        
        if sentiment_scores:
            # Create a radar chart for sentiment scores
            categories = ['Negative', 'Neutral', 'Positive', 'Compound']
            values = [sentiment_scores['neg'], sentiment_scores['neu'], sentiment_scores['pos'], sentiment_scores['compound']]

            fig = go.Figure(data=go.Scatterpolar(
                r=values,
                theta=categories,
                fill='toself',
                line=dict(color='#2E86C1')
            ))

            fig.update_layout(
                polar=dict(
                    radialaxis=dict(visible=True, range=[0, 1])
                ),
                showlegend=False
            )

            st.plotly_chart(fig, use_container_width=True)

            st.markdown(f"<h2 style='text-align: center; color: #2E86C1;'>Overall Sentiment: {highest_sentiment}</h2>", unsafe_allow_html=True)

            # Display detailed scores
            col1, col2, col3, col4 = st.columns(4)
            col1.metric("Negative", f"{sentiment_scores['neg']:.2f}")
            col2.metric("Neutral", f"{sentiment_scores['neu']:.2f}")
            col3.metric("Positive", f"{sentiment_scores['pos']:.2f}")
            col4.metric("Compound", f"{sentiment_scores['compound']:.2f}")

    with st.spinner("Loading ad..."):
        time.sleep(3)  # Simulate a delay for loading
        webbrowser.open("https://www.highrevenuenetwork.com/dt90hpe6y?key=472b3a55761032431e75af65c838cef4")

# Footer
st.markdown(
    """
    <div style='text-align: center; padding: 20px; animation: fadeIn 2s;'>
        <p>Powered by Theaimart Β© 2024</p>
    </div>
    """,
    unsafe_allow_html=True
)