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
JavaScript
x
11
11
1
if str(course) == 'Multi-layer Perceptron':
2
file = open("MLP.pkl",'rb')
3
if str(course) == 'Logistic classifier':
4
file = open("log_classifier.pkl",'rb')
5
if str(course) == 'K-neighbour':
6
file = open("Kneighbor_classifier.pkl",'rb')
7
f str(course) == 'Naivebayes':
8
file = open("naivebayes_classifier.pkl",'rb')
9
10
model = pickle.load(f)
11
It runs perfect in local. But in streamlit it has some bug
JavaScript
1
7
1
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.
2
Traceback:
3
File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/script_runner.py", line 354, in _run_script
4
exec(code, module.__dict__)
5
File "/app/nm-khdl/streamlit_app.py", line 45, in <module>
6
model = pickle.load(file)
7
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
JavaScript
1
6
1
numpy==1.22.0
2
pandas==1.3.4
3
plotly==5.5.0
4
streamlit==1.3.0
5
scikit-learn==0.24.2
6