idtoLabel argument in "text-classification" pipeline

I am using the text classification pipeline of huggingface to generate the emotions.

from transformers import pipeline
classifier = pipeline("text-classification",model='distilbert-base-uncased-emotion', return_all_scores=True)
prediction = classifier("I love using transformers. The best part is wide range of support and its easy to use", )
print(prediction)

but this gives result as [[{'label': 'LABEL_0', 'score': 0.005389929749071598}, {'label': 'LABEL_1', 'score': 0.0026532604824751616}, {'label': 'LABEL_2', 'score': 0.03011970967054367}, {'label': 'LABEL_3', 'score': 0.25675541162490845}, {'label': 'LABEL_4', 'score': 0.06454580277204514}, {'label': 'LABEL_5', 'score': 0.017099281772971153}]]

I want to change the label according to the data I trained the model with, I know I can change that in models config file but I want to change that programatically, we have the parameter for idtolabel to change the label with our own data while loading the model from pretrained. can anybody help with me how can I change that, also how to restrict the result, is there any parameter like top_n or top_k to return only that number of results?

@bhadresh-savani you also used this, can you help with the arguments for the pipeline?

Hi @sridharnlp,

There is an argument in the classifier top_k for selecting top k predictions,

prediction = classifier("I love using transformers. The best part is wide 
range of support and its easy to use", top_k=2)
print(prediction)

and for changing label2id programmatically, first you need to load model like this,

from transformers import AutoModelForSequenceClassification

model = AutoModelForSequenceClassification.from_pretrained("bhadresh-savani/distilbert-base-uncased-finetuned-emotion")

Once you load the model you can access model config and properties, ex,

model.config.id2label
"""output: {'sadness': 0, 'joy': 1, 'love': 2, 'anger': 3, 'fear': 4, 'surprise': 5}
"""

you can change it with simple assigning new dictionary,

model.config.id2label = {'s': 0, 'j': 1, 'l': 2, 'a': 3, 'f': 4, 's': 5}
model.config.id2label
""" output: {'s': 5, 'j': 1, 'l': 2, 'a': 3, 'f': 4}
"""

similarly, label2id can be reassigned and the updated model can be saved and reused.

I hope this will help you.

Regards,
Bhadresh