LinkLinkWu's picture
Update app.py
eeeb3ea verified
raw
history blame
3.67 kB
import streamlit as st
from func import (
get_sentiment_pipeline,
get_ner_pipeline,
fetch_news,
analyze_sentiment,
extract_org_entities,
)
import time
# ----------- Page Config -----------
st.set_page_config(
page_title="EquiPulse: Real-Time Stock News Sentiment",
layout="centered",
initial_sidebar_state="collapsed"
)
# ----------- Custom Styling -----------
st.markdown("""
<style>
body {
background-color: #ffffff;
}
h1 {
color: #002b45;
font-family: 'Segoe UI', sans-serif;
font-size: 32px;
}
.stTextInput > div > div > input,
.stTextArea textarea {
font-size: 16px;
}
.stButton>button {
background-color: #002b45;
color: white;
font-size: 16px;
padding: 0.4rem 1rem;
border-radius: 6px;
}
.stButton>button:hover {
background-color: #004b78;
}
.stMarkdown {
font-size: 16px;
}
</style>
""", unsafe_allow_html=True)
# ----------- Header Section -----------
col1, col2 = st.columns([1, 9])
with col1:
st.image("https://cdn-icons-png.flaticon.com/512/2721/2721203.png", width=48)
with col2:
st.markdown("<h1 style='margin-bottom: 0.2rem;'>EquiPulse: Real-Time Stock News Sentiment</h1>", unsafe_allow_html=True)
# ----------- Description -----------
st.markdown("""
<div style='font-size:16px; line-height:1.6;'>
Uncover real-time sentiment from financial headlines mentioning your target companies.<br>
<b>πŸ’¬ Try:</b> <i>Apple, Tesla, and Microsoft</i>
</div>
""", unsafe_allow_html=True)
# ----------- Input Area -----------
st.markdown("#### 🎯 Enter Your Target Company Tickers")
free_text = st.text_area("Example: Apple, Nvidia, Google", height=90)
ner_pipeline = get_ner_pipeline()
tickers = extract_org_entities(free_text, ner_pipeline)
if tickers:
st.markdown(f"βœ… **Identified Tickers:** `{', '.join(tickers)}`")
else:
tickers = []
# ----------- Action Button -----------
if st.button("Get News and Sentiment"):
if not tickers:
st.warning("⚠️ Please enter at least one recognizable company name.")
else:
sentiment_pipeline = get_sentiment_pipeline()
progress_bar = st.progress(0)
total_stocks = len(tickers)
for idx, ticker in enumerate(tickers):
st.markdown(f"---\n#### πŸ” Analyzing: `{ticker}`")
news_list = fetch_news(ticker)
if news_list:
sentiments = [analyze_sentiment(news['title'], sentiment_pipeline) for news in news_list]
pos_count = sentiments.count("Positive")
neg_count = sentiments.count("Negative")
total = len(sentiments)
pos_ratio = pos_count / total if total else 0
neg_ratio = neg_count / total if total else 0
if pos_ratio >= 0.25:
overall = "Positive"
elif neg_ratio >= 0.75:
overall = "Negative"
else:
overall = "Neutral"
st.markdown(f"**πŸ“° Top News for `{ticker}`:**")
for i, news in enumerate(news_list[:3]):
st.markdown(f"{i+1}. [{news['title']}]({news['link']}) β€” **{sentiments[i]}**")
st.success(f"πŸ“ˆ **Overall Sentiment for `{ticker}`: {overall}**")
else:
st.info(f"No recent news available for `{ticker}`.")
progress_bar.progress((idx + 1) / total_stocks)
time.sleep(0.1)