Spaces:
Running
Running
import streamlit as st | |
from gfnews import GoogleBussinessNews | |
from datetime import datetime | |
from transformers import pipeline | |
financial_business_news_domains = [ | |
"economictimes.indiatimes.com", "business-standard.com", "financialexpress.com", | |
"livemint.com", "thehindubusinessline.com", "moneycontrol.com", "bloombergquint.com", | |
"cnbctv18.com", "businesstoday.in", "forbesindia.com", "reuters.com", "bloomberg.com", | |
"ft.com", "wsj.com", "cnbc.com", "marketwatch.com", "investing.com", "finance.yahoo.com", | |
"seekingalpha.com", "businessinsider.com" | |
] | |
st.markdown( | |
""" | |
<style> | |
.stMainBlockContainer{ | |
max-width: 70vw; | |
padding-top: 4rem; | |
} | |
</style> | |
""", | |
unsafe_allow_html=True | |
) | |
sentiment_analyzer = pipeline("sentiment-analysis") | |
if 'search_history' not in st.session_state: | |
st.session_state.search_history = [] | |
common_topics = [ | |
"HCLTech", "Stock Market", "Interest Rates", "RBI Policies", | |
"Nifty 50", "Banking Sector", "Mutual Funds", "Corporate Earnings", "Budget 2025" | |
] | |
nifty_50_companies = [ | |
"Reliance", "TCS", "HDFC", "Infosys", "HUL", "ICICI", "SBI", | |
"Bajaj Finance", "Airtel", "Wipro", "Kotak Mahindra", "Adani Enterprises", | |
"Larsen & Toubro", "Tata Motors", "Maruti Suzuki", "Asian Paints", | |
"NTPC", "Nestle India", "Sun Pharma", "Power Grid", "ITC", "HDFC", "Tata Steel" | |
] | |
st.title("π° Finance News App") | |
st.markdown( | |
""" | |
**Welcome to the Finance News App!** | |
Stay informed with the latest financial news from trusted sources. Use the search bar or select from common topics. | |
""", | |
unsafe_allow_html=True | |
) | |
col1, col2 = st.columns([2, 1]) | |
scraper = None | |
with col2: | |
selected_sources = st.multiselect("Select News Sources", default=financial_business_news_domains[:2], options=financial_business_news_domains) | |
scraper = GoogleBussinessNews(selected_sources,max_articles=25) | |
with col1: | |
st.markdown("## π Search for News") | |
st.markdown("") | |
topic_disabled = False | |
query = st.text_input("Enter search query:") | |
if query: | |
topic_disabled = True | |
st.markdown("") | |
topic = st.selectbox("OR Search by topic:", common_topics + nifty_50_companies, disabled=topic_disabled) | |
st.markdown("") | |
query = query if query else topic | |
first_day_of_current_month = datetime(datetime.today().year, datetime.today().month, 1) | |
start_date = st.date_input("Start Date:", value=first_day_of_current_month) | |
end_date = st.date_input("End Date:", value=datetime.today()) | |
if st.button("Search", type="primary"): | |
if start_date > end_date: | |
st.error("Start date cannot be after the end date.") | |
elif not query: | |
st.warning("Please enter or select a query.") | |
else: | |
st.session_state.search_history.insert(0, query) | |
if len(st.session_state.search_history) > 5: | |
st.session_state.search_history.pop() | |
with st.spinner("Fetching news..."): | |
results = scraper.scrape(query=query, start_date=start_date, end_date=end_date) | |
if results: | |
for result in results: | |
sentiment = sentiment_analyzer(result["description"])[0] | |
sentiment_label = sentiment["label"] | |
sentiment_score = sentiment["score"] | |
if sentiment_label == "POSITIVE": | |
progress_color = "green" | |
elif sentiment_label == "NEGATIVE": | |
progress_color = "red" | |
else: | |
progress_color = "gray" | |
st.markdown("---") | |
with st.container(): | |
st.subheader(result["title"]) | |
st.write(f"**Source:** {result['source']}") | |
st.write(f"**Date:** {result['date']}") | |
st.write(f"**Description:** {result['description']}") | |
st.markdown(f"**Sentiment:** <span style='color:{progress_color}; font-weight:bold;'>{sentiment_label}</span> (Score: {sentiment_score:.2f})", unsafe_allow_html=True) | |
st.markdown( | |
f""" | |
<div style="height: 10px; background-color: {progress_color}; width: {sentiment_score * 100}%;"> | |
</div> | |
""", | |
unsafe_allow_html=True | |
) | |
st.markdown(f"[Go to Page]({result['url']})", unsafe_allow_html=True) | |
else: | |
st.info("Please try again, might be a network delay.") | |
with col2: | |
st.markdown("") | |
st.markdown("---") | |
st.markdown("") | |
st.markdown("## π Recent Searches") | |
if st.session_state.search_history: | |
for idx, past_query in enumerate(st.session_state.search_history): | |
st.button(past_query, key=f"search_{idx}_{past_query}") | |
else: | |
st.text('No previous searches.') | |