Spaces:
Sleeping
Sleeping
File size: 20,672 Bytes
bf4ee4b f18e43c bf4ee4b 52c4b49 bf4ee4b 52c4b49 bf4ee4b a3d8fca bf4ee4b a3d8fca bf4ee4b a3d8fca bf4ee4b f18e43c a3d8fca f18e43c a3d8fca f18e43c a3d8fca f18e43c a3d8fca f18e43c bf4ee4b f18e43c bf4ee4b |
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
"""Streamlit frontend for the News Summarization application."""
import streamlit as st
import pandas as pd
import json
import os
import plotly.express as px
import altair as alt
from utils import analyze_company_data, TextToSpeechConverter
# Set page config
st.set_page_config(
page_title="News Summarization App",
page_icon="π°",
layout="wide"
)
# Show loading message
with st.spinner("Initializing the application... Please wait while we load the models."):
# Initialize components
try:
from utils import NewsExtractor, SentimentAnalyzer, TextSummarizer, TextToSpeechConverter
st.success("Application initialized successfully!")
except Exception as e:
st.error(f"Error initializing application: {str(e)}")
st.info("Please try refreshing the page.")
def process_company(company_name):
"""Process company data directly."""
try:
# Call the analysis function directly from utils
data = analyze_company_data(company_name)
# Generate Hindi audio if needed
if 'summary' in data:
tts_converter = TextToSpeechConverter()
audio_path = tts_converter.generate_audio(data['summary'], f'{company_name}_summary')
data['audio_path'] = audio_path
return data
except Exception as e:
st.error(f"Error processing company: {str(e)}")
return {"articles": [], "comparative_sentiment_score": {}, "final_sentiment_analysis": "", "audio_path": None}
def main():
st.title("π° News Summarization and Analysis")
# Sidebar
st.sidebar.header("Settings")
# Replace dropdown with text input
company = st.sidebar.text_input(
"Enter Company Name",
placeholder="e.g., Tesla, Apple, Microsoft, or any other company",
help="Enter the name of any company you want to analyze"
)
if st.sidebar.button("Analyze") and company:
if len(company.strip()) < 2:
st.sidebar.error("Please enter a valid company name (at least 2 characters)")
else:
with st.spinner("Analyzing news articles..."):
try:
# Process company data
data = analyze_company_data(company)
if not data["articles"]:
st.error("No articles found for analysis.")
return
# Display Articles
st.header("π News Articles")
for idx, article in enumerate(data["articles"], 1):
with st.expander(f"Article {idx}: {article['title']}"):
st.write("**Content:**", article.get("content", "No content available"))
if "summary" in article:
st.write("**Summary:**", article["summary"])
st.write("**Source:**", article.get("source", "Unknown"))
# Enhanced sentiment display
if "sentiment" in article:
sentiment_col1, sentiment_col2 = st.columns(2)
with sentiment_col1:
st.write("**Sentiment:**", article["sentiment"])
st.write("**Confidence Score:**", f"{article.get('sentiment_score', 0)*100:.1f}%")
with sentiment_col2:
# Display fine-grained sentiment if available
if "fine_grained_sentiment" in article and article["fine_grained_sentiment"]:
fine_grained = article["fine_grained_sentiment"]
if "category" in fine_grained:
st.write("**Detailed Sentiment:**", fine_grained["category"])
if "confidence" in fine_grained:
st.write("**Confidence:**", f"{fine_grained['confidence']*100:.1f}%")
# Display sentiment indices if available
if "sentiment_indices" in article and article["sentiment_indices"]:
st.markdown("**Sentiment Indices:**")
indices = article["sentiment_indices"]
# Create columns for displaying indices
idx_cols = st.columns(3)
# Display positivity and negativity in first column
with idx_cols[0]:
if "positivity_index" in indices:
st.markdown(f"**Positivity:** {indices['positivity_index']:.2f}")
if "negativity_index" in indices:
st.markdown(f"**Negativity:** {indices['negativity_index']:.2f}")
# Display emotional intensity and controversy in second column
with idx_cols[1]:
if "emotional_intensity" in indices:
st.markdown(f"**Emotional Intensity:** {indices['emotional_intensity']:.2f}")
if "controversy_score" in indices:
st.markdown(f"**Controversy:** {indices['controversy_score']:.2f}")
# Display confidence and ESG in third column
with idx_cols[2]:
if "confidence_score" in indices:
st.markdown(f"**Confidence:** {indices['confidence_score']:.2f}")
if "esg_relevance" in indices:
st.markdown(f"**ESG Relevance:** {indices['esg_relevance']:.2f}")
# Display entities if available
if "entities" in article and article["entities"]:
st.markdown("**Named Entities:**")
entities = article["entities"]
# Organizations
if "ORG" in entities and entities["ORG"]:
st.write("**Organizations:**", ", ".join(entities["ORG"]))
# People
if "PERSON" in entities and entities["PERSON"]:
st.write("**People:**", ", ".join(entities["PERSON"]))
# Locations
if "GPE" in entities and entities["GPE"]:
st.write("**Locations:**", ", ".join(entities["GPE"]))
# Money
if "MONEY" in entities and entities["MONEY"]:
st.write("**Financial Values:**", ", ".join(entities["MONEY"]))
# Display sentiment targets if available
if "sentiment_targets" in article and article["sentiment_targets"]:
st.markdown("**Sentiment Targets:**")
targets = article["sentiment_targets"]
for target in targets:
st.markdown(f"**{target['entity']}** ({target['type']}): {target['sentiment']} ({target['confidence']*100:.1f}%)")
st.markdown(f"> {target['context']}")
st.markdown("---")
if "url" in article:
st.write("**[Read More](%s)**" % article["url"])
# Display Comparative Analysis
st.header("π Comparative Analysis")
analysis = data.get("comparative_sentiment_score", {})
# Sentiment Distribution
if "sentiment_distribution" in analysis:
st.subheader("Sentiment Distribution")
sentiment_dist = analysis["sentiment_distribution"]
try:
# Extract basic sentiment data
if isinstance(sentiment_dist, dict):
if "basic" in sentiment_dist and isinstance(sentiment_dist["basic"], dict):
basic_dist = sentiment_dist["basic"]
elif any(k in sentiment_dist for k in ['positive', 'negative', 'neutral']):
basic_dist = {k: v for k, v in sentiment_dist.items()
if k in ['positive', 'negative', 'neutral']}
else:
basic_dist = {'positive': 0, 'negative': 0, 'neutral': 1}
else:
basic_dist = {'positive': 0, 'negative': 0, 'neutral': 1}
# Calculate percentages
total_articles = sum(basic_dist.values())
if total_articles > 0:
percentages = {
k: (v / total_articles) * 100
for k, v in basic_dist.items()
}
else:
percentages = {k: 0 for k in basic_dist}
# Display as metrics
st.write("**Sentiment Distribution:**")
col1, col2, col3 = st.columns(3)
with col1:
st.metric(
"Positive",
basic_dist.get('positive', 0),
f"{percentages.get('positive', 0):.1f}%"
)
with col2:
st.metric(
"Negative",
basic_dist.get('negative', 0),
f"{percentages.get('negative', 0):.1f}%"
)
with col3:
st.metric(
"Neutral",
basic_dist.get('neutral', 0),
f"{percentages.get('neutral', 0):.1f}%"
)
# Create visualization
chart_data = pd.DataFrame({
'Sentiment': ['Positive', 'Negative', 'Neutral'],
'Count': [
basic_dist.get('positive', 0),
basic_dist.get('negative', 0),
basic_dist.get('neutral', 0)
],
'Percentage': [
f"{percentages.get('positive', 0):.1f}%",
f"{percentages.get('negative', 0):.1f}%",
f"{percentages.get('neutral', 0):.1f}%"
]
})
chart = alt.Chart(chart_data).mark_bar().encode(
y='Sentiment',
x='Count',
color=alt.Color('Sentiment', scale=alt.Scale(
domain=['Positive', 'Negative', 'Neutral'],
range=['green', 'red', 'gray']
)),
tooltip=['Sentiment', 'Count', 'Percentage']
).properties(
width=600,
height=300
)
text = chart.mark_text(
align='left',
baseline='middle',
dx=3
).encode(
text='Percentage'
)
chart_with_text = (chart + text)
st.altair_chart(chart_with_text, use_container_width=True)
except Exception as e:
st.error(f"Error creating visualization: {str(e)}")
# Display sentiment indices if available
if "sentiment_indices" in analysis and analysis["sentiment_indices"]:
st.subheader("Sentiment Indices")
indices = analysis["sentiment_indices"]
try:
if isinstance(indices, dict):
# Display as metrics in columns
cols = st.columns(3)
display_names = {
"positivity_index": "Positivity",
"negativity_index": "Negativity",
"emotional_intensity": "Emotional Intensity",
"controversy_score": "Controversy",
"confidence_score": "Confidence",
"esg_relevance": "ESG Relevance"
}
for i, (key, value) in enumerate(indices.items()):
if isinstance(value, (int, float)):
with cols[i % 3]:
display_name = display_names.get(key, key.replace("_", " ").title())
st.metric(display_name, f"{value:.2f}")
# Create visualization
chart_data = pd.DataFrame({
'Index': [display_names.get(k, k.replace("_", " ").title()) for k in indices.keys()],
'Value': [v if isinstance(v, (int, float)) else 0 for v in indices.values()]
})
chart = alt.Chart(chart_data).mark_bar().encode(
x='Value',
y='Index',
color=alt.Color('Index')
).properties(
width=600,
height=300
)
st.altair_chart(chart, use_container_width=True)
# Add descriptions
with st.expander("Sentiment Indices Explained"):
st.markdown("""
- **Positivity**: Measures the positive sentiment in the articles (0-1)
- **Negativity**: Measures the negative sentiment in the articles (0-1)
- **Emotional Intensity**: Measures the overall emotional content (0-1)
- **Controversy**: High when both positive and negative sentiments are strong (0-1)
- **Confidence**: Confidence in the sentiment analysis (0-1)
- **ESG Relevance**: Relevance to Environmental, Social, and Governance topics (0-1)
""")
except Exception as e:
st.error(f"Error creating indices visualization: {str(e)}")
# Display Final Analysis and Audio
st.header("π― Final Analysis")
if "final_sentiment_analysis" in data:
st.write(data["final_sentiment_analysis"])
# Display sentiment indices in the sidebar
if "sentiment_indices" in analysis and analysis["sentiment_indices"]:
indices = analysis["sentiment_indices"]
if indices and any(isinstance(v, (int, float)) for v in indices.values()):
st.sidebar.markdown("### Sentiment Indices")
for idx_name, idx_value in indices.items():
if isinstance(idx_value, (int, float)):
formatted_name = " ".join(word.capitalize() for word in idx_name.replace("_", " ").split())
st.sidebar.metric(formatted_name, f"{idx_value:.2f}")
# Display ensemble model information if available
if "ensemble_info" in data:
with st.expander("Ensemble Model Details"):
ensemble = data["ensemble_info"]
if "agreement" in ensemble:
st.metric("Model Agreement", f"{ensemble['agreement']*100:.1f}%")
if "models" in ensemble:
st.subheader("Individual Model Results")
models_data = []
for model_name, model_info in ensemble["models"].items():
models_data.append({
"Model": model_name,
"Sentiment": model_info.get("sentiment", "N/A"),
"Confidence": f"{model_info.get('confidence', 0)*100:.1f}%"
})
if models_data:
st.table(pd.DataFrame(models_data))
# Audio Playback Section
st.subheader("π Listen to Analysis (Hindi)")
if data.get("audio_path") and os.path.exists(data["audio_path"]):
st.audio(data["audio_path"])
else:
st.warning("Hindi audio summary not available")
# Total Articles
if "total_articles" in analysis:
st.sidebar.info(f"Found {analysis['total_articles']} articles")
except Exception as e:
st.error(f"Error analyzing company data: {str(e)}")
print(f"Error: {str(e)}")
# Add a disclaimer
st.sidebar.markdown("---")
st.sidebar.markdown("### About")
st.sidebar.write("This app analyzes news articles and provides sentiment analysis for any company.")
if __name__ == "__main__":
main()
|