|
import streamlit as st |
|
from func import fetch_news, analyze_sentiment, extract_org_entities |
|
import time |
|
|
|
|
|
st.set_page_config(page_title="Stock News Sentiment Analysis", layout="centered") |
|
|
|
|
|
col1, col2 = st.columns([1, 10]) |
|
with col1: |
|
st.image( |
|
"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Finance_Icon_Set_2013_-_Analytics.svg/1024px-Finance_Icon_Set_2013_-_Analytics.svg.png", |
|
width=48 |
|
) |
|
with col2: |
|
st.markdown("<h1 style='margin-bottom: 0;'>Stock News Sentiment Analysis</h1>", unsafe_allow_html=True) |
|
|
|
|
|
st.markdown(""" |
|
<p style='font-size:17px; margin-top: 0.5rem;'> |
|
Analyze the latest news sentiment of companies mentioned in your input. |
|
</p> |
|
<p style='font-size:17px;'> |
|
<strong>💡 Try input like:</strong> <em>I want to check Apple, Tesla, and Microsoft.</em> |
|
</p> |
|
<p style='font-size:17px;'> |
|
<strong>Note:</strong> News may fail to load if the Finviz website structure changes or access is restricted. |
|
</p> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
free_text = st.text_area("Enter text mentioning companies:", height=100) |
|
tickers = extract_org_entities(free_text) |
|
|
|
if tickers: |
|
st.markdown(f"\U0001F50E **Identified Tickers:** `{', '.join(tickers)}`") |
|
else: |
|
tickers = [] |
|
|
|
|
|
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.success(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) |
|
|