processing_whisper.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # coding=utf-8
  2. # Copyright 2022 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. Speech processor class for Whisper
  17. """
  18. from ...processing_utils import ProcessorMixin
  19. class WhisperProcessor(ProcessorMixin):
  20. r"""
  21. Constructs a Whisper processor which wraps a Whisper feature extractor and a Whisper tokenizer into a single
  22. processor.
  23. [`WhisperProcessor`] offers all the functionalities of [`WhisperFeatureExtractor`] and [`WhisperTokenizer`]. See
  24. the [`~WhisperProcessor.__call__`] and [`~WhisperProcessor.decode`] for more information.
  25. Args:
  26. feature_extractor (`WhisperFeatureExtractor`):
  27. An instance of [`WhisperFeatureExtractor`]. The feature extractor is a required input.
  28. tokenizer (`WhisperTokenizer`):
  29. An instance of [`WhisperTokenizer`]. The tokenizer is a required input.
  30. """
  31. feature_extractor_class = "WhisperFeatureExtractor"
  32. tokenizer_class = ("WhisperTokenizer", "WhisperTokenizerFast")
  33. def __init__(self, feature_extractor, tokenizer):
  34. super().__init__(feature_extractor, tokenizer)
  35. self.current_processor = self.feature_extractor
  36. self._in_target_context_manager = False
  37. def get_decoder_prompt_ids(self, task=None, language=None, no_timestamps=True):
  38. return self.tokenizer.get_decoder_prompt_ids(task=task, language=language, no_timestamps=no_timestamps)
  39. def __call__(self, *args, **kwargs):
  40. """
  41. Forwards the `audio` argument to WhisperFeatureExtractor's [`~WhisperFeatureExtractor.__call__`] and the `text`
  42. argument to [`~WhisperTokenizer.__call__`]. Please refer to the docstring of the above two methods for more
  43. information.
  44. """
  45. # For backward compatibility
  46. if self._in_target_context_manager:
  47. return self.current_processor(*args, **kwargs)
  48. audio = kwargs.pop("audio", None)
  49. sampling_rate = kwargs.pop("sampling_rate", None)
  50. text = kwargs.pop("text", None)
  51. if len(args) > 0:
  52. audio = args[0]
  53. args = args[1:]
  54. if audio is None and text is None:
  55. raise ValueError("You need to specify either an `audio` or `text` input to process.")
  56. if audio is not None:
  57. inputs = self.feature_extractor(audio, *args, sampling_rate=sampling_rate, **kwargs)
  58. if text is not None:
  59. encodings = self.tokenizer(text, **kwargs)
  60. if text is None:
  61. return inputs
  62. elif audio is None:
  63. return encodings
  64. else:
  65. inputs["labels"] = encodings["input_ids"]
  66. return inputs
  67. def get_prompt_ids(self, text: str, return_tensors="np"):
  68. return self.tokenizer.get_prompt_ids(text, return_tensors=return_tensors)
  69. __all__ = ["WhisperProcessor"]