a
    þdà  ã                   @   sP   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 G d	d
„ d
eƒZ
dS )z%
Speech processor class for Wav2Vec2
é    N)Úcontextmanageré   )ÚProcessorMixiné   )ÚWav2Vec2FeatureExtractor)ÚWav2Vec2CTCTokenizerc                       sd   e Zd ZdZdZdZ‡ fdd„Ze‡ fdd„ƒZdd	„ Z	d
d„ Z
dd„ Zdd„ Zedd„ ƒZ‡  ZS )ÚWav2Vec2ProcessoraŸ  
    Constructs a Wav2Vec2 processor which wraps a Wav2Vec2 feature extractor and a Wav2Vec2 CTC tokenizer into a single
    processor.

    [`Wav2Vec2Processor`] offers all the functionalities of [`Wav2Vec2FeatureExtractor`] and [`PreTrainedTokenizer`].
    See the docstring of [`~Wav2Vec2Processor.__call__`] and [`~Wav2Vec2Processor.decode`] for more information.

    Args:
        feature_extractor (`Wav2Vec2FeatureExtractor`):
            An instance of [`Wav2Vec2FeatureExtractor`]. The feature extractor is a required input.
        tokenizer ([`PreTrainedTokenizer`]):
            An instance of [`PreTrainedTokenizer`]. The tokenizer is a required input.
    r   ZAutoTokenizerc                    s    t ƒ  ||¡ | j| _d| _d S )NF)ÚsuperÚ__init__Úfeature_extractorÚcurrent_processorÚ_in_target_context_manager)Úselfr   Ú	tokenizer©Ú	__class__© úy/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/transformers/models/wav2vec2/processing_wav2vec2.pyr
   +   s    zWav2Vec2Processor.__init__c                    st   zt ƒ j|fi |¤ŽW S  tyn   t d| j› dt¡ tj|fi |¤Ž}tj|fi |¤Ž}| ||d Y S 0 d S )NzLoading a tokenizer inside a   from a config that does not include a `tokenizer_class` attribute is deprecated and will be removed in v5. Please add `'tokenizer_class': 'Wav2Vec2CTCTokenizer'` attribute to either your `config.json` or `tokenizer_config.json` file to suppress this warning: )r   r   )	r	   Úfrom_pretrainedÚOSErrorÚwarningsÚwarnÚ__name__ÚFutureWarningr   r   )ÚclsZpretrained_model_name_or_pathÚkwargsr   r   r   r   r   r   0   s    ú	z!Wav2Vec2Processor.from_pretrainedc                 O   sú   | j r| j|i |¤ŽS d|v r4t d¡ | d¡}n| dd¡}| dd¡}| dd¡}t|ƒdkrx|d }|dd… }|du r|du rtd	ƒ‚|dur´| j|g|¢R d|i|¤Ž}|durÎ| j|fi |¤Ž}|du rÚ|S |du ræ|S |d
 |d< |S dS )a¹  
        When used in normal mode, this method forwards all its arguments to Wav2Vec2FeatureExtractor's
        [`~Wav2Vec2FeatureExtractor.__call__`] and returns its output. If used in the context
        [`~Wav2Vec2Processor.as_target_processor`] this method forwards all its arguments to PreTrainedTokenizer's
        [`~PreTrainedTokenizer.__call__`]. Please refer to the docstring of the above two methods for more information.
        Z
raw_speechzLUsing `raw_speech` as a keyword argument is deprecated. Use `audio` instead.ÚaudioNÚsampling_rateÚtextr   r   zAYou need to specify either an `audio` or `text` input to process.Ú	input_idsÚlabels)	r   r   r   r   ÚpopÚlenÚ
ValueErrorr   r   )r   Úargsr   r   r   r   ÚinputsÚ	encodingsr   r   r   Ú__call__C   s.    
zWav2Vec2Processor.__call__c                 O   sº   | j r| jj|i |¤ŽS | dd¡}| dd¡}t|ƒdkrP|d }|dd… }|durr| jj|g|¢R i |¤Ž}|durŽ| jj|fi |¤Ž}|du rš|S |du r¦|S |d |d< |S dS )a¯  
        When used in normal mode, this method forwards all its arguments to Wav2Vec2FeatureExtractor's
        [`~Wav2Vec2FeatureExtractor.pad`] and returns its output. If used in the context
        [`~Wav2Vec2Processor.as_target_processor`] this method forwards all its arguments to PreTrainedTokenizer's
        [`~PreTrainedTokenizer.pad`]. Please refer to the docstring of the above two methods for more information.
        Úinput_featuresNr    r   r   r   )r   r   Úpadr!   r"   r   r   )r   r$   r   r(   r    r   r   r   r)   i   s"    zWav2Vec2Processor.padc                 O   s   | j j|i |¤ŽS )zÃ
        This method forwards all its arguments to PreTrainedTokenizer's [`~PreTrainedTokenizer.batch_decode`]. Please
        refer to the docstring of this method for more information.
        )r   Úbatch_decode©r   r$   r   r   r   r   r*   ‡   s    zWav2Vec2Processor.batch_decodec                 O   s   | j j|i |¤ŽS )z½
        This method forwards all its arguments to PreTrainedTokenizer's [`~PreTrainedTokenizer.decode`]. Please refer
        to the docstring of this method for more information.
        )r   Údecoder+   r   r   r   r,   Ž   s    zWav2Vec2Processor.decodec                 c   s0   t  d¡ d| _| j| _dV  | j| _d| _dS )zŒ
        Temporarily sets the tokenizer for processing the input. Useful for encoding the labels when fine-tuning
        Wav2Vec2.
        zî`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your labels by using the argument `text` of the regular `__call__` method (either in the same call as your audio inputs, or in a separate call.TNF)r   r   r   r   r   r   )r   r   r   r   Úas_target_processor•   s    ÿz%Wav2Vec2Processor.as_target_processor)r   Ú
__module__Ú__qualname__Ú__doc__Zfeature_extractor_classZtokenizer_classr
   Úclassmethodr   r'   r)   r*   r,   r   r-   Ú__classcell__r   r   r   r   r      s   &r   )r0   r   Ú
contextlibr   Zprocessing_utilsr   Zfeature_extraction_wav2vec2r   Ztokenization_wav2vec2r   r   r   r   r   r   Ú<module>   s   