Change label names on inference API
Hi there,I recently uploaded myfirst modelto the model hub and I’m wondering how I can change the label names that are returned by the inference API. Right now, the API returns “LABEL_0”, “LABEL_1”, etc. with the predictions and I would like it to be something like “Economy”, “Welfare”, etc.I looked at the files of other hosted models and I saw that others changed theid2labelandlabel2idin theconfig.jsonfile, so I also did thathere, but the inference API still returns “LABEL_0”. Do I need to change this somewhere else too? (Or maybe I just need to wait for a day or so until the model is refreshed on AWS?)Update: I looked more deeply into thedocs hereand I didn’t find an explanation for how to change the label names. Maybe this could be added?Thanks for your advice,Moritz
2021-02-10T13:26:55Z
Hey, does someone have advice on this? Would really like to change the output of my models on the model hub, but I don’t understand how to make it return something else than “LABEL_0”, “LABEL_1” etc. See above what I’ve tried.
2021-02-10T13:54:56Z
Hey@MoritzLaurer, the way I usually do this is by specifying thelabel2idandid2labeldictionaries in the model’s config class, e.g. for a text classifier you can do this:from transformers import AutoConfig, AutoModelForSequenceClassification

# define the mappings as dictionaries
label2id = ...
id2label = ...
# define model checkpoint - can be the same model that you already have on the hub
model_ckpt = ...
# define config
config = AutoConfig.from_pretrained(model_ckpt, label2id=label2id, id2label=id2label)
# load model with config
model = AutoModelForSequenceClassification.from_pretrained(model_ckpt, config=config)
# export model
model.save_pretrained(target_name_or_path)Then you can push the files to the hub as usual. HTH!
2021-02-10T18:29:38Z
Thanks for your response@lewtun! Ok great, now I know how to do it at the point of model creation in my script.Do you happen to know the  necessary steps for when the model is already on the hub? For example, I have uploaded a model on the model hub (without taking the steps you described) and now I want to change it in hindsight. I changed the config file by changing label2id and id2label to the strings I want, but that doesn’t seem to be sufficient.see the updated config file here:config.json · MoritzLaurer/policy-distilbert-7d at mainBut the inference API still returns “LABEL_0” etc., see here:MoritzLaurer/policy-distilbert-7d · Hugging Face.
2021-02-10T20:32:06Z
Assuming you uploaded your model using the instructions in thedocs, my suggestion would be to copy all the contents in the folder produced by saving your modelmodel.save_pretrained(target_name_or_path)to the repository where you original model is stored. Then you can runcd path/to/original/model
git add . && git commit -m "Update labels"
git pushand that should update bothconfig.jsonandpytorch_model.bin. If you don’t have the repository of the original model you can just clone it againgit lfs install
git clone https://huggingface.co/MoritzLaurer/policy-distilbert-7dHTH!
2023-03-21T22:33:34Z
@lewtunThis is happening with me too. Even when the config file has label2id and id2label defined it still takes the old labels or the auto-defined labels.https://huggingface.co/aadishhug/distilbert-tweet-analysis/blob/main/config.jsonImportant thing to know is that this is not happening on mycollabnotebook. Please note that I have used the pipeline API for output.
2023-03-22T08:30:36Z
Hi@aadishhug, if I remember correctly, when I did these updates by hand in the config.json on the hub, the changes first did not show up in the inference widget, but then after a few days they did. Maybe the internal updates on the hub just take a while.Otherwise, the solutions from@lewtunoutlined above worked for me. the best is do input the label2id/id2label directly when you train/upload
2023-03-23T17:21:28Z
Thanks@MoritzLaurerfor letting me know. Will let it take its own course then
2024-07-01T13:12:56Z
It does not show up immediately in the inference api provided on your model repository and indeed takes 5-10 minutes for the labels to be updated.Was trying the same, the labels were not immediately updated, Did everything about pushing the config file to hub, even though the file was updated in the repo, the Inference API was not taking in the changes immediately, only after some time was it taking the labels from the config.json file and was displaying the label_names properly.