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 processor class for pop2piano import os from typing import list optional union import numpy as np from featureextractionutils import batchfeature from processingutils import processormixin from tokenizationutils import batchencoding paddingstrategy truncationstrategy from utils import tensortype class pop2pianoprocessorprocessormixin r constructs an pop2piano processor which wraps a pop2piano feature extractor and pop2piano tokenizer into a single processor pop2pianoprocessor offers all the functionalities of pop2pianofeatureextractor and pop2pianotokenizer see the docstring of pop2pianoprocessor call and pop2pianoprocessor decode for more information args featureextractor pop2pianofeatureextractor an instance of pop2pianofeatureextractor the feature extractor is a required input tokenizer pop2pianotokenizer an instance of pop2pianotokenizer the tokenizer is a required input this method uses pop2pianofeatureextractor call method to prepare logmelspectrograms for the model and pop2pianotokenizer call to prepare tokenids from notes please refer to the docstring of the above two methods for more information since feature extractor needs both audio and samplingrate and tokenizer needs both tokenids and featureextractoroutput we must check for both this method uses pop2pianotokenizer batchdecode method to convert model generated tokenids to midinotes please refer to the docstring of the above two methods 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 processor class for pop2piano constructs an pop2piano processor which wraps a pop2piano feature extractor and pop2piano tokenizer into a single processor pop2pianoprocessor offers all the functionalities of pop2pianofeatureextractor and pop2pianotokenizer see the docstring of pop2pianoprocessor __call__ and pop2pianoprocessor decode for more information args feature_extractor pop2pianofeatureextractor an instance of pop2pianofeatureextractor the feature extractor is a required input tokenizer pop2pianotokenizer an instance of pop2pianotokenizer the tokenizer is a required input this method uses pop2pianofeatureextractor __call__ method to prepare log mel spectrograms for the model and pop2pianotokenizer __call__ to prepare token_ids from notes please refer to the docstring of the above two methods for more information since feature extractor needs both audio and sampling_rate and tokenizer needs both token_ids and feature_extractor_output we must check for both this method uses pop2pianotokenizer batch_decode method to convert model generated token_ids to midi_notes please refer to the docstring of the above two methods for more information
import os
from typing import List, Optional, Union

import numpy as np

from ...feature_extraction_utils import BatchFeature
from ...processing_utils import ProcessorMixin
from ...tokenization_utils import BatchEncoding, PaddingStrategy, TruncationStrategy
from ...utils import TensorType


class Pop2PianoProcessor(ProcessorMixin):
    r

    attributes = ["feature_extractor", "tokenizer"]
    feature_extractor_class = "Pop2PianoFeatureExtractor"
    tokenizer_class = "Pop2PianoTokenizer"

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

    def __call__(
        self,
        audio: Union[np.ndarray, List[float], List[np.ndarray]] = None,
        sampling_rate: Union[int, List[int]] = None,
        steps_per_beat: int = 2,
        resample: Optional[bool] = True,
        notes: Union[List, TensorType] = None,
        padding: Union[bool, str, PaddingStrategy] = False,
        truncation: Union[bool, str, TruncationStrategy] = None,
        max_length: Optional[int] = None,
        pad_to_multiple_of: Optional[int] = None,
        verbose: bool = True,
        **kwargs,
    ) -> Union[BatchFeature, BatchEncoding]:
        

        
        
        if (audio is None and sampling_rate is None) and (notes is None):
            raise ValueError(
                "You have to specify at least audios and sampling_rate in order to use feature extractor or "
                "notes to use the tokenizer part."
            )

        if audio is not None and sampling_rate is not None:
            inputs = self.feature_extractor(
                audio=audio,
                sampling_rate=sampling_rate,
                steps_per_beat=steps_per_beat,
                resample=resample,
                **kwargs,
            )
        if notes is not None:
            encoded_token_ids = self.tokenizer(
                notes=notes,
                padding=padding,
                truncation=truncation,
                max_length=max_length,
                pad_to_multiple_of=pad_to_multiple_of,
                verbose=verbose,
                **kwargs,
            )

        if notes is None:
            return inputs

        elif audio is None or sampling_rate is None:
            return encoded_token_ids

        else:
            inputs["token_ids"] = encoded_token_ids["token_ids"]
            return inputs

    def batch_decode(
        self,
        token_ids,
        feature_extractor_output: BatchFeature,
        return_midi: bool = True,
    ) -> BatchEncoding:
        

        return self.tokenizer.batch_decode(
            token_ids=token_ids, feature_extractor_output=feature_extractor_output, return_midi=return_midi
        )

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

    def save_pretrained(self, save_directory, **kwargs):
        if os.path.isfile(save_directory):
            raise ValueError(f"Provided path ({save_directory}) should be a directory, not a file")
        os.makedirs(save_directory, exist_ok=True)
        return super().save_pretrained(save_directory, **kwargs)

    @classmethod
    def from_pretrained(cls, pretrained_model_name_or_path, **kwargs):
        args = cls._get_arguments_from_pretrained(pretrained_model_name_or_path, **kwargs)
        return cls(*args)