Spaces:
Running
Running
Dhahlan2000
commited on
Commit
·
18e88fc
1
Parent(s):
d72c6ad
Refactor requirements.txt to remove finbert dependency and maintain timedelta. This change streamlines the project's dependencies while ensuring necessary libraries for trading and data analysis remain intact.
Browse files- finbert_utils.py +28 -0
- requirements.txt +1 -2
finbert_utils.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
+
import torch
|
3 |
+
from typing import Tuple
|
4 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
5 |
+
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained("ProsusAI/finbert").to(device)
|
8 |
+
labels = ["positive", "negative", "neutral"]
|
9 |
+
|
10 |
+
def estimate_sentiment(news):
|
11 |
+
if news:
|
12 |
+
tokens = tokenizer(news, return_tensors="pt", padding=True).to(device)
|
13 |
+
|
14 |
+
result = model(tokens["input_ids"], attention_mask=tokens["attention_mask"])[
|
15 |
+
"logits"
|
16 |
+
]
|
17 |
+
result = torch.nn.functional.softmax(torch.sum(result, 0), dim=-1)
|
18 |
+
probability = result[torch.argmax(result)]
|
19 |
+
sentiment = labels[torch.argmax(result)]
|
20 |
+
return probability, sentiment
|
21 |
+
else:
|
22 |
+
return 0, labels[-1]
|
23 |
+
|
24 |
+
|
25 |
+
if __name__ == "__main__":
|
26 |
+
tensor, sentiment = estimate_sentiment(['markets responded negatively to the news!','traders were displeased!'])
|
27 |
+
print(tensor, sentiment)
|
28 |
+
print(torch.cuda.is_available())
|
requirements.txt
CHANGED
@@ -10,5 +10,4 @@ torch
|
|
10 |
transformers
|
11 |
lumibot
|
12 |
alpaca-trade-api
|
13 |
-
timedelta
|
14 |
-
finbert
|
|
|
10 |
transformers
|
11 |
lumibot
|
12 |
alpaca-trade-api
|
13 |
+
timedelta
|
|