Skip to content

Commit

Permalink
Require tf_keras to be installed if tf.keras would be Keras 3
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 583534936
  • Loading branch information
TensorFlow Hub Authors authored and copybara-github committed Nov 18, 2023
1 parent 290d0c6 commit d76ea21
Showing 2 changed files with 26 additions and 1 deletion.
26 changes: 26 additions & 0 deletions tensorflow_hub/__init__.py
Original file line number Diff line number Diff line change
@@ -85,6 +85,32 @@ def _ensure_tf_install():
_ensure_tf_install()


def _ensure_keras_2_importable():
"""Check that Keras 2 can be used by attempting to import tf_keras.
Raises:
ImportError: if using tf.keras would mean using Keras 3 and tf_keras is not
installed.
"""
import tensorflow as tf

version_fn = getattr(tf.keras, "version", None)
if version_fn and version_fn().startswith("3."):
try:
import tf_keras as keras
except ImportError:
# Print more informative error message, then reraise.
print(
"\n\nFailed to import tf_keras. Please note that tf_keras is not"
" installed by default when you install tensorflow_hub. This is so"
" that users can decide which tf_keras package to use. To use"
" tensorflow_hub, please install a current version of tf_keras.\n\n"
)
raise

_ensure_keras_2_importable()


# pylint: disable=g-import-not-at-top
# pylint: disable=g-bad-import-order
# Symbols exposed via tensorflow_hub.
1 change: 0 additions & 1 deletion tensorflow_hub/pip_package/setup.py
Original file line number Diff line number Diff line change
@@ -28,7 +28,6 @@
REQUIRED_PACKAGES = [
'numpy >= 1.12.0',
'protobuf >= 3.19.6', # No less than what ../WORKSPACE uses.
'tf-keras >= 2.14.1',
]

project_name = 'tensorflow-hub'

0 comments on commit d76ea21

Please sign in to comment.