Views
No views yet
1from transformers import pipeline
2
3model = "VictorYeste/deberta-based-human-value-detection"
4tokenizer = "VictorYeste/deberta-based-human-value-detection"
5
6values_detection = pipeline("text-classification", model=model, tokenizer=tokenizer, top_k=None)
7
8values_detection("We would like to share this model with the research community.")[[{'label': 'Self-direction: thought', 'score': 0.02448045276105404},
{'label': 'Stimulation', 'score': 0.01451807003468275},
{'label': 'Universalism: concern', 'score': 0.006046739872545004},
{'label': 'Self-direction: action', 'score': 0.004837467335164547},
{'label': 'Benevolence: dependability', 'score': 0.001295178197324276},
{'label': 'Benevolence: caring', 'score': 0.0009907316416501999},
{'label': 'Conformity: interpersonal', 'score': 0.0004476217145565897},
{'label': 'Security: societal', 'score': 0.00039295252645388246},
{'label': 'Universalism: tolerance', 'score': 0.0003538706514518708},
{'label': 'Power: dominance', 'score': 0.00016191638133022934},
{'label': 'Power: resources', 'score': 0.0001522471575299278},
{'label': 'Universalism: nature', 'score': 0.00014803129306528717},
{'label': 'Humility', 'score': 0.0001100009903893806},
{'label': 'Face', 'score': 9.083452459890395e-05},
{'label': 'Conformity: rules', 'score': 8.524076838511974e-05},
{'label': 'Achievement', 'score': 6.411433423636481e-05},
{'label': 'Security: personal', 'score': 5.183048051549122e-05},
{'label': 'Hedonism', 'score': 3.167059549014084e-05},
{'label': 'Tradition', 'score': 2.4977327484521084e-05}]]1import torch
2import numpy as np
3import transformers
4
5def multilabel_pipeline(text, model, tokenizer, id2label):
6 # Code adapted from: https://github.com/NielsRogge/Transformers-Tutorials/blob/master/BERT/Fine_tuning_BERT_(and_friends)_for_multi_label_text_classification.ipynb
7 """ Predicts the value probabilities (attained and constrained) for each sentence """
8 encoding = tokenizer(text, return_tensors="pt")
9 encoding = {k: v for k,v in encoding.items()}
10 outputs = model(**encoding)
11 logits = outputs.logits
12 sigmoid = torch.nn.Sigmoid()
13 probs = sigmoid(logits.squeeze().cpu())
14 predictions = np.zeros(probs.shape)
15 predictions[np.where(probs >= 0.5)] = 1
16 predicted_labels = [id2label[idx] for idx, label in enumerate(predictions) if label == 1.0]
17 return predicted_labels
18
19values = ["Self-direction: thought", "Self-direction: action", "Stimulation", "Hedonism", "Achievement", "Power: dominance", "Power: resources", "Face", "Security: personal", "Security: societal", "Tradition", "Conformity: rules", "Conformity: interpersonal", "Humility", "Benevolence: caring", "Benevolence: dependability", "Universalism: concern", "Universalism: nature", "Universalism: tolerance" ]
20id2label = {idx:label for idx, label in enumerate(values)}
21model_name = "VictorYeste/deberta-based-human-value-detection"
22tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
23model = transformers.AutoModelForSequenceClassification.from_pretrained(model_name)@inproceedings{yeste2024philo,
title={Philo of Alexandria at touch{\'e}: a cascade model approach to human value detection},
author={Yeste, V{\'\i}ctor and Coll-Ardanuy, M and Rosso, Paolo},
booktitle={Working Notes of the Conference and Labs of the Evaluation Forum (CLEF 2024). CEUR Workshop Proceedings, CEUR-WS. org},
year={2024}
}