proKBD's picture
Update app.py
52c4b49 verified
raw
history blame
3.37 kB
"""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 # Import the analysis function directly
# 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 audio if needed
if 'summary' in data:
from gtts import gTTS
tts = gTTS(text=data['summary'], lang='en')
audio_path = os.path.join('audio_output', f'{company_name}_summary.mp3')
os.makedirs('audio_output', exist_ok=True)
tts.save(audio_path)
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")
# Company name input
company_name = st.text_input("Enter Company Name", "")
if company_name:
with st.spinner("Analyzing company news..."):
data = process_company(company_name)
# Display results
if data["articles"]:
st.subheader("πŸ“Š Analysis Results")
# Display sentiment analysis
if data["final_sentiment_analysis"]:
st.write("Sentiment Analysis:", data["final_sentiment_analysis"])
# Display articles
st.subheader("πŸ“° Recent Articles")
for article in data["articles"]:
with st.expander(article["title"]):
st.write(article["summary"])
st.write("Source:", article["source"])
st.write("Sentiment:", article["sentiment"])
# Display audio if available
if data.get("audio_path"):
st.audio(data["audio_path"])
# Display visualizations
if data.get("comparative_sentiment_score"):
st.subheader("πŸ“ˆ Sentiment Distribution")
sentiment_df = pd.DataFrame(data["comparative_sentiment_score"])
fig = px.bar(sentiment_df, title="Sentiment Analysis by Source")
st.plotly_chart(fig)
else:
st.warning("No articles found for this company.")
if __name__ == "__main__":
main()