Aluode/PerceptionLabPortable
0
1# coding=utf-82# 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 at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# 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 and14# limitations under the License.15from typing import Optional, Union16 17from ...audio_utils import AudioInput, make_list_of_audio18from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack19from ...tokenization_utils_base import PreTokenizedInput, TextInput20from ...utils import logging21 22 23logger = logging.get_logger(__name__)24 25 26class ParakeetProcessorKwargs(ProcessingKwargs, total=False):27 _defaults = {28 "audio_kwargs": {29 "sampling_rate": 16000,30 "padding": "longest",31 },32 "text_kwargs": {33 "padding": True,34 "padding_side": "right",35 "add_special_tokens": False,36 },37 "common_kwargs": {"return_tensors": "pt"},38 }39 40 41class ParakeetProcessor(ProcessorMixin):42 attributes = ["feature_extractor", "tokenizer"]43 feature_extractor_class = "ParakeetFeatureExtractor"44 tokenizer_class = "ParakeetTokenizerFast"45 46 def __call__(47 self,48 audio: AudioInput,49 text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput], None] = None,50 sampling_rate: Optional[int] = None,51 **kwargs: Unpack[ParakeetProcessorKwargs],52 ):53 audio = make_list_of_audio(audio)54 55 output_kwargs = self._merge_kwargs(56 ParakeetProcessorKwargs,57 tokenizer_init_kwargs=self.tokenizer.init_kwargs,58 **kwargs,59 )60 61 if sampling_rate is None:62 logger.warning_once(63 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."64 )65 elif sampling_rate != output_kwargs["audio_kwargs"]["sampling_rate"]:66 raise ValueError(67 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."68 )69 70 if audio is not None:71 inputs = self.feature_extractor(audio, **output_kwargs["audio_kwargs"])72 if text is not None:73 encodings = self.tokenizer(text, **output_kwargs["text_kwargs"])74 75 if text is None:76 return inputs77 else:78 inputs["labels"] = encodings["input_ids"]79 return inputs80 81 @property82 def model_input_names(self):83 feature_extractor_input_names = self.feature_extractor.model_input_names84 return feature_extractor_input_names + ["labels"]85 86 87__all__ = ["ParakeetProcessor"]88 