Skip to content
Advertisement

Unable to load pickle file in streamlit

I have some code to deploy model in streamlit. I just upload all file to github and share it in streamlit app. Here is some code

if str(course) == 'Multi-layer Perceptron':
    file = open("MLP.pkl",'rb')
if str(course) == 'Logistic classifier':
    file = open("log_classifier.pkl",'rb')
if str(course) == 'K-neighbour':
    file = open("Kneighbor_classifier.pkl",'rb')
f str(course) == 'Naivebayes':
    file = open("naivebayes_classifier.pkl",'rb')

model = pickle.load(f)

It runs perfect in local. But in streamlit it has some bug

ModuleNotFoundError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs.
Traceback:
File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/script_runner.py", line 354, in _run_script
    exec(code, module.__dict__)
File "/app/nm-khdl/streamlit_app.py", line 45, in <module>
    model = pickle.load(file)

It’s the first time that I work on streamlit. So, thank you for reading! Have a nice day!

Advertisement

Answer

I had the same issue. As I saved my model built it in scikit-learn, the pickle file depends on scikit-learn for its structure.

Additionally, I didn’t import sckilearn in the python file for the web app streamlit. Therefore, using pipreqs ./ to generate pip requirements.txt file based on imports of this project did not include sklearn.

Thus, I had to include manually it inside of requirements.txt: scikit-learn==0.24.2

numpy==1.22.0
pandas==1.3.4
plotly==5.5.0
streamlit==1.3.0
scikit-learn==0.24.2
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement