Views
No views yet
1import pandas as pd
2import numpy as np
3from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
5# Load model
6model = AutoModelForSequenceClassification.from_pretrained("lkonle/EMO_Agitation_gbert")
7
8# Load tokenizer
9tokenizer = AutoTokenizer.from_pretrained("lkonle/EMO_Agitation_gbert")
10tokenizer.pad_token = "[PAD]"
11tokenizer.add_special_tokens({'pad_token': '[PAD]'})
12
13# define input text
14myinput = ["Paul war sehr sehr glücklich über seinen Welpen.",
15 "Paul war sehr traurig über sein Frühstück.",
16 "Paul hatte große Langeweile."]
17
18# tokenize, encode, format as batch and return pytorch tensors
19input_ids = tokenizer.batch_encode_plus(myinput, truncation=True, padding="max_length", padding_side="right", return_tensors="pt")
20
21# predict
22logits = model(**input_ids)["logits"]
23
24# get the predicted label
25result = logits.detach().numpy()
26prediction = np.argmax(result, axis=1)
27
28# store result in pandas
29output = pd.DataFrame()
30output["inputs"] = myinput
31output["prediction"] = prediction
32print(output)