Skip to content

Commit 3d8a2b5

Browse files
committed
Skip model manager import if error
1 parent ffd53d2 commit 3d8a2b5

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

sdks/python/apache_beam/ml/inference/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@
5959
from apache_beam.utils import multi_process_shared
6060
from apache_beam.utils import retry
6161
from apache_beam.utils import shared
62-
from apache_beam.ml.inference.model_manager import ModelManager
6362

6463
try:
6564
# pylint: disable=wrong-import-order, wrong-import-position
6665
import resource
66+
from apache_beam.ml.inference.model_manager import ModelManager
6767
except ImportError:
6868
resource = None # type: ignore[assignment]
69+
ModelManager = None # type: ignore[assignment]
6970

7071
_NANOSECOND_TO_MILLISECOND = 1_000_000
7172
_NANOSECOND_TO_MICROSECOND = 1_000

sdks/python/apache_beam/ml/inference/base_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ def validate_inference_args(self, inference_args):
317317
pass
318318

319319

320+
def try_import_model_manager():
321+
try:
322+
# pylint: disable=unused-import
323+
from apache_beam.ml.inference.model_manager import ModelManager
324+
return True
325+
except ImportError:
326+
return False
327+
328+
320329
class RunInferenceBaseTest(unittest.TestCase):
321330
def test_run_inference_impl_simple_examples(self):
322331
with TestPipeline() as pipeline:
@@ -1894,6 +1903,7 @@ def test_model_status_provides_valid_garbage_collection(self):
18941903

18951904
self.assertEqual(0, len(tags))
18961905

1906+
@skipIf(try_import_model_manager() is False, 'Model Manager not available')
18971907
def test_run_inference_impl_with_model_manager(self):
18981908
with TestPipeline() as pipeline:
18991909
examples = [1, 5, 3, 10]
@@ -1903,6 +1913,7 @@ def test_run_inference_impl_with_model_manager(self):
19031913
SimpleFakeModelHanlder(), use_model_manager=True)
19041914
assert_that(actual, equal_to(expected), label='assert:inferences')
19051915

1916+
@skipIf(try_import_model_manager() is False, 'Model Manager not available')
19061917
def test_run_inference_impl_with_model_manager_args(self):
19071918
with TestPipeline() as pipeline:
19081919
examples = [1, 5, 3, 10]

0 commit comments

Comments
 (0)