Spaces:
Running
Running
Robert Castagna
commited on
Commit
·
50bed66
1
Parent(s):
fbe72f1
rollback
Browse files- .gitignore +4 -1
- app.py +25 -6
- fin_data_api.py +52 -18
- requirements.txt +2 -1
.gitignore
CHANGED
@@ -1,2 +1,5 @@
|
|
1 |
secrets.json
|
2 |
-
fin_data.db
|
|
|
|
|
|
|
|
1 |
secrets.json
|
2 |
+
fin_data.db
|
3 |
+
streamlit_config.json
|
4 |
+
edgar-crawler/
|
5 |
+
.venv/
|
app.py
CHANGED
@@ -2,15 +2,23 @@ import streamlit as st
|
|
2 |
import sqlite3
|
3 |
import pandas as pd
|
4 |
import streamlit as st
|
|
|
5 |
|
6 |
-
|
7 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
conn = sqlite3.connect('fin_data.db')
|
11 |
c = conn.cursor()
|
12 |
c.execute("""
|
13 |
-
select * from
|
14 |
""")
|
15 |
|
16 |
rows = c.fetchall()
|
@@ -18,11 +26,22 @@ rows = c.fetchall()
|
|
18 |
# Extract column names from the cursor
|
19 |
column_names = [description[0] for description in c.description]
|
20 |
|
|
|
|
|
|
|
21 |
# Create a DataFrame
|
22 |
df = pd.DataFrame(rows, columns=column_names)
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
st.dataframe(df)
|
26 |
|
27 |
-
|
28 |
-
|
|
|
2 |
import sqlite3
|
3 |
import pandas as pd
|
4 |
import streamlit as st
|
5 |
+
import pygwalker as pyg
|
6 |
|
7 |
+
|
8 |
+
st.set_page_config(
|
9 |
+
page_title="Financial Data",
|
10 |
+
page_icon="📈",
|
11 |
+
layout="wide",
|
12 |
+
initial_sidebar_state="expanded",
|
13 |
+
)
|
14 |
+
|
15 |
+
st.set_title('Financial Data')
|
16 |
|
17 |
|
18 |
conn = sqlite3.connect('fin_data.db')
|
19 |
c = conn.cursor()
|
20 |
c.execute("""
|
21 |
+
select * from company_news
|
22 |
""")
|
23 |
|
24 |
rows = c.fetchall()
|
|
|
26 |
# Extract column names from the cursor
|
27 |
column_names = [description[0] for description in c.description]
|
28 |
|
29 |
+
conn.commit()
|
30 |
+
conn.close()
|
31 |
+
|
32 |
# Create a DataFrame
|
33 |
df = pd.DataFrame(rows, columns=column_names)
|
34 |
|
35 |
+
# setup pygwalker configuration: https://github.com/Kanaries/pygwalker
|
36 |
+
def load_config(file_path):
|
37 |
+
with open(file_path, 'r') as config_file:
|
38 |
+
config_str = config_file.read()
|
39 |
+
return config_str
|
40 |
+
config = load_config('config.json')
|
41 |
+
pyg.walk(df, env='Streamlit', dark='dark', spec=config)
|
42 |
+
|
43 |
+
# show the dataframe just to test
|
44 |
st.dataframe(df)
|
45 |
|
46 |
+
|
47 |
+
|
fin_data_api.py
CHANGED
@@ -30,38 +30,72 @@ def sentiment_analysis(headline:str) -> str:
|
|
30 |
|
31 |
:param1 headline: Text string: 'Apple is the best company in the world'
|
32 |
"""
|
33 |
-
nlp = pipeline("sentiment-analysis")
|
34 |
return nlp(headline)
|
35 |
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
## get basic financials
|
46 |
#res_basic_fins = get_finnhub_data('/stock/metric?symbol=AAPL&metric=all')
|
47 |
#print(res_basic_fins['metric'].keys())
|
48 |
#print(res_basic_fins['series']['annual'].keys())
|
49 |
#print(res_basic_fins['series']['quarterly'].keys())
|
50 |
|
51 |
|
52 |
-
|
|
|
53 |
#res_sentiment = get_finnhub_data('/stock/insider-sentiment?symbol=AAPL')
|
54 |
#print(res_sentiment['data'][0].keys())
|
55 |
|
56 |
|
57 |
-
# # put data in database after figuring out what data we want to store
|
58 |
-
# conn = sqlite3.connect('fin_data.db')
|
59 |
-
# c = conn.cursor()
|
60 |
-
# c.execute("""
|
61 |
-
# insert into test values (2, 'test value two')
|
62 |
-
# """)
|
63 |
-
|
64 |
|
65 |
|
66 |
-
|
67 |
-
|
|
|
30 |
|
31 |
:param1 headline: Text string: 'Apple is the best company in the world'
|
32 |
"""
|
33 |
+
nlp = pipeline("sentiment-analysis", model="ProsusAI/finbert")
|
34 |
return nlp(headline)
|
35 |
|
36 |
|
37 |
+
# --------------------------------- get news articles for a company --------------------------------- #
|
38 |
+
conn = sqlite3.connect('fin_data.db')
|
39 |
+
c = conn.cursor()
|
40 |
+
|
41 |
+
c.execute("""create table if not exists company_news (
|
42 |
+
id text primary key,
|
43 |
+
ticker text,
|
44 |
+
category text,
|
45 |
+
headline text,
|
46 |
+
date_stamp text,
|
47 |
+
sentiment_label text,
|
48 |
+
sentiment_score real
|
49 |
+
)""")
|
50 |
+
|
51 |
+
|
52 |
+
res_news = get_finnhub_data('/company-news?symbol=AAPL&from=2023-08-15&to=2023-08-17')
|
53 |
+
print(res_news[0].keys())
|
54 |
+
for item in res_news:
|
55 |
+
dt_object = datetime.datetime.fromtimestamp(item['datetime']).strftime("%Y-%m-%d")
|
56 |
+
sentiment = sentiment_analysis(item['headline'])
|
57 |
+
sentiment_label = sentiment[0]['label']
|
58 |
+
sentiment_score = sentiment[0]['score']
|
59 |
+
print(item['headline'], dt_object, sentiment[0]['label'])
|
60 |
+
|
61 |
+
# Prepare your query and data
|
62 |
+
query = """
|
63 |
+
INSERT INTO company_news
|
64 |
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
65 |
+
"""
|
66 |
+
data = (item['id'], 'AAPL', item['category'], item['headline'], dt_object, sentiment_label, sentiment_score)
|
67 |
+
|
68 |
+
# Execute the query with the data
|
69 |
+
#c.execute(query, data)
|
70 |
+
|
71 |
+
|
72 |
+
rows = c.execute("""
|
73 |
+
select * from company_news
|
74 |
+
""")
|
75 |
+
|
76 |
+
column_names = [description[0] for description in c.description]
|
77 |
|
78 |
+
# Create a DataFrame
|
79 |
+
df = pd.DataFrame(rows, columns=column_names)
|
80 |
+
|
81 |
+
print(df)
|
82 |
+
|
83 |
+
|
84 |
+
# --------------------------------- get basic financials ---------------------------------#
|
85 |
|
|
|
86 |
#res_basic_fins = get_finnhub_data('/stock/metric?symbol=AAPL&metric=all')
|
87 |
#print(res_basic_fins['metric'].keys())
|
88 |
#print(res_basic_fins['series']['annual'].keys())
|
89 |
#print(res_basic_fins['series']['quarterly'].keys())
|
90 |
|
91 |
|
92 |
+
# --------------------------------- get insider sentiment --------------------------------- #
|
93 |
+
|
94 |
#res_sentiment = get_finnhub_data('/stock/insider-sentiment?symbol=AAPL')
|
95 |
#print(res_sentiment['data'][0].keys())
|
96 |
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
|
100 |
+
conn.commit()
|
101 |
+
conn.close()
|
requirements.txt
CHANGED
@@ -17,4 +17,5 @@ transformers
|
|
17 |
requests
|
18 |
datetime
|
19 |
pysqlite3 == 0.5.2
|
20 |
-
streamlit
|
|
|
|
17 |
requests
|
18 |
datetime
|
19 |
pysqlite3 == 0.5.2
|
20 |
+
streamlit
|
21 |
+
pygwalker
|