processing_bros.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # coding=utf-8
  2. # Copyright 2023 The HuggingFace Inc. team.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """
  16. Processor class for Bros.
  17. """
  18. from typing import List, Optional, Union
  19. from ...processing_utils import ProcessorMixin
  20. from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
  21. from ...utils import TensorType
  22. class BrosProcessor(ProcessorMixin):
  23. r"""
  24. Constructs a Bros processor which wraps a BERT tokenizer.
  25. [`BrosProcessor`] offers all the functionalities of [`BertTokenizerFast`]. See the docstring of
  26. [`~BrosProcessor.__call__`] and [`~BrosProcessor.decode`] for more information.
  27. Args:
  28. tokenizer (`BertTokenizerFast`, *optional*):
  29. An instance of ['BertTokenizerFast`]. The tokenizer is a required input.
  30. """
  31. attributes = ["tokenizer"]
  32. tokenizer_class = ("BertTokenizer", "BertTokenizerFast")
  33. def __init__(self, tokenizer=None, **kwargs):
  34. if tokenizer is None:
  35. raise ValueError("You need to specify a `tokenizer`.")
  36. super().__init__(tokenizer)
  37. def __call__(
  38. self,
  39. text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None,
  40. add_special_tokens: bool = True,
  41. padding: Union[bool, str, PaddingStrategy] = False,
  42. truncation: Union[bool, str, TruncationStrategy] = None,
  43. max_length: Optional[int] = None,
  44. stride: int = 0,
  45. pad_to_multiple_of: Optional[int] = None,
  46. return_token_type_ids: Optional[bool] = None,
  47. return_attention_mask: Optional[bool] = None,
  48. return_overflowing_tokens: bool = False,
  49. return_special_tokens_mask: bool = False,
  50. return_offsets_mapping: bool = False,
  51. return_length: bool = False,
  52. verbose: bool = True,
  53. return_tensors: Optional[Union[str, TensorType]] = None,
  54. **kwargs,
  55. ) -> BatchEncoding:
  56. """
  57. This method uses [`BertTokenizerFast.__call__`] to prepare text for the model.
  58. Please refer to the docstring of the above two methods for more information.
  59. """
  60. encoding = self.tokenizer(
  61. text=text,
  62. add_special_tokens=add_special_tokens,
  63. padding=padding,
  64. truncation=truncation,
  65. max_length=max_length,
  66. stride=stride,
  67. pad_to_multiple_of=pad_to_multiple_of,
  68. return_token_type_ids=return_token_type_ids,
  69. return_attention_mask=return_attention_mask,
  70. return_overflowing_tokens=return_overflowing_tokens,
  71. return_special_tokens_mask=return_special_tokens_mask,
  72. return_offsets_mapping=return_offsets_mapping,
  73. return_length=return_length,
  74. verbose=verbose,
  75. return_tensors=return_tensors,
  76. **kwargs,
  77. )
  78. return encoding
  79. def batch_decode(self, *args, **kwargs):
  80. """
  81. This method forwards all its arguments to BertTokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
  82. refer to the docstring of this method for more information.
  83. """
  84. return self.tokenizer.batch_decode(*args, **kwargs)
  85. def decode(self, *args, **kwargs):
  86. """
  87. This method forwards all its arguments to BertTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
  88. the docstring of this method for more information.
  89. """
  90. return self.tokenizer.decode(*args, **kwargs)
  91. @property
  92. def model_input_names(self):
  93. tokenizer_input_names = self.tokenizer.model_input_names
  94. return list(dict.fromkeys(tokenizer_input_names))