Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import json
|
|
| 6 |
import os
|
| 7 |
import plotly.express as px
|
| 8 |
import altair as alt
|
| 9 |
-
from utils import analyze_company_data # Import
|
| 10 |
|
| 11 |
# Set page config
|
| 12 |
st.set_page_config(
|
|
@@ -31,61 +31,163 @@ def process_company(company_name):
|
|
| 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 |
-
|
| 37 |
-
|
| 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 |
-
|
| 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("
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
# Display articles
|
| 70 |
-
st.subheader("
|
| 71 |
for article in data["articles"]:
|
| 72 |
with st.expander(article["title"]):
|
| 73 |
-
st.write(article["
|
| 74 |
-
st.write("
|
| 75 |
-
st.write("Sentiment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
# Display audio if available
|
| 78 |
-
if data.get("audio_path"):
|
|
|
|
| 79 |
st.audio(data["audio_path"])
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
sentiment_df = pd.DataFrame(data["comparative_sentiment_score"])
|
| 85 |
-
fig = px.bar(sentiment_df, title="Sentiment Analysis by Source")
|
| 86 |
-
st.plotly_chart(fig)
|
| 87 |
-
else:
|
| 88 |
-
st.warning("No articles found for this company.")
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
main()
|
|
|
|
| 6 |
import os
|
| 7 |
import plotly.express as px
|
| 8 |
import altair as alt
|
| 9 |
+
from utils import analyze_company_data, TextToSpeechConverter # Import TextToSpeechConverter
|
| 10 |
|
| 11 |
# Set page config
|
| 12 |
st.set_page_config(
|
|
|
|
| 31 |
# Call the analysis function directly from utils
|
| 32 |
data = analyze_company_data(company_name)
|
| 33 |
|
| 34 |
+
# Generate Hindi audio if needed
|
| 35 |
if 'summary' in data:
|
| 36 |
+
tts_converter = TextToSpeechConverter()
|
| 37 |
+
audio_path = tts_converter.generate_audio(data['summary'], f'{company_name}_summary')
|
|
|
|
|
|
|
|
|
|
| 38 |
data['audio_path'] = audio_path
|
| 39 |
|
| 40 |
+
return data
|
| 41 |
except Exception as e:
|
| 42 |
st.error(f"Error processing company: {str(e)}")
|
| 43 |
return {"articles": [], "comparative_sentiment_score": {}, "final_sentiment_analysis": "", "audio_path": None}
|
| 44 |
|
| 45 |
def main():
|
| 46 |
+
st.title("News Summarization App")
|
| 47 |
+
st.write("Analyze news articles and get sentiment analysis for any company.")
|
| 48 |
+
|
| 49 |
+
# User input
|
| 50 |
+
company_name = st.text_input("Enter company name:", "Tesla")
|
| 51 |
+
|
| 52 |
+
if st.button("Analyze"):
|
| 53 |
+
with st.spinner("Analyzing news articles..."):
|
| 54 |
+
try:
|
| 55 |
+
# Process company data
|
| 56 |
+
data = analyze_company_data(company_name)
|
| 57 |
+
|
| 58 |
+
if not data["articles"]:
|
| 59 |
+
st.error("No articles found for analysis.")
|
| 60 |
+
return
|
| 61 |
+
|
| 62 |
+
# Display overall sentiment
|
| 63 |
+
st.subheader("Overall Sentiment Analysis")
|
| 64 |
+
st.write(data["final_sentiment_analysis"])
|
| 65 |
+
|
| 66 |
+
# Create DataFrame for sentiment scores
|
| 67 |
+
sentiment_df = pd.DataFrame(data["comparative_sentiment_score"])
|
| 68 |
+
|
| 69 |
+
# Display sentiment distribution by source
|
| 70 |
+
st.subheader("Sentiment Distribution by Source")
|
| 71 |
+
|
| 72 |
+
# Convert sentiment labels to numeric values for visualization
|
| 73 |
+
sentiment_map = {'positive': 1, 'neutral': 0, 'negative': -1}
|
| 74 |
+
numeric_df = sentiment_df.replace(sentiment_map)
|
| 75 |
+
|
| 76 |
+
# Calculate sentiment distribution
|
| 77 |
+
sentiment_dist = numeric_df.apply(lambda x: x.value_counts(normalize=True).to_dict())
|
| 78 |
+
|
| 79 |
+
# Create a new DataFrame for visualization
|
| 80 |
+
viz_data = []
|
| 81 |
+
for source in sentiment_df.columns:
|
| 82 |
+
dist = sentiment_dist[source]
|
| 83 |
+
for sentiment, percentage in dist.items():
|
| 84 |
+
viz_data.append({
|
| 85 |
+
'Source': source,
|
| 86 |
+
'Sentiment': sentiment,
|
| 87 |
+
'Percentage': percentage * 100
|
| 88 |
+
})
|
| 89 |
+
|
| 90 |
+
viz_df = pd.DataFrame(viz_data)
|
| 91 |
+
|
| 92 |
+
# Create stacked bar chart
|
| 93 |
+
fig = px.bar(viz_df,
|
| 94 |
+
x='Source',
|
| 95 |
+
y='Percentage',
|
| 96 |
+
color='Sentiment',
|
| 97 |
+
title='Sentiment Distribution by Source',
|
| 98 |
+
barmode='stack')
|
| 99 |
+
|
| 100 |
+
fig.update_layout(
|
| 101 |
+
yaxis_title='Percentage',
|
| 102 |
+
xaxis_title='News Source',
|
| 103 |
+
legend_title='Sentiment'
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
st.plotly_chart(fig)
|
| 107 |
+
|
| 108 |
+
# Display fine-grained sentiment analysis
|
| 109 |
+
st.subheader("Fine-grained Sentiment Analysis")
|
| 110 |
+
|
| 111 |
+
# Create tabs for different fine-grained analyses
|
| 112 |
+
tab1, tab2, tab3 = st.tabs(["Financial Sentiment", "Emotional Sentiment", "ESG Sentiment"])
|
| 113 |
+
|
| 114 |
+
with tab1:
|
| 115 |
+
st.write("Financial Market Impact Analysis")
|
| 116 |
+
financial_data = []
|
| 117 |
+
for article in data["articles"]:
|
| 118 |
+
if "financial_sentiment" in article:
|
| 119 |
+
financial_data.append({
|
| 120 |
+
"Article": article["title"],
|
| 121 |
+
"Financial Impact": article["financial_sentiment"],
|
| 122 |
+
"Confidence": article.get("fine_grained_sentiment", {}).get("models", {}).get("financial", {}).get("confidence", 0)
|
| 123 |
+
})
|
| 124 |
+
if financial_data:
|
| 125 |
+
st.dataframe(pd.DataFrame(financial_data))
|
| 126 |
+
else:
|
| 127 |
+
st.info("Financial sentiment analysis not available for these articles.")
|
| 128 |
+
|
| 129 |
+
with tab2:
|
| 130 |
+
st.write("Emotional Sentiment Analysis")
|
| 131 |
+
emotional_data = []
|
| 132 |
+
for article in data["articles"]:
|
| 133 |
+
if "emotional_sentiment" in article:
|
| 134 |
+
emotional_data.append({
|
| 135 |
+
"Article": article["title"],
|
| 136 |
+
"Emotional Impact": article["emotional_sentiment"],
|
| 137 |
+
"Confidence": article.get("fine_grained_sentiment", {}).get("models", {}).get("emotion", {}).get("confidence", 0)
|
| 138 |
+
})
|
| 139 |
+
if emotional_data:
|
| 140 |
+
st.dataframe(pd.DataFrame(emotional_data))
|
| 141 |
+
else:
|
| 142 |
+
st.info("Emotional sentiment analysis not available for these articles.")
|
| 143 |
|
| 144 |
+
with tab3:
|
| 145 |
+
st.write("ESG (Environmental, Social, Governance) Analysis")
|
| 146 |
+
esg_data = []
|
| 147 |
+
for article in data["articles"]:
|
| 148 |
+
if "esg_sentiment" in article:
|
| 149 |
+
esg_data.append({
|
| 150 |
+
"Article": article["title"],
|
| 151 |
+
"ESG Impact": article["esg_sentiment"],
|
| 152 |
+
"Confidence": article.get("fine_grained_sentiment", {}).get("models", {}).get("esg", {}).get("confidence", 0)
|
| 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 |
+
fine_grained = article.get("fine_grained_sentiment", {})
|
| 169 |
+
if fine_grained:
|
| 170 |
+
st.write("**Fine-grained Analysis:**")
|
| 171 |
+
for model_name, model_data in fine_grained.get("models", {}).items():
|
| 172 |
+
st.write(f"- {model_name.title()}: {model_data.get('category', 'N/A')} (Confidence: {model_data.get('confidence', 0):.2f})")
|
| 173 |
+
|
| 174 |
+
# Display sentiment indices if available
|
| 175 |
+
indices = article.get("sentiment_indices", {})
|
| 176 |
+
if indices:
|
| 177 |
+
st.write("**Sentiment Indices:**")
|
| 178 |
+
for index_name, value in indices.items():
|
| 179 |
+
st.write(f"- {index_name.replace('_', ' ').title()}: {value:.2f}")
|
| 180 |
+
|
| 181 |
+
st.write(f"**URL:** [{article['url']}]({article['url']})")
|
| 182 |
|
| 183 |
+
# Display audio player if audio is available
|
| 184 |
+
if data.get("audio_path") and os.path.exists(data["audio_path"]):
|
| 185 |
+
st.subheader("Hindi Audio Summary")
|
| 186 |
st.audio(data["audio_path"])
|
| 187 |
|
| 188 |
+
except Exception as e:
|
| 189 |
+
st.error(f"Error analyzing company data: {str(e)}")
|
| 190 |
+
print(f"Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
if __name__ == "__main__":
|
| 193 |
main()
|