Views
No views yet
1import torch, os
2import pandas as pd
3from tqdm import tqdm
4import transformers
5from transformers import AutoModel
6from transformers import AutoConfig
7from transformers import BertTokenizerFast
8from SL_utils import *
9
10Coding_emotions = {
11 "AN": "Anger",
12 "AP": "Apprehension",
13 "SD": "Sadness",
14 "CO": "Confusion",
15 "HA": "Happiness",
16}
17
18emotions_list = list(Coding_emotions.keys())
19
20test_sentences = [
21 "In my dream I was follwed by the scary monster.",
22 "I was walking in a forest, sorrounded by singing birds. I was in calm and peace."
23]
24
25test_sentences_target = len(test_sentences)*[[0, 0, 0, 0, 0]]
26test_sentences_df = pd.DataFrame.from_dict(
27 {
28 "report":test_sentences,
29 "Report_as_Multilabel":test_sentences_target
30 }
31)1model_name = "bert-large-cased"
2model_config = AutoConfig.from_pretrained(model_name)
3tokenizer = BertTokenizerFast.from_pretrained(model_name, do_lower_case=False)
4testing_set = CustomDataset(test_sentences_df, tokenizer, max_length=512)
5
6test_params = {
7 'batch_size': 2,
8 'shuffle': True,
9 'num_workers': 0
10}
11
12testing_loader = DataLoader(testing_set, **test_params)
13
14model = BERT_PTM(
15 model_config,
16 model_name=model_name,
17 n_classes=len(emotions_list),
18 freeze_BERT=False,
19)
20
21# Load the models' weights from the pre-treined model
22model.load_state_dict(torch.load("path/to/pytorch_model.bin"))
23model.to("cuda")1outputs, targets, ids = validation(model, testing_loader, device="cuda", return_inputs=True)
2
3corr_outputs = np.array(outputs) >= 0.5
4corr_outputs_df = pd.DataFrame(corr_outputs, columns=emotions_list)
5corr_outputs_df = corr_outputs_df.astype(int)
6
7corr_outputs_df["report"] = decoded_ids = [decode_clean(x, tokenizer) for x in tqdm(ids)]1@inproceedings{bertolini-etal-2024-automatic,
2 title = "Automatic Annotation of Dream Report{'}s Emotional Content with Large Language Models",
3 author = "Bertolini, Lorenzo and
4 Elce, Valentina and
5 Michalak, Adriana and
6 Widhoelzl, Hanna-Sophia and
7 Bernardi, Giulio and
8 Weeds, Julie",
9 booktitle = "Proceedings of the 9th Workshop on Computational Linguistics and Clinical Psychology (CLPsych 2024)",
10 month = mar,
11 year = "2024",
12 address = "St. Julians, Malta",
13 publisher = "Association for Computational Linguistics",
14 url = "https://aclanthology.org/2024.clpsych-1.7",
15 pages = "92--107",
16}
17