Views
No views yet
1from transformers import AutoModelForSequenceClassification
2from transformers import TFAutoModelForSequenceClassification
3from transformers import AutoTokenizer
4import numpy as np
5from scipy.special import softmax
6import csv
7import urllib.request
8# Preprocess text (username and link placeholders)
9def preprocess(text):
10 new_text = []
11 for t in text.split(" "):
12 t = '@user' if t.startswith('@') and len(t) > 1 else t
13 t = 'http' if t.startswith('http') else t
14 new_text.append(t)
15 return " ".join(new_text)
16
17MODEL = f"ccarvajal/beto-emoji"
18tokenizer = AutoTokenizer.from_pretrained(MODEL)
19# download label mapping
20labels=[]
21mapping_link = f"https://raw.githubusercontent.com/camilocarvajalreyes/beto-emoji/main/es_mapping.txt"
22with urllib.request.urlopen(mapping_link) as f:
23 html = f.read().decode('utf-8').split("\n")
24 csvreader = csv.reader(html, delimiter='\t')
25labels = [row[1] for row in csvreader if len(row) > 1]
26
27model = AutoModelForSequenceClassification.from_pretrained(MODEL)
28model.save_pretrained(MODEL)
29text = "que viva españa"
30text = preprocess(text)
31encoded_input = tokenizer(text, return_tensors='pt')
32output = model(**encoded_input)
33scores = output[0][0].detach().numpy()
34scores = softmax(scores)
35
36ranking = np.argsort(scores)
37ranking = ranking[::-1]
38for i in range(scores.shape[0]):
39 l = labels[ranking[i]]
40 s = scores[ranking[i]]
41 print(f"{i+1}) {l} {np.round(float(s), 4)}")11) 🇪🇸 0.2508
22) 😍 0.238
33) 👌 0.2225
44) 😂 0.0806
55) ❤ 0.0489
66) 😁 0.0415
77) 😜 0.0232
88) 😎 0.0229
99) 😊 0.0156
1010) 😉 0.0119
1111) 💜 0.0079
1212) 💕 0.0077
1313) 💪 0.0066
1414) 💘 0.0054
1515) 💙 0.0052
1616) 💞 0.005
1717) 😘 0.0034
1818) 🎶 0.0022
1919) ✨ 0.0007 precision recall f1-score support
❤ 0.39 0.43 0.41 2141
😍 0.29 0.39 0.33 1408
😂 0.51 0.51 0.51 1499
💕 0.09 0.05 0.06 352
😊 0.12 0.23 0.16 514
😘 0.24 0.23 0.24 397
💪 0.37 0.43 0.40 307
😉 0.15 0.17 0.16 453
👌 0.09 0.16 0.11 180
🇪🇸 0.46 0.46 0.46 424
😎 0.12 0.11 0.11 339
💙 0.36 0.02 0.04 413
💜 0.00 0.00 0.00 235
😜 0.04 0.02 0.02 274
💞 0.00 0.00 0.00 93
✨ 0.26 0.12 0.17 416
🎶 0.25 0.24 0.24 212
💘 0.00 0.00 0.00 134
😁 0.05 0.03 0.04 209
accuracy 0.30 10000
macro_avg 0.20 0.19 0.18 10000
weighted avg 0.29 0.30 0.29 100001training_args = TrainingArguments(
2 output_dir="./results",
3 learning_rate=2e-5,
4 per_device_train_batch_size=16,
5 per_device_eval_batch_size=16,
6 num_train_epochs=5,
7 weight_decay=0.01
8)