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 speech processor class for speecht5 from processingutils import processormixin class speecht5processorprocessormixin r constructs a speecht5 processor which wraps a feature extractor and a tokenizer into a single processor speecht5processor offers all the functionalities of speecht5featureextractor and speecht5tokenizer see the docstring of speecht5processor call and speecht5processor decode for more information args featureextractor speecht5featureextractor an instance of speecht5featureextractor the feature extractor is a required input tokenizer speecht5tokenizer an instance of speecht5tokenizer the tokenizer is a required input processes audio and text input as well as audio and text targets you can process audio by using the argument audio or process audio targets by using the argument audiotarget this forwards the arguments to speecht5featureextractor s speecht5featureextractor call you can process text by using the argument text or process text labels by using the argument texttarget this forwards the arguments to speecht5tokenizer s speecht5tokenizer call valid input combinations are text only audio only texttarget only audiotarget only text and audiotarget audio and audiotarget text and texttarget audio and texttarget please refer to the docstring of the above two methods for more information collates the audio and text inputs as well as their targets into a padded batch audio inputs are padded by speecht5featureextractor s speecht5featureextractor pad text inputs are padded by speecht5tokenizer s speecht5tokenizer pad valid input combinations are inputids only inputvalues only labels only either logmel spectrograms or text tokens inputids and logmel spectrogram labels inputvalues and text labels please refer to the docstring of the above two methods for more information this method forwards all its arguments to speecht5tokenizer s speecht5tokenizer batchdecode please refer to the docstring of this method for more information this method forwards all its arguments to speecht5tokenizer s speecht5tokenizer decode please refer to the docstring of this method for more information 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 speech processor class for speecht5 constructs a speecht5 processor which wraps a feature extractor and a tokenizer into a single processor speecht5processor offers all the functionalities of speecht5featureextractor and speecht5tokenizer see the docstring of speecht5processor __call__ and speecht5processor decode for more information args feature_extractor speecht5featureextractor an instance of speecht5featureextractor the feature extractor is a required input tokenizer speecht5tokenizer an instance of speecht5tokenizer the tokenizer is a required input processes audio and text input as well as audio and text targets you can process audio by using the argument audio or process audio targets by using the argument audio_target this forwards the arguments to speecht5featureextractor s speecht5featureextractor __call__ you can process text by using the argument text or process text labels by using the argument text_target this forwards the arguments to speecht5tokenizer s speecht5tokenizer __call__ valid input combinations are text only audio only text_target only audio_target only text and audio_target audio and audio_target text and text_target audio and text_target please refer to the docstring of the above two methods for more information collates the audio and text inputs as well as their targets into a padded batch audio inputs are padded by speecht5featureextractor s speecht5featureextractor pad text inputs are padded by speecht5tokenizer s speecht5tokenizer pad valid input combinations are input_ids only input_values only labels only either log mel spectrograms or text tokens input_ids and log mel spectrogram labels input_values and text labels please refer to the docstring of the above two methods for more information this method forwards all its arguments to speecht5tokenizer s speecht5tokenizer batch_decode please refer to the docstring of this method for more information this method forwards all its arguments to speecht5tokenizer s speecht5tokenizer decode please refer to the docstring of this method for more information
from ...processing_utils import ProcessorMixin


class SpeechT5Processor(ProcessorMixin):
    r

    feature_extractor_class = "SpeechT5FeatureExtractor"
    tokenizer_class = "SpeechT5Tokenizer"

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

    def __call__(self, *args, **kwargs):
        
        audio = kwargs.pop("audio", None)
        text = kwargs.pop("text", None)
        text_target = kwargs.pop("text_target", None)
        audio_target = kwargs.pop("audio_target", None)
        sampling_rate = kwargs.pop("sampling_rate", None)

        if audio is not None and text is not None:
            raise ValueError(
                "Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?"
            )
        if audio_target is not None and text_target is not None:
            raise ValueError(
                "Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?"
            )
        if audio is None and audio_target is None and text is None and text_target is None:
            raise ValueError(
                "You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process."
            )

        if audio is not None:
            inputs = self.feature_extractor(audio, *args, sampling_rate=sampling_rate, **kwargs)
        elif text is not None:
            inputs = self.tokenizer(text, **kwargs)
        else:
            inputs = None

        if audio_target is not None:
            targets = self.feature_extractor(audio_target=audio_target, *args, sampling_rate=sampling_rate, **kwargs)
            labels = targets["input_values"]
        elif text_target is not None:
            targets = self.tokenizer(text_target, **kwargs)
            labels = targets["input_ids"]
        else:
            targets = None

        if inputs is None:
            return targets

        if targets is not None:
            inputs["labels"] = labels

            decoder_attention_mask = targets.get("attention_mask")
            if decoder_attention_mask is not None:
                inputs["decoder_attention_mask"] = decoder_attention_mask

        return inputs

    def pad(self, *args, **kwargs):
        
        input_values = kwargs.pop("input_values", None)
        input_ids = kwargs.pop("input_ids", None)
        labels = kwargs.pop("labels", None)

        if input_values is not None and input_ids is not None:
            raise ValueError("Cannot process both `input_values` and `input_ids` inputs.")
        if input_values is None and input_ids is None and labels is None:
            raise ValueError(
                "You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded."
            )

        if input_values is not None:
            inputs = self.feature_extractor.pad(input_values, *args, **kwargs)
        elif input_ids is not None:
            inputs = self.tokenizer.pad(input_ids, **kwargs)
        else:
            inputs = None

        if labels is not None:
            if "input_ids" in labels or (isinstance(labels, list) and "input_ids" in labels[0]):
                targets = self.tokenizer.pad(labels, **kwargs)
                labels = targets["input_ids"]
            else:
                feature_size_hack = self.feature_extractor.feature_size
                self.feature_extractor.feature_size = self.feature_extractor.num_mel_bins
                targets = self.feature_extractor.pad(labels, *args, **kwargs)
                self.feature_extractor.feature_size = feature_size_hack
                labels = targets["input_values"]
        else:
            targets = None

        if inputs is None:
            return targets

        if targets is not None:
            inputs["labels"] = labels

            decoder_attention_mask = targets.get("attention_mask")
            if decoder_attention_mask is not None:
                inputs["decoder_attention_mask"] = decoder_attention_mask

        return inputs

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

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