<jupyter_start><jupyter_text>Save/Load models<jupyter_code>import joblib
from sklearn.linear_model import LogisticRegression
from sklearn import datasets

iris = datasets.load_iris()
X, y = iris.data, iris.target

model = LogisticRegression(random_state=0, max_iter=1000)
model.fit(X, y)  
model.predict(X)
X[0]
model.predict([X[0]])
joblib.dump(model, "iris.model")
model = joblib.load("iris.model")<jupyter_output><empty_output><jupyter_text>Serve models<jupyter_code>!pip install flask-ngrok -q
from flask import Flask
from flask_ngrok import run_with_ngrok  # To use flask server in colab

app = Flask(__name__)
run_with_ngrok(app)   
  
@app.route("/")
def hello():
    return "hello"
    
app.run() # Use gunicorn in production evironment
from flask import Flask, request
from flask_ngrok import run_with_ngrok  # To use flask server in colab

app = Flask(__name__)
run_with_ngrok(app)   
  
@app.route("/", methods=["POST"])
def hello():
    data = request.get_json(True)
    sepal_length = data["sepal_length"]
    sepal_width = data["sepal_width"]
    petal_length = data["petal_length"]
    petal_width = data["petal_width"]

    input = [sepal_length, sepal_width, petal_length, petal_width]
    species = model.predict([input])
    return str(species[0])
    
app.run() # Use gunicorn in production evironment<jupyter_output><empty_output><jupyter_text>Cache(Single machine level)<jupyter_code>from flask import Flask, request
from flask_ngrok import run_with_ngrok  # To use flask server in colab


cache = {}

app = Flask(__name__)
run_with_ngrok(app)   
  
@app.route("/", methods=["POST"])
def hello():
    data = request.get_json(True)
    sepal_length = data["sepal_length"]
    sepal_width = data["sepal_width"]
    petal_length = data["petal_length"]
    petal_width = data["petal_width"]

    key = (sepal_length, sepal_width, petal_length, petal_width)
    if key in cache:
        print("cached")
        return cache[key]

    input = list(key)
    species = model.predict([input])
    cache[key] = str(species[0])
    return str(species[0])

app.run()<jupyter_output><empty_output><jupyter_text>High availability(Single machine Level)<jupyter_code>%%writefile app.py
import joblib
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
from flask import Flask, request
from flask_ngrok import run_with_ngrok  # To use flask server in colab

model = joblib.load("iris.model")
cache = {}

app = Flask(__name__)
  
@app.route("/", methods=["GET", "POST"])
def hello():
    data = request.get_json(True)
    sepal_length = data["sepal_length"]
    sepal_width = data["sepal_width"]
    petal_length = data["petal_length"]
    petal_width = data["petal_width"]

    key = (sepal_length, sepal_width, petal_length, petal_width)
    if key in cache:
        print("cached")
        return cache[key]

    input = list(key)
    species = model.predict([input])
    cache[key] = str(species[0])
    return str(species[0])
!pip install gunicorn -q
!gunicorn app:app -w 4
!nohup gunicorn app:app -w 4 &
!ps --forest
!curl 127.0.0.1:8000 \
-H 'Context-Type: application/json' \
--data-raw '{"sepal_length": 1.0,"sepal_width": 2.0,"petal_length": 2.0,"petal_width": 1.0}'
!kill  336<jupyter_output><empty_output><jupyter_text>BentoML<jupyter_code>!pip install bentoml -q
%%writefile bento_service.py
import pandas as pd

from bentoml import env, artifacts, api, BentoService
from bentoml.adapters import DataframeInput
from bentoml.frameworks.sklearn import SklearnModelArtifact

@env(infer_pip_packages=True)
@artifacts([SklearnModelArtifact('model')])
class IrisClassifier(BentoService):
    @api(input=DataframeInput(), batch=True)
    def predict(self, df: pd.DataFrame):
        return self.artifacts.model.predict(df)
from bento_service import IrisClassifier

iris_classifier_service = IrisClassifier()
iris_classifier_service.pack('model', model)
iris_classifier_service.predict([[1.1, 2.3, 2.3, 3.4]])
iris_classifier_service.start_dev_server()
!curl 127.0.0.1:5000/predict \
-H 'Context-Type: application/json' \
--data-raw '[{"sepal_length": 1.0,"sepal_width": 2.0,"petal_length": 2.0,"petal_width": 1.0}]'
iris_classifier_service.stop_dev_server()
iris_classifier_service.save()
!bentoml serve IrisClassifier:latest --run-with-ngrok
!bentoml serve-gunicorn IrisClassifier:latest --workers 4<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12565804531929503, 'fraction_numerical': 0.014190890363927672, 'mean_word_length': 5.198581560283688, 'pattern_counts': {'":': 8, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '18736', 'license': 'Apache-2.0', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1564, 'n_words': 331, 'path': 'client-iris.ipynb', 'repo_name': 'VHEX-LAB/VHEX-Tech', 'size': '4369'}