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

Ignore errors in UTF-8 decoding #1150

Merged
merged 4 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion keras_nlp/utils/tensor_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _decode_strings_to_utf8(inputs):
"""Recursively decodes to list of strings with 'utf-8' encoding."""
if isinstance(inputs, bytes):
# Handles the case when the input is a scalar string.
return inputs.decode("utf-8")
return inputs.decode("utf-8", errors="ignore")
else:
# Recursively iterate when input is a list.
return [_decode_strings_to_utf8(x) for x in inputs]
Expand Down
5 changes: 5 additions & 0 deletions keras_nlp/utils/tensor_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def test_scalar_string(self):
detokenize_output = tensor_to_list(input_data)
self.assertEqual(detokenize_output, "▀▁▂▃")

def test_string_with_utf8_error(self):
input_data = tf.constant([b"hello\xf2\xf1\x91\xe5"])
detokenize_output = tensor_to_list(input_data)
self.assertEqual(detokenize_output, ["hello"])


class ConvertToRaggedBatch(TestCase):
def test_convert_1d_python(self):
Expand Down