Skip to content

Commit fcf9576

Browse files
authored
Raise error if tensorflow_text is not found (#2427)
1 parent 669b054 commit fcf9576

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

keras_cv/src/models/feature_extractor/clip/clip_processor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from keras_nlp.layers import StartEndPacker
3434
except ImportError:
3535
keras_nlp = None
36+
StartEndPacker = None
3637

3738

3839
@keras_cv_export("keras_cv.models.feature_extractor.CLIPProcessor")

keras_cv/src/models/feature_extractor/clip/clip_tokenizer.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
# limitations under the License.
1414
import regex as re
1515
import tensorflow as tf
16-
import tensorflow_text as tf_text
16+
17+
try:
18+
import tensorflow_text as tf_text
19+
except ImportError:
20+
tf_text = None
1721

1822
try:
1923
import keras_nlp
2024
from keras_nlp.tokenizers import BytePairTokenizer
2125
except ImportError:
2226
keras_nlp = None
27+
BytePairTokenizer = object
2328

2429
# As python and TF handles special spaces differently, we need to
2530
# manually handle special spaces during string split.
@@ -41,6 +46,11 @@ def split_strings_for_bpe(inputs, unsplittable_tokens=None):
4146
# support lookahead match, we are using an alternative insert a special
4247
# token "६" before leading space of non-space characters and after the
4348
# trailing space, e.g., " keras" will be "६ keras".
49+
if tf_text is None:
50+
raise ImportError(
51+
"BytePairTokenization requires `tensorflow_text`."
52+
"Please install with `pip install tensorflow_text`."
53+
)
4454
inputs = tf.strings.regex_replace(
4555
inputs, rf"( )([^\s{SPECIAL_WHITESPACES}])", r"६\1\2"
4656
)

0 commit comments

Comments
 (0)