File size: 9,075 Bytes
aa675fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f8d0f6
 
 
 
aa675fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f8d0f6
 
aa675fc
 
8f8d0f6
 
 
aa675fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f8d0f6
aa675fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f8d0f6
aa675fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f8d0f6
aa675fc
 
 
 
 
 
 
 
 
 
8f8d0f6
aa675fc
 
 
 
e5b0bb1
 
 
 
aa675fc
 
e5b0bb1
 
 
 
 
 
 
 
 
 
8f8d0f6
e5b0bb1
aa675fc
 
 
e5b0bb1
 
 
8f8d0f6
aa675fc
 
 
 
 
8f8d0f6
aa675fc
 
 
 
 
e5b0bb1
aa675fc
 
 
 
 
 
 
8f8d0f6
aa675fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f8d0f6
aa675fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py
"""
Streamlit UI for the News Sentiment Analyzer.
- Calls the in-process FastAPI orchestrator (NewsAnalyzer) directly for zero-latency on Spaces.
- Lightweight, CPU-safe widgets with progress, charts, tables, and exports (CSV/JSON/PDF + Audio).
"""

from __future__ import annotations

import io
import json
import logging
from datetime import datetime
from typing import Any, Dict, List

import streamlit as st
import pandas as pd
import plotly.express as px

# Local modules
from api import analyzer  # global NewsAnalyzer instance
from utils import (
    setup_logging,
    load_config,
    calculate_sentiment_distribution,
    format_number,
)
from report import generate_pdf_report  # your existing PDF generator

# ------------------------------------------------------------------------------
# App setup
# ------------------------------------------------------------------------------

setup_logging()
logger = logging.getLogger("app")

st.set_page_config(
    page_title="News Sentiment Analyzer",
    page_icon="📰",
    layout="wide",
)

# Minimal CSS polish
st.markdown(
    """
    <style>
    .small { font-size: 0.85rem; color: #666; }
    .ok { color: #1b8a5a; }
    .bad { color: #b00020; }
    .neutral { color: #666; }
    .stProgress > div > div > div { background-color: #4b8bf4; }
    .block-container { padding-top: 2rem; }
    </style>
    """,
    unsafe_allow_html=True,
)

# ------------------------------------------------------------------------------
# Sidebar controls
# ------------------------------------------------------------------------------

cfg = load_config()

st.sidebar.header("Settings")
default_query = st.sidebar.text_input("Company / Keyword", value="Tesla")
num_articles = st.sidebar.slider("Number of articles", 5, 50, 20, step=1)
languages = st.sidebar.multiselect(
    "Summaries in languages",
    options=["English", "Hindi", "Tamil"],
    default=["English"],
)
include_audio = st.sidebar.checkbox("Generate audio summary", value=True)
sentiment_models = st.sidebar.multiselect(
    "Sentiment models",
    options=["VADER", "Loughran-McDonald", "FinBERT"],
    default=["VADER", "Loughran-McDonald", "FinBERT"],
)
st.sidebar.caption("Tip: disable FinBERT if your Space has < 2GB RAM.")

run_btn = st.sidebar.button("Analyze", use_container_width=True, type="primary")

# ------------------------------------------------------------------------------
# Header
# ------------------------------------------------------------------------------

st.title("📰 News Sentiment Analyzer")
st.caption("Scrape → Summarize → Sentiment → Keywords → Multilingual → Audio — deployed on Hugging Face Spaces")

# ------------------------------------------------------------------------------
# Helper functions
# ------------------------------------------------------------------------------

def _articles_to_df(articles: List[Dict[str, Any]]) -> pd.DataFrame:
    rows = []
    for a in articles:
        rows.append(
            {
                "title": a.get("title", ""),
                "source": a.get("source", ""),
                "date": a.get("date"),
                "url": a.get("url", ""),
                "summary": a.get("summary", ""),
                "sentiment_compound": a.get("sentiment", {}).get("compound", 0.0),
            }
        )
    df = pd.DataFrame(rows)
    if "date" in df.columns:
        try:
            df["date"] = pd.to_datetime(df["date"])
        except Exception:
            pass
    return df


def _render_distribution(dist: Dict[str, Any]):
    cols = st.columns(4)
    cols[0].metric("Total", dist.get("total", 0))
    cols[1].metric("Positive", dist.get("positive", 0))
    cols[2].metric("Negative", dist.get("negative", 0))
    cols[3].metric("Neutral", dist.get("neutral", 0))

    chart_df = pd.DataFrame(
        {
            "Sentiment": ["Positive", "Negative", "Neutral"],
            "Count": [
                dist.get("positive", 0),
                dist.get("negative", 0),
                dist.get("neutral", 0),
            ],
        }
    )
    fig = px.bar(chart_df, x="Sentiment", y="Count", title="Sentiment distribution")
    st.plotly_chart(fig, use_container_width=True)


def _download_buttons(results: Dict[str, Any], df: pd.DataFrame):
    c1, c2, c3 = st.columns(3)

    # JSON
    with c1:
        json_bytes = json.dumps(results, default=str, indent=2).encode("utf-8")
        st.download_button(
            "Download JSON",
            data=json_bytes,
            file_name=f"news_analysis_{results['query']}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
            mime="application/json",
            use_container_width=True,
        )

    # CSV
    with c2:
        csv_bytes = df.to_csv(index=False).encode("utf-8")
        st.download_button(
            "Download CSV",
            data=csv_bytes,
            file_name=f"news_analysis_{results['query']}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
            mime="text/csv",
            use_container_width=True,
        )

    # PDF
    with c3:
        try:
            pdf_obj = generate_pdf_report(results)

            # Accept bytes, BytesIO/file-like, or path
            pdf_bytes = None
            if isinstance(pdf_obj, (bytes, bytearray)):
                pdf_bytes = pdf_obj
            elif hasattr(pdf_obj, "read"):  # file-like object
                try:
                    pdf_bytes = pdf_obj.read()
                    if not pdf_bytes and hasattr(pdf_obj, "getvalue"):
                        pdf_bytes = pdf_obj.getvalue()
                finally:
                    try:
                        pdf_obj.close()
                    except Exception:
                        pass
            else:
                # Assume file path
                with open(pdf_obj, "rb") as f:
                    pdf_bytes = f.read()

            if not pdf_bytes:
                raise ValueError("Empty PDF bytes")

            st.download_button(
                "Download PDF",
                data=pdf_bytes,
                file_name=f"news_analysis_{results['query']}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf",
                mime="application/pdf",
                use_container_width=True,
            )
        except Exception as e:
            st.info("PDF generator not available or failed. You can still export JSON/CSV.")
            logger.exception(f"PDF generation failed: {e}")



def _render_audio(audio_files: Dict[str, Any]):
    if not audio_files:
        return
    st.subheader("Audio summaries")
    for lang, path in audio_files.items():
        if path:
            st.markdown(f"**{lang}**")
            try:
                with open(path, "rb") as f:
                    st.audio(f.read(), format="audio/mp3")
            except Exception:
                # Some Spaces require passing the path directly
                st.audio(path)


# ------------------------------------------------------------------------------
# Main flow
# ------------------------------------------------------------------------------

if run_btn:
    st.info("Starting analysis… this may take ~30–60 seconds on a CPU Space (FinBERT/summarizer/translation are heavy).")

    progress = st.progress(0, text="Initializing…")

    def _cb(p: int, status: str):
        try:
            progress.progress(p, text=status)
        except Exception:
            pass

    config = {
        "query": default_query,
        "num_articles": num_articles,
        "languages": languages or ["English"],
        "include_audio": include_audio,
        "sentiment_models": sentiment_models or ["VADER", "Loughran-McDonald", "FinBERT"],
    }

    try:
        results: Dict[str, Any] = analyzer.analyze_news(config, progress_callback=_cb)
    except Exception as e:
        progress.empty()
        st.error(f"Analysis failed: {e}")
        st.stop()

    progress.empty()

    # Handle empty gracefully
    if not results.get("articles"):
        st.warning("No articles found or scraping failed. Try a different query or reduce filters.")
        st.stop()

    # Header summary
    st.subheader(f"Results — {results['query']}")
    dist = results["summary"]["distribution"]
    _render_distribution(dist)

    # Keywords
    if results.get("keywords"):
        top_kw = ", ".join(kw["keyword"] for kw in results["keywords"][:12])
        st.markdown(f"**Top keywords:** {top_kw}")

    # Articles table
    df = _articles_to_df(results["articles"])
    st.dataframe(df, use_container_width=True, hide_index=True)

    # Audio (optional)
    if results.get("audio_files"):
        _render_audio(results["audio_files"])

    # Exports
    st.divider()
    _download_buttons(results, df)

else:
    st.info("Enter a company/keyword on the left and click Analyze. Example: Tesla, Nvidia, Reliance, HDFC, Adani, BYD.")

# Footer
st.markdown(
    "<p class='small'>Built with Streamlit + FastAPI · CPU-only · "
    "FinBERT/VADER/LM sentiment · BART/T5 summarization · YAKE keywords · gTTS audio.</p>",
    unsafe_allow_html=True,
)