'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!
Solution 1:[1]
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 sckilearn.
Thus, I had to include manually it inside of requiremts.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
I hope it will work!
Solution 2:[2]
I had the same bug and it's quite easy to fix.
Simply create a new txt file. Inside, type
scikit-learn
Save it as requirements.txt in the same directory as your script_runner.py file.
Voila, that should do it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Thiago Pauli |
| Solution 2 | Sillette |
