Kovila
commited on
Commit
·
fce802f
1
Parent(s):
5ca58af
Add application file
Browse files
financial_news_sentiment_and_summarization.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# imports
|
2 |
+
import toml
|
3 |
+
import finnhub
|
4 |
+
import datetime
|
5 |
+
from transformers import pipeline
|
6 |
+
import streamlit as st
|
7 |
+
|
8 |
+
|
9 |
+
# load API key
|
10 |
+
# get API Keys
|
11 |
+
#with open('secrets.toml', 'r') as f:
|
12 |
+
# config = toml.load(f)
|
13 |
+
|
14 |
+
#FINNHUB_API_KEY = config['FINNHUB']
|
15 |
+
FINNHUB_API_KEY = st.secrets['FINNHUB']
|
16 |
+
# function to get financial news
|
17 |
+
def financial_news(stock_ticker, start_date, end_date, include_headline=True, include_summary=False):
|
18 |
+
"""
|
19 |
+
Retrieves financial news for a specified stock within a date range.
|
20 |
+
|
21 |
+
Args:
|
22 |
+
stock_ticker (str): The ticker symbol of the stock (e.g., "AAPL" for Apple Inc.).
|
23 |
+
start_date (str): The start date for the news search in the format "YYYY-MM-DD".
|
24 |
+
end_date (str): The end date for the news search in the format "YYYY-MM-DD".
|
25 |
+
include_headline (bool, optional): If True, includes both headlines and summaries in the result.
|
26 |
+
Defaults to True.
|
27 |
+
|
28 |
+
Returns:
|
29 |
+
str: A concatenated string of headlines and summaries for the specified stock within the date range.
|
30 |
+
If `include_headline` is False, only the summary is included.
|
31 |
+
"""
|
32 |
+
finnhub_client = finnhub.Client(api_key=FINNHUB_API_KEY)
|
33 |
+
news_list = finnhub_client.company_news(stock_ticker, _from=start_date, to=end_date)
|
34 |
+
|
35 |
+
news = ''
|
36 |
+
for news_i in news_list:
|
37 |
+
if stock_ticker in news_i['headline']:
|
38 |
+
if include_headline:
|
39 |
+
news = news + ' ' + news_i['headline'] + '.\n\n'
|
40 |
+
if include_summary:
|
41 |
+
news = news + ' ' + news_i['summary'] + '.\n\n'
|
42 |
+
|
43 |
+
return news
|
44 |
+
|
45 |
+
# get financial news summary
|
46 |
+
@st.cache_resource
|
47 |
+
def load_summarizer():
|
48 |
+
return pipeline("summarization", model="facebook/bart-large-cnn")
|
49 |
+
|
50 |
+
SUMMARIZER = load_summarizer()
|
51 |
+
|
52 |
+
def get_news_summary(news):
|
53 |
+
news = news.replace('\n\n', ' ')
|
54 |
+
news_summary = SUMMARIZER(news)
|
55 |
+
return news_summary[0]['summary_text']
|
56 |
+
|
57 |
+
# APP
|
58 |
+
st.title('Financial News Summarization and Sentiment')
|
59 |
+
st.write('Enter the stock ticker and period for which you want financial news.')
|
60 |
+
stock_ticker = st.text_input('Stock Ticker:', 'AAPL')
|
61 |
+
start_date = st.date_input('Start Date:', datetime.datetime.today()-datetime.timedelta(days=20))
|
62 |
+
end_date = st.date_input('End Date:', datetime.datetime.today())
|
63 |
+
news = financial_news(stock_ticker, start_date, end_date)
|
64 |
+
st.write(news)
|
65 |
+
|
66 |
+
st.header('Financial News Sentiment')
|
67 |
+
|
68 |
+
st.header('Financial News Summary')
|
69 |
+
with st.spinner('facebook bart model is summarizing the news...'):
|
70 |
+
news_summary = get_news_summary(news)
|
71 |
+
st.write(news_summary)
|
requirements.txt
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
certifi==2024.8.30
|
2 |
+
charset-normalizer==3.4.0
|
3 |
+
docopt==0.6.2
|
4 |
+
filelock==3.16.1
|
5 |
+
finnhub-python==2.4.21
|
6 |
+
fsspec==2024.10.0
|
7 |
+
huggingface-hub==0.26.2
|
8 |
+
idna==3.10
|
9 |
+
numpy==2.1.3
|
10 |
+
packaging==24.2
|
11 |
+
pipreqs==0.4.13
|
12 |
+
PyYAML==6.0.2
|
13 |
+
regex==2024.11.6
|
14 |
+
requests==2.32.3
|
15 |
+
safetensors==0.4.5
|
16 |
+
setuptools==75.1.0
|
17 |
+
tokenizers==0.20.3
|
18 |
+
toml==0.10.2
|
19 |
+
tqdm==4.67.0
|
20 |
+
torch
|
21 |
+
transformers==4.46.2
|
22 |
+
typing_extensions==4.12.2
|
23 |
+
urllib3==2.2.3
|
24 |
+
wheel==0.44.0
|
25 |
+
yarg==0.1.10
|
26 |
+
streamlit
|