Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,9 @@
|
|
1 |
-
import nltk
|
2 |
-
try:
|
3 |
-
nltk.data.find('tokenizers/punkt')
|
4 |
-
except LookupError:
|
5 |
-
nltk.download('punkt')
|
6 |
-
|
7 |
-
import nltk
|
8 |
-
nltk.download('punkt')
|
9 |
-
|
10 |
-
|
11 |
import streamlit as st
|
12 |
from api import get_news, NewsRequest
|
13 |
from utils import text_to_speech
|
14 |
-
import time
|
15 |
import base64
|
16 |
-
import os
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
# Function to play audio automatically
|
21 |
def autoplay_audio(file_path):
|
22 |
with open(file_path, "rb") as audio_file:
|
23 |
audio_bytes = audio_file.read()
|
@@ -29,20 +15,19 @@ def autoplay_audio(file_path):
|
|
29 |
"""
|
30 |
st.markdown(audio_tag, unsafe_allow_html=True)
|
31 |
|
32 |
-
# UI
|
33 |
def main():
|
34 |
st.set_page_config(page_title="News Sentiment & Summarization", layout="wide")
|
35 |
|
36 |
-
# Sidebar
|
37 |
st.sidebar.markdown(
|
38 |
"<h1 style='color:#00FFAF; text-align:center;'>π° News Finder</h1>",
|
39 |
unsafe_allow_html=True
|
40 |
)
|
41 |
-
st.sidebar.write("Enter a company name
|
42 |
|
43 |
company_name = st.sidebar.text_input("π Company Name:")
|
44 |
fetch_button = st.sidebar.button("Fetch News")
|
45 |
-
|
46 |
st.sidebar.markdown("---")
|
47 |
sentiment_summary_placeholder = st.sidebar.empty()
|
48 |
|
@@ -53,13 +38,13 @@ def main():
|
|
53 |
|
54 |
if fetch_button and company_name:
|
55 |
response = get_news(NewsRequest(company=company_name))
|
56 |
-
articles = response
|
57 |
|
58 |
if not articles:
|
59 |
st.warning("No news found for this company.")
|
60 |
return
|
61 |
|
62 |
-
#
|
63 |
sentiments = [article['sentiment'] for article in articles]
|
64 |
positive = sentiments.count("Positive")
|
65 |
negative = sentiments.count("Negative")
|
@@ -74,6 +59,7 @@ def main():
|
|
74 |
</div>
|
75 |
""", unsafe_allow_html=True)
|
76 |
|
|
|
77 |
all_headlines = ". ".join([f"{idx + 1}. {article['title']}" for idx, article in enumerate(articles)])
|
78 |
audio_file = text_to_speech(all_headlines)
|
79 |
if audio_file:
|
@@ -81,6 +67,7 @@ def main():
|
|
81 |
|
82 |
st.write("## Latest News Articles")
|
83 |
|
|
|
84 |
for idx, article in enumerate(articles, start=1):
|
85 |
sentiment_color = {
|
86 |
"Positive": "#4CAF50",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from api import get_news, NewsRequest
|
3 |
from utils import text_to_speech
|
|
|
4 |
import base64
|
|
|
5 |
|
6 |
+
# Function to auto-play audio
|
|
|
|
|
7 |
def autoplay_audio(file_path):
|
8 |
with open(file_path, "rb") as audio_file:
|
9 |
audio_bytes = audio_file.read()
|
|
|
15 |
"""
|
16 |
st.markdown(audio_tag, unsafe_allow_html=True)
|
17 |
|
18 |
+
# Streamlit UI
|
19 |
def main():
|
20 |
st.set_page_config(page_title="News Sentiment & Summarization", layout="wide")
|
21 |
|
22 |
+
# Sidebar UI
|
23 |
st.sidebar.markdown(
|
24 |
"<h1 style='color:#00FFAF; text-align:center;'>π° News Finder</h1>",
|
25 |
unsafe_allow_html=True
|
26 |
)
|
27 |
+
st.sidebar.write("Enter a company name to fetch the latest summarized news with sentiment analysis:")
|
28 |
|
29 |
company_name = st.sidebar.text_input("π Company Name:")
|
30 |
fetch_button = st.sidebar.button("Fetch News")
|
|
|
31 |
st.sidebar.markdown("---")
|
32 |
sentiment_summary_placeholder = st.sidebar.empty()
|
33 |
|
|
|
38 |
|
39 |
if fetch_button and company_name:
|
40 |
response = get_news(NewsRequest(company=company_name))
|
41 |
+
articles = response.get("articles", [])
|
42 |
|
43 |
if not articles:
|
44 |
st.warning("No news found for this company.")
|
45 |
return
|
46 |
|
47 |
+
# Sentiment summary display
|
48 |
sentiments = [article['sentiment'] for article in articles]
|
49 |
positive = sentiments.count("Positive")
|
50 |
negative = sentiments.count("Negative")
|
|
|
59 |
</div>
|
60 |
""", unsafe_allow_html=True)
|
61 |
|
62 |
+
# Auto play audio summary of headlines
|
63 |
all_headlines = ". ".join([f"{idx + 1}. {article['title']}" for idx, article in enumerate(articles)])
|
64 |
audio_file = text_to_speech(all_headlines)
|
65 |
if audio_file:
|
|
|
67 |
|
68 |
st.write("## Latest News Articles")
|
69 |
|
70 |
+
# News cards
|
71 |
for idx, article in enumerate(articles, start=1):
|
72 |
sentiment_color = {
|
73 |
"Positive": "#4CAF50",
|