Andreagus commited on
Commit
415516b
1 Parent(s): 0c62332

Upload 8 files

Browse files
aggregator.py ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from bezinga_caller import bezinga_get
2
+ from finub_caller import get_finhub
3
+ from marketaux_caller import get_marketaux
4
+ from newsapi_caller import get_newsapi
5
+ from newsdata_caller import get_newsdata
6
+ from vantage_caller import get_vantage
7
+ from transformers import pipeline
8
+
9
+
10
+ def get_articles_sentiment(ticker, model):
11
+ pipe = pipeline("text-classification", model=model)
12
+
13
+ # getting a list of article of given ticket from different sources
14
+ try:
15
+ bezinga_list = bezinga_get(ticker)
16
+ bezinga_results = pipe(bezinga_list)
17
+ except Exception as e:
18
+ print(e)
19
+ bezinga_results = False
20
+
21
+ try:
22
+ newsapi_list = get_newsapi(ticker)
23
+ newsapi_results = pipe(newsapi_list)
24
+ except Exception as e:
25
+ print(e)
26
+ newsapi_results = False
27
+ try:
28
+ newsdata_list = get_newsdata(ticker)
29
+ newsdata_results = pipe(newsdata_list)
30
+ except Exception as e:
31
+ print(e)
32
+ newsdata_results = False
33
+
34
+ try:
35
+ finhub_list = get_finhub(ticker)
36
+ finhub_results = pipe(finhub_list)
37
+ except Exception as e:
38
+ print(e)
39
+ finhub_results = False
40
+
41
+ try:
42
+ vantage_list = get_vantage(ticker)
43
+ vantage_results = pipe(vantage_list)
44
+ except Exception as e:
45
+ print(e)
46
+ vantage_results = False
47
+
48
+ try:
49
+ marketaux_list = get_marketaux(ticker)
50
+ marketaux_results = pipe(marketaux_list)
51
+ except Exception as e:
52
+ print(e)
53
+ marketaux_results = False
54
+
55
+
56
+
57
+ # finhub_list = get_finhub(ticker)
58
+ # marketaux_list = get_marketaux(ticker)
59
+ # newsapi_list = get_newsapi(ticker)
60
+ # newsdata_list = get_newsdata(ticker)
61
+ # vantage_list = get_vantage(ticker)
62
+
63
+
64
+ # calling ai model on each list
65
+ # finhub_results = pipe(finhub_list)
66
+ # marketaux_results = pipe(marketaux_list)
67
+ # newsapi_results = pipe(newsapi_list)
68
+ # newsdata_results = pipe(newsdata_list)
69
+ # vantage_results = pipe(vantage_list)
70
+
71
+ # replacing values for calculations and doing the sentiment for each source
72
+ def replace_values(result):
73
+ # Replace values in the label column
74
+ for dict in result:
75
+ if dict["label"] == "LABEL_1":
76
+ dict["label"] = 2
77
+ else:
78
+ dict["label"] = 1
79
+
80
+ try:
81
+ replace_values(bezinga_results)
82
+
83
+ bezinga_label_mean = float(sum(d['label'] for d in bezinga_results)) / len(bezinga_results)
84
+
85
+ bezinga_positives = []
86
+ bezinga_negatives = []
87
+
88
+ for dict in bezinga_results:
89
+ if dict["label"] == 2:
90
+ bezinga_positives.append(dict)
91
+ else:
92
+ bezinga_negatives.append(dict)
93
+
94
+ if len(bezinga_positives) > 0:
95
+ bezinga_positive_score_mean = float(sum(d['score'] for d in bezinga_positives)) / len(bezinga_positives)
96
+
97
+ if len(bezinga_negatives) > 0:
98
+ bezinga_negative_score_mean = float(sum(d['score'] for d in bezinga_negatives)) / len(bezinga_negatives)
99
+ except Exception as e:
100
+ print(e)
101
+
102
+ # finhub
103
+ if finhub_results:
104
+ replace_values(finhub_results)
105
+
106
+ finhub_label_mean = float(sum(d['label'] for d in finhub_results)) / len(finhub_results)
107
+
108
+ finhub_positives = []
109
+ finhub_negatives = []
110
+
111
+ for dict in finhub_results:
112
+ if dict["label"] == 2:
113
+ finhub_positives.append(dict)
114
+ else:
115
+ finhub_negatives.append(dict)
116
+
117
+ if len(finhub_positives) > 0:
118
+ finhub_positive_score_mean = float(sum(d['score'] for d in finhub_positives)) / len(finhub_positives)
119
+
120
+ if len(finhub_negatives) > 0:
121
+ finhub_negative_score_mean = float(sum(d['score'] for d in finhub_negatives)) / len(finhub_negatives)
122
+
123
+ # marketaux
124
+ if marketaux_results:
125
+ replace_values(marketaux_results)
126
+
127
+ marketaux_label_mean = float(sum(d['label'] for d in marketaux_results)) / len(marketaux_results)
128
+
129
+ marketaux_positives = []
130
+ marketaux_negatives = []
131
+
132
+ for dict in marketaux_results:
133
+ if dict["label"] == 2:
134
+ marketaux_positives.append(dict)
135
+ else:
136
+ marketaux_negatives.append(dict)
137
+
138
+ if len(marketaux_positives) > 0:
139
+ marketaux_positive_score_mean = float(sum(d['score'] for d in marketaux_positives)) / len(marketaux_positives)
140
+
141
+ if len(marketaux_negatives) > 0:
142
+ marketaux_negative_score_mean = float(sum(d['score'] for d in marketaux_negatives)) / len(marketaux_negatives)
143
+
144
+ # newsapi
145
+ if newsapi_results:
146
+ replace_values(newsapi_results)
147
+
148
+ newsapi_label_mean = float(sum(d['label'] for d in newsapi_results) + 1) / (len(newsapi_results) + 2)
149
+
150
+ newsapi_positives = []
151
+ newsapi_negatives = []
152
+
153
+ for dict in newsapi_results:
154
+ if dict["label"] == 2:
155
+ newsapi_positives.append(dict)
156
+ else:
157
+ newsapi_negatives.append(dict)
158
+
159
+ if len(newsapi_positives) > 0:
160
+ newsapi_positive_score_mean = float(sum(d['score'] for d in newsapi_positives)) / len(newsapi_positives)
161
+
162
+ if len(newsapi_negatives) > 0:
163
+ newsapi_negative_score_mean = float(sum(d['score'] for d in newsapi_negatives)) / len(newsapi_negatives)
164
+
165
+
166
+ # newsdata
167
+ if newsdata_results:
168
+ replace_values(newsdata_results)
169
+
170
+ newsdata_label_mean = float(sum(d['label'] for d in newsdata_results)) / len(newsdata_results)
171
+
172
+ newsdata_positives = []
173
+ newsdata_negatives = []
174
+
175
+ for dict in newsdata_results:
176
+ if dict["label"] == 2:
177
+ newsdata_positives.append(dict)
178
+ else:
179
+ newsdata_negatives.append(dict)
180
+
181
+ if len(newsdata_positives) > 0:
182
+ newsdata_positive_score_mean = float(sum(d['score'] for d in newsdata_positives)) / len(newsdata_positives)
183
+
184
+ if len(newsdata_negatives) > 0:
185
+ newsdata_negative_score_mean = float(sum(d['score'] for d in newsdata_negatives)) / len(newsdata_negatives)
186
+
187
+ # vantage
188
+ if vantage_results:
189
+ replace_values(vantage_results)
190
+
191
+ vantage_label_mean = float(sum(d['label'] for d in vantage_results)) / len(vantage_results)
192
+
193
+ vantage_positives = []
194
+ vantage_negatives = []
195
+
196
+ for dict in vantage_results:
197
+ if dict["label"] == 2:
198
+ vantage_positives.append(dict)
199
+ else:
200
+ vantage_negatives.append(dict)
201
+
202
+ if len(vantage_positives) > 0:
203
+ vantage_positive_score_mean = float(sum(d['score'] for d in vantage_positives)) / len(vantage_positives)
204
+
205
+ if len(vantage_negatives) > 0:
206
+ vantage_negative_score_mean = float(sum(d['score'] for d in vantage_negatives)) / len(vantage_negatives)
207
+
208
+ results_dict = {
209
+ "bezinga": {
210
+ "bezinga_articles": len(bezinga_results) if bezinga_results else 0,
211
+ "bezinga_positives": len(bezinga_positives) if bezinga_results else 0,
212
+ "bezinga_negatives": len(bezinga_negatives) if bezinga_results else 0,
213
+ "bezinga_sentiment_mean": bezinga_label_mean if bezinga_results else 0,
214
+ "bezinga_positive_score_mean": bezinga_positive_score_mean if bezinga_results else 0,
215
+ "bezinga_negative_score_mean": bezinga_negative_score_mean if bezinga_results else 0
216
+ },
217
+ "finhub": {
218
+ "finhub_articles": len(finhub_results) if finhub_results else 0,
219
+ "finhub_positives": len(finhub_positives) if finhub_results else 0,
220
+ "finhub_negatives": len(finhub_negatives) if finhub_results else 0,
221
+ "finhub_sentiment_mean": finhub_label_mean if finhub_results else 0,
222
+ "finhub_positive_score_mean": finhub_positive_score_mean if finhub_results else 0,
223
+ "finhub_negative_score_mean": finhub_negative_score_mean if finhub_results else 0
224
+ },
225
+ "marketaux": {
226
+ "marketaux_articles": len(marketaux_results) if marketaux_results else 0,
227
+ "marketaux_positives": len(marketaux_positives) if marketaux_results else 0,
228
+ "marketaux_negatives": len(marketaux_negatives) if marketaux_results else 0,
229
+ "marketaux_sentiment_mean": marketaux_label_mean if marketaux_results else 0,
230
+ "marketaux_positive_score_mean": marketaux_positive_score_mean if marketaux_results else 0,
231
+ "marketaux_negative_score_mean": marketaux_negative_score_mean if marketaux_results else 0
232
+ },
233
+ "newsapi": {
234
+ "newsapi_articles": len(newsapi_results) if newsapi_results else 0,
235
+ "newsapi_positives": len(newsapi_positives) if newsapi_results else 0,
236
+ "newsapi_negatives": len(newsapi_negatives) if newsapi_results else 0,
237
+ "newsapi_sentiment_mean": newsapi_label_mean if newsapi_results else 0,
238
+ "newsapi_positive_score_mean": newsapi_positive_score_mean if newsapi_results else 0,
239
+ "newsapi_negative_score_mean": newsapi_negative_score_mean if newsapi_results else 0
240
+ },
241
+ "newsdata": {
242
+ "newsdata_articles": len(newsdata_results) if newsdata_results else 0,
243
+ "newsdata_positives": len(newsdata_positives) if newsdata_results else 0,
244
+ "newsdata_negatives": len(newsdata_negatives) if newsdata_results else 0,
245
+ "newsdata_sentiment_mean": newsdata_label_mean if newsdata_results else 0,
246
+ "newsdata_positive_score_mean": newsdata_positive_score_mean if newsdata_results else 0,
247
+ "newsdata_negative_score_mean": newsdata_negative_score_mean if newsdata_results else 0
248
+ },
249
+ "vantage": {
250
+ "vantage_articles": len(vantage_results) if vantage_results else 0,
251
+ "vantage_positives": len(vantage_positives) if vantage_results else 0,
252
+ "vantage_negatives": len(vantage_negatives) if vantage_results else 0,
253
+ "vantage_sentiment_mean": vantage_label_mean if vantage_results else 0,
254
+ "vantage_positive_score_mean": vantage_positive_score_mean if vantage_results else 0,
255
+ "vantage_negative_score_mean": vantage_negative_score_mean if vantage_results else 0
256
+ }
257
+ }
258
+
259
+ return results_dict
app.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from aggregator import get_articles_sentiment
3
+
4
+ st.title("Real time financial news fast sentiment")
5
+
6
+ results = get_articles_sentiment("AAPL", "Andreagus/fin_miniLM_16")
7
+
8
+ st.write(results)
bezinga_caller.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from streamlit import secrets
3
+
4
+ bezinga_api = secrets["bezinga_api"]
5
+
6
+
7
+ def bezinga_get(ticker):
8
+
9
+ bezinga_list = []
10
+
11
+ url = f"https://api.benzinga.com/api/v2/news?token={bezinga_api}&pageSize=1000&tickers={ticker}"
12
+
13
+ headers = {"accept": "application/json"}
14
+
15
+ r = requests.request("GET", url, headers=headers)
16
+
17
+ data = r.json()
18
+ print(data)
19
+ for article in data:
20
+ print(article)
21
+ bezinga_list.append(article['title'])
22
+
23
+ return bezinga_list
24
+
finub_caller.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import finnhub
2
+ from streamlit import secrets
3
+
4
+ finhub_api = secrets["finhub_api"]
5
+
6
+ from datetime import datetime, timedelta
7
+
8
+ def get_finhub(ticker):
9
+
10
+ finhub_list = []
11
+
12
+ def get_yesterday(frmt='%Y-%m-%d', string=True):
13
+ yesterday = datetime.now() - timedelta(days=1)
14
+ if string:
15
+ return yesterday.strftime(frmt)
16
+ return yesterday
17
+ from datetime import datetime
18
+
19
+ def get_today(frmt='%Y-%m-%d'):
20
+ today = datetime.now()
21
+ return today.strftime(frmt)
22
+
23
+ # Example usage:
24
+ today = get_today() # Output: '2024-02-04'
25
+
26
+ # Example usage:
27
+ yesterday = get_yesterday()
28
+
29
+ finnhub_client = finnhub.Client(api_key=f"{finhub_api}")
30
+
31
+
32
+
33
+ data = finnhub_client.company_news(ticker, _from=yesterday, to=today)
34
+
35
+ for article in data:
36
+ finhub_list.append(article['headline'])
37
+
38
+ return finhub_list
39
+
marketaux_caller.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from streamlit import secrets
3
+
4
+ marketaux_api = secrets["marketaux_api"]
5
+
6
+ def get_marketaux(ticker):
7
+
8
+ marketaux_list = []
9
+ url = f'https://api.marketaux.com/v1/news/all?symbols={ticker}&filter_entities=true&language=en&api_token={marketaux_api}'
10
+ r = requests.get(url)
11
+ data = r.json()
12
+
13
+ for article in data['data']:
14
+ marketaux_list.append(article['title'])
15
+
16
+ return marketaux_list
newsapi_caller.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ from api_keys import news_api
3
+ import requests
4
+ from streamlit import secrets
5
+
6
+ news_api = secrets["news_api"]
7
+
8
+ def get_newsapi(company):
9
+
10
+ newsapi_list = []
11
+
12
+ def get_today(frmt='%Y-%m-%d'):
13
+ today = datetime.date.today()
14
+ return today.strftime(frmt)
15
+
16
+ today = get_today()
17
+
18
+ url = f"https://newsapi.org/v2/everything?q={company}&from={today}&sortBy=popularity&apiKey={news_api}"
19
+
20
+ headers = {"accept": "application/json"}
21
+
22
+ r = requests.request("GET", url, headers=headers)
23
+
24
+ data = r.json()
25
+
26
+ for article in data['articles']:
27
+ newsapi_list.append(article['title'])
28
+
29
+ return newsapi_list
30
+
31
+
newsdata_caller.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from api_keys import newsdata_api
2
+ import requests
3
+ from streamlit import secrets
4
+
5
+ newsdata_api = secrets["newsdata_api"]
6
+
7
+ def get_newsdata(company):
8
+
9
+ newsdata_list = []
10
+
11
+ url = f"https://newsdata.io/api/1/news?apikey={newsdata_api}&q={company}&language=en"
12
+
13
+ headers = {"accept": "application/json"}
14
+
15
+ r = requests.request("GET", url, headers=headers)
16
+
17
+ data = r.json()
18
+
19
+ for article in data['results']:
20
+ newsdata_list.append(article['title'])
21
+
22
+ return newsdata_list
vantage_caller.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from api_keys import vantage_api
2
+ import requests
3
+ from streamlit import secrets
4
+
5
+ vantage_api = secrets["vantage_api"]
6
+
7
+ def get_vantage(ticker):
8
+
9
+ vantage_list = []
10
+
11
+ url = f'https://www.alphavantage.co/query?function=NEWS_SENTIMENT&tickers={ticker}&apikey={vantage_api}&limit=1000'
12
+ r = requests.get(url)
13
+ data = r.json()
14
+
15
+ for article in data['feed']:
16
+ vantage_list.append(article['title'])
17
+
18
+ return vantage_list