I tried deploying a model with streamlit sometime in the past month. After building and testing locally, I found it works perfectly fine.
I went ahead with pushing to GitHub and opening a Streamlit account to host the model with. Sadly I’ve only gotten errors. The problem seems to be that Streamlit (or GitHub ?) can’t locate the files with the paths provided. E.g. It can’t locate the dataset file, or the saved model. I get FileNotFoundError or ModuleNotFoundError.
I attempted moving all files to the root of the project, but with same results, also tried getting the file from via url in the github repo, but that returns a webpage instead of the intended file. Various tricks to building the filepath (os, Posix, etc) didn’t work either. Nothing on forums seem to work.
Could someone spare some time to look at it and suggest what I might be missing?
Here’s the repo:
Thanks for your response. Here is the traceback I get:
Traceback (most recent call last):
File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.__dict__)
File "/app/africa-financial-inclusion/deployment/app.py", line 6, in <module>
from prediction import predict
File "/app/africa-financial-inclusion/deployment/prediction.py", line 8, in <module>
with open(path_to_model, 'rb') as file:
FileNotFoundError: [Errno 2] No such file or directory: '../models/final_model.pkl'
I have a structure like so:
root-|
deployment-|
app.py
predictions.py
utils.py
models-|
…model.pkl
data-|
…data.csv
I’m having no trouble importing modules between the .py files in deployment. But I have to load the trained model and data flies from their respective folders, and that’s where the error comes from. E.g. I try loading the a model from predictions.py by providing a path like:
…/models/mymodel.pkl
but I get the file not found error.
The same goes for the files in the data directory
I recently attempted downloading the file from GitHub with wget to avoid working with saved files/filepaths. Again, I got it to work locally, but on deployment, I get a modulenotfound error with wget. Adding a requirements.txt gives ‘Error installing requirements.’
This error occur due to path problem. I mean it can’t locate file from given path.
You need to append parent directory path.
Please install path module also add it to requirements.txt
and make following changes in code.
from utils import wrangle
import pickle
import sys
import path
dir = path.Path(__file__).abspath()
sys.append.path(dir.parent.parent)
# load model
path_to_model = './models/final_model.pkl'
with open(path_to_model, 'rb') as file:
model = pickle.load(file)
def predict(data):
"""Make predictions"""
processed = wrangle(data, test=True)
result = model.predict(processed)
output = ''
if result == 0:
output = "Not Likely To Have Bank Account"
else:
output = "Likely To Have a Bank Account"
return output
Processing /opt/conda/conda-bld/bottleneck_1657175564434/work
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/opt/conda/conda-bld/bottleneck_1657175564434/work'
WARNING: You are using pip version 22.0.3; however, version 23.0.1 is available.
You should consider upgrading via the '/home/appuser/venv/bin/python -m pip install --upgrade pip' command.
Checking if Streamlit is installed
Thank you.
I have implemented the suggestions. Still it works fine locally, but I get this on deployment. I’ve also updated the pip version to v23 but it still shows this.
Oh, right. Thank you so much.
I removed those “@ [path]” after the packages. I noticed they had the /work at the end, which seemed to be the same thing in the traceback.
It all works fine now.
I appreciate everyone for their contributions.
Finally works after almost a month of frustrations.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.