trade-master / finbert_utils.py
Dhahlan2000's picture
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.
18e88fc
raw
history blame
1.07 kB
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
from typing import Tuple
device = "cuda:0" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
model = AutoModelForSequenceClassification.from_pretrained("ProsusAI/finbert").to(device)
labels = ["positive", "negative", "neutral"]
def estimate_sentiment(news):
if news:
tokens = tokenizer(news, return_tensors="pt", padding=True).to(device)
result = model(tokens["input_ids"], attention_mask=tokens["attention_mask"])[
"logits"
]
result = torch.nn.functional.softmax(torch.sum(result, 0), dim=-1)
probability = result[torch.argmax(result)]
sentiment = labels[torch.argmax(result)]
return probability, sentiment
else:
return 0, labels[-1]
if __name__ == "__main__":
tensor, sentiment = estimate_sentiment(['markets responded negatively to the news!','traders were displeased!'])
print(tensor, sentiment)
print(torch.cuda.is_available())