Skip to content

Commit 54d3439

Browse files
Copilotalvarolopez
andcommitted
Simplify model management for single model use case - remove dictionary complexity
Co-authored-by: alvarolopez <[email protected]>
1 parent 2401eed commit 54d3439

File tree

3 files changed

+36
-31
lines changed

3 files changed

+36
-31
lines changed

deepaas/model/__init__.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@
1616

1717
from deepaas.model import v2
1818

19-
# Create a proper module-level property using __getattr__ (Python 3.7+)
20-
def __getattr__(name):
21-
if name == 'V2_MODELS':
22-
return v2.MODELS
23-
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
24-
25-
# Backwards compatibility - these will be updated after model registration
19+
# Direct access to the single model
2620
V2_MODEL = None
2721
V2_MODEL_NAME = None
2822

@@ -35,12 +29,18 @@ def register_v2_models(app):
3529
"""
3630
global V2_MODEL, V2_MODEL_NAME
3731

38-
result = v2.register_models(app)
39-
40-
# Update backwards compatibility variables
41-
if v2.MODELS:
42-
model_names = list(v2.MODELS.keys())
43-
V2_MODEL_NAME = model_names[0]
44-
V2_MODEL = v2.MODELS[V2_MODEL_NAME]
32+
v2.register_models(app)
4533

46-
return result
34+
# Update module-level variables for easy access
35+
V2_MODEL = v2.MODEL
36+
V2_MODEL_NAME = v2.MODEL_NAME
37+
38+
39+
# For compatibility with code that expects V2_MODELS dictionary
40+
def __getattr__(name):
41+
if name == 'V2_MODELS':
42+
# Return a dict-like interface for the single model
43+
if V2_MODEL and V2_MODEL_NAME:
44+
return {V2_MODEL_NAME: V2_MODEL}
45+
return {}
46+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

deepaas/model/v2/__init__.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,21 @@
2424

2525
CONF = config.CONF
2626

27-
# Model registry
28-
MODELS = {}
29-
MODELS_LOADED = False
27+
# Model registry - simplified for single model use case
28+
MODEL = None
29+
MODEL_NAME = ""
3030

3131

3232
def register_models(app):
33-
global MODELS
34-
global MODELS_LOADED
33+
"""Register a single V2 model.
34+
35+
Since DEEPaaS only handles one model at a time, this is simplified
36+
to manage a single model instance.
37+
"""
38+
global MODEL
39+
global MODEL_NAME
3540

36-
if MODELS_LOADED:
41+
if MODEL:
3742
return
3843

3944
if CONF.model_name:
@@ -53,15 +58,14 @@ def register_models(app):
5358
raise exceptions.MultipleModelsFound()
5459

5560
try:
56-
MODELS[model_name] = wrapper.ModelWrapper(
61+
MODEL = wrapper.ModelWrapper(
5762
model_name,
5863
loading.get_model_by_name(model_name, "v2"),
5964
)
65+
MODEL_NAME = model_name
6066
except exceptions.ModuleNotFoundError:
6167
LOG.error("Model not found: %s", model_name)
6268
raise
6369
except Exception as e:
6470
LOG.exception("Error loading model: %s", e)
6571
raise e
66-
67-
MODELS_LOADED = True

deepaas/tests/test_v2_models.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ async def mocks(monkeypatch):
4747
"get_model_by_name",
4848
lambda x, y: fake_v2_model.TestModel,
4949
)
50-
monkeypatch.setattr(deepaas.model.v2, "MODELS", {})
51-
monkeypatch.setattr(deepaas.model.v2, "MODELS_LOADED", False)
50+
monkeypatch.setattr(deepaas.model.v2, "MODEL", None)
51+
monkeypatch.setattr(deepaas.model.v2, "MODEL_NAME", "")
5252

5353

5454
def test_abc():
@@ -194,8 +194,8 @@ async def test_model_with_not_implemented_attributes_and_wrapper(mocks):
194194
async def test_loading_ok(mocks):
195195
deepaas.model.v2.register_models(None)
196196

197-
for m in deepaas.model.v2.MODELS.values():
198-
assert isinstance(m, v2_wrapper.ModelWrapper)
197+
m = deepaas.model.v2.MODEL
198+
assert isinstance(m, v2_wrapper.ModelWrapper)
199199

200200

201201
async def test_loading_ok_singleton(mocks, monkeypatch):
@@ -208,9 +208,10 @@ async def test_loading_ok_singleton(mocks, monkeypatch):
208208
)
209209
deepaas.model.v2.register_models(None)
210210

211-
for name, m in deepaas.model.v2.MODELS.items():
212-
assert isinstance(m, v2_wrapper.ModelWrapper)
213-
assert name != new_model_name
211+
name = deepaas.model.v2.MODEL_NAME
212+
m = deepaas.model.v2.MODEL
213+
assert isinstance(m, v2_wrapper.ModelWrapper)
214+
assert name != new_model_name
214215

215216

216217
def test_loading_error(monkeypatch):

0 commit comments

Comments
 (0)