Views
No views yet
1import torch
2from transformers import AutoTokenizer, AutoModelForTokenClassification
3
4tokenizer = AutoTokenizer.from_pretrained("zaemyung/DElIteraTeR-RoBERTa-Intent-Span-Detector")
5
6# update tokenizer with special tokens
7INTENT_CLASSES = ['none', 'clarity', 'fluency', 'coherence', 'style', 'meaning-changed'] # `meaning-changed` is not used
8INTENT_OPENED_TAGS = [f'<{intent_class}>' for intent_class in INTENT_CLASSES]
9INTENT_CLOSED_TAGS = [f'</{intent_class}>' for intent_class in INTENT_CLASSES]
10INTENT_TAGS = set(INTENT_OPENED_TAGS + INTENT_CLOSED_TAGS)
11special_tokens_dict = {'additional_special_tokens': ['<bos>', '<eos>'] + list(INTENT_TAGS)}
12tokenizer.add_special_tokens(special_tokens_dict)
13
14model = AutoModelForTokenClassification.from_pretrained("zaemyung/DElIteraTeR-RoBERTa-Intent-Span-Detector")
15
16id2label = {0: "none", 1: "clarity", 2: "fluency", 3: "coherence", 4: "style", 5: "meaning-changed"}
17
18before_text = '<bos>I likes coffee?<eos>'
19model_input = tokenizer(before_text, return_tensors='pt')
20model_output = model(**model_input)
21softmax_scores = torch.softmax(model_output.logits, dim=-1)
22pred_ids = torch.argmax(softmax_scores, axis=-1)[0].tolist()
23pred_intents = [id2label[_id] for _id in pred_ids]
24
25tokens = tokenizer.convert_ids_to_tokens(model_input['input_ids'][0])
26
27for token, pred_intent in zip(tokens, pred_intents):
28 print(f"{token}: {pred_intent}")
29
30"""
31<s>: none
32<bos>: none
33I: fluency
34Ġlikes: fluency
35Ġcoffee: none
36?: none
37<eos>: none
38</s>: none
39"""