.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/repository.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_repository.py: Creating a basic repository for your artifacts ============================================== In this tutorial, we will demonstrate how you can create a repository and log artifacts. A repository is an organized structure that stores and versions your artifacts. It makes it easy to retrieve older versions of artifacts, log new ones or time travel. .. GENERATED FROM PYTHON SOURCE LINES 12-21 .. code-block:: Python import tempfile from pathlib import Path from sklearn.datasets import make_classification from sklearn.linear_model import LogisticRegression from sklearn.svm import SVC from lazyscribe import Repository .. GENERATED FROM PYTHON SOURCE LINES 22-23 First, we're going to train version 0 of our model that we want to deploy .. GENERATED FROM PYTHON SOURCE LINES 23-28 .. code-block:: Python X, y = make_classification(n_samples=1000, n_features=10) model = SVC(kernel="linear") model.fit(X, y) .. raw:: html
SVC(kernel='linear')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 29-30 Next, we initialize our repository. Here, we're using a temporary path, but you can use a local path or an external filesystem URI as well. .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: Python tmpdir = Path(tempfile.mkdtemp()) repository = Repository(tmpdir / "repository.json", mode="w") .. GENERATED FROM PYTHON SOURCE LINES 34-35 Let's log version 0 of our model to our repository. Remember to save! Nothing is actually written to file until you save. .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python repository.log_artifact("model", model, handler="joblib") repository.save() .. GENERATED FROM PYTHON SOURCE LINES 40-41 If we want to retrieve our artifact, we need to initialize a read-only instance of ``Repository``. .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. code-block:: Python repository_read = Repository(tmpdir / "repository.json", mode="r") repository_read.load_artifact("model") .. raw:: html
SVC(kernel='linear')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 46-47 Say we have trained a newer, better version of our model. We can log it as version 1 of our model artifact. Remember to save! .. GENERATED FROM PYTHON SOURCE LINES 47-53 .. code-block:: Python modelv1 = LogisticRegression() modelv1.fit(X, y) repository.log_artifact("model", modelv1, handler="joblib") repository.save() .. GENERATED FROM PYTHON SOURCE LINES 54-55 Now, if we load our model artifact, it defaults to the most recent. .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: Python repository_read = Repository(tmpdir / "repository.json", mode="r") repository_read.load_artifact("model") .. raw:: html
LogisticRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 58-59 We can still time travel back to version 0 of our model artifact by specifying either the integer version, the datetime it was created, or a string version of the datetime. .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python repository_read.load_artifact("model", version=0) .. raw:: html
SVC(kernel='linear')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.841 seconds) .. _sphx_glr_download_tutorials_repository.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: repository.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: repository.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: repository.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_