How to use Django ML Template Lite
1. Test the model on your local machine (assuming it's a mac)
Go to the root directory of this project (not the second django_ml_lite dir)
You should see the following
[django_ml_lite]$ ls Procfile README.md db.sqlite3 django_ml_lite manage.py pages requirements.txt sentiment_analysis
Create a virtual environment and activate it
virtualenv -p python3 venv source venv/bin/activate
Install required libraries
pip install -r requirements.txt
Test it by running local server
(venv) [django_ml_lite]$ python manage.py runserver
Open your browser and visit http://localhost:8000/, from where you can confirm the dev server is up and running, you should see something like the hompage of the demo site: https://demo.djangomachinelearning.com/
2. Deploy this model to production (Heroku)
a. Set up your heroku account
- Visit heroku.com and sign up an account
- Afterwards, install a command line tool that heroku provides called heroku toolbelt
You can get detailed install instructions on https://devcenter.heroku.com/articles/heroku-cli
For example: if you on a mac and installed homebrew, you can install easily with the following command:
brew tap heroku/brew && brew install heroku
b. (Inside of the project root dir) Run the following commands to set up your heroku environment for production
- Initialize this git repo
git init git add . git commit -m 'Initialize project'
c. Deploy to Heroku (Finally the most exciting part! 😁)
- Tell heroku your app is based on python:
heroku create <your-app-name> --buildpack https://github.com/heroku/heroku-buildpack-python
Set up a random django security code
heroku config:set PYTHONHASHSEED=random heroku config:set DJANGO_SECRET_KEY="$(openssl rand -base64 64)"
Set up concurrency (note: if you set this number high, you might run out of memory on a free heroku instance), for more details, visit: https://devcenter.heroku.com/articles/optimizing-dyno-usage#concurrent-web-servers
heroku config:set WEB_CONCURRENCY=1
- Turn off django debug for your website security
heroku config:set DJANGO_DEBUG=False
- Deploy (might take a while)
git push heroku master
Open your app
This template ships with one machine learning model for you to check out and you can modify it to serve your own model.
Sentimental Analysis Prediction
You can Visit https://demo.djangomachinelearning.com to test out the API.
This package ships with two existing users so you can go ahead and test it out. To change their passwords, in command line (project root dir), run the following and follow the prompt.
python manage.py changepassword data_scientist
admin (super user)
username: data_scientist pwd: django_ml_2020!
non-admin user (your customers who consume your model output through your api)
username: ml_engineer pwd: django_ml_2020!
Sometimes if you have models that are memory intensive, especially if you have multiple models one one app, you might run out of memoery for the default free Heroku instance. To solve this, you can upgrade your Heroku instance to get more powerful machine with a larger memory, or reduce the number of models and concurrent workers. You can also deploy multiple heroku apps for different models (one app for one model).
When the app starts, the model file is deserialized and loaded to memory to serve incoming web requests, the model is loaded only once.