<jupyter_start><jupyter_text>Deploy a ML model > More about freeze for requirements.txt : https://medium.com/@boscacci/why-and-how-to-make-a-requirements-txt-f329c685181e > Basics of using Docker for Amazon : https://docs.aws.amazon.com/fr_fr/AmazonECS/latest/developerguide/docker-basics.html > Simple way to deploy machine learning model to cloud : https://towardsdatascience.com/simple-way-to-deploy-machine-learning-models-to-cloud-fd58b771fdcf<jupyter_code>from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.externals import joblib
from sklearn.ensemble import RandomForestRegressor
X, y = load_boston(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = RandomForestRegressor(n_estimators=200, criterion='mse', max_depth=5)
model.fit(X_train, y_train)
model.score(X_test, y_test)
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
test = load_boston()
names_and_feature_imp = pd.DataFrame({'names':load_boston().feature_names,
                                     'importance': model.feature_importances_})
names_and_feature_imp.sort_values(["importance"], ascending=True, inplace=True)
names_and_feature_imp.plot(kind='barh', title="Features' importances")
X_train, X_test = X_train[:, [5, 12, 7, 0, 4]], X_test[:, [5, 12, 7, 0, 4]]
model = RandomForestRegressor(n_estimators=200, criterion='mse', max_depth=5)
model.fit(X_train, y_train)
model.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Save the model (using joblib)<jupyter_code>import os
foldername = 'bonus_deploy_a_model_ressources/'
if not os.path.exists(foldername):
    os.mkdir(foldername)
joblib.dump(model, filename = foldername+'model.sav')<jupyter_output><empty_output><jupyter_text>Load the model<jupyter_code>model = joblib.load(foldername+"model.sav")
model.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Flask<jupyter_code>from flask import Flask, request
model = None
foldername = 'bonus_deploy_a_model_ressources/'

def load_model():
    global model
    with open(foldername+"model.sav", 'rb') as file:
        model = joblib.load(file)
app = Flask(__name__)
app.name
@app.route('/')
def home_page():
    return "Hello World !\n Send us some data and we will predict the outcome for you"
import requests
test = np.array([1,2,3,4,5,6])
test.shape
np.array([1,2,3])
test[:, np.newaxis] #shape (6,1)
test.flags
test[:,np.newaxis]
test[np.newaxis]
@app.route('/predict', methods=['POST'])
def get_prediction():
    if request.method == "POST":
        data = request.get_json() # get data posted as json
        data = np.array(data)[np.newaxis, :] # shape (4,) to (1,4)
        prediction = model.predict(data)
    return str(prediction[0])
if __name__ == '__main__':
    load_model()  # load model at the beginning once only
    app.run(host='0.0.0.0', port=80)
!conda env list
!pip freeze > requirements.txt
!docker build -t model_deployment ./bonus_deploy_a_model_ressources
!docker run -p 80:80 model_deployment
import urllib3
urllib3.urllib<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12861736334405144, 'fraction_numerical': 0.020257234726688104, 'mean_word_length': 5.748373101952278, 'pattern_counts': {'":': 1, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '457408', 'license': 'CNRI-Python', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1108, 'n_words': 233, 'path': 'BONUS_Deploy_A_ML_Model.ipynb', 'repo_name': 'SimonADDA/TDs_ESILV', 'size': '3110'}