Views
No views yet
LABEL_0: NOT disaster-relatedLABEL_1: Disaster-related1from transformers import pipeline
2MODEL_NAME = 'hannybal/disaster-twitter-xlm-roberta-al'
3classifier = pipeline('text-classification', model=MODEL_NAME, tokenizer='cardiffnlp/twitter-xlm-roberta-base')
4classifier('I can see fire and smoke from the nearby fire!')[{'label': 'LABEL_0', 'score': 0.9967854022979736}]1from transformers import AutoModelForSequenceClassification
2from transformers import AutoTokenizer, AutoConfig
3import numpy as np
4from scipy.special import softmax
5
6def preprocess(text: str) -> str:
7 """Pre-process texts by replacing usernames and links with placeholders.
8 """
9 new_text: list[str] = []
10 for t in text.split(" "):
11 t: str = '@user' if t.startswith('@') and len(t) > 1 else t
12 t = 'http' if t.startswith('http') else t
13 new_text.append(t)
14 return " ".join(new_text)
15
16MODEL_NAME = 'hannybal/disaster-twitter-xlm-roberta-al'
17
18tokenizer = AutoTokenizer.from_pretrained('cardiffnlp/twitter-xlm-roberta-base')
19config = AutoConfig.from_pretrained(MODEL_NAME)
20
21# example classification
22text = "Das ist alles, was von meinem Keller noch übrig ist... #flood #ahr @ Bad Neuenahr-Ahrweiler https://t.co/C68fBaKZWR"
23text = preprocess(text)
24encoded_input = tokenizer(text, return_tensors='pt')
25output = model(**encoded_input)
26scores = output[0][0].detach().numpy()
27scores = softmax(scores)
28
29# print labels and their respective scores
30ranking = np.argsort(scores)
31ranking = ranking[::-1]
32for i in range(scores.shape[0]):
33 l = config.id2label[ranking[i]]
34 s = scores[ranking[i]]
35 print(f"{i+1}) {l} {np.round(float(s), 4)}")1) LABEL_1 0.9999
2) LABEL_0 0.0001@inproceedings{Hanny.2024a,
title = {Active {{Learning}} for~{{Identifying Disaster-Related Tweets}}: {{A Comparison}} with~{{Keyword Filtering}} and~{{Generic Fine-Tuning}}},
shorttitle = {Active {{Learning}} for~{{Identifying Disaster-Related Tweets}}},
booktitle = {Intelligent {{Systems}} and {{Applications}}},
author = {Hanny, David and Schmidt, Sebastian and Resch, Bernd},
editor = {Arai, Kohei},
year = {2024},
pages = {126--142},
publisher = {Springer Nature Switzerland},
address = {Cham},
doi = {10.1007/978-3-031-66428-1_8},
isbn = {978-3-031-66428-1},
langid = {english}
}