processing_parakeet.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # coding=utf-8
  2. # Copyright 2025 The HuggingFace Inc. team. All rights reserved.
  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. from typing import Optional, Union
  16. from ...audio_utils import AudioInput, make_list_of_audio
  17. from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack
  18. from ...tokenization_utils_base import PreTokenizedInput, TextInput
  19. from ...utils import logging
  20. logger = logging.get_logger(__name__)
  21. class ParakeetProcessorKwargs(ProcessingKwargs, total=False):
  22. _defaults = {
  23. "audio_kwargs": {
  24. "sampling_rate": 16000,
  25. "padding": "longest",
  26. },
  27. "text_kwargs": {
  28. "padding": True,
  29. "padding_side": "right",
  30. "add_special_tokens": False,
  31. },
  32. "common_kwargs": {"return_tensors": "pt"},
  33. }
  34. class ParakeetProcessor(ProcessorMixin):
  35. attributes = ["feature_extractor", "tokenizer"]
  36. feature_extractor_class = "ParakeetFeatureExtractor"
  37. tokenizer_class = "ParakeetTokenizerFast"
  38. def __call__(
  39. self,
  40. audio: AudioInput,
  41. text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput], None] = None,
  42. sampling_rate: Optional[int] = None,
  43. **kwargs: Unpack[ParakeetProcessorKwargs],
  44. ):
  45. audio = make_list_of_audio(audio)
  46. output_kwargs = self._merge_kwargs(
  47. ParakeetProcessorKwargs,
  48. tokenizer_init_kwargs=self.tokenizer.init_kwargs,
  49. **kwargs,
  50. )
  51. if sampling_rate is None:
  52. logger.warning_once(
  53. f"You've provided audio without specifying the sampling rate. It will be assumed to be {output_kwargs['audio_kwargs']['sampling_rate']}, which can result in silent errors."
  54. )
  55. elif sampling_rate != output_kwargs["audio_kwargs"]["sampling_rate"]:
  56. raise ValueError(
  57. f"The sampling rate of the audio ({sampling_rate}) does not match the sampling rate of the processor ({output_kwargs['audio_kwargs']['sampling_rate']}). Please provide resampled the audio to the expected sampling rate."
  58. )
  59. if audio is not None:
  60. inputs = self.feature_extractor(audio, **output_kwargs["audio_kwargs"])
  61. if text is not None:
  62. encodings = self.tokenizer(text, **output_kwargs["text_kwargs"])
  63. if text is None:
  64. return inputs
  65. else:
  66. inputs["labels"] = encodings["input_ids"]
  67. return inputs
  68. @property
  69. def model_input_names(self):
  70. feature_extractor_input_names = self.feature_extractor.model_input_names
  71. return feature_extractor_input_names + ["labels"]
  72. __all__ = ["ParakeetProcessor"]