Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
+
|
4 |
+
def get_sentiment(sentences):
|
5 |
+
bert_dict = {}
|
6 |
+
vectors = tokenizer(sentences, padding = True, max_length = 65, return_tensors='pt').to(device)
|
7 |
+
outputs = bert_model(**vectors).logits
|
8 |
+
probs = torch.nn.functional.softmax(outputs, dim = 1)
|
9 |
+
for prob in probs:
|
10 |
+
bert_dict['neg'] = round(prob[0].item(), 3)
|
11 |
+
bert_dict['neu'] = round(prob[1].item(), 3)
|
12 |
+
bert_dict['pos'] = round(prob[2].item(), 3)
|
13 |
+
print (bert_dict)
|
14 |
+
|
15 |
+
MODEL_NAME = 'RashidNLP/Finance-Sentiment-Classification'
|
16 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
17 |
+
|
18 |
+
bert_model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels = 3).to(device)
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
20 |
+
|
21 |
+
get_sentiment(["The stock market will struggle until debt ceiling is increased", "ChatGPT is boosting Microsoft's search engine market share"])
|
22 |
+
|