├── Dockerfile ├── model.pkl ├── model.py ├── request.py ├── requirements.txt ├── server.py └── shell.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:16.04 2 | 3 | FROM python:3.6.5 4 | 5 | RUN apt-get update -y && \ 6 | apt-get install -y python-pip python-dev 7 | 8 | # We copy just the requirements.txt first to leverage Docker cache 9 | COPY ./requirements.txt /app/requirements.txt 10 | 11 | WORKDIR /app 12 | 13 | RUN pip install -r requirements.txt 14 | 15 | COPY . /app 16 | 17 | CMD python /app/model.py && python /app/server.py 18 | 19 | 20 | -------------------------------------------------------------------------------- /model.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EkramulHoque/docker-jenkins-flask-tutorial/e4f7d1ed388e1d9c3e752a2ccc9699c6674bcad8/model.pkl -------------------------------------------------------------------------------- /model.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | import pickle 4 | from sklearn import datasets 5 | iris=datasets.load_iris() 6 | x=iris.data 7 | y=iris.target 8 | 9 | #labels for iris dataset 10 | labels ={ 11 | 0: "setosa", 12 | 1: "versicolor", 13 | 2: "virginica" 14 | } 15 | 16 | #split the data set 17 | from sklearn.model_selection import train_test_split 18 | x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.25) 19 | 20 | #Using decision tree algorithm 21 | from sklearn import tree 22 | classifier=tree.DecisionTreeClassifier() 23 | classifier.fit(x_train,y_train) 24 | predictions=classifier.predict(x_test) 25 | 26 | #export the model 27 | pickle.dump(classifier, open('model.pkl','wb')) 28 | 29 | #load the model and test with a custom input 30 | model = pickle.load( open('model.pkl','rb')) 31 | x = [[6.7, 3.3, 5.7, 2.1]] 32 | predict = model.predict(x) 33 | print("Hello Worlds") 34 | print(labels[predict[0]]) -------------------------------------------------------------------------------- /request.py: -------------------------------------------------------------------------------- 1 | import requests 2 | # Change the value of experience that you want to test 3 | url = 'http://192.168.99.100:5000/api' 4 | feature = [[5.8, 4.0, 1.2, 0.2]] 5 | labels ={ 6 | 0: "setosa", 7 | 1: "versicolor", 8 | 2: "virginica" 9 | } 10 | 11 | r = requests.post(url,json={'feature': feature}) 12 | print(labels[r.json()]) -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | flask 2 | flask_restful 3 | sklearn 4 | pandas 5 | numpy 6 | -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request, jsonify 2 | import json 3 | import pickle 4 | import pandas as pd 5 | import numpy as np 6 | 7 | app = Flask(__name__) 8 | 9 | # Load the model 10 | model = pickle.load(open('model.pkl','rb')) 11 | labels ={ 12 | 0: "versicolor", 13 | 1: "setosa", 14 | 2: "virginica" 15 | } 16 | 17 | @app.route('/api',methods=['POST']) 18 | def predict(): 19 | # Get the data from the POST request. 20 | data = request.get_json(force=True) 21 | predict = model.predict(data['feature']) 22 | return jsonify(predict[0].tolist()) 23 | 24 | if __name__ == '__main__': 25 | app.run(debug=True,host='0.0.0.0') -------------------------------------------------------------------------------- /shell.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | python model.py && python server.py --------------------------------------------------------------------------------