Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally import tf text #452

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions keras_nlp/tokenizers/byte_pair_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@
from typing import List

import tensorflow as tf
import tensorflow_text as tf_text
from tensorflow import keras

from keras_nlp.tokenizers import tokenizer
from keras_nlp.utils.tf_utils import assert_tf_text_installed

try:
import tensorflow_text as tf_text
except ImportError:
tf_text = None

# As python and TF handles special spaces differently, we need to
# manually handle special spaces during string split.
Expand Down Expand Up @@ -220,6 +225,8 @@ def __init__(
sequence_length=None,
**kwargs,
) -> None:
assert_tf_text_installed(self.__class__.__name__)

# Check dtype and provide a default.
if "dtype" not in kwargs or kwargs["dtype"] is None:
kwargs["dtype"] = tf.int32
Expand All @@ -241,7 +248,8 @@ def __init__(
else:
raise ValueError(
"Vocabulary must be an file path or dictionary mapping string "
f"token to int ids. Received: `type(vocabulary)={type(vocabulary)}`."
"token to int ids. Received: "
f"`type(vocabulary)={type(vocabulary)}`."
)
if isinstance(merges, str):
self.merges = [bp.rstrip() for bp in tf.io.gfile.GFile(merges)]
Expand Down