restore_model_dependencies_example.ipynb
1 { 2 "cells": [ 3 { 4 "cell_type": "markdown", 5 "id": "ec6a8805", 6 "metadata": {}, 7 "source": [ 8 "# Restore model dependencies with mlflow.pyfunc.get_model_dependencies()" 9 ] 10 }, 11 { 12 "cell_type": "code", 13 "execution_count": null, 14 "id": "70a2b95e", 15 "metadata": {}, 16 "outputs": [], 17 "source": [ 18 "from pathlib import Path\n", 19 "\n", 20 "from sklearn import datasets\n", 21 "from sklearn.neighbors import KNeighborsClassifier\n", 22 "\n", 23 "import mlflow\n", 24 "\n", 25 "X, y = datasets.load_iris(as_frame=True, return_X_y=True)\n", 26 "model = KNeighborsClassifier()\n", 27 "model.fit(X, y)\n", 28 "\n", 29 "model_path = \"/tmp/sk_model_01\"\n", 30 "\n", 31 "mlflow.sklearn.save_model(model, model_path)\n", 32 "\n", 33 "model_requirements_file_path = mlflow.pyfunc.get_model_dependencies(model_path)" 34 ] 35 }, 36 { 37 "cell_type": "code", 38 "execution_count": null, 39 "id": "238d6445", 40 "metadata": {}, 41 "outputs": [], 42 "source": [ 43 "print(Path(model_requirements_file_path).read_text())" 44 ] 45 }, 46 { 47 "cell_type": "code", 48 "execution_count": null, 49 "id": "1f9b51b9", 50 "metadata": {}, 51 "outputs": [], 52 "source": [ 53 "%pip install -r $model_requirements_file_path" 54 ] 55 }, 56 { 57 "cell_type": "code", 58 "execution_count": null, 59 "id": "9b0a183b", 60 "metadata": {}, 61 "outputs": [], 62 "source": [ 63 "# In order to enable the environment restored by %pip command above,\n", 64 "# you need to manually click the kernel restart button." 65 ] 66 }, 67 { 68 "cell_type": "code", 69 "execution_count": null, 70 "id": "1262da7f", 71 "metadata": {}, 72 "outputs": [], 73 "source": [ 74 "import mlflow\n", 75 "\n", 76 "model = mlflow.pyfunc.load_model(\"/tmp/sk_model_01\")\n", 77 "\n", 78 "from sklearn import datasets\n", 79 "\n", 80 "X, y = datasets.load_iris(as_frame=True, return_X_y=True)\n", 81 "result = model.predict(X)\n", 82 "\n", 83 "print(result)" 84 ] 85 } 86 ], 87 "metadata": { 88 "kernelspec": { 89 "display_name": "Python 3 (ipykernel)", 90 "language": "python", 91 "name": "python3" 92 }, 93 "language_info": { 94 "codemirror_mode": { 95 "name": "ipython", 96 "version": 3 97 }, 98 "file_extension": ".py", 99 "mimetype": "text/x-python", 100 "name": "python", 101 "nbconvert_exporter": "python", 102 "pygments_lexer": "ipython3", 103 "version": "3.8.12" 104 } 105 }, 106 "nbformat": 4, 107 "nbformat_minor": 5 108 }