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 seamlessm4t featureextractorclass seamlessm4tfeatureextractor tokenizerclass seamlessm4ttokenizer seamlessm4ttokenizerfast def initself featureextractor tokenizer super initfeatureextractor tokenizer def callself textnone audiosnone srclangnone tgtlangnone 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 elif text is not none and audios is not none raise valueerror text and audios are mututally exclusive when passed to seamlessm4t specify one or another elif text is not none if tgtlang is not none self tokenizer tgtlang tgtlang if srclang is not none self tokenizer srclang srclang encoding self tokenizertext kwargs return encoding else encoding self featureextractoraudios samplingratesamplingrate kwargs return encoding 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 seamlessm4t constructs a seamlessm4t processor which wraps a seamlessm4t feature extractor and a seamlessm4t tokenizer into a single processor seamlessm4tprocessor offers all the functionalities of seamlessm4tfeatureextractor and seamlessm4ttokenizerfast see the seamlessm4tprocessor __call__ and seamlessm4tprocessor decode for more information args feature_extractor seamlessm4tfeatureextractor the audio processor is a required input tokenizer seamlessm4ttokenizerfast 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 seamlessm4ttokenizerfast s seamlessm4ttokenizerfast __call__ if text is not none to encode the text to prepare the audio s this method forwards the audios and kwrags arguments to seamlessm4tfeatureextractor s seamlessm4tfeatureextractor __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 src_lang str optional the language code of the input texts audios if not specified the last src_lang specified will be used tgt_lang str optional the code of the target language if not specified the last tgt_lang specified will be used kwargs optional remaining dictionary of keyword arguments that will be passed to the feature extractor and or the tokenizer 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 input_features audio input features to be fed to a model returned when audios is not none this method forwards all its arguments to seamlessm4ttokenizerfast s pretrainedtokenizer batch_decode please refer to the docstring of this method for more information this method forwards all its arguments to seamlessm4ttokenizerfast s pretrainedtokenizer decode please refer to the docstring of this method for more information
from ...processing_utils import ProcessorMixin


class SeamlessM4TProcessor(ProcessorMixin):
    r

    feature_extractor_class = "SeamlessM4TFeatureExtractor"
    tokenizer_class = ("SeamlessM4TTokenizer", "SeamlessM4TTokenizerFast")

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

    def __call__(self, text=None, audios=None, src_lang=None, tgt_lang=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.")
        elif text is not None and audios is not None:
            raise ValueError(
                "Text and audios are mututally exclusive when passed to `SeamlessM4T`. Specify one or another."
            )
        elif text is not None:
            if tgt_lang is not None:
                self.tokenizer.tgt_lang = tgt_lang
            if src_lang is not None:
                self.tokenizer.src_lang = src_lang
            encoding = self.tokenizer(text, **kwargs)

            return encoding

        else:
            encoding = self.feature_extractor(audios, sampling_rate=sampling_rate, **kwargs)
            return encoding

    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))