LinkLinkWu's picture
Update app.py
4a525ac verified
raw
history blame
3.13 kB
import streamlit as st
from func import fetch_news, analyze_sentiment, extract_org_entities
import time
# ---------------- Page Setup ----------------
st.set_page_config(page_title="Stock News Sentiment Analysis", layout="centered")
st.markdown("""
<style>
.main { background-color: #f9fbfc; }
.stTextInput>div>div>input, .stTextArea textarea {
font-size: 16px;
padding: 0.5rem;
}
.stButton>button {
background-color: #4CAF50;
color: white;
font-size: 16px;
padding: 0.5rem 1rem;
border-radius: 8px;
}
.stButton>button:hover {
background-color: #45a049;
}
</style>
""", unsafe_allow_html=True)
# ---------------- User Interface ----------------
st.title("\U0001F4CA Stock News Sentiment Analysis")
st.markdown("""
This tool analyzes the sentiment of news articles related to companies you mention in text.
\U0001F4A1 *Try input like:* `I want to check Apple, Tesla, and Microsoft.`
**Note:** If news fetching fails, it may be due to changes in the Finviz website or access restrictions.
""")
free_text = st.text_area("Enter text mentioning companies:", height=100)
tickers = extract_org_entities(free_text)
if tickers:
cleaned_input = ", ".join(tickers)
st.markdown(f"\U0001F50E **Identified Tickers:** `{cleaned_input}`")
else:
tickers = []
# ---------------- Button Trigger ----------------
if st.button("Get News and Sentiment"):
if not tickers:
st.warning("Please mention at least one recognizable company.")
else:
progress_bar = st.progress(0)
total_stocks = len(tickers)
for idx, ticker in enumerate(tickers):
st.subheader(f"Analyzing {ticker}...")
news_list = fetch_news(ticker)
if news_list:
sentiments = []
for news in news_list:
sentiment = analyze_sentiment(news['title'])
sentiments.append(sentiment)
positive_count = sentiments.count("Positive")
negative_count = sentiments.count("Negative")
total = len(sentiments)
positive_ratio = positive_count / total if total else 0
negative_ratio = negative_count / total if total else 0
if positive_ratio >= 0.4:
overall_sentiment = "Positive"
elif negative_ratio >= 0.6:
overall_sentiment = "Negative"
else:
overall_sentiment = "Neutral"
st.write(f"**Top 3 News Articles for {ticker}**")
for i, news in enumerate(news_list[:3], 1):
sentiment = sentiments[i-1]
st.markdown(f"{i}. [{news['title']}]({news['link']}) - **{sentiment}**")
st.write(f"**Overall Sentiment for {ticker}: {overall_sentiment}**")
else:
st.write(f"No news available for {ticker}.")
progress_bar.progress((idx + 1) / total_stocks)
time.sleep(0.1)