"""
cols[0].markdown(article_html, unsafe_allow_html=True)
def display_sentiment_distribution(analysis):
"""Display sentiment distribution chart with enhanced styling"""
if 'Comparative Sentiment Score' in analysis and 'Sentiment Distribution' in analysis['Comparative Sentiment Score']:
dist = analysis['Comparative Sentiment Score']['Sentiment Distribution']
data = {
'Sentiment': list(dist.keys()),
'Count': list(dist.values())
}
df = pd.DataFrame(data)
# Create color map
color_map = {
'Positive': '#4CAF50',
'Negative': '#F44336',
'Neutral': '#9E9E9E'
}
# Create a card container for the chart
st.markdown("""
Sentiment Distribution
""", unsafe_allow_html=True)
# Create pie chart for sentiment distribution
labels = list(dist.keys())
values = list(dist.values())
colors = [color_map[label] for label in labels]
# Create two columns for different chart types
col1, col2 = st.columns(2)
with col1:
# Bar chart
fig_bar = px.bar(
df,
x='Sentiment',
y='Count',
color='Sentiment',
color_discrete_map=color_map,
title="Sentiment Distribution (Bar Chart)"
)
fig_bar.update_layout(
plot_bgcolor='rgba(0,0,0,0)',
paper_bgcolor='rgba(0,0,0,0)',
font=dict(size=14),
margin=dict(l=20, r=20, t=40, b=20),
height=350
)
st.plotly_chart(fig_bar, use_container_width=True)
with col2:
# Pie chart
fig_pie = go.Figure(data=[go.Pie(
labels=labels,
values=values,
marker=dict(colors=colors),
textinfo='percent+label',
hole=.4
)])
fig_pie.update_layout(
title_text="Sentiment Distribution (Pie Chart)",
annotations=[dict(text='Sentiment', x=0.5, y=0.5, font_size=14, showarrow=False)],
plot_bgcolor='rgba(0,0,0,0)',
paper_bgcolor='rgba(0,0,0,0)',
font=dict(size=14),
margin=dict(l=20, r=20, t=40, b=20),
height=350
)
st.plotly_chart(fig_pie, use_container_width=True)
# Add a summary of the sentiment distribution
total = sum(values)
if total > 0:
percentages = {label: (count/total*100) for label, count in zip(labels, values)}
# Create a summary card
summary_html = """
Summary
"""
for label in labels:
if label in percentages:
color = color_map[label]
summary_html += f'{label}: {percentages[label]:.1f}% | '
summary_html = summary_html.rstrip(' | ') + '
'
st.markdown(summary_html, unsafe_allow_html=True)
def display_topic_analysis(analysis):
"""Display topic analysis visualization"""
if 'Comparative Sentiment Score' in analysis and 'Topic Overlap' in analysis['Comparative Sentiment Score']:
topic_data = analysis['Comparative Sentiment Score']['Topic Overlap']
# Prepare data for visualization
all_topics = set()
if 'Common Topics' in topic_data:
all_topics.update(topic_data['Common Topics'])
for i in range(1, 11): # Check for unique topics in each article
key = f"Unique Topics in Article {i}"
if key in topic_data and topic_data[key]:
all_topics.update(topic_data[key])
# Count topic occurrences across articles
topic_counts = {}
for topic in all_topics:
count = 0
if 'Common Topics' in topic_data and topic in topic_data['Common Topics']:
count += len(analysis['Articles']) # All articles have common topics
for i in range(1, 11):
key = f"Unique Topics in Article {i}"
if key in topic_data and topic in topic_data[key]:
count += 1
topic_counts[topic] = count
# Create DataFrame and visualization
topic_df = pd.DataFrame({
'Topic': list(topic_counts.keys()),
'Occurrence': list(topic_counts.values())
}).sort_values('Occurrence', ascending=False)
fig = px.bar(
topic_df,
x='Topic',
y='Occurrence',
title="Topic Distribution Across Articles",
color='Occurrence',
color_continuous_scale=px.colors.sequential.Viridis
)
st.plotly_chart(fig, use_container_width=True)
def display_comparative_analysis(analysis):
"""Display comparative analysis details"""
if 'Comparative Sentiment Score' in analysis and 'Coverage Differences' in analysis['Comparative Sentiment Score']:
differences = analysis['Comparative Sentiment Score']['Coverage Differences']
st.subheader("Comparative Analysis")
for i, diff in enumerate(differences):
with st.expander(f"Comparison {i+1}"):
st.write(f"**Comparison**: {diff['Comparison']}")
st.write(f"**Impact**: {diff['Impact']}")
# Main app layout with enhanced design and better readability
st.markdown("""
📰 News Summarization & Sentiment Analysis
Analyze recent news articles about any company. Get sentiment analysis, topic extraction,
and multilingual text-to-speech summaries instantly in 10 different languages.
✅ Real-time News Analysis
📊 Sentiment Visualization
🔍 Topic Extraction
🎧 Multilingual Text-to-Speech
""", unsafe_allow_html=True)
# Input form with enhanced styling
st.markdown("""
🔍 Search for Company News
Enter a company name below to analyze its recent news coverage.
Try companies like Tesla, Apple, Microsoft, Google, or Amazon.
""", unsafe_allow_html=True)
with st.form("search_form"):
col1, col2 = st.columns([3, 1])
with col1:
company_name = st.text_input("Company Name", placeholder="Enter company name (e.g., Tesla)", label_visibility="collapsed")
with col2:
submit_button = st.form_submit_button("🔍 Analyze News")
# Add some example buttons below the form
st.markdown("""
Try analyzing news for:
Tesla
Apple
Microsoft
Google
Amazon
""", unsafe_allow_html=True)
# Process form submission
if submit_button and company_name:
with st.spinner(f"Fetching news articles about {company_name}..."):
articles_data = get_company_news(company_name)
if articles_data and 'articles' in articles_data and len(articles_data['articles']) > 0:
articles = articles_data['articles']
with st.spinner("Performing sentiment analysis..."):
analysis_result = get_analysis(company_name, articles)
if analysis_result:
# Store complete analysis in session state
st.session_state.analysis = analysis_result
# Display summary and stats
st.header(f"Analysis Results for {company_name}")
# Create a nice header with company logo or icon
company_icon = "🏢" # Default company icon
if company_name.lower() == "tesla":
company_icon = "🚗"
elif company_name.lower() == "apple":
company_icon = "🍎"
elif company_name.lower() == "microsoft":
company_icon = "💻"
elif company_name.lower() == "amazon":
company_icon = "📦"
elif company_name.lower() == "google":
company_icon = "🔍"
st.markdown(f"""
{company_icon} {company_name} News Analysis
Analysis of {len(analysis_result['Articles'])} news articles | Generated on {datetime.now().strftime('%B %d, %Y')}
""", unsafe_allow_html=True)
# Display visualization tabs with custom styling
st.markdown("""
""", unsafe_allow_html=True)
tab1, tab2, tab3, tab4 = st.tabs(["📊 Overview", "😊 Sentiment Analysis", "🔍 Topic Analysis", "📰 Article Details"])
with tab1:
# Create a card-style container for the summary
st.markdown("""