Views
No views yet
1from transformers import AutoConfig, AutoModelForSequenceClassification, AutoTokenizer
2
3LABELS = ["Other", "Belief", "Perspective", "Feeling", "Experience",
4 "Reflection", "Difficulty", "Intention", "Learning"]
5
6class NeuralClassifier:
7
8 def __init__(self, model_path: str, uses_context: bool, device: str):
9 self.config = AutoConfig.from_pretrained(model_path)
10 self.device = device
11 self.model = AutoModelForSequenceClassification.from_pretrained(model_path, config=self.config).to(device)
12 self.tokenizer = AutoTokenizer.from_pretrained(model_path)
13 self.uses_context = uses_context
14
15 def predict_sentence(self, sentence: str, context: str = None):
16 if context is None and self.uses_context:
17 raise ValueError("You need to pass in context argument, including the sentence")
18
19 features = self.tokenizer(sentence, text_pair=context,
20 padding="max_length", truncation=True, return_tensors='pt')
21 outputs = self.model(**features.to(self.device), return_dict=True)
22 argmax = outputs.logits.argmax(dim=-1).detach().cpu().tolist()[0]
23 labels = LABELS[argmax]
24
25 return labels1classifier = NeuralClassifier(model_path="MU-NLPC/XLM-R-large-reflective-conf4",
2 uses_context=False,
3 device="cpu")
4
5test_sentences = ["And one day I will be a real teacher and I will try to do the best I can for the children.",
6 "I felt really well!",
7 "gfagdhj gjfdjgh dg"]
8
9y_pred = [classifier.predict_sentence(sentence) for sentence in tqdm(test_sentences)]
10
11print(y_pred)
12
13>>> ['Intention', 'Feeling', 'Other']1@Article{Nehyba2022applications,
2 author={Nehyba, Jan and {\v{S}}tef{\'a}nik, Michal},
3 title={Applications of deep language models for reflective writings},
4 journal={Education and Information Technologies},
5 year={2022},
6 month={Sep},
7 day={05},
8 issn={1573-7608},
9 doi={10.1007/s10639-022-11254-7},
10 url={https://doi.org/10.1007/s10639-022-11254-7}
11}