This was written on June 3, 2025. If you happen to’re studying this within the distant future, the issue I’m about to explain may already be fastened!
This situation got here up whereas I used to be learning Lesson 2 of Sensible Deep Studying for Coders by Jeremy Howard. The lesson focuses on deploying your personal deep studying mannequin to Hugging Face Areas with Gradio.
The issue occurred after I pushed the mannequin from my native machine right into a Hugging Face repository. Right here’s the error message I obtained:
runtime error
Exit code: 1. Motive: /usr/native/lib/python3.10/site-packages/fastai/learner.py:455: UserWarning: load_learner` makes use of Python's insecure pickle module, which may execute malicious arbitrary code when loading. Solely load information you belief.
If you happen to solely have to load mannequin weights and optimizer state, use the protected `Learner.load` as an alternative.
warn("load_learner` makes use of Python's insecure pickle module, which may execute malicious arbitrary code when loading. Solely load information you belief.nIf you solely have to load mannequin weights and optimizer state, use the protected `Learner.load` as an alternative.")
Traceback (most up-to-date name final):
File "/residence/consumer/app/app.py", line 11, in
be taught = load_learner("mannequin.pkl")
File "/usr/native/lib/python3.10/site-packages/fastai/learner.py", line 457, in load_learner
res = torch.load(fname, map_location=map_loc, pickle_module=pickle_module, **load_kwargs)
File "/usr/native/lib/python3.10/site-packages/torch/serialization.py", line 1471, in load
return _load(
File "/usr/native/lib/python3.10/site-packages/torch/serialization.py", line 1964, in _load
consequence = unpickler.load()
TypeError: code anticipated at most 16 arguments, obtained 18
I spent practically a complete day researching and making an attempt to repair this. You may marvel, “Why spend a lot time on a easy runtime error?” And sure — it might appear easy in hindsight. However the actuality is, nobody actually talks about this particular situation, and each time I attempted a brand new answer, I needed to wait by your entire deployment and construct course of, which took ages. That’s what made this downside so tedious and complicated.
Okay, I do know I’ve taken a number of paragraphs to introduce this situation — sorry about that 😅. However since that is my first Medium submit, I hope my writing is at the least comprehensible — and perhaps even useful to somebody on the market.
The core situation lies in module compatibility between the atmosphere the place the mannequin was educated and the atmosphere utilized by Hugging Face throughout deployment.
Right here’s what it regarded like in my case:
🧑🏻💻 Native (growth atmosphere):
Python
: 3.12.xfastai
: 2.8.2
😄 Hugging Face (inference atmosphere):
Python
: 3.11.xfastai
: 2.7.19
As you may see, the variations are barely totally different — however on the earth of Python and pickled information (.pkl
), even a minor mismatch can break every part.
If you happen to’re growing your deep studying mannequin in your native machine (or one other on-line kernel), right here’s my advisable repair:
- Add your coaching pocket book (
.ipynb
) to Google Colab. - Re-train and export the mannequin instantly in Colab.
- Obtain the brand new
.pkl
mannequin from Colab. - Exchange the previous mannequin file in your Hugging Face repository with the brand new one.
- Specify the model of fastai module in
necessities.txt
file:
fastai==2.7.19
gradio
6. Then:
git add .
git commit -m "repair: Exchange new mannequin to resolve the compatibility situation"
git push
7. Re-deploy and verify if it really works!
You might need some questions, equivalent to:
- Why use Google Colab?
As a result of it’s suitable with Hugging Face. Easy as that. Utilizing the identical atmosphere helps keep away from model mismatches and unusual deployment points. - Why specify
fastai==2.7.19
innecessities.txt
?
If you happen to don’t specify the module variations, Hugging Face will routinely set up the newest variations.
The issue is that the newestfastai
model (2.8.2) is not but suitable with the model oftorch
utilized in Hugging Face’s atmosphere. That mismatch is what causes the runtime error.