Load_learner` uses Python's insecure pickle module

When running the following line.

learn = load_learner('model.pkl')

I get this error. At the begging I thought it was a thing about hugging face app, but running this locally I get the same error. Any Idea on how to fix it?

python3.10/site-packages/fastai/learner.py:455: UserWarning: load_learner` uses Python's insecure pickle module, which can execute malicious arbitrary code when loading. Only load files you trust.
If you only need to load model weights and optimizer state, use the safe `Learner.load` instead.
  warn("load_learner` uses Python's insecure pickle module, which can execute malicious arbitrary code when loading. Only load files you trust.\nIf you only need to load model weights and optimizer state, use the safe `Learner.load` instead.")
1 Like

When you create ‘requirements.txt’ file for Hugging face it should also tells the version of fastai library.
you can check
import fastai
fastai.version
requirement file should be like this in hugging face
“”"
fastai==2.7.19
gradio
scikit-image

“”"

Check Repo

Hello!
The “error” you’re seeing from fastai is actually a UserWarning about Python’s pickle module, not a fatal error. It’s a security alert advising that load_learner can execute arbitrary code if the model.pkl file comes from an untrusted source. If you created the HumanToDog com file or trust its origin, you can safely proceed in your local environment. For a more secure approach, especially if you only need model weights, use Learner.load() after defining your model architecture, as it avoids pickle’s potential security risks.

1 Like

@AntonioML I end up using Docker to deploy into Hugging Face as even after I resolved that unsecure model because of using PIL library to train. I end up with another issue with Python version missmatch between Kaggle where I train the model and Hugging Face version.

If you interested in using Docker, you can check my post at Lesson 2 - Chihuahua vs Biscuit

Sorry I didn’t answer, Haven’t had much time lately. I’m not sure how to mark as trusted the file on Hugging face.
But the manually setting the version of fastai solved the issue for me.

1 Like