Ë
    T^(hà  ã                   óæ   — d Z ddlZddlZddlmZmZmZmZ ddlZ	ddl
mZ ddlmZmZmZmZmZ ddlmZmZmZmZmZ  e«       rddlZ ej2                  e«      Zdd	iZd
„ Zd„ Z G d„ de«      ZdgZ y)z!Tokenization class for Pop2Piano.é    N)ÚListÚOptionalÚTupleÚUnioné   )ÚBatchFeature)Ú
AddedTokenÚBatchEncodingÚPaddingStrategyÚPreTrainedTokenizerÚTruncationStrategy)Ú
TensorTypeÚis_pretty_midi_availableÚloggingÚrequires_backendsÚto_numpyÚvocabz
vocab.jsonc                 ó,   — || z  }|�t        ||«      }|S ©N)Úmin©ÚnumberÚcutoff_time_idxÚcurrent_idxs      úr/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/pop2piano/tokenization_pop2piano.pyÚtoken_time_to_noter   '   s$   € Ø�6Ñ€KØÐ"Ü˜+ Ó7ˆàÐó    c                 ó|   — ||    �1||    }||k  r%|}|j                  ||| |g«       |dk(  rd n|}||| <   |S ||| <   |S )Nr   )Úappend)	r   Úcurrent_velocityÚdefault_velocityÚnote_onsets_readyr   ÚnotesÚ	onset_idxÚ
offset_idxÚonsets_readys	            r   Útoken_note_to_noter'   /   sl   € Ø˜Ñ Ð,à% fÑ-ˆ	Ø�{Ò"à$ˆJØ�L‰L˜) Z°Ð9IÐJÔKØ#3°qÒ#8™4¸kˆLØ(4Ð˜fÑ%ð €Lð %0Ð˜&Ñ!Ø€Lr   c                   ó"  ‡ — e Zd ZdZddgZeZ	 	 	 	 	 	 d(ˆ fd„	Zed„ «       Z	d„ Z
dedefd	„Zd)defd
„Zdej                   dededefd„Z	 	 	 d*dej                   dej                   dededef
d„Z	 d+dej                   dedee   fd„Zd,dej                   dej                   defd„Zd+dedee   dee   fd„Z	 	 d-deej                   eej:                     f   dee   dee   defd„Z 	 	 d-deej                   eej:                     f   dee   dee   defd„Z!	 	 	 	 	 	 	 d.deej                   eej:                     eeej:                        f   dee"ee#f   dee"eef   dee   d ee   d!ee"   d"eeee$f      d#e"defd$„Z%	 d/d%e&d&e"fd'„Z'ˆ xZ(S )0ÚPop2PianoTokenizeraš  
    Constructs a Pop2Piano tokenizer. This tokenizer does not require training.

    This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
    this superclass for more information regarding those methods.

    Args:
        vocab (`str`):
            Path to the vocab file which contains the vocabulary.
        default_velocity (`int`, *optional*, defaults to 77):
            Determines the default velocity to be used while creating midi Notes.
        num_bars (`int`, *optional*, defaults to 2):
            Determines cutoff_time_idx in for each token.
        unk_token (`str` or `tokenizers.AddedToken`, *optional*, defaults to `"-1"`):
            The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
            token instead.
        eos_token (`str` or `tokenizers.AddedToken`, *optional*, defaults to 1):
            The end of sequence token.
        pad_token (`str` or `tokenizers.AddedToken`, *optional*, defaults to 0):
             A special token used to make arrays of tokens the same size for batching purpose. Will then be ignored by
            attention mechanisms or loss computation.
        bos_token (`str` or `tokenizers.AddedToken`, *optional*, defaults to 2):
            The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.
    Ú	token_idsÚattention_maskc                 ó2  •— t        |t        «      rt        |dd¬«      n|}t        |t        «      rt        |dd¬«      n|}t        |t        «      rt        |dd¬«      n|}t        |t        «      rt        |dd¬«      n|}|| _        || _        t        |d«      5 }	t        j                  |	«      | _        d d d «       | j                  j                  «       D �
�ci c]  \  }
}||
“Œ
 c}}
| _
        t        ‰| �0  d||||dœ|¤Ž y # 1 sw Y   ŒQxY wc c}}
w )NF)ÚlstripÚrstripÚrb)Ú	unk_tokenÚ	eos_tokenÚ	pad_tokenÚ	bos_token© )Ú
isinstanceÚstrr	   r!   Únum_barsÚopenÚjsonÚloadÚencoderÚitemsÚdecoderÚsuperÚ__init__)Úselfr   r!   r7   r0   r1   r2   r3   ÚkwargsÚfileÚkÚvÚ	__class__s               €r   r?   zPop2PianoTokenizer.__init__[   s  ø€ ô JTÐT]Ô_bÔIc”J˜y°¸uÕEÐirˆ	ÜISÐT]Ô_bÔIc”J˜y°¸uÕEÐirˆ	ÜISÐT]Ô_bÔIc”J˜y°¸uÕEÐirˆ	ÜISÐT]Ô_bÔIc”J˜y°¸uÕEÐirˆ	à 0ˆÔØ ˆŒô �%˜Óð 	+ $ÜŸ9™9 T›?ˆDŒL÷	+ð *.¯©×);Ñ);Ó)=×>¡  A˜˜1™Ó>ˆŒä‰Ñð 	
ØØØØñ		
ð
 ó	
÷	+ð 	+üó ?s   ÂDÃDÄDc                 ó,   — t        | j                  «      S )z-Returns the vocabulary size of the tokenizer.)Úlenr;   ©r@   s    r   Ú
vocab_sizezPop2PianoTokenizer.vocab_size}   s   € ô �4—<‘<Ó Ð r   c                 óB   — t        | j                  fi | j                  ¤ŽS )z(Returns the vocabulary of the tokenizer.)Údictr;   Úadded_tokens_encoderrH   s    r   Ú	get_vocabzPop2PianoTokenizer.get_vocab‚   s   € ä�D—L‘LÑ> D×$=Ñ$=Ñ>Ð>r   Útoken_idÚreturnc                 óÂ   — | j                   j                  || j                  › d�«      }|j                  d«      }dj	                  |dd «      t        |d   «      }}||gS )a?  
        Decodes the token ids generated by the transformer into notes.

        Args:
            token_id (`int`):
                This denotes the ids generated by the transformers to be converted to Midi tokens.

        Returns:
            `List`: A list consists of token_type (`str`) and value (`int`).
        Ú_TOKEN_TIMEÚ_é   Nr   )r=   Úgetr0   ÚsplitÚjoinÚint)r@   rN   Útoken_type_valueÚ
token_typeÚvalues        r   Ú_convert_id_to_tokenz'Pop2PianoTokenizer._convert_id_to_token†   si   € ð  Ÿ<™<×+Ñ+¨H¸¿¹Ð8HÈÐ6TÓUÐØ+×1Ñ1°#Ó6ÐØŸH™HÐ%5°a°bÐ%9Ó:¼CÐ@PÐQRÑ@SÓ<T�Eˆ
à˜EÐ"Ð"r   c                 ój   — | j                   j                  |› d|› �t        | j                  «      «      S )a¹  
        Encodes the Midi tokens to transformer generated token ids.

        Args:
            token (`int`):
                This denotes the token value.
            token_type (`str`):
                This denotes the type of the token. There are four types of midi tokens such as "TOKEN_TIME",
                "TOKEN_VELOCITY", "TOKEN_NOTE" and "TOKEN_SPECIAL".

        Returns:
            `int`: returns the id of the token.
        rR   )r;   rT   rW   r0   )r@   ÚtokenrY   s      r   Ú_convert_token_to_idz'Pop2PianoTokenizer._convert_token_to_id˜   s/   € ð �|‰|×Ñ 5 '¨¨:¨,Ð 7¼¸T¿^¹^Ó9LÓMÐMr   ÚtokensÚbeat_offset_idxÚbars_per_batchr   c                 óð   — d}t        t        |«      «      D ]X  }||   }|||z  dz  z   }||z   }	| j                  |||	¬«      }
t        |
«      dk(  rŒ;|€|
}Œ@t        j                  ||
fd¬«      }ŒZ |€g S |S )a  
        Converts relative tokens to notes which are then used to generate pretty midi object.

        Args:
            tokens (`numpy.ndarray`):
                Tokens to be converted to notes.
            beat_offset_idx (`int`):
                Denotes beat offset index for each note in generated Midi.
            bars_per_batch (`int`):
                A parameter to control the Midi output generation.
            cutoff_time_idx (`int`):
                Denotes the cutoff time index for each note in generated Midi.
        Né   )Ú	start_idxr   r   )Úaxis)ÚrangerG   Úrelative_tokens_ids_to_notesÚnpÚconcatenate)r@   r_   r`   ra   r   r#   ÚindexÚ_tokensÚ
_start_idxÚ_cutoff_time_idxÚ_notess              r   Ú"relative_batch_tokens_ids_to_notesz5Pop2PianoTokenizer.relative_batch_tokens_ids_to_notes¨   s©   € ð* ˆäœ3˜v›;Ó'ò 	@ˆEØ˜U‘mˆGØ(¨5°>Ñ+AÀAÑ+EÑEˆJØ.°Ñ;ÐØ×6Ñ6ØØ$Ø 0ð 7ó ˆFô �6‹{˜aÒØØ�Ø‘äŸ™¨¨v ¸QÔ?‘ð	@ð" ˆ=ØˆIØˆr   Úbeatstepc                 ój   — |€dn|}| j                  ||||¬«      }| j                  ||||   ¬«      }|S )al  
        Converts tokens to Midi. This method calls `relative_batch_tokens_ids_to_notes` method to convert batch tokens
        to notes then uses `notes_to_midi` method to convert them to Midi.

        Args:
            tokens (`numpy.ndarray`):
                Denotes tokens which alongside beatstep will be converted to Midi.
            beatstep (`np.ndarray`):
                We get beatstep from feature extractor which is also used to get Midi.
            beat_offset_idx (`int`, *optional*, defaults to 0):
                Denotes beat offset index for each note in generated Midi.
            bars_per_batch (`int`, *optional*, defaults to 2):
                A parameter to control the Midi output generation.
            cutoff_time_idx (`int`, *optional*, defaults to 12):
                Denotes the cutoff time index for each note in generated Midi.
        r   )r_   r`   ra   r   )Ú
offset_sec)ro   Únotes_to_midi)r@   r_   rp   r`   ra   r   r#   Úmidis           r   Ú!relative_batch_tokens_ids_to_midiz4Pop2PianoTokenizer.relative_batch_tokens_ids_to_midiÔ   sT   € ð0  /Ð6™!¸OˆØ×7Ñ7ØØ+Ø)Ø+ð	 8ó 
ˆð ×!Ñ! %¨¸hÀÑ>WÐ!ÓXˆØˆr   rd   c           
      ó&  — |D �cg c]  }| j                  |«      ‘Œ }}|}d}t        t        | j                  j	                  «       D �cg c]  }|j                  d«      ‘Œ c}«      dz   «      D �	cg c]  }	d‘Œ }
}	g }|D ]Y  \  }}|dk(  r|dk(  sŒ nI|dk(  rt        |||¬«      }Œ'|dk(  r|}Œ/|d	k(  rt        ||| j                  |
||¬
«      }ŒPt        d«      ‚ t        |
«      D ]J  \  }}|€Œ	|€|dz   }nt        ||dz   «      }t        ||«      }|j                  |||| j                  g«       ŒL t        |«      dk(  rg S t        j                  |«      }|dd…df   dz  |dd…df   z   }||j!                  «          }|S c c}w c c}w c c}	w )a¶  
        Converts relative tokens to notes which will then be used to create Pretty Midi objects.

        Args:
            tokens (`numpy.ndarray`):
                Relative Tokens which will be converted to notes.
            start_idx (`float`):
                A parameter which denotes the starting index.
            cutoff_time_idx (`float`, *optional*):
                A parameter used while converting tokens to notes.
        r   ÚNOTErS   NÚTOKEN_SPECIALÚ
TOKEN_TIMEr   ÚTOKEN_VELOCITYÚ
TOKEN_NOTE)r   r    r!   r"   r   r#   zToken type not understood!é€   )r[   rf   Úsumr;   ÚkeysÚendswithr   r'   r!   Ú
ValueErrorÚ	enumerateÚmaxr   rG   rh   ÚarrayÚargsort)r@   r_   rd   r   r]   Úwordsr   r    rC   Úir"   r#   rY   r   ÚpitchÚ
note_onsetÚcutoffr%   Ú
note_orders                      r   rg   z/Pop2PianoTokenizer.relative_tokens_ids_to_notesø   sÕ  € ð @FÖF°e�×*Ñ*¨5Õ1ÐFˆÐFàˆØÐÜ+0´ÐRV×R^ÑR^×RcÑRcÓReÖ5fÈQ°a·j±jÀÕ6HÒ5fÓ1gÐjkÑ1kÓ+lÖm ašTÐmÐÐmØˆØ"'ò 	?ÑˆJ˜Ø˜_Ò,Ø˜Q“;ÙØ˜|Ò+Ü0Ø!°?ÐP[ô‘ð Ð/Ò/Ø#)Ñ à˜|Ò+Ü*Ø!Ø%5Ø%)×%:Ñ%:Ø&7Ø +Øô‘ô !Ð!=Ó>Ð>ð+	?ô. "+Ð+<Ó!=ò 		UÑˆE�:àÑ%Ø"Ð*Ø'¨!™^‘Fä  °*¸q±.ÓA�Fä  ¨fÓ5�
Ø—‘˜j¨*°e¸T×=RÑ=RÐSÕTð		Uô ˆu‹:˜Š?ØˆIä—H‘H˜U“OˆEØšq !˜t™ sÑ*¨U²1°a°4©[Ñ8ˆJØ˜*×,Ñ,Ó.Ñ/ˆEØˆLùò] Gùò 6gùÒms   …FÁ	F	Á1	Fr#   rr   c                 óf  — t        | dg«       t        j                  dd¬«      }t        j                  d¬«      }g }|D ]=  \  }}}	}
t        j                  |
|	||   |z
  ||   |z
  ¬«      }|j                  |«       Œ? ||_        |j                  j                  |«       |j                  «        |S )a»  
        Converts notes to Midi.

        Args:
            notes (`numpy.ndarray`):
                This is used to create Pretty Midi objects.
            beatstep (`numpy.ndarray`):
                This is the extrapolated beatstep that we get from feature extractor.
            offset_sec (`int`, *optional*, defaults to 0.0):
                This represents the offset seconds which is used while creating each Pretty Midi Note.
        Úpretty_midii€  g      ^@)Ú
resolutionÚinitial_tempor   )Úprogram)Úvelocityr‡   ÚstartÚend)	r   rŒ   Ú
PrettyMIDIÚ
InstrumentÚNoter   r#   ÚinstrumentsÚremove_invalid_notes)r@   r#   rp   rr   Únew_pmÚnew_instÚ	new_notesr$   r%   r‡   r�   Únew_notes               r   rs   z Pop2PianoTokenizer.notes_to_midi6  s½   € ô 	˜$  Ô0ä×'Ñ'°3ÀeÔLˆÜ×)Ñ)°!Ô4ˆØˆ	à6;ò 	'Ñ2ˆI�z 5¨(Ü"×'Ñ'Ø!ØØ˜yÑ)¨JÑ6Ø˜ZÑ(¨:Ñ5ô	ˆHð ×Ñ˜XÕ&ð	'ð #ˆŒØ×Ñ×!Ñ! (Ô+Ø×#Ñ#Ô%Øˆr   Úsave_directoryÚfilename_prefixc                 ó~  — t         j                  j                  |«      st        j	                  d|› d�«       yt         j                  j                  ||r|dz   ndt        d   z   «      }t        |d«      5 }|j                  t        j                  | j                  «      «       ddd«       |fS # 1 sw Y   |fS xY w)a}  
        Saves the tokenizer's vocabulary dictionary to the provided save_directory.

        Args:
            save_directory (`str`):
                A path to the directory where to saved. It will be created if it doesn't exist.
            filename_prefix (`Optional[str]`, *optional*):
                A prefix to add to the names of the files saved by the tokenizer.
        zVocabulary path (z) should be a directoryNú-Ú r   Úw)ÚosÚpathÚisdirÚloggerÚerrorrV   ÚVOCAB_FILES_NAMESr8   Úwriter9   Údumpsr;   )r@   rœ   r�   Úout_vocab_filerB   s        r   Úsave_vocabularyz"Pop2PianoTokenizer.save_vocabularyV  sª   € ô �w‰w�}‰}˜^Ô,Ü�L‰LÐ,¨^Ð,<Ð<SÐTÔUØô Ÿ™Ÿ™Ø±o˜_¨sÒ2È2ÔQbÐcjÑQkÑkó
ˆô �. #Ó&ð 	1¨$Ø�J‰J”t—z‘z $§,¡,Ó/Ô0÷	1ð Ð Ð ÷	1ð Ð Ð ús   Á7/B1Â1B<Útruncation_strategyÚ
max_lengthc           	      óR  — t        | dg«       t        |d   t        j                  «      r_t	        j
                  |D �cg c]0  }|j                  |j                  |j                  |j                  g‘Œ2 c}«      j                  dd«      }t	        j                  |«      j                  t        j                  «      }|dd…dd…f   j                  «       }t        |dz   «      D �cg c]  }g ‘Œ }}|D ]3  \  }	}
}}||	   j!                  ||g«       ||
   j!                  |dg«       Œ5 g }d}t#        |«      D ]–  \  }}t%        |«      dk(  rŒ|j!                  | j'                  |d«      «       |D ]\  \  }}t)        |dkD  «      }||k7  r#|}|j!                  | j'                  |d	«      «       |j!                  | j'                  |d
«      «       Œ^ Œ˜ t%        |«      }|t*        j,                  k7  r$|r"||kD  r | j.                  d|||z
  |dœ|¤Ž\  }}}t1        d|i«      S c c}w c c}w )až  
        This is the `encode_plus` method for `Pop2PianoTokenizer`. It converts the midi notes to the transformer
        generated token ids. It only works on a single batch, to process multiple batches please use
        `batch_encode_plus` or `__call__` method.

        Args:
            notes (`numpy.ndarray` of shape `[sequence_length, 4]` or `list` of `pretty_midi.Note` objects):
                This represents the midi notes. If `notes` is a `numpy.ndarray`:
                    - Each sequence must have 4 values, they are `onset idx`, `offset idx`, `pitch` and `velocity`.
                If `notes` is a `list` containing `pretty_midi.Note` objects:
                    - Each sequence must have 4 attributes, they are `start`, `end`, `pitch` and `velocity`.
            truncation_strategy ([`~tokenization_utils_base.TruncationStrategy`], *optional*):
                Indicates the truncation strategy that is going to be used during truncation.
            max_length (`int`, *optional*):
                Maximum length of the returned list and optionally padding length (see above).

        Returns:
            `BatchEncoding` containing the tokens ids.
        rŒ   r   éÿÿÿÿrc   Né   rS   ry   rz   r{   )ÚidsÚnum_tokens_to_remover¬   r*   r4   )r   r5   rŒ   r•   rh   rƒ   r‘   r’   r‡   r�   ÚreshapeÚroundÚastypeÚint32r‚   rf   r   r�   rG   r^   rW   r   ÚDO_NOT_TRUNCATEÚtruncate_sequencesr
   )r@   r#   r¬   r­   rA   Ú	each_noteÚmax_time_idxr†   ÚtimesÚonsetÚoffsetr‡   r�   r_   r    ÚtimeÚ	total_lenrR   s                     r   Úencode_pluszPop2PianoTokenizer.encode_plusm  s2  € ô6 	˜$  Ô0ô �e˜A‘h¤× 0Ñ 0Ô1Ü—H‘HØhmÖnÐ[d�)—/‘/ 9§=¡=°)·/±/À9×CUÑCUÒVÒnóç‰g�b˜!‹nð ô
 —‘˜“×&Ñ&¤r§x¡xÓ0ˆØšQ   ˜U‘|×'Ñ'Ó)ˆä" L°1Ñ$4Ó6Ö7˜’Ð7ˆÐ7Ø.3ò 	-Ñ*ˆE�6˜5 (Ø�%‰L×Ñ ¨Ð 1Ô2Ø�&‰M× Ñ  %¨ Õ,ð	-ð ˆØÐÜ  Ó'ò 		N‰GˆAˆtÜ�4‹y˜AŠ~ØØ�M‰M˜$×3Ñ3°A°|ÓDÔEØ#'ò N‘��xÜ˜x¨!™|Ó,�Ø# xÒ/Ø'/Ð$Ø—M‘M $×";Ñ";¸HÐFVÓ"WÔXØ—‘˜d×7Ñ7¸¸|ÓLÕMñNð			Nô ˜“Kˆ	ð Ô"4×"DÑ"DÒDÉÐXaÐdnÒXnØ2˜4×2Ñ2ð ØØ%.°Ñ%;Ø$7ñð ñ	‰LˆF�A�qô ˜k¨6Ð2Ó3Ð3ùòI oùò 8s   ¾5HÃ%	H$c           	      ó¨   — g }t        t        |«      «      D ]-  }|j                   | j                  ||   f||dœ|¤Žd   «       Œ/ t	        d|i«      S )a†  
        This is the `batch_encode_plus` method for `Pop2PianoTokenizer`. It converts the midi notes to the transformer
        generated token ids. It works on multiple batches by calling `encode_plus` multiple times in a loop.

        Args:
            notes (`numpy.ndarray` of shape `[batch_size, sequence_length, 4]` or `list` of `pretty_midi.Note` objects):
                This represents the midi notes. If `notes` is a `numpy.ndarray`:
                    - Each sequence must have 4 values, they are `onset idx`, `offset idx`, `pitch` and `velocity`.
                If `notes` is a `list` containing `pretty_midi.Note` objects:
                    - Each sequence must have 4 attributes, they are `start`, `end`, `pitch` and `velocity`.
            truncation_strategy ([`~tokenization_utils_base.TruncationStrategy`], *optional*):
                Indicates the truncation strategy that is going to be used during truncation.
            max_length (`int`, *optional*):
                Maximum length of the returned list and optionally padding length (see above).

        Returns:
            `BatchEncoding` containing the tokens ids.
        )r¬   r­   r*   )rf   rG   r   rÀ   r
   )r@   r#   r¬   r­   rA   Úencoded_batch_token_idsr†   s          r   Úbatch_encode_plusz$Pop2PianoTokenizer.batch_encode_plus´  sy   € ð4 #%ÐÜ”s˜5“zÓ"ò 	ˆAØ#×*Ñ*Ø �× Ñ Ø˜!‘Hðà(;Ø)ñð ñ	ð
 ñõð	ô ˜kÐ+BÐCÓDÐDr   ÚpaddingÚ
truncationÚpad_to_multiple_ofÚreturn_attention_maskÚreturn_tensorsÚverbosec	           	      óR  — t        |t        j                  «      r|j                  dk(  nt        |d   t        «      }
 | j
                  d|||||dœ|	¤Ž\  }}}}	|
r|€dn|} | j                  d|||dœ|	¤Ž}n | j                  d|||dœ|	¤Ž}| j                  |||||||¬«      }|S )a¼  
        This is the `__call__` method for `Pop2PianoTokenizer`. It converts the midi notes to the transformer generated
        token ids.

        Args:
            notes (`numpy.ndarray` of shape `[batch_size, max_sequence_length, 4]` or `list` of `pretty_midi.Note` objects):
                This represents the midi notes.

                If `notes` is a `numpy.ndarray`:
                    - Each sequence must have 4 values, they are `onset idx`, `offset idx`, `pitch` and `velocity`.
                If `notes` is a `list` containing `pretty_midi.Note` objects:
                    - Each sequence must have 4 attributes, they are `start`, `end`, `pitch` and `velocity`.
            padding (`bool`, `str` or [`~file_utils.PaddingStrategy`], *optional*, defaults to `False`):
                Activates and controls padding. Accepts the following values:

                - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
                  sequence if provided).
                - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum
                  acceptable input length for the model if that argument is not provided.
                - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different
                  lengths).
            truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):
                Activates and controls truncation. Accepts the following values:

                - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or
                  to the maximum acceptable input length for the model if that argument is not provided. This will
                  truncate token by token, removing a token from the longest sequence in the pair if a pair of
                  sequences (or a batch of pairs) is provided.
                - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the
                  maximum acceptable input length for the model if that argument is not provided. This will only
                  truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
                - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the
                  maximum acceptable input length for the model if that argument is not provided. This will only
                  truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
                - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths
                  greater than the model maximum admissible input size).
            max_length (`int`, *optional*):
                Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to
                `None`, this will use the predefined model maximum length if a maximum length is required by one of the
                truncation/padding parameters. If the model has no specific maximum input length (like XLNet)
                truncation/padding to a maximum length will be deactivated.
            pad_to_multiple_of (`int`, *optional*):
                If set will pad the sequence to a multiple of the provided value. This is especially useful to enable
                the use of Tensor Cores on NVIDIA hardware with compute capability `>= 7.5` (Volta).
            return_attention_mask (`bool`, *optional*):
                Whether to return the attention mask. If left to the default, will return the attention mask according
                to the specific tokenizer's default, defined by the `return_outputs` attribute.

                [What are attention masks?](../glossary#attention-mask)
            return_tensors (`str` or [`~file_utils.TensorType`], *optional*):
                If set, will return tensors instead of list of python integers. Acceptable values are:

                - `'tf'`: Return TensorFlow `tf.constant` objects.
                - `'pt'`: Return PyTorch `torch.Tensor` objects.
                - `'np'`: Return Numpy `np.ndarray` objects.
            verbose (`bool`, *optional*, defaults to `True`):
                Whether or not to print more information and warnings.

        Returns:
            `BatchEncoding` containing the token_ids.
        r   r   )rÄ   rÅ   r­   rÆ   rÉ   T)r#   r¬   r­   )rÄ   r­   rÆ   rÇ   rÈ   rÉ   r4   )	r5   rh   ÚndarrayÚndimÚlistÚ"_get_padding_truncation_strategiesrÃ   rÀ   Úpad)r@   r#   rÄ   rÅ   r­   rÆ   rÇ   rÈ   rÉ   rA   Ú
is_batchedÚpadding_strategyr¬   r*   s                 r   Ú__call__zPop2PianoTokenizer.__call__Û  s  € ôd )3°5¼"¿*¹*Ô(E�U—Z‘Z 1’_Ì:ÐV[Ð\]ÑV^Ô`dÓKeˆ
ð ElÀD×DkÑDkð E
ØØ!Ø!Ø1ØñE
ð ñE
ÑAÐÐ-¨z¸6ñ à,AÐ,I¡DÐOdÐ!Ø.˜×.Ñ.ð ØØ$7Ø%ñð ñ	‰Ið )˜×(Ñ(ð ØØ$7Ø%ñð ñ	ˆIð —H‘HØØ$Ø!Ø1Ø"7Ø)Øð ó 
ˆ	ð Ðr   Úfeature_extractor_outputÚreturn_midic                 óN  — t        t        |d«      xr t        |d«      xr t        |d«      «      }|s |d   j                  d   dkD  rt        d«      ‚|rát	        |d   dd…df   dk(  «      |d   j                  d   k7  s%|d   j                  d   |d	   j                  d   k7  rAt        d
|j                  d   › d|d   j                  d   › d|d	   j                  d   › �«      ‚|d   j                  d   |j                  d   k7  rŠt        d|d   j                  d   › d|j                  d   › �«      ‚|d   j                  d   dk7  s|d	   j                  d   dk7  r2t        d|d   j                  d   › d|d	   j                  d   › d�«      ‚|r&t        j                  |d   dd…df   dk(  «      d   }n|j                  d   g}g }g }d}t        |«      D �]ª  \  }	}
|||
 }|dd…dt        j                  t        j                  |t        | j                  «      k(  «      d   «      dz   …f   }|d   |	   }|d	   |	   }|rx|d   |	   }|d   |	   }|dt        j                  t        j                  |dk(  «      d   «      dz    }|dt        j                  t        j                  |dk(  «      d   «      dz    }t        |«      }t        |«      }t        |«      }| j                  ||| j                  | j                  dz   dz  ¬«      }|j                  d   j                  D ]C  }|xj                   |d   z  c_        |xj"                  |d   z  c_        |j%                  |«       ŒE |j%                  |«       ||
dz   z  }�Œ­ |rt'        ||dœ«      S t'        d|i«      S )aF  
        This is the `batch_decode` method for `Pop2PianoTokenizer`. It converts the token_ids generated by the
        transformer to midi_notes and returns them.

        Args:
            token_ids (`Union[np.ndarray, torch.Tensor, tf.Tensor]`):
                Output token_ids of `Pop2PianoConditionalGeneration` model.
            feature_extractor_output (`BatchFeature`):
                Denotes the output of `Pop2PianoFeatureExtractor.__call__`. It must contain `"beatstep"` and
                `"extrapolated_beatstep"`. Also `"attention_mask_beatsteps"` and
                `"attention_mask_extrapolated_beatstep"`
                 should be present if they were returned by the feature extractor.
            return_midi (`bool`, *optional*, defaults to `True`):
                Whether to return midi object or not.
        Returns:
            If `return_midi` is True:
                - `BatchEncoding` containing both `notes` and `pretty_midi.pretty_midi.PrettyMIDI` objects.
            If `return_midi` is False:
                - `BatchEncoding` containing `notes`.
        r+   Úattention_mask_beatstepsÚ$attention_mask_extrapolated_beatstepÚ	beatstepsr   rS   z—attention_mask, attention_mask_beatsteps and attention_mask_extrapolated_beatstep must be present for batched inputs! But one of them were not present.NÚextrapolated_beatstepzbLength mistamtch between token_ids, beatsteps and extrapolated_beatstep! Found token_ids length - z, beatsteps shape - z$ and extrapolated_beatsteps shape - z!Found attention_mask of length - z but token_ids of length - zœLength mistamtch of beatsteps and extrapolated_beatstep! Since attention_mask is not present the number of examples must be 1, But found beatsteps length - z", extrapolated_beatsteps length - ú.rc   )r_   rp   ra   r   )r#   Úpretty_midi_objectsr#   )ÚboolÚhasattrÚshaper€   r}   rh   Úwherer�   r‚   rW   r1   r   ru   r7   r–   r#   r‘   r’   r   r
   )r@   r*   rÓ   rÔ   Úattention_masks_presentÚ	batch_idxÚ
notes_listÚpretty_midi_objects_listrd   rj   Úend_idxÚeach_tokens_idsrØ   rÙ   rÖ   r×   Úpretty_midi_objectÚnotes                     r   Úbatch_decodezPop2PianoTokenizer.batch_decodeW  sÓ  € ô8 #'ÜÐ,Ð.>Ó?ò ZÜÐ0Ð2LÓMòZäÐ0Ð2XÓYó#
Ðñ 'Ð+CÀKÑ+P×+VÑ+VÐWXÑ+YÐ\]Ò+]ÜðHóð ñ #ô Ð,Ð-=Ñ>ºqÀ!¸tÑDÈÑIÓJØ+¨KÑ8×>Ñ>¸qÑAòBà+¨KÑ8×>Ñ>¸qÑAØ+Ð,CÑD×JÑJÈ1ÑMòNô !ð*Ø*3¯/©/¸!Ñ*<Ð)=Ð=QÐRjÐkvÑRw×R}ÑR}Ð~ñ  SAð  RBð B:Ø:RÐSjÑ:k×:qÑ:qÐrsÑ:tÐ9uðwóð ð
 (Ð(8Ñ9×?Ñ?ÀÑBÀiÇoÁoÐVWÑFXÒXÜ Ø7Ð8PÐQaÑ8b×8hÑ8hÐijÑ8kÐ7lð  mHð  IR÷  IXñ  IXð  YZñ  I[ð  H\ð  ]óð ð )¨Ñ5×;Ñ;¸AÑ>À!ÒCØ+Ð,CÑD×JÑJÈ1ÑMÐQRÒRä ð4Ø4LÈ[Ñ4Y×4_Ñ4_Ð`aÑ4bÐ3cð  dFð  G_ð  `wñ  Gx÷  G~ñ  G~ð  @ñ  GAð  FBð  BCðDóð ñ
 #äŸ™Ð!9Ð:JÑ!KÊAÈqÈDÑ!QÐUVÑ!VÓWÐXYÑZ‰Ià"Ÿ™¨Ñ+Ð,ˆIàˆ
Ø#%Ð Øˆ	Ü'¨	Ó2ó #	%‰NˆE�7Ø'¨	°'Ð:ˆOà-ªaÐ1r´2·6±6¼"¿(¹(À?ÔVYÐZ^×ZhÑZhÓViÑCiÓ:jÐklÑ:mÓ3nÐqrÑ3rÐ1rÐ.rÑsˆOØ0°Ñ=¸eÑDˆIØ$<Ð=TÑ$UÐV[Ñ$\Ð!ñ 'Ø+CÐD^Ñ+_Ð`eÑ+fÐ(Ø7OØ:ñ8àñ8Ð4ð &Ð&^¬¯©¬r¯x©xÐ8PÐTUÑ8UÓ/VÐWXÑ/YÓ(ZÐ]^Ñ(^Ð_�	Ø(=ØX”b—f‘fœRŸX™XÐ&JÈaÑ&OÓPÐQRÑSÓTÐWXÑXð)Ð%ô ' Ó7ˆOÜ  Ó+ˆIÜ$,Ð-BÓ$CÐ!à!%×!GÑ!GØ&Ø.Ø#Ÿ}™}Ø!%§¡°Ñ!2°aÑ 7ð	 "Hó "Ðð +×6Ñ6°qÑ9×?Ñ?ò (�Ø—
’
˜i¨™lÑ*•
Ø—’˜I a™LÑ(•Ø×!Ñ! $Õ'ð(ð
 %×+Ñ+Ð,>Ô?Ø˜ 1™Ñ$ŠIðG#	%ñJ Ü ¨:ÐNfÑ!gÓhÐhä˜g zÐ2Ó3Ð3r   )éM   r°   z-1Ú1Ú0Ú2)ry   )r   r°   é   r   )g        )NN)FNNNNNT)T))Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesr§   Úvocab_files_namesr?   ÚpropertyrI   rM   rW   rÍ   r[   r^   rh   rË   ro   ru   Úfloatr   rg   rs   r6   r   r«   r   r   rŒ   r•   r   r
   rÀ   rÃ   rÜ   r   r   rÒ   r   rè   Ú__classcell__)rE   s   @r   r)   r)   >   s  ø„ ñð2 %Ð&6Ð7ÐØ)Ðð
 ØØØØØõ 
ðD ñ!ó ð!ò?ð#¨Sð #°Tó #ñ$NÀcó Nð *à—
‘
ð*ð ð*ð ð	*ð
 ó*ð`  !ØØ!ñ à—
‘
ð ð —*‘*ð ð ð	 ð
 ð ð ó ðJ X\ñ<Ø—j‘jð<Ø-2ð<ØEMÈeÁ_ó<ñ| 2§:¡:ð ¸¿¹ð ÐQTó ñ@!¨cð !ÀHÈSÁMð !Ð]bÐcfÑ]gó !ð4 =AØ$(ñ	E4à�R—Z‘Z  k×&6Ñ&6Ñ!7Ð7Ñ8ðE4ð &Ð&8Ñ9ðE4ð ˜S‘Mð	E4ð 
óE4ðT =AØ$(ñ	%Eà�R—Z‘Z  k×&6Ñ&6Ñ!7Ð7Ñ8ð%Eð &Ð&8Ñ9ð%Eð ˜S‘Mð	%Eð 
ó%Eð\ 6;Ø;?Ø$(Ø,0Ø04Ø;?ØñzàØ�J‰JØ�×!Ñ!Ñ"Ø��k×&Ñ&Ñ'Ñ(ð*ñ
ðzð �t˜S /Ð1Ñ2ðzð ˜$ Ð%7Ð7Ñ8ðzð ˜S‘Mðzð % S™Mðzð  (¨™~ðzð !  s¨J Ñ!7Ñ8ðzð ðzð 
ózð@ !ñ	w4ð #/ðw4ð ÷	w4r   r)   )!rñ   r9   r¢   Útypingr   r   r   r   Únumpyrh   Úfeature_extraction_utilsr   Útokenization_utilsr	   r
   r   r   r   Úutilsr   r   r   r   r   rŒ   Ú
get_loggerrî   r¥   r§   r   r'   r)   Ú__all__r4   r   r   ú<module>rþ      sx   ðñ (ã Û 	ß /Ó /ã å 4ß uÕ uß _Õ _ñ ÔÛà	ˆ×	Ñ	˜HÓ	%€ð ˆ\ðÐ ò
òôP
4Ð,ô P
4ðf  Ð
 �r   