Skip to content

Commit

Permalink
Don't assume env vars are set in model handler (#29200)
Browse files Browse the repository at this point in the history
* Don't assume env vars are set in model handler

* Patch notebook
  • Loading branch information
damccorm authored Oct 30, 2023
1 parent 8a28100 commit 28f6e33
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/notebooks/beam-ml/run_custom_inference.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@
" model_name: The spaCy model name. Default is en_core_web_sm.\n",
" \"\"\"\n",
" self._model_name = model_name\n",
" self._env_vars = {}\n",
"\n",
" def load_model(self) -> Language:\n",
" \"\"\"Loads and initializes a model for processing.\"\"\"\n",
Expand Down
10 changes: 5 additions & 5 deletions sdks/python/apache_beam/ml/inference/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def __init__(
'postprocessing functions defined into a keyed model handler. All '
'pre/postprocessing functions must be defined on the outer model'
'handler.')
self._env_vars = unkeyed._env_vars
self._env_vars = getattr(unkeyed, '_env_vars', {})
self._unkeyed = unkeyed
return

Expand Down Expand Up @@ -553,7 +553,7 @@ def __init__(
'overriding the KeyedModelHandler.batch_elements_kwargs() method.',
hints,
batch_kwargs)
env_vars = mh._env_vars
env_vars = getattr(mh, '_env_vars', {})
if len(env_vars) > 0:
logging.warning(
'mh %s defines the following _env_vars which will be ignored %s. '
Expand Down Expand Up @@ -816,7 +816,7 @@ def __init__(self, unkeyed: ModelHandler[ExampleT, PredictionT, ModelT]):
'pre/postprocessing functions must be defined on the outer model'
'handler.')
self._unkeyed = unkeyed
self._env_vars = unkeyed._env_vars
self._env_vars = getattr(unkeyed, '_env_vars', {})

def load_model(self) -> ModelT:
return self._unkeyed.load_model()
Expand Down Expand Up @@ -895,7 +895,7 @@ def __init__(
preprocess_fn: the preprocessing function to use.
"""
self._base = base
self._env_vars = base._env_vars
self._env_vars = getattr(base, '_env_vars', {})
self._preprocess_fn = preprocess_fn

def load_model(self) -> ModelT:
Expand Down Expand Up @@ -951,7 +951,7 @@ def __init__(
postprocess_fn: the preprocessing function to use.
"""
self._base = base
self._env_vars = base._env_vars
self._env_vars = getattr(base, '_env_vars', {})
self._postprocess_fn = postprocess_fn

def load_model(self) -> ModelT:
Expand Down

0 comments on commit 28f6e33

Please sign in to comment.