A deployed machine learning REST API using Django and Docker.
Python 3
-
Clone this repo.
git clone https://github.com/juccaicedoac03/ML_dep.git
-
Create a virtual environment in ML_dep folder and install requirements.txt.
mkdir mldep_env python -m venv mldep_env source ml_env/bin/activate pip install requirements.txt
-
Make API model migrations.
cd Project_MLdep python manage.py migrate
-
Run server.
python manage.py runserver
And in our browser (or in postman), make a request GET a http://127.0.0.1:8000/api/test
You can create or donwload a Docker image:
Once our project is inside a github repository, and Docker is installed, We will create a directory in to make our Dockerfile.
mkdir MLdep_docker
cd MLdep_docker
We create a Dockerfile and write the steps for creating the image that contains everything we need.
# syntax=docker/dockerfile:1
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
RUN git clone https://github.com/juccaicedoac03/ML_dep
WORKDIR "ML_dep/"
RUN pip install gdown
RUN pip install -U git+https://github.com/juccaicedoac03/ML_dep.git >/dev/null
RUN pip install -r requirements.txt
WORKDIR "Project_MLdep/"
RUN gdown 1lWfMIZLNehFV65AgeJK_UjSToylPFiBi # Google drive link of ML model
RUN python manage.py migrate
Also, we create a docker-compose.yml file with:
version: "3.9"
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
container_name: ML_dep_container
ports:
- "8000:8000"
Finally run compose
docker-compose up
docker pull juccaicedoac/mldep_docker_web:latest
docker-compose up
The ML REST API methods are described below.
HOST: http://0.0.0.0:8000/
GET
/api/Orders/
{
"message": "Success",
"orders": [
{
"order_id": 15031281,
"store_id": 900006856,
"to_user_distance": 1.298663,
"to_user_elevation": -19.70105,
"total_earning": 5500,
"created_at": "2017-09-19T15:36:49Z",
"taken": false
},
{
"order_id": 15213594,
"store_id": 900011745,
"to_user_distance": 0.750715,
"to_user_elevation": -34.016602,
"total_earning": 5700,
"created_at": "2017-09-22T21:38:58Z",
"taken": true
}
]
}
{
"message": "No orders found",
}
GET
/api/Orders/order_id
order_id=[integer]
{
"message": "Success",
"orders": [
{
"order_id": 15031281,
"store_id": 900006856,
"to_user_distance": 1.298663,
"to_user_elevation": -19.70105,
"total_earning": 5500,
"created_at": "2017-09-19T15:36:49Z",
"taken": false
},
]
}
{
"message": "No orders found",
}
POST
/api/Orders/
{
"orders": [
{
"order_id": 15031281,
"store_id": 900006856,
"to_user_distance": 1.298663,
"to_user_elevation": -19.70105,
"total_earning": 5500,
"created_at": "2017-09-19T15:36:49Z"
},
{
"order_id": 15213594,
"store_id": 900011745,
"to_user_distance": 0.750715,
"to_user_elevation": -34.016602,
"total_earning": 5700,
"created_at": "2017-09-22T21:38:58Z"
}
]
}
{
"message": "Success",
}
{
"message": "No orders found",
}
PUT
/api/Orders/order_id/
order_id=[integer]
{
"orders": [
{
"order_id": 15031281,
"store_id": 900006856,
"to_user_distance": 1.298663,
"to_user_elevation": -19.70105,
"total_earning": 5500,
"created_at": "2017-09-19T15:36:49Z",
"taken": false
},
]
}
{
"message": "Success",
}
{
"message": "No orders found",
}
DELETE
/api/Orders/order_id/
order_id=[integer]
{
"message": "Success",
}
{
"message": "No orders found",
}