-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
39 lines (33 loc) · 927 Bytes
/
analysis.py
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
import requests
import certifi
from apikey import API_KEY
from GoogleNews import GoogleNews
from sources.indianews import IndiaNews
requests.utils.DEFAULT_CA_BUNDLE_PATH = certifi.where()
API_URL = "https://api-inference.huggingface.co/models/ProsusAI/finbert"
API = f"Bearer {API_KEY} "
headers = {"Authorization": API}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
output = response.json()
return output[0][0]['score']
def get_title(query):
news = GoogleNews()
news.search(query)
return [story['title'] for story in news.results()]
def analysis(ticker):
if API_KEY == '':
return 'ENTER THE HUGGING FACE API KEY'
query_list = get_title(ticker)
index = 0
number = 0
for query_title in query_list:
index += query(query_title)
number += 1
ratio = index / number
if ratio > 0.7:
return 'Postive'
elif ratio > 0.4:
return 'Neutral'
else:
return 'Negative'