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 10, 2024
1 parent bd66e82 commit b753631
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 41 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
31 changes: 23 additions & 8 deletions deepaas/tests/test_v2_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,34 +203,49 @@ async def test_model_with_not_implemented_attributes_and_wrapper(self, m_clean):
self.assertIsInstance(val, fields.Field)

@mock.patch("deepaas.model.v2.wrapper.ModelWrapper._setup_cleanup")
@mock.patch("deepaas.model.loading.get_available_models")
def test_loading_ok(self, mock_loading, m_clean):
@mock.patch("deepaas.model.loading.get_model_by_name")
@mock.patch("deepaas.model.loading.get_available_model_names")
def test_loading_ok(self, mock_loading, mock_loading_get_model, m_clean):
app = self.get_application()

mock_loading.return_value = {uuid.uuid4().hex: "bar"}
model_name = uuid.uuid4().hex
mock_loading.return_value = frozenset([model_name])
mock_loading_get_model.return_value = fake_v2_model.TestModel()

deepaas.model.v2.register_models(app)
mock_loading.assert_called()
for m in deepaas.model.v2.MODELS.values():
self.assertIsInstance(m, v2_wrapper.ModelWrapper)

mock_loading_get_model.assert_called_once_with(model_name, "v2")
self.assertEqual(1, len(deepaas.model.v2.MODELS))

@mock.patch("deepaas.model.v2.wrapper.ModelWrapper._setup_cleanup")
@mock.patch("deepaas.model.loading.get_available_models")
def test_loading_ok_singleton(self, mock_loading, m_clean):
@mock.patch("deepaas.model.loading.get_model_by_name")
@mock.patch("deepaas.model.loading.get_available_model_names")
def test_loading_ok_singleton(self, mock_loading, mock_loading_get_model, m_clean):
app = self.get_application()

mock_loading.return_value = {uuid.uuid4().hex: "bar"}
model_name = uuid.uuid4().hex
mock_loading.return_value = frozenset([model_name])
mock_loading_get_model.return_value = fake_v2_model.TestModel()

deepaas.model.v2.register_models(app)
deepaas.model.v2.register_models(app)
mock_loading.return_value = frozenset([uuid.uuid4().hex])
deepaas.model.v2.register_models(app)
mock_loading.assert_called_once()
for m in deepaas.model.v2.MODELS.values():
self.assertIsInstance(m, v2_wrapper.ModelWrapper)

mock_loading_get_model.assert_called_once_with(model_name, "v2")
self.assertEqual(1, len(deepaas.model.v2.MODELS))

@mock.patch("deepaas.model.v2.wrapper.ModelWrapper._setup_cleanup")
@mock.patch("deepaas.model.loading.get_available_models")
@mock.patch("deepaas.model.loading.get_available_model_names")
def test_loading_error(self, mock_loading, m_clean):
app = self.get_application()
mock_loading.return_value = {}
mock_loading.return_value = frozenset({})
self.assertRaises(
exceptions.NoModelsAvailable, deepaas.model.v2.register_models, app
)
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 b753631

Please sign in to comment.