cardiffnlp commited on
Commit
203e724
β€’
1 Parent(s): a2c1de9
.ipynb_checkpoints/README-checkpoint.md ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Twitter-roBERTa-base
2
+
3
+ This is a roBERTa-base model trained on ~58M tweets and finetuned for the emoji prediction task at Semeval 2018.
4
+ For full description: [_TweetEval_ benchmark (Findings of EMNLP 2020)](https://arxiv.org/pdf/2010.12421.pdf).
5
+ To evaluate this and other models on Twitter-specific data, please refer to the [Tweeteval official repository](https://github.com/cardiffnlp/tweeteval).
6
+
7
+ ## Example of classification
8
+
9
+ ```python
10
+ from transformers import AutoModelForSequenceClassification
11
+ from transformers import TFAutoModelForSequenceClassification
12
+ from transformers import AutoTokenizer
13
+ import numpy as np
14
+ from scipy.special import softmax
15
+ import csv
16
+ import urllib.request
17
+
18
+ # Tasks:
19
+ # emoji, emotion, hate, irony, offensive, sentiment
20
+ # stance/abortion, stance/atheism, stance/climate, stance/feminist, stance/hillary
21
+
22
+ task='emoji'
23
+ MODEL = f"cardiffnlp/twitter-roberta-base-{task}"
24
+
25
+ tokenizer = AutoTokenizer.from_pretrained(MODEL)
26
+
27
+ # download label mapping
28
+ labels=[]
29
+ mapping_link = f"https://raw.githubusercontent.com/cardiffnlp/tweeteval/main/datasets/{task}/mapping.txt"
30
+ with urllib.request.urlopen(mapping_link) as f:
31
+ html = f.read().decode('utf-8').split("\n")
32
+ spamreader = csv.reader(html[:-1], delimiter='\t')
33
+ labels = [row[1] for row in spamreader]
34
+
35
+ # PT
36
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL)
37
+ model.save_pretrained(MODEL)
38
+
39
+ text = "Good night 😊"
40
+ encoded_input = tokenizer(text, return_tensors='pt')
41
+ output = model(**encoded_input)
42
+ scores = output[0][0].detach().numpy()
43
+ scores = softmax(scores)
44
+
45
+ # # TF
46
+ # model = TFAutoModelForSequenceClassification.from_pretrained(MODEL)
47
+ # model.save_pretrained(MODEL)
48
+
49
+ # text = "Good night 😊"
50
+ # encoded_input = tokenizer(text, return_tensors='tf')
51
+ # output = model(encoded_input)
52
+ # scores = output[0][0].numpy()
53
+ # scores = softmax(scores)
54
+
55
+ ranking = np.argsort(scores)
56
+ ranking = ranking[::-1]
57
+ for i in range(scores.shape[0]):
58
+ l = labels[ranking[i]]
59
+ s = scores[ranking[i]]
60
+ print(f"{i+1}) {l} {np.round(float(s), 4)}")
61
+
62
+ ```
63
+
64
+ Output:
65
+
66
+ ```
67
+ 1) 😘 0.2637
68
+ 2) ❀️ 0.1952
69
+ 3) πŸ’• 0.1171
70
+ 4) ✨ 0.0927
71
+ 5) 😊 0.0756
72
+ 6) πŸ’œ 0.046
73
+ 7) πŸ’™ 0.0444
74
+ 8) 😍 0.0272
75
+ 9) πŸ˜‰ 0.0228
76
+ 10) 😎 0.0198
77
+ 11) 😜 0.0166
78
+ 12) πŸ˜‚ 0.0132
79
+ 13) 😁 0.0131
80
+ 14) β˜€ 0.0112
81
+ 15) πŸŽ„ 0.009
82
+ 16) πŸ’― 0.009
83
+ 17) πŸ”₯ 0.008
84
+ 18) πŸ“· 0.0057
85
+ 19) πŸ‡ΊπŸ‡Έ 0.005
86
+ 20) πŸ“Έ 0.0048
87
+ ```
README.md ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Twitter-roBERTa-base
2
+
3
+ This is a roBERTa-base model trained on ~58M tweets and finetuned for the emoji prediction task at Semeval 2018.
4
+ For full description: [_TweetEval_ benchmark (Findings of EMNLP 2020)](https://arxiv.org/pdf/2010.12421.pdf).
5
+ To evaluate this and other models on Twitter-specific data, please refer to the [Tweeteval official repository](https://github.com/cardiffnlp/tweeteval).
6
+
7
+ ## Example of classification
8
+
9
+ ```python
10
+ from transformers import AutoModelForSequenceClassification
11
+ from transformers import TFAutoModelForSequenceClassification
12
+ from transformers import AutoTokenizer
13
+ import numpy as np
14
+ from scipy.special import softmax
15
+ import csv
16
+ import urllib.request
17
+
18
+ # Tasks:
19
+ # emoji, emotion, hate, irony, offensive, sentiment
20
+ # stance/abortion, stance/atheism, stance/climate, stance/feminist, stance/hillary
21
+
22
+ task='emoji'
23
+ MODEL = f"cardiffnlp/twitter-roberta-base-{task}"
24
+
25
+ tokenizer = AutoTokenizer.from_pretrained(MODEL)
26
+
27
+ # download label mapping
28
+ labels=[]
29
+ mapping_link = f"https://raw.githubusercontent.com/cardiffnlp/tweeteval/main/datasets/{task}/mapping.txt"
30
+ with urllib.request.urlopen(mapping_link) as f:
31
+ html = f.read().decode('utf-8').split("\n")
32
+ spamreader = csv.reader(html[:-1], delimiter='\t')
33
+ labels = [row[1] for row in spamreader]
34
+
35
+ # PT
36
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL)
37
+ model.save_pretrained(MODEL)
38
+
39
+ text = "Good night 😊"
40
+ encoded_input = tokenizer(text, return_tensors='pt')
41
+ output = model(**encoded_input)
42
+ scores = output[0][0].detach().numpy()
43
+ scores = softmax(scores)
44
+
45
+ # # TF
46
+ # model = TFAutoModelForSequenceClassification.from_pretrained(MODEL)
47
+ # model.save_pretrained(MODEL)
48
+
49
+ # text = "Good night 😊"
50
+ # encoded_input = tokenizer(text, return_tensors='tf')
51
+ # output = model(encoded_input)
52
+ # scores = output[0][0].numpy()
53
+ # scores = softmax(scores)
54
+
55
+ ranking = np.argsort(scores)
56
+ ranking = ranking[::-1]
57
+ for i in range(scores.shape[0]):
58
+ l = labels[ranking[i]]
59
+ s = scores[ranking[i]]
60
+ print(f"{i+1}) {l} {np.round(float(s), 4)}")
61
+
62
+ ```
63
+
64
+ Output:
65
+
66
+ ```
67
+ 1) 😘 0.2637
68
+ 2) ❀️ 0.1952
69
+ 3) πŸ’• 0.1171
70
+ 4) ✨ 0.0927
71
+ 5) 😊 0.0756
72
+ 6) πŸ’œ 0.046
73
+ 7) πŸ’™ 0.0444
74
+ 8) 😍 0.0272
75
+ 9) πŸ˜‰ 0.0228
76
+ 10) 😎 0.0198
77
+ 11) 😜 0.0166
78
+ 12) πŸ˜‚ 0.0132
79
+ 13) 😁 0.0131
80
+ 14) β˜€ 0.0112
81
+ 15) πŸŽ„ 0.009
82
+ 16) πŸ’― 0.009
83
+ 17) πŸ”₯ 0.008
84
+ 18) πŸ“· 0.0057
85
+ 19) πŸ‡ΊπŸ‡Έ 0.005
86
+ 20) πŸ“Έ 0.0048
87
+ ```