Minimum proposal to implement a machine learning model in production
by Johni Douglas Marangon
full code - https://gist.github.com/johnidm/7c2b9292abf0057532ee2ae1f23b3cbb
What are we looking for?
Writing less code
Stack
Python (API or message-broker) + Docker
Project structure
myapp/
│
├── requirements.txt
├── Dockerfile
├── myapp/__init__.py
└── myapp/app.py
Code
requirements.txt
------
numpy
scikit-learn
flask
myapp/__init__.py
------
__author__ = "<someone>"
Dockerfile
------
FROM python:3.8.6-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY myapp myapp
CMD ["python", "-u", "/app/myapp/app.py"]
Code
myapp/app.py
------
from flask import Flask, request, jsonify
import numpy as np
import sklearn
import flask
app = Flask(__name__)
@app.route("/", methods=["GET"])
def home():
return jsonify(
{
"status": "Running RESTful Machine Learning",
"versions": {
"numpy": np.__version__,
"scikit-learn": sklearn.__version__,
"flask": flask.__version__,
},
}
)
@app.route("/do", methods=["POST"])
def do():
content = request.json
text = content.get("text", "")
# put your machine learning code here
return (
jsonify(
{
"text": text,
# put machine learning result here
}
),
201,
)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0")
Building and running the Docker image
docker build -t iara-ml:0.0.1 .
docker run -p 5000:5000 iara-ml:0.0.1
Demo
curl -i -X GET \
'http://0.0.0.0:5000/'
curl -i -X POST \
-H "Content-Type:application/json" \
-d \
'{
"text": "Turn off and try again later"
}' \
'http://0.0.0.0:5000/do'
Implementing a machine lenarning model
Training a model
Demo Code
weight = data["weight"]
height = data["height"]
with open("/app/myapp/model.bin", 'rb') as f:
model = pickle.load(f)
predicts = np.array(model.predict_proba([[weight, height]])).flatten()
targets = [
({str(target): round(prob, 3)}) for target, prob in zip(model.classes_, predicts)
]
{
"weight": weight,
"height": height,
"targets": targets,
"datetime": str(datetime.datetime.now())
}
Using RabbitMQ
Starting RabbitMQ
docker run --rm -p 5672:5672 -p 8080:15672 rabbitmq:3-management
Code
myapp/app.py
-----
import pika
import json
import datetime
QUEUE = "iara.do"
QUEUE_REPONSE = "iara.do.response"
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='192.168.15.112'))
channel = connection.channel()
channel.queue_declare(queue=QUEUE)
channel.queue_declare(queue=QUEUE_REPONSE)
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
data = json.loads(body.decode())
data["datetime"] = str(datetime.datetime.now())
channel.basic_publish(
exchange='', routing_key=QUEUE_REPONSE, body=json.dumps(data))
channel.basic_consume(
queue=QUEUE, on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
DONE
Minimum proposal to implement a machine learning model in production
By Johni Douglas Marangon
Minimum proposal to implement a machine learning model in production
- 319