How can I deploy a Django project with a pickled model in Railway app?

38 views Asked by At

I would like to deploy my Django project on a Railway app, but the server does not find the pickled model. Although I made sure that the file exists in the github repository that I am deploying, Railway app continuously displays this error:

from core import views

File "/app/core/views.py", line 37, in

binary_clf = joblib.load(os.path.abspath('binary_classifier.pkl'))

File "/opt/venv/lib/python3.8/site-packages/joblib/numpy_pickle.py", line 650, in load

with open(filename, 'rb') as f:

FileNotFoundError: [Errno 2] No such file or directory: '/app/binary_classifier.pkl'

The thing is, I don't have any static files except for this pickled ML model.

But, I tried including the pickled model in a 'static' folder like it was described here, in which case I got this error:

from core import views

File "/app/core/views.py", line 37, in

binary_clf = joblib.load("static/binary_classifier.pkl")

File "/opt/venv/lib/python3.8/site-packages/joblib/numpy_pickle.py", line 650, in load

with open(filename, 'rb') as f:

FileNotFoundError: [Errno 2] No such file or directory: 'static/binary_classifier.pkl'

How are we supposed to deploy Django apps when we have pickled files in it?

Here is the organization of my project:

baby_cry_proj/
├── baby_cry/
│   ├── _init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── migrations
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   ├── views.py
│   └── __init__.py
├── core/
│   ├── _init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── migrations
│   ├── models.py
├.      templates/
│       ├── base.html
│       ├── index.html
│       └── ... 
│   ├── tests.py
│   ├── urls.py
│   ├── views.py (it calls the 'binary_classifier.pkl')
│   └── __init__.py
├── env
├── manage.py
├── Procfile
├── requirements.txt
├── settings.py
├── static/
│   ├── binary_classifier.pkl
├── staticfiles/
├── admin/
│   ├── binary_classifier.pkl
├.  ├── binary_classifier.pkl
1

There are 1 answers

0
Galym On

Ok, I created a separate folder called 'models' inside 'core/' and moved my 'binary_classifier.pkl' into that folder. Then, I modified the path to the file like this: binary_clf = joblib.load("core/models/binary_classifier.pkl")

It worked, but I have no idea why