Views
No views yet
- Acc : 48.516 %
- Mac-F1 : 33.907 %
- Also see our GitHub Repo
- Python 3.6+
- PyTorch 1.7.0+
- Transformers 4.0.0+
To use the model, first install thetransformerspackage from Hugging Face:
pip install transformersThen, you can load the model and tokenizer using the following code:
1from transformers import AutoModelForSequenceClassification, AutoTokenizer
2import numpy as np
3import urllib.request
4import csv1MODEL = "Karim-Gamal/MMiniLM-L12-finetuned-SemEval-2018-emojis-IID-Fed"
2tokenizer = AutoTokenizer.from_pretrained(MODEL)
3model = AutoModelForSequenceClassification.from_pretrained(MODEL)Once you have the tokenizer and model, you can preprocess your text and pass it to the model for prediction:
1# Preprocess text (username and link placeholders)
2def preprocess(text):
3 new_text = []
4 for t in text.split(" "):
5 t = '@user' if t.startswith('@') and len(t) > 1 else t
6 t = 'http' if t.startswith('http') else t
7 new_text.append(t)
8 return " ".join(new_text)
9
10text = "Hello world"
11text = preprocess(text)
12encoded_input = tokenizer(text, return_tensors='pt')
13output = model(**encoded_input)
14scores = output[0][0].detach().numpy()The scores variable contains the probabilities for each of the possible emoji labels. To get the top k predictions, you can use the following code:
1# download label mapping
2labels=[]
3mapping_link = "https://raw.githubusercontent.com/cardiffnlp/tweeteval/main/datasets/emoji/mapping.txt"
4with urllib.request.urlopen(mapping_link) as f:
5 html = f.read().decode('utf-8').split("\n")
6 csvreader = csv.reader(html, delimiter='\t')
7labels = [row[1] for row in csvreader if len(row) > 1]
8
9k = 3 # number of top predictions to show
10ranking = np.argsort(scores)
11ranking = ranking[::-1]
12for i in range(k):
13 l = labels[ranking[i]]
14 s = scores[ranking[i]]
15 print(f"{i+1}) {l} {np.round(float(s), 4)}")