|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "ae6b5e80", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [ |
| 9 | + { |
| 10 | + "name": "stderr", |
| 11 | + "output_type": "stream", |
| 12 | + "text": [ |
| 13 | + "C:\\Users\\ABC\\AppData\\Local\\Temp\\ipykernel_16292\\506102746.py:12: FutureWarning: ``mlflow.tracking.client.MlflowClient.transition_model_version_stage`` is deprecated since 2.9.0. Model registry stages will be removed in a future major release. To learn more about the deprecation of model registry stages, see our migration guide here: https://mlflow.org/docs/latest/model-registry.html#migrating-from-stages\n", |
| 14 | + " client.transition_model_version_stage(\n" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "data": { |
| 19 | + "text/plain": [ |
| 20 | + "<ModelVersion: aliases=[], creation_timestamp=1751283921891, current_stage='Staging', deployment_job_state=None, description=None, last_updated_timestamp=1751283921936, metrics=None, model_id=None, name='best_model', params=None, run_id='<your_run_id>', run_link=None, source='runs:/<your_run_id>/model', status='READY', status_message=None, tags={}, user_id=None, version=1>" |
| 21 | + ] |
| 22 | + }, |
| 23 | + "execution_count": 1, |
| 24 | + "metadata": {}, |
| 25 | + "output_type": "execute_result" |
| 26 | + } |
| 27 | + ], |
| 28 | + "source": [ |
| 29 | + "from mlflow.tracking import MlflowClient\n", |
| 30 | + "\n", |
| 31 | + "client = MlflowClient()\n", |
| 32 | + "\n", |
| 33 | + "run_id = \"<your_run_id>\" # Run where model was logged\n", |
| 34 | + "model_uri = f\"runs:/{run_id}/model\"\n", |
| 35 | + "\n", |
| 36 | + "model_details = client.create_registered_model(\"best_model\") # Create model in registry if not exists\n", |
| 37 | + "mv = client.create_model_version(\"best_model\", model_uri, run_id)\n", |
| 38 | + "\n", |
| 39 | + "# Transition to staging\n", |
| 40 | + "client.transition_model_version_stage(\n", |
| 41 | + " name=\"best_model\",\n", |
| 42 | + " version=mv.version,\n", |
| 43 | + " stage=\"Staging\"\n", |
| 44 | + ")\n" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": 5, |
| 50 | + "id": "5a4642cb", |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [ |
| 53 | + { |
| 54 | + "name": "stdout", |
| 55 | + "output_type": "stream", |
| 56 | + "text": [ |
| 57 | + "[<ModelVersion: aliases=[], creation_timestamp=1751336471270, current_stage='Staging', deployment_job_state=None, description=None, last_updated_timestamp=1751336471924, metrics=[], model_id='m-4eb2843bda5d4e60b07f3a7e2dbc78cf', name='credit-risk-model', params={}, run_id='4d40cba2b1ef4491813380e7a40eb4f9', run_link=None, source='models:/m-4eb2843bda5d4e60b07f3a7e2dbc78cf', status='READY', status_message=None, tags={}, user_id=None, version=1>]\n" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "name": "stderr", |
| 62 | + "output_type": "stream", |
| 63 | + "text": [ |
| 64 | + "C:\\Users\\ABC\\AppData\\Local\\Temp\\ipykernel_12312\\1572118651.py:9: FutureWarning: ``mlflow.tracking.client.MlflowClient.get_latest_versions`` is deprecated since 2.9.0. Model registry stages will be removed in a future major release. To learn more about the deprecation of model registry stages, see our migration guide here: https://mlflow.org/docs/latest/model-registry.html#migrating-from-stages\n", |
| 65 | + " versions = client.get_latest_versions(\"credit-risk-model\")\n" |
| 66 | + ] |
| 67 | + } |
| 68 | + ], |
| 69 | + "source": [ |
| 70 | + "import mlflow\n", |
| 71 | + "from mlflow.tracking import MlflowClient\n", |
| 72 | + "\n", |
| 73 | + "# Set tracking URI to point to your local mlruns folder\n", |
| 74 | + "mlflow.set_tracking_uri(\"file:///C:/Users/ABC/Desktop/10Acadamy/Week 5/Credit-Risk-Probability-Model/src/model/mlruns\")\n", |
| 75 | + "\n", |
| 76 | + "client = MlflowClient()\n", |
| 77 | + "try:\n", |
| 78 | + " versions = client.get_latest_versions(\"credit-risk-model\")\n", |
| 79 | + " print(versions)\n", |
| 80 | + "except Exception as e:\n", |
| 81 | + " print(\"Model not found or no versions:\", e)\n", |
| 82 | + "\n" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": 6, |
| 88 | + "id": "e43e31b8", |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [ |
| 91 | + { |
| 92 | + "name": "stderr", |
| 93 | + "output_type": "stream", |
| 94 | + "text": [ |
| 95 | + "C:\\Users\\ABC\\AppData\\Local\\Temp\\ipykernel_12312\\77176695.py:3: FutureWarning: ``mlflow.tracking.client.MlflowClient.get_latest_versions`` is deprecated since 2.9.0. Model registry stages will be removed in a future major release. To learn more about the deprecation of model registry stages, see our migration guide here: https://mlflow.org/docs/latest/model-registry.html#migrating-from-stages\n", |
| 96 | + " versions = client.get_latest_versions(\"credit-risk-model\")\n" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "name": "stdout", |
| 101 | + "output_type": "stream", |
| 102 | + "text": [ |
| 103 | + "Model versions found: [<ModelVersion: aliases=[], creation_timestamp=1751336471270, current_stage='Staging', deployment_job_state=None, description=None, last_updated_timestamp=1751336471924, metrics=[], model_id='m-4eb2843bda5d4e60b07f3a7e2dbc78cf', name='credit-risk-model', params={}, run_id='4d40cba2b1ef4491813380e7a40eb4f9', run_link=None, source='models:/m-4eb2843bda5d4e60b07f3a7e2dbc78cf', status='READY', status_message=None, tags={}, user_id=None, version=1>]\n", |
| 104 | + "Loading model from: C:\\Users\\ABC\\Desktop\\10Acadamy\\Week 5\\Credit-Risk-Probability-Model\\src\\model\\mlruns\\0\\models\\m-4eb2843bda5d4e60b07f3a7e2dbc78cf\\artifacts\\artifacts\\model.pkl\n" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "name": "stderr", |
| 109 | + "output_type": "stream", |
| 110 | + "text": [ |
| 111 | + "c:\\Users\\ABC\\Desktop\\10Acadamy\\Week 5\\Credit-Risk-Probability-Model\\env\\Lib\\site-packages\\sklearn\\base.py:440: InconsistentVersionWarning: Trying to unpickle estimator DecisionTreeClassifier from version 1.6.1 when using version 1.7.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", |
| 112 | + "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", |
| 113 | + " warnings.warn(\n", |
| 114 | + "c:\\Users\\ABC\\Desktop\\10Acadamy\\Week 5\\Credit-Risk-Probability-Model\\env\\Lib\\site-packages\\sklearn\\base.py:440: InconsistentVersionWarning: Trying to unpickle estimator RandomForestClassifier from version 1.6.1 when using version 1.7.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", |
| 115 | + "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", |
| 116 | + " warnings.warn(\n" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "name": "stdout", |
| 121 | + "output_type": "stream", |
| 122 | + "text": [ |
| 123 | + "Model loaded successfully.\n" |
| 124 | + ] |
| 125 | + } |
| 126 | + ], |
| 127 | + "source": [ |
| 128 | + "# Check if the model exists\n", |
| 129 | + "try:\n", |
| 130 | + " versions = client.get_latest_versions(\"credit-risk-model\")\n", |
| 131 | + " print(\"Model versions found:\", versions)\n", |
| 132 | + "except Exception as e:\n", |
| 133 | + " print(\"Error fetching model versions:\", e)\n", |
| 134 | + "\n", |
| 135 | + "# Load model from MLflow Model Registry (Staging version)\n", |
| 136 | + "try:\n", |
| 137 | + " model = mlflow.pyfunc.load_model(\"models:/credit-risk-model/Staging\")\n", |
| 138 | + " print(\"Model loaded successfully.\")\n", |
| 139 | + "except Exception as e:\n", |
| 140 | + " print(\"Error loading model:\", e)" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": 10, |
| 146 | + "id": "06a72fc9", |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [ |
| 149 | + { |
| 150 | + "name": "stdout", |
| 151 | + "output_type": "stream", |
| 152 | + "text": [ |
| 153 | + "Version: 1, Stage: Staging\n" |
| 154 | + ] |
| 155 | + } |
| 156 | + ], |
| 157 | + "source": [ |
| 158 | + "from mlflow.tracking import MlflowClient\n", |
| 159 | + "import mlflow\n", |
| 160 | + "from mlflow.tracking import MlflowClient\n", |
| 161 | + "# Set tracking URI to point to your local mlruns folder\n", |
| 162 | + "mlflow.set_tracking_uri(\"file:///C:/Users/ABC/Desktop/10Acadamy/Week 5/Credit-Risk-Probability-Model/src/model/mlruns\")\n", |
| 163 | + "\n", |
| 164 | + "client = MlflowClient()\n", |
| 165 | + "model_name = \"credit-risk-model\"\n", |
| 166 | + "model_versions = client.get_registered_model(model_name)\n", |
| 167 | + "\n", |
| 168 | + "# Print details about the model versions\n", |
| 169 | + "for version in model_versions.latest_versions:\n", |
| 170 | + " print(f\"Version: {version.version}, Stage: {version.current_stage}\")" |
| 171 | + ] |
| 172 | + } |
| 173 | + ], |
| 174 | + "metadata": { |
| 175 | + "kernelspec": { |
| 176 | + "display_name": "env", |
| 177 | + "language": "python", |
| 178 | + "name": "python3" |
| 179 | + }, |
| 180 | + "language_info": { |
| 181 | + "codemirror_mode": { |
| 182 | + "name": "ipython", |
| 183 | + "version": 3 |
| 184 | + }, |
| 185 | + "file_extension": ".py", |
| 186 | + "mimetype": "text/x-python", |
| 187 | + "name": "python", |
| 188 | + "nbconvert_exporter": "python", |
| 189 | + "pygments_lexer": "ipython3", |
| 190 | + "version": "3.13.2" |
| 191 | + } |
| 192 | + }, |
| 193 | + "nbformat": 4, |
| 194 | + "nbformat_minor": 5 |
| 195 | +} |
0 commit comments