Spaces:
Sleeping
Sleeping
Update api.py
Browse files
api.py
CHANGED
@@ -1,72 +1,182 @@
|
|
1 |
-
"""
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
import
|
8 |
-
|
9 |
-
import
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
company
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Streamlit frontend for the News Summarization application."""
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
import pandas as pd
|
5 |
+
import json
|
6 |
+
import os
|
7 |
+
import plotly.express as px
|
8 |
+
import altair as alt
|
9 |
+
from utils import analyze_company_data # Import the analysis function directly
|
10 |
+
|
11 |
+
# Set page config
|
12 |
+
st.set_page_config(
|
13 |
+
page_title="News Summarization App",
|
14 |
+
page_icon="📰",
|
15 |
+
layout="wide"
|
16 |
+
)
|
17 |
+
|
18 |
+
# Show loading message
|
19 |
+
with st.spinner("Initializing the application... Please wait while we load the models."):
|
20 |
+
# Initialize components
|
21 |
+
try:
|
22 |
+
from utils import NewsExtractor, SentimentAnalyzer, TextSummarizer, TextToSpeechConverter
|
23 |
+
st.success("Application initialized successfully!")
|
24 |
+
except Exception as e:
|
25 |
+
st.error(f"Error initializing application: {str(e)}")
|
26 |
+
st.info("Please try refreshing the page.")
|
27 |
+
|
28 |
+
def process_company(company_name):
|
29 |
+
"""Process company data directly."""
|
30 |
+
try:
|
31 |
+
# Call the analysis function directly from utils
|
32 |
+
data = analyze_company_data(company_name)
|
33 |
+
|
34 |
+
# Generate audio if needed
|
35 |
+
if 'summary' in data:
|
36 |
+
from gtts import gTTS
|
37 |
+
tts = gTTS(text=data['summary'], lang='en')
|
38 |
+
audio_path = os.path.join('audio_output', f'{company_name}_summary.mp3')
|
39 |
+
os.makedirs('audio_output', exist_ok=True)
|
40 |
+
tts.save(audio_path)
|
41 |
+
data['audio_path'] = audio_path
|
42 |
+
|
43 |
+
return data
|
44 |
+
except Exception as e:
|
45 |
+
st.error(f"Error processing company: {str(e)}")
|
46 |
+
return {"articles": [], "comparative_sentiment_score": {}, "final_sentiment_analysis": "", "audio_path": None}
|
47 |
+
|
48 |
+
def main():
|
49 |
+
st.title("News Summarization App")
|
50 |
+
st.write("Analyze news articles and get sentiment analysis for any company.")
|
51 |
+
|
52 |
+
# User input
|
53 |
+
company_name = st.text_input("Enter company name:", "Tesla")
|
54 |
+
|
55 |
+
if st.button("Analyze"):
|
56 |
+
with st.spinner("Analyzing news articles..."):
|
57 |
+
try:
|
58 |
+
# Process company data
|
59 |
+
data = analyze_company_data(company_name)
|
60 |
+
|
61 |
+
if not data["articles"]:
|
62 |
+
st.error("No articles found for analysis.")
|
63 |
+
return
|
64 |
+
|
65 |
+
# Display overall sentiment
|
66 |
+
st.subheader("Overall Sentiment Analysis")
|
67 |
+
st.write(data["final_sentiment_analysis"])
|
68 |
+
|
69 |
+
# Create DataFrame for sentiment scores
|
70 |
+
sentiment_df = pd.DataFrame(data["comparative_sentiment_score"])
|
71 |
+
|
72 |
+
# Display sentiment distribution by source
|
73 |
+
st.subheader("Sentiment Distribution by Source")
|
74 |
+
|
75 |
+
# Convert sentiment labels to numeric values for visualization
|
76 |
+
sentiment_map = {'positive': 1, 'neutral': 0, 'negative': -1}
|
77 |
+
numeric_df = sentiment_df.replace(sentiment_map)
|
78 |
+
|
79 |
+
# Calculate sentiment distribution
|
80 |
+
sentiment_dist = numeric_df.apply(lambda x: x.value_counts(normalize=True).to_dict())
|
81 |
+
|
82 |
+
# Create a new DataFrame for visualization
|
83 |
+
viz_data = []
|
84 |
+
for source in sentiment_df.columns:
|
85 |
+
dist = sentiment_dist[source]
|
86 |
+
for sentiment, percentage in dist.items():
|
87 |
+
viz_data.append({
|
88 |
+
'Source': source,
|
89 |
+
'Sentiment': sentiment,
|
90 |
+
'Percentage': percentage * 100
|
91 |
+
})
|
92 |
+
|
93 |
+
viz_df = pd.DataFrame(viz_data)
|
94 |
+
|
95 |
+
# Create stacked bar chart
|
96 |
+
fig = px.bar(viz_df,
|
97 |
+
x='Source',
|
98 |
+
y='Percentage',
|
99 |
+
color='Sentiment',
|
100 |
+
title='Sentiment Distribution by Source',
|
101 |
+
barmode='stack')
|
102 |
+
|
103 |
+
fig.update_layout(
|
104 |
+
yaxis_title='Percentage',
|
105 |
+
xaxis_title='News Source',
|
106 |
+
legend_title='Sentiment'
|
107 |
+
)
|
108 |
+
|
109 |
+
st.plotly_chart(fig)
|
110 |
+
|
111 |
+
# Display fine-grained sentiment analysis
|
112 |
+
st.subheader("Fine-grained Sentiment Analysis")
|
113 |
+
|
114 |
+
# Create tabs for different fine-grained analyses
|
115 |
+
tab1, tab2, tab3 = st.tabs(["Financial Sentiment", "Emotional Sentiment", "ESG Sentiment"])
|
116 |
+
|
117 |
+
with tab1:
|
118 |
+
st.write("Financial Market Impact Analysis")
|
119 |
+
financial_data = []
|
120 |
+
for article in data["articles"]:
|
121 |
+
if "financial_sentiment" in article:
|
122 |
+
financial_data.append({
|
123 |
+
"Article": article["title"],
|
124 |
+
"Financial Impact": article["financial_sentiment"]
|
125 |
+
})
|
126 |
+
if financial_data:
|
127 |
+
st.dataframe(pd.DataFrame(financial_data))
|
128 |
+
else:
|
129 |
+
st.info("Financial sentiment analysis not available for these articles.")
|
130 |
+
|
131 |
+
with tab2:
|
132 |
+
st.write("Emotional Sentiment Analysis")
|
133 |
+
emotional_data = []
|
134 |
+
for article in data["articles"]:
|
135 |
+
if "emotional_sentiment" in article:
|
136 |
+
emotional_data.append({
|
137 |
+
"Article": article["title"],
|
138 |
+
"Emotional Impact": article["emotional_sentiment"]
|
139 |
+
})
|
140 |
+
if emotional_data:
|
141 |
+
st.dataframe(pd.DataFrame(emotional_data))
|
142 |
+
else:
|
143 |
+
st.info("Emotional sentiment analysis not available for these articles.")
|
144 |
+
|
145 |
+
with tab3:
|
146 |
+
st.write("ESG (Environmental, Social, Governance) Analysis")
|
147 |
+
esg_data = []
|
148 |
+
for article in data["articles"]:
|
149 |
+
if "esg_sentiment" in article:
|
150 |
+
esg_data.append({
|
151 |
+
"Article": article["title"],
|
152 |
+
"ESG Impact": article["esg_sentiment"]
|
153 |
+
})
|
154 |
+
if esg_data:
|
155 |
+
st.dataframe(pd.DataFrame(esg_data))
|
156 |
+
else:
|
157 |
+
st.info("ESG sentiment analysis not available for these articles.")
|
158 |
+
|
159 |
+
# Display articles with detailed sentiment analysis
|
160 |
+
st.subheader("Recent Articles")
|
161 |
+
for article in data["articles"]:
|
162 |
+
with st.expander(article["title"]):
|
163 |
+
st.write(f"**Source:** {article['source']}")
|
164 |
+
st.write(f"**Summary:** {article['summary']}")
|
165 |
+
st.write(f"**Overall Sentiment:** {article['sentiment']}")
|
166 |
+
|
167 |
+
# Display fine-grained sentiment if available
|
168 |
+
if "financial_sentiment" in article:
|
169 |
+
st.write(f"**Financial Impact:** {article['financial_sentiment']}")
|
170 |
+
if "emotional_sentiment" in article:
|
171 |
+
st.write(f"**Emotional Impact:** {article['emotional_sentiment']}")
|
172 |
+
if "esg_sentiment" in article:
|
173 |
+
st.write(f"**ESG Impact:** {article['esg_sentiment']}")
|
174 |
+
|
175 |
+
st.write(f"**URL:** [{article['url']}]({article['url']})")
|
176 |
+
|
177 |
+
except Exception as e:
|
178 |
+
st.error(f"Error analyzing company data: {str(e)}")
|
179 |
+
print(f"Error: {str(e)}")
|
180 |
+
|
181 |
+
if __name__ == "__main__":
|
182 |
+
main()
|