Views
No views yet
1from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
2
3# Carregar tokenizer
4tokenizer = AutoTokenizer.from_pretrained("roberta-base")
5
6# Map de id2label
7id2label = {
8 0: 'admiration', 1: 'amusement', 2: 'anger', 3: 'annoyance', 4: 'approval',
9 5: 'caring', 6: 'confusion', 7: 'curiosity', 8: 'desire', 9: 'disappointment',
10 10: 'disapproval', 11: 'disgust', 12: 'embarrassment', 13: 'excitement', 14: 'fear',
11 15: 'gratitude', 16: 'grief', 17: 'joy', 18: 'love', 19: 'nervousness', 20: 'optimism',
12 21: 'pride', 22: 'realization', 23: 'relief', 24: 'remorse', 25: 'sadness', 26: 'surprise', 27: 'neutral'
13}
14
15# Carregar modelo
16modelo = AutoModelForSequenceClassification.from_pretrained("pinheiroxs/pinheiro-roberta-goemotions-v2",id2label=id2label)
17
18# Criar pipeline
19classifier = pipeline("text-classification", model=modelo, tokenizer=tokenizer, top_k=None)1# Exemplo de uso
2texto = "I am very happy today!"
3resultado = classifier(texto)[0]
4
5# Formatar resultados
6resultado_ordenado = sorted(resultado, key=lambda x: x['score'], reverse=True)
7for r in resultado_ordenado:
8 print(f"{r['label']:15} : {r['score']*100:.2f}%")