codingutf8 2023 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license audiotext processor class for clap featureextractorclass clapfeatureextractor tokenizerclass robertatokenizer robertatokenizerfast def initself featureextractor tokenizer super initfeatureextractor tokenizer def callself textnone audiosnone returntensorsnone kwargs samplingrate kwargs popsamplingrate none if text is none and audios is none raise valueerroryou have to specify either text or audios both cannot be none if text is not none encoding self tokenizertext returntensorsreturntensors kwargs if audios is not none audiofeatures self featureextractor audios samplingratesamplingrate returntensorsreturntensors kwargs if text is not none and audios is not none encodinginputfeatures audiofeatures inputfeatures return encoding elif text is not none return encoding else return batchencodingdatadictaudiofeatures tensortypereturntensors def batchdecodeself args kwargs return self tokenizer batchdecodeargs kwargs def decodeself args kwargs return self tokenizer decodeargs kwargs property def modelinputnamesself tokenizerinputnames self tokenizer modelinputnames featureextractorinputnames self featureextractor modelinputnames return listdict fromkeystokenizerinputnames featureextractorinputnames coding utf 8 2023 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license audio text processor class for clap constructs a clap processor which wraps a clap feature extractor and a roberta tokenizer into a single processor clapprocessor offers all the functionalities of clapfeatureextractor and robertatokenizerfast see the clapprocessor __call__ and clapprocessor decode for more information args feature_extractor clapfeatureextractor the audio processor is a required input tokenizer robertatokenizerfast the tokenizer is a required input main method to prepare for the model one or several sequences s and audio s this method forwards the text and kwargs arguments to robertatokenizerfast s robertatokenizerfast __call__ if text is not none to encode the text to prepare the audio s this method forwards the audios and kwrags arguments to clapfeatureextractor s clapfeatureextractor __call__ if audios is not none please refer to the doctsring of the above two methods for more information args text str list str list list str the sequence or batch of sequences to be encoded each sequence can be a string or a list of strings pretokenized string if the sequences are provided as list of strings pretokenized you must set is_split_into_words true to lift the ambiguity with a batch of sequences audios np ndarray torch tensor list np ndarray list torch tensor the audio or batch of audios to be prepared each audio can be numpy array or pytorch tensor in case of a numpy array pytorch tensor each audio should be of shape c t where c is a number of channels and t the sample length of the audio return_tensors str or utils tensortype optional if set will return tensors of a particular framework acceptable values are tf return tensorflow tf constant objects pt return pytorch torch tensor objects np return numpy np ndarray objects jax return jax jnp ndarray objects returns batchencoding a batchencoding with the following fields input_ids list of token ids to be fed to a model returned when text is not none attention_mask list of indices specifying which tokens should be attended to by the model when return_attention_mask true or if attention_mask is in self model_input_names and if text is not none audio_features audio features to be fed to a model returned when audios is not none this method forwards all its arguments to robertatokenizerfast s pretrainedtokenizer batch_decode please refer to the docstring of this method for more information this method forwards all its arguments to robertatokenizerfast s pretrainedtokenizer decode please refer to the docstring of this method for more information
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding


class ClapProcessor(ProcessorMixin):
    r

    feature_extractor_class = "ClapFeatureExtractor"
    tokenizer_class = ("RobertaTokenizer", "RobertaTokenizerFast")

    def __init__(self, feature_extractor, tokenizer):
        super().__init__(feature_extractor, tokenizer)

    def __call__(self, text=None, audios=None, return_tensors=None, **kwargs):
        
        sampling_rate = kwargs.pop("sampling_rate", None)

        if text is None and audios is None:
            raise ValueError("You have to specify either text or audios. Both cannot be none.")

        if text is not None:
            encoding = self.tokenizer(text, return_tensors=return_tensors, **kwargs)

        if audios is not None:
            audio_features = self.feature_extractor(
                audios, sampling_rate=sampling_rate, return_tensors=return_tensors, **kwargs
            )

        if text is not None and audios is not None:
            encoding["input_features"] = audio_features.input_features
            return encoding
        elif text is not None:
            return encoding
        else:
            return BatchEncoding(data=dict(**audio_features), tensor_type=return_tensors)

    def batch_decode(self, *args, **kwargs):
        
        return self.tokenizer.batch_decode(*args, **kwargs)

    def decode(self, *args, **kwargs):
        
        return self.tokenizer.decode(*args, **kwargs)

    @property
    def model_input_names(self):
        tokenizer_input_names = self.tokenizer.model_input_names
        feature_extractor_input_names = self.feature_extractor.model_input_names
        return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names))