File size: 5,930 Bytes
1b8aae0
3860703
8bc4463
8c60672
1852717
1b8aae0
8c60672
 
e636dcd
1b8aae0
3860703
37430cf
 
 
 
 
a58b274
 
 
 
1852717
37430cf
3860703
8c60672
 
 
3860703
8c60672
 
 
 
 
 
 
 
 
3860703
 
8c60672
 
 
 
 
 
 
 
 
 
 
 
3860703
 
1b8aae0
e636dcd
8c60672
37430cf
 
8c60672
1b8aae0
8c60672
 
6ee5d7e
 
 
e636dcd
8c60672
e636dcd
 
 
 
8c60672
e636dcd
 
 
 
 
 
8c60672
e636dcd
 
8c60672
e636dcd
8c60672
e636dcd
8c60672
e636dcd
8c60672
3860703
6ee5d7e
e636dcd
8c60672
e636dcd
8c60672
6ee5d7e
8c60672
6ee5d7e
8c60672
 
 
 
3860703
8c60672
a0616bd
6ee5d7e
 
3860703
 
8c60672
e636dcd
3860703
 
d166bb5
a0616bd
e0c4337
 
c20d356
e0c4337
c20d356
 
 
8c60672
c20d356
 
8c60672
 
 
 
 
 
 
c20d356
ee05df2
 
 
a58b274
 
 
 
 
 
 
 
 
 
ee05df2
 
a58b274
 
 
 
 
 
 
 
ee05df2
a58b274
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
import streamlit as st
import requests

# Define API URL
FASTAPI_URL = "http://127.0.0.1:8000"

# Configure Streamlit app layout
st.set_page_config(page_title="Insightful News AI", page_icon="πŸ€–", layout="centered")
st.title("Sentiment-Driven News Summarization with AI-Powered Speech")

# Sidebar Controls
st.sidebar.header("Controls")
company = st.sidebar.text_input("Enter Company Name")
get_news = st.sidebar.button("Get News Summary")
compare_news = st.sidebar.button("Comparative Analysis")
generate_audio = st.sidebar.button("Generate Audio")

st.sidebar.subheader("πŸ” Search & Filter News")
search_keyword = st.sidebar.text_input("Search by keyword")
search_sentiment = st.sidebar.selectbox("Filter by Sentiment", ["", "Positive", "Negative", "Neutral"])
search = st.sidebar.button("Search")


def fetch_data(endpoint, params=None):
    """
    Fetch data from the given API endpoint.
    
    Args:
        endpoint (str): API endpoint to fetch data from.
        params (dict, optional): Query parameters for the request.
    
    Returns:
        dict: JSON response if successful, else None.
    """
    response = requests.get(f"{FASTAPI_URL}/{endpoint}", params=params)
    return response.json() if response.status_code == 200 else None


# Fetch News Summary
if get_news:
    st.write("## Company: ", company)
    news_data = fetch_data("news-analysis", {"company": company})

    if news_data:
        for i, article in enumerate(news_data.get("Articles", []), start=1):
            st.write(f"### {i}. {article.get('Title', 'No Title')}")
            st.write(f"**Summary:** {article.get('Summary', 'No Summary')}")
            st.write(f"**Sentiment:** {article.get('Sentiment', 'Unknown')}")
            st.write(f"**Topics:** {', '.join(article.get('Topics', []))}")
            st.markdown("---")  # Separator between articles
    else:
        st.error("Error fetching news. Please try again.")


# Comparative Analysis
if compare_news:
    st.write("## Comparative Analysis")
    comparison_data = fetch_data("comparative-analyst", {"company": company})

    if comparison_data:
        # Extract overall sentiment analysis
        sentiment_data = comparison_data.get("Sentiment Analysis", {})
        sentiment_distribution = sentiment_data.get("Sentiment Distribution", {})
        final_sentiment = sentiment_data.get("Final Sentiment Summary", "No sentiment summary available.")

        # Extract sentiment counts safely
        positive_count = sentiment_distribution.get("Positive", 0)
        negative_count = sentiment_distribution.get("Negative", 0)
        neutral_count = sentiment_distribution.get("Neutral", 0)

        # Display sentiment distribution
        st.write("### Sentiment Distribution")
        col1, col2, col3 = st.columns(3)
        col1.metric(label="Positive", value=positive_count)
        col2.metric(label="Negative", value=negative_count)
        col3.metric(label="Neutral", value=neutral_count)

        # Display final sentiment summary
        st.write("### Final Sentiment")
        if positive_count > negative_count:
            st.success(f"**{final_sentiment}**")  # Green for Positive
        elif positive_count < negative_count:
            st.error(f"**{final_sentiment}**")  # Red for Negative
        else:
            st.warning(f"**{final_sentiment}**")  # Yellow for Neutral

        # Display topic overlap
        st.write("### Topic Overlap")
        topic_overlap = comparison_data.get("Topic Overlap", {})

        # Display common topics
        common_topics = topic_overlap.get("Common Topics", [])
        st.write(f"**Common Topics (Appearing in β‰₯3 articles):** {', '.join(common_topics) if common_topics else 'None'}")

        # Display unique topics per article
        unique_topics_per_article = topic_overlap.get("Unique Topics Per Article", [])
        for topic_data in unique_topics_per_article:
            article_number = topic_data.get("Article", "Unknown")
            unique_topics = topic_data.get("Unique Topics", [])
            st.write(f"**Unique Topics in Article {article_number}:** {', '.join(unique_topics) if unique_topics else 'None'}")

        # Display Final LLM-Based Sentiment Analysis
        st.write("## Overall Sentiment Summary")
        final_llm_summary = comparison_data.get("Final Sentiment Analysis", "No summary available.")
        st.info(f"**{final_llm_summary}**")

    else:
        st.error("Error fetching comparative analysis. Please try again.")


# Generate Hindi Speech Audio
if generate_audio:
    st.write("### Hindi Audio Summary")

    audio_url = f"{FASTAPI_URL}/generate-audio/?company={company}"
    response = requests.get(audio_url)

    if response.status_code == 200:
        audio_data = response.content

        # Play the audio
        st.audio(audio_data, format="audio/mp3")

        # Provide a download button
        st.download_button(
            label="Download Hindi Audio",
            data=audio_data,
            file_name="hindi_summary.mp3",
            mime="audio/mpeg"
        )
    else:
        st.error("Failed to generate audio. Please try again.")



# (BONUS) Search for Articles based on Keyword or Sentiment
if search:
    params = {"company": company}
    if search_keyword:
        params["keyword"] = search_keyword
    if search_sentiment:
        params["sentiment"] = search_sentiment

    response = requests.get(f"{FASTAPI_URL}/search-news/", params=params)

    if response.status_code == 200:
        search_results = response.json()["Filtered Articles"]
        if search_results:
            for i, article in enumerate(search_results, start=1):
                st.write(f"### {i}. {article['title']}")
                st.write(f"**Summary:** {article['summary']}")
                st.markdown("---")
        else:
            st.warning("No matching articles found.")
    else:
        st.error("Failed to fetch search results.")