Skip to content

Commit

Permalink
feat!: remove loading of various modules
Browse files Browse the repository at this point in the history
Feature has marked as deprecated, now it is time to remove it

Fixes: #128
  • Loading branch information
alvarolopez committed Jun 12, 2024
1 parent f4ba953 commit ffdb54a
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 62 deletions.
9 changes: 2 additions & 7 deletions deepaas/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,8 @@
"model-name",
default=os.environ.get("DEEPAAS_V2_MODEL", ""),
help="""
Specify the model to be used. If not specified, DEEPaaS will serve all the models that
are available. If specified, DEEPaaS will serve only the specified model. You can also
use the DEEPAAS_V2_MODEL environment variable.
WARNING: Serving multiple models is deprecated and will be removed in the future,
therefore it is strongly suggested that you specify the model you want to
or that you ensure that only one model is available.
Specify the model to be used. If not specified, DEEPaaS will fail if there are
more than only one models available.
""",
),
]
Expand Down
4 changes: 4 additions & 0 deletions deepaas/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ class ModuleNotFoundError(Exception):

class NoModelsAvailable(Exception):
"""No models are available in the system."""


class MultipleModelsFound(Exception):
"""Multiple models found."""
49 changes: 23 additions & 26 deletions deepaas/model/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
# License for the specific language governing permissions and limitations
# under the License.

import warnings

from oslo_log import log

from deepaas import config
Expand All @@ -39,34 +37,33 @@ def register_models(app):
if MODELS_LOADED:
return

try:
if CONF.model_name:
MODELS[CONF.model_name] = wrapper.ModelWrapper(
CONF.model_name,
loading.get_model_by_name(CONF.model_name, "v2"),
app,
)
if CONF.model_name:
model_name = CONF.model_name
else:
model_names = list(loading.get_available_model_names("v2"))
if not model_names:
LOG.error("No models found.")
raise exceptions.NoModelsAvailable()
elif len(model_names) == 1:
model_name = model_names[0]
else:
for name, model in loading.get_available_models("v2").items():
MODELS[name] = wrapper.ModelWrapper(name, model, app)
LOG.error(
"Multiple models found, but no model has been specified, please "
"specify a model using the --model-name option."
)
raise exceptions.MultipleModelsFound()

try:
MODELS[model_name] = wrapper.ModelWrapper(
model_name,
loading.get_model_by_name(model_name, "v2"),
app,
)
except exceptions.ModuleNotFoundError:
LOG.error("Model not found: %s", CONF.model_name)
LOG.error("Model not found: %s", model_name)
raise
except Exception as e:
LOG.warning("Error loading models: %s", e)
LOG.exception("Error loading model: %s", e)
raise e

if MODELS:
if len(MODELS) > 1:
# Loading several models will be deprecated in the future
warn_msg = "Loading several models is deprecated."
warnings.warn(warn_msg, DeprecationWarning, stacklevel=2)
LOG.warning(warn_msg)

MODELS_LOADED = True
return

if not MODELS:
LOG.error("No models found in V2, loading test model")
raise exceptions.NoModelsAvailable()
MODELS_LOADED = True
13 changes: 12 additions & 1 deletion deepaas/tests/test_v2_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,21 @@ async def application():
@pytest.fixture
async def mocks(monkeypatch):
monkeypatch.setattr(v2_wrapper.ModelWrapper, "_setup_cleanup", lambda x: None)
model_name = uuid.uuid4().hex
monkeypatch.setattr(
deepaas.model.loading,
"get_available_models",
lambda x: {uuid.uuid4().hex: "bar"},
lambda x: {model_name: "foo"},
)
monkeypatch.setattr(
deepaas.model.loading,
"get_available_model_names",
lambda x: [model_name],
)
monkeypatch.setattr(
deepaas.model.loading,
"get_model_by_name",
lambda x, y: fake_v2_model.TestModel,
)
monkeypatch.setattr(deepaas.model.v2, "MODELS_LOADED", False)
monkeypatch.setattr(deepaas.model.v2, "MODELS", {})
Expand Down
9 changes: 2 additions & 7 deletions doc/source/cli/deepaas-cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,8 @@ Options

.. option:: --model-name MODEL_NAME

Specify the model to be used. If not specified, DEEPaaS will serve all the models
that are available. If specified, DEEPaaS will serve only the specified model. You
can also use the DEEPAAS_V2_MODEL environment variable.

WARNING: Serving multiple models is deprecated and will be removed in the future,
therefore it is strongly suggested that you specify the model you want to or that
you ensure that only one model is available.
Specify the model to be used. If not specified, DEEPaaS will fail if there are
more than only one models available.

Files
=====
Expand Down
9 changes: 2 additions & 7 deletions doc/source/cli/deepaas-run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ Options

.. option:: --model-name MODEL_NAME

Specify the model to be used. If not specified, DEEPaaS will serve all the models
that are available. If specified, DEEPaaS will serve only the specified model. You
can also use the DEEPAAS_V2_MODEL environment variable.

WARNING: Serving multiple models is deprecated and will be removed in the future,
therefore it is strongly suggested that you specify the model you want to or that
you ensure that only one model is available.
Specify the model to be used. If not specified, DEEPaaS will fail if there are
more than only one models available.

.. option:: --debug, -d

Expand Down
25 changes: 11 additions & 14 deletions doc/source/user/v2-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,21 @@ Defining what to load
---------------------

The DEEPaaS API uses Python's `Setuptools`_ entry points that are dynamically
loaded to offer the model functionality through the API. This allows you to
offer several models using a single DEEPaaS instance, by defining different
entry points for the different models.

.. warning::
Serving multiple models is marked as deprecated, and will be removed in a
future major version of the API. Please ensure that you start using the `model-name`
configuration option in your configuration file or the `--model-name` command line
option as soon as possible.
loaded to offer the model functionality through the API. This allows you to have more
than one model installed in your system, and choose which one to serve via the
`model-name` configuration option in your configuration file or the `--model-name`
command line option.

.. _Setuptools: https://setuptools.readthedocs.io/en/latest/setuptools.html

When the DEEPaaS API is spawned it will look for the ``deepaas.v2.model``
entrypoint namespace, loading and adding the names found into the API
namespace. In order to define your entry points, your module should leverage
setuptools and be ready to be installed in the system. Then, in order to define
your entry points, you should add the following to your ``setup.cfg``
configuration file:
entrypoint namespace and the configured `model-name`. If there are more than one
model available you need to specify it via the option. If there is only one model, that
one will be served.

In order to define your entry points, your module should leverage setuptools and be
ready to be installed in the system. Then, in order to define your entry points, you
should add the following to your ``setup.cfg`` configuration file:

.. code-block:: ini
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/deprecation-removals-1f43c9301633ac76.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ upgrade:
- |
Removed deprecated loading of "deepaas-test" model. If you wish to develop
or debug your model, use the "demo-app" instead: https://github.com/ai4os-hub/ai4os-demo-app
- |
Removed the loading of various models, now only a single model can be served. If you
have more than one model available in the system please specify a single one via
the command line option ``--model-name``.

0 comments on commit ffdb54a

Please sign in to comment.