Create Roberta installation
Browse files- Roberta installation +36 -0
Roberta installation
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
In [ ]:
|
2 |
+
from transformers import AutoTokenizer
|
3 |
+
from transformers import AutoModelForSequenceClassification
|
4 |
+
from scipy.special import softmax
|
5 |
+
Running cells with 'c:\Users\dell\AppData\Local\Microsoft\WindowsApps\python3.10.exe' requires ipykernel package.
|
6 |
+
|
7 |
+
Run the following command to install 'ipykernel' into the Python environment.
|
8 |
+
|
9 |
+
Command: 'c:/Users/dell/AppData/Local/Microsoft/WindowsApps/python3.10.exe -m pip install ipykernel -U --user --force-reinstall'
|
10 |
+
In [ ]:
|
11 |
+
MODEL = f"cardiffnlp/twitter-roberta-base-sentiment"
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
13 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
14 |
+
Running cells with 'c:\Users\dell\AppData\Local\Microsoft\WindowsApps\python3.10.exe' requires ipykernel package.
|
15 |
+
|
16 |
+
Run the following command to install 'ipykernel' into the Python environment.
|
17 |
+
|
18 |
+
Command: 'c:/Users/dell/AppData/Local/Microsoft/WindowsApps/python3.10.exe -m pip install ipykernel -U --user --force-reinstall'
|
19 |
+
In [5]:
|
20 |
+
def sentiment(tweet):
|
21 |
+
encoded_text = tokenizer(tweet,return_tensors='pt')
|
22 |
+
output = model(**encoded_text)
|
23 |
+
scores = output[0][0].detach().numpy()
|
24 |
+
scores = softmax(scores)
|
25 |
+
scores_dict = {
|
26 |
+
'NEGATIVE' : scores[0],
|
27 |
+
'NEUTRAL' : scores[1],
|
28 |
+
'POSITIVE' : scores[2]
|
29 |
+
}
|
30 |
+
return scores_dict
|
31 |
+
In [7]:
|
32 |
+
tweet = "you're a sweet person😤"
|
33 |
+
sentiment(tweet)
|
34 |
+
Out[7]:
|
35 |
+
{'NEGATIVE': 0.85113734, 'NEUTRAL': 0.13698761, 'POSITIVE': 0.011875027}
|
36 |
+
In [ ]:
|