File size: 7,034 Bytes
9efa351 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
import streamlit as st
import requests
import re
import torch
from transformers import BertTokenizer, BertForSequenceClassification
# Load tokenizer and model
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model_path = '/Users/kartikrathi/Documents/News_sentiment_analysis/model'
model = BertForSequenceClassification.from_pretrained(model_path)
# Function to analyze sentiment
def analyze_sentiment(text):
# Tokenize inputs
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
# Perform inference
with torch.no_grad():
outputs = model(**inputs)
# Get predicted logits
logits = outputs.logits
# Determine predicted sentiment
predicted_class_id = torch.argmax(logits, dim=1).item()
sentiment = {0: "positive", 1: "negative", 2: "neutral"}
return sentiment[predicted_class_id]
# Function to fetch news using an API
def fetch_stock_news(symbol):
api_token = '6679177763bcd9.48465511' # Replace with your actual API token
url = f'https://eodhd.com/api/news?s={symbol}&offset=0&limit=2&api_token={api_token}&fmt=json'
try:
response = requests.get(url)
response.raise_for_status() # Raise exception for bad status codes
data = response.json()
news_list = []
for article in data:
title = article.get('title', 'Title not available')
content = article.get('content', 'Content not available')
url = article.get('url', 'URL not available')
date = article.get('date', 'Date not available')
# Remove unwanted sections from content
cleaned_content = remove_sections(content)
news_list.append({'title': title, 'content': cleaned_content, 'url': url, 'date': date})
return news_list
except requests.exceptions.RequestException as e:
print(f"Error fetching news for {symbol}: {e}")
return None
# Function to remove unwanted sections
def remove_sections(content):
# Patterns to remove
patterns = [
r'About\s+.*?[\n\r]+', # 'About {stock}' section
r'Safe\s+Harbor.*', # 'Safe Harbor' section
r'Story\s+continues.*', # 'Story continues'
r'Visit\s+www\.infosys\.com.*', # 'Visit www.infosys.com...'
r'Logo', # 'Logo'
]
for pattern in patterns:
content = re.sub(pattern, '', content, flags=re.IGNORECASE)
return content.strip()
# Streamlit app with multi-page support
def main():
st.sidebar.title('Navigation')
page = st.sidebar.radio("Go to", ('Home', 'Portfolio & News'))
if page == 'Home':
st.title("Financial News Sentiment Analysis")
st.markdown("""
This app performs sentiment analysis on financial news using FinBERT model.
""")
# Input text box for user input
user_input = st.text_area("Enter your financial news text here:", height=200)
# Perform sentiment analysis when user clicks the button
if st.button("Analyze"):
if user_input:
sentiment = analyze_sentiment(user_input)
if sentiment == "positive":
st.markdown(f"<p style='color:green;font-size:40px;font-weight:bold'>{sentiment}</p>", unsafe_allow_html=True)
elif sentiment == "negative":
st.markdown(f"<p style='color:red;font-size:40px;font-weight:bold'>{sentiment}</p>", unsafe_allow_html=True)
elif sentiment == "neutral":
st.markdown(f"<p style='color:blue;font-size:40px;font-weight:bold'>{sentiment}</p>", unsafe_allow_html=True)
else:
st.warning("Please enter some text.")
elif page == 'Portfolio & News':
st.title('Portfolio & News')
# Sidebar to manage portfolio and fetch news
st.sidebar.subheader('Manage Portfolio & Fetch News')
st.sidebar.info("Enter your portfolio/company names here to fetch news.")
# Input fields for portfolio management and news fetching
portfolio = st.sidebar.text_area("Enter your portfolio/company names (one per line):", height=200)
if st.sidebar.button("Save"):
# Add ".NSE" suffix to each company name
shares = portfolio.split("\n")
shares = [share.strip() + ".NSE" for share in shares if share.strip()]
st.sidebar.markdown("**Shares**")
st.sidebar.markdown("\n".join(shares))
# Button to fetch news and perform sentiment analysis
if st.sidebar.button("Fetch News & Analyze Sentiment"):
if portfolio:
companies = portfolio.split("\n")
companies = [company.strip() + ".NSE" for company in companies if company.strip()]
for company in companies:
st.subheader(f"Latest News for {company}:")
news = fetch_stock_news(company)
if news:
for article in news:
title = article['title']
content = article['content']
url = article['url']
date = article['date']
st.markdown(f"**Title:** [{title}]({url}) <span style='color:green;font-size:12px;'>({date})</span>", unsafe_allow_html=True)
# Display a truncated version of the content
truncated_content = content[:200] + "..."
st.markdown(f"**Content:** {truncated_content}")
# Expander for full content
with st.expander("Expand more"):
st.markdown(f"{content}")
st.markdown("---")
# Analyze sentiment of news content (assuming content is available)
if content:
sentiment = analyze_sentiment(content)
if sentiment == "positive":
sentiment_color = "green"
elif sentiment == "negative":
sentiment_color = "red"
elif sentiment == "neutral":
sentiment_color = "blue"
st.markdown(f"**Sentiment:** <span style='color:{sentiment_color};font-weight:bold'>{sentiment}</span>", unsafe_allow_html=True)
st.markdown("---")
else:
st.warning(f"No news articles found for {company}.")
else:
st.warning("Please enter portfolio/company names to fetch news.")
if __name__ == '__main__':
main()
|