<jupyter_start><jupyter_text>Serving using FlaskLinks* making flask server directly run on Colab: https://stackoverflow.com/questions/54465816/how-to-use-flask-in-google-colaboratory-python-notebook* logging  * https://flask.palletsprojects.com/en/1.1.x/logging/  * https://docs.python.org/3/howto/logging-cookbook.html* In a real production environment you would either use a "real" web server that directs to flask or use a hosted solution  * https://flask.palletsprojects.com/en/1.1.x/deploying/  * https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface<jupyter_code>!curl -O https://raw.githubusercontent.com/DJCordhose/ml-workshop/master/prod/prod_pipeline_pca_std_rf_2.tgz
!tar -xzf prod_pipeline_pca_std_rf_2.tgz
!ls -lR
import pickle
model_versions = pickle.load(open('stats/versions.pickle', 'rb'))
model_versions
import numpy, sklearn, pandas

versions = {
    'numpy': numpy.__version__,
    'sklearn': sklearn.__version__, 
    'pandas': pandas.__version__,
    'model': 2,
    'model_name': 'pipeline_pca_std_rf'
}
versions
assert model_versions == versions
import logging

logging.basicConfig(filename='req.log')

data_logger = logging.getLogger('DataLogger')
data_logger.setLevel(logging.INFO)

file_handler = logging.FileHandler('data.log', )

data_logger.addHandler(file_handler)
# loading models only once on startup
model = pickle.load(open('models/model.pickle', 'rb'))

def predict(speed, age, miles):
    sample = [[speed, age, miles]]

    result = int(model.predict(sample)[0])
    prediction = model.predict_proba(sample)[0].tolist()
    
    return result, prediction
# should be category 1
# * 0 - red: many accidents
# * 1 - blue: no accidents
# * 2 - yellow: few accidents   
predict(100, 48, 10)
from flask import Flask, request, jsonify
import socket
print(socket.gethostbyname(socket.getfqdn(socket.gethostname())))

app = Flask(__name__)

@app.route("/ping")
def ping():
    return "pong"

@app.route('/predict', methods=['GET', 'POST'])
def do_predict():
    speed = request.json['speed']
    age = request.json['age']
    miles = request.json['miles']

    predicted_category, probabilities = predict(speed, age, miles)

    response = {
        'category': predicted_category,
        'prediction': probabilities,
    }
    
    dataset = {
        'out': response,
        'in': {
            'speed': speed, 'age': age, 'miles': miles
        }
    }

    data_logger.info(dataset)

    return jsonify(response)
  
import threading
threading.Thread(target=app.run, kwargs={'host': '0.0.0.0', 'port': 80}).start()  
import requests
r = requests.get("http://172.28.0.2/ping")
print(r.status_code)
print(r.encoding)
print(r.apparent_encoding)
print(r.text)
r = requests.post("http://172.28.0.2/predict", json={'speed': 100, 'age': 48, 'miles': 10})
r.status_code
response = r.json()
response
response['prediction']
!cat data.log<jupyter_output>{'out': {'category': 1, 'prediction': [0.010217113665389526, 0.6180901330470296, 0.37169275328758095]}, 'in': {'speed': 100, 'age': 48, 'miles': 10}}<jupyter_text>Exercise: deploy your model using this notebook* load describe stats and scores as well and print them out* how would you use them in a manual deployment process to find out if you want to deploy the new model at all?* how would you automate the process?* how could you use the logged out?<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14087007990529743, 'fraction_numerical': 0.03492157443030482, 'mean_word_length': 4.559210526315789, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '657872', 'license': 'MIT', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1127, 'n_words': 268, 'path': 'notebooks/process/flask-server.ipynb', 'repo_name': 'kmunve/ml-workshop', 'size': '3379'}