File size: 11,978 Bytes
f5b7e31
 
 
 
 
 
 
 
eabeab3
f5b7e31
29055b4
 
f5b7e31
 
 
 
 
 
 
 
 
 
 
0f2373c
f5b7e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e84885b
eabeab3
 
 
 
 
e84885b
0f2373c
f5b7e31
 
 
0f2373c
f5b7e31
 
 
 
 
 
 
 
 
 
 
1d2a918
f5b7e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d2a918
f5b7e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f2373c
f5b7e31
 
 
 
 
 
 
 
 
 
 
 
 
0f2373c
 
 
f5b7e31
 
 
 
 
 
 
 
eabeab3
 
 
f5b7e31
 
 
 
eabeab3
f5b7e31
 
 
 
 
 
 
 
 
 
 
0f2373c
 
 
 
 
 
f5b7e31
 
 
 
d4fa864
f5b7e31
 
 
 
86fdee3
f5b7e31
 
d4fa864
86fdee3
 
 
 
f5b7e31
eabeab3
 
f5b7e31
 
 
eabeab3
f5b7e31
 
 
 
eabeab3
f5b7e31
eabeab3
f5b7e31
eabeab3
f5b7e31
eabeab3
f5b7e31
 
 
 
 
 
86fdee3
f5b7e31
 
 
 
 
 
 
 
 
 
 
 
 
29055b4
7e4f1e1
 
f5b7e31
6627f5c
7e4f1e1
f5b7e31
 
29055b4
f5b7e31
 
 
 
 
 
 
 
 
 
 
 
29055b4
 
7e4f1e1
f5b7e31
 
29055b4
7e4f1e1
 
 
 
 
 
 
 
cb05e7b
 
 
 
 
 
 
7e4f1e1
 
 
 
 
29055b4
7e4f1e1
 
 
 
 
 
 
cb05e7b
 
 
 
 
 
 
7e4f1e1
 
 
 
 
29055b4
7e4f1e1
cb05e7b
f5b7e31
7e4f1e1
 
 
 
cb05e7b
 
 
 
 
 
 
7e4f1e1
 
 
 
 
29055b4
f5b7e31
7e4f1e1
 
 
 
 
 
 
 
f5b7e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e4f1e1
29055b4
 
 
f5b7e31
 
 
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
"""
AI News Sentiment Analyzer - Streamlit Web Application
Interactive dashboard for analyzing sentiment of AI-related news
"""

import streamlit as st
import pandas as pd
import plotly.express as px
import json
from api_handler import AINewsAnalyzer
import io


# Page configuration
st.set_page_config(
    page_title="AI News Sentiment Analyzer",
    page_icon="๐Ÿค–",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS for better styling
st.markdown("""
<style> 
    .main-header {
        font-size: 2.5rem;
        font-weight: bold;
        color: #1f77b4;
        text-align: center;
        margin-bottom: 2rem;
    }
    .metric-card {
        background-color: #f0f2f6;
        padding: 1rem;
        border-radius: 0.5rem;
        border-left: 5px solid #1f77b4;
    }
    .positive { color: #28a745; }
    .negative { color: #dc3545; }
    .neutral { color: #6c757d; }
</style>
""", unsafe_allow_html=True)

# @st.cache_data(ttl=1800)  # Cache for 30 minutes
def load_config():
    """Load configuration from config.json"""
    with open('config.json', 'r') as f:
        return json.load(f)

# @st.cache_data(ttl=1800)  # Cache for 30 minutes
def load_news_data(query, days, sources=None, model="TextBlob"):
    """Load and cache news data"""
    try:
        analyzer = AINewsAnalyzer()
        df = analyzer.get_ai_news_with_sentiment(query=query, days=days, sources=sources, model=model)
        return df, None
    except Exception as e:
        return pd.DataFrame(), str(e)


def create_sentiment_distribution(df):
    """Create sentiment distribution pie chart"""
    if df.empty:
        return None
    
    sentiment_counts = df['sentiment_label'].value_counts()
    print("sentiment counts", sentiment_counts)
    fig = px.pie(
        values=sentiment_counts.values,
        names=sentiment_counts.index,
        title="๐ŸŽฏ Sentiment Distribution",
        color_discrete_map={
            'positive': '#28a745',
            'negative': '#dc3545',
            'neutral': '#6c757d'
        }
    )
    
    fig.update_traces(textposition='inside', textinfo='percent+label')
    return fig

def create_source_analysis(df):
    """Create source analysis chart"""
    if df.empty:
        return None
    
    source_sentiment = df.groupby(['source', 'sentiment_label']).size().unstack(fill_value=0)
    source_sentiment = source_sentiment.loc[source_sentiment.sum(axis=1).nlargest(10).index]
    print("source Sentiment", source_sentiment)
    fig = px.bar(
        source_sentiment.reset_index(),
        x='source',
        y=['positive', 'negative', 'neutral'],
        title="๐Ÿ“ฐ Sentiment by News Source (Top 10)",
        color_discrete_map={
            'positive': '#28a745',
            'negative': '#dc3545',
            'neutral': '#6c757d'
        }
    )
    
    fig.update_layout(
        xaxis_title="News Source",
        yaxis_title="Number of Articles",
        xaxis_tickangle=-45
    )
    
    return fig

def create_polarity_distribution(df, thresh: float):
    """Create sentiment polarity distribution"""
    if df.empty:
        return None
    
    fig = px.histogram(
        df,
        x='sentiment_polarity',
        nbins=30,
        title="๐Ÿ“Š Sentiment Polarity Distribution",
        labels={'sentiment_polarity': 'Sentiment Polarity', 'count': 'Number of Articles'}
    )
    
    # Add vertical lines for sentiment boundaries
    fig.add_vline(x=thresh, line_dash="dash", line_color="green", annotation_text="Positive Threshold", annotation_position="top right")
    fig.add_vline(x=-thresh, line_dash="dash", line_color="red", annotation_text="Negative Threshold", annotation_position="top left")
    fig.add_vline(x=0, line_dash="dash", line_color="gray", annotation_text="Neutral", annotation_position="top")
    return fig


def main():
    # Header
    st.markdown("<h1 class='main-header'>๐Ÿค– AI News Sentiment Analyzer</h1>", unsafe_allow_html=True)
    st.markdown("### Discover the sentiment trends in AI-related news from around the world")
    
    # Load configuration
    config = load_config()
    
    # Sidebar controls
    st.sidebar.header("๐Ÿ”ง Analysis Settings")
    
    # Query input
    query_options = config["search_queries"]
    
    selected_query = st.sidebar.selectbox(
        "๐Ÿ“ Search Topic:",
        options=query_options,
        index=0
    )
    
    custom_query = st.sidebar.text_input(
        "Or enter custom search:",
        placeholder="e.g., 'generative AI'"
    )

    model_query = st.sidebar.selectbox(
        "๐Ÿ“ Search a Sentiment Model:",
        options=config["model_options"],
        index=0
    )
    
    # Use custom query if provided
    final_query = custom_query if custom_query else selected_query
    
    # Time range (days)
    days = st.sidebar.slider(
        "๐Ÿ“… Days to analyze:",
        min_value=1,
        max_value=30,
        value=(7,14),
        help="How many days back to search for news"
    )

    # # Date range filter (optional, after data is loaded)
    # st.sidebar.markdown("---")
    # st.sidebar.markdown("#### Optional: Filter by Date Range")

    
    # News sources from config
    news_sources = config["news_sources"]
    
    source_option = st.sidebar.selectbox(
        "๐Ÿ“ฐ Source Category:",
        options=config["source_categories"],
        index=0
    )
    
    if source_option == "Tech Media":
        sources = news_sources["tech_media"]
    elif source_option == "General News":
        sources = news_sources["general_news"]
    elif source_option == "US News":
        sources = news_sources["us_news"]
    elif source_option == "Financial News":
        sources = news_sources["financial_news"]
    else:
        sources = None
    
    # Load data
    if st.sidebar.button("๐Ÿš€ Analyze News", type="primary"):
        with st.spinner(f"Fetching and analyzing news about '{final_query}'..."):
            df, error = load_news_data(final_query, days=days, sources=sources, model=model_query)
            
            if error:
                st.error(f"Error loading data: {error}")
                st.stop()
            
            if df.empty:
                st.warning("No articles found. Try adjusting your search parameters.")
                st.stop()
            
            # Store results in session state
            st.session_state.df = df
            st.session_state.query = final_query
            st.session_state.days = days

    # ===== Display results if data is available =====
    if 'df' in st.session_state and not st.session_state.df.empty:
        df = st.session_state.df
        print(df.info)
        # ===== Summary Metrics =====
        st.markdown("### ๐Ÿ“Š Analysis Summary")
        col1, col2, col3, col4 = st.columns(4)

        with col1:
            st.metric("๐Ÿ“ฐ Total Articles", len(df))
        with col2:
            avg_polarity = df['sentiment_polarity'].mean()
            delta_polarity = f"{avg_polarity:+.3f}"
            st.metric("๐ŸŽญ Avg Sentiment", f"{avg_polarity:.3f}", delta_polarity)
        with col3:
            positive_pct = (len(df[df['sentiment_label'] == 'positive']) / len(df) * 100)
            st.metric("๐Ÿ˜Š Positive %", f"{positive_pct:.1f}%")
        with col4:
            unique_sources = df['source'].nunique()
            st.metric("๐Ÿ“บ News Sources", unique_sources)


        # ===== Charts =====
        st.markdown("### ๐Ÿ“ˆ Visual Analysis")
        col1, col2 = st.columns(2)

        # Sentiment Distribution
        dist_fig = create_sentiment_distribution(df)
        if dist_fig:
            st.plotly_chart(dist_fig, use_container_width=True, key="dist_fig")
            # Export buttons
            buf = io.BytesIO()
            dist_fig.update_layout(template="plotly_white")
            dist_fig.update_layout(plot_bgcolor='white', paper_bgcolor='white')  # ่ฎพ็ฝฎ็™ฝๅบ•
            try:
                dist_fig.write_image(buf, format="png", engine="kaleido")
            except RuntimeError:
                # Fallback if Chrome/Kaleido not available
                html_buf = io.StringIO()
                dist_fig.write_html(html_buf)
                buf = io.BytesIO(html_buf.getvalue().encode('utf-8'))
            st.download_button("๐Ÿ“ท Download Distribution Chart as PNG", buf.getvalue(),
                            "distribution_chart.png", mime="image/png")
            st.download_button("๐ŸŒ Download Distribution Chart as HTML",
                            dist_fig.to_html().encode("utf-8"), "distribution_chart.html",
                            mime="text/html")

        # Source Analysis
        source_fig = create_source_analysis(df)
        if source_fig:
            st.plotly_chart(source_fig, use_container_width=True, key="source_fig")
            buf = io.BytesIO()
            source_fig.update_layout(template="plotly_white")
            source_fig.update_layout(plot_bgcolor='white', paper_bgcolor='white')  # ็™ฝๅบ•
            try:
                source_fig.write_image(buf, format="png", engine="kaleido")
            except RuntimeError:
                # Fallback if Chrome/Kaleido not available
                html_buf = io.StringIO()
                source_fig.write_html(html_buf)
                buf = io.BytesIO(html_buf.getvalue().encode('utf-8'))
            st.download_button("๐Ÿ“ท Download Source Chart as PNG", buf.getvalue(),
                            "source_chart.png", mime="image/png")
            st.download_button("๐ŸŒ Download Source Chart as HTML",
                            source_fig.to_html().encode("utf-8"), "source_chart.html",
                            mime="text/html")

        # Polarity Distribution
        polarity_fig = create_polarity_distribution(df, 0.1)
        if polarity_fig:
            st.plotly_chart(polarity_fig, use_container_width=True, key="polarity_fig")
            buf = io.BytesIO()
            polarity_fig.update_layout(template="plotly_white")
            polarity_fig.update_layout(plot_bgcolor='white', paper_bgcolor='white')  # ็™ฝๅบ•
            try:
                polarity_fig.write_image(buf, format="png", engine="kaleido")
            except RuntimeError:
                # Fallback if Chrome/Kaleido not available
                html_buf = io.StringIO()
                polarity_fig.write_html(html_buf)
                buf = io.BytesIO(html_buf.getvalue().encode('utf-8'))
            st.download_button("๐Ÿ“ท Download Polarity Chart as PNG", buf.getvalue(),
                            "polarity_chart.png", mime="image/png")
            st.download_button("๐ŸŒ Download Polarity Chart as HTML",
                            polarity_fig.to_html().encode("utf-8"), "polarity_chart.html",
                            mime="text/html")

    
        # ===== Export CSV button =====
        csv_data = df.to_csv(index=False).encode('utf-8')
        st.download_button(
            label="๐Ÿ’พ Export Analysis as CSV",
            data=csv_data,
            file_name=f"ai_news_analysis_{st.session_state.query.replace(' ', '_')}.csv",
            mime='text/csv'
        )
    
    else:
        # Welcome message
        st.info("๐Ÿ‘‹ Welcome! Configure your analysis settings in the sidebar and click 'Analyze News' to get started.")
        
        # Sample visualization or instructions
        st.markdown("""
        ### ๐Ÿš€ How to Use:
        
        1. **Choose a topic** from the dropdown or enter your own search term
        2. **Select time range** (1-30 days) to analyze recent news
        3. **Pick news sources** or leave as 'All Sources' for comprehensive coverage
        4. **Click 'Analyze News'** to fetch and analyze articles
        
        ### ๐Ÿ“Š What You'll Get:
        
        - **Sentiment Analysis** of headlines and descriptions
        - **Interactive Charts** showing trends over time
        - **Source Breakdown** to see which outlets cover your topic
        """)
    pass




if __name__ == "__main__":
    main()