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(""" """, unsafe_allow_html=True) # App title with animation st.markdown( """
Analyze the sentiment of your text with our advanced AI-powered tool!
""", 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"Powered by Theaimart © 2024