Lesson 2: Deploy to HF Spaces: pkl file needs to be compatible

For those who are experiencing a torch serialization issue of TypeError: code expected at most 16 arguments, got 18 when running load_learner(): This is caused by a mismatch of the model.pkl with the environment of your HuggingFace space.

I overcame this problem by finetuning the cat/dog classifier model on my local machine using specific versions for Python and the required packages:

  • python==3.11
  • torch==2.5.1
  • fastai==2.7.19

Now you can export the model.pkl file.

For your HuggingFace Space, use the same versions mentioned above. I.e. in your README.md, add a line of python_version: 3.11. In your requirements.txt, add this:

torch==2.5.1
fastai==2.7.19
gradio==5.29.0

The gradio version does not matter, but I decided to specify it as well. I hope that is helpful.