§
    ‚ŠtjS+  ã                   ó  — d dl Z d dlmZ d dlZd dlZddlmZmZm	Z	m
Z
mZ ddlmZmZ  e¦   «         rddlmZ  ej        e¦  «        Zded	ed
ej        fd„Z e ed¬¦  «        ¦  «         G d„ de¦  «        ¦   «         ZdS )é    N)ÚAnyé   )Úadd_end_docstringsÚis_torch_availableÚis_torchaudio_availableÚis_torchcodec_availableÚloggingé   )ÚPipelineÚbuild_pipeline_init_args)Ú,MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMESÚbpayloadÚsampling_rateÚreturnc                 ó~  — |› }d}d}dddd|d|d|d	d
ddg}	 t          j        |t           j        t           j        ¬¦  «        }n# t          $ r t	          d¦  «        ‚w xY w|                     | ¦  «        }|d         }t          j        |t          j        ¦  «        }	|	j	        d         dk    rt	          d¦  «        ‚|	S )z?
    Helper function to read an audio file through ffmpeg.
    Ú1Úf32leÚffmpegz-izpipe:0z-acz-arz-fz-hide_bannerz	-loglevelÚquietzpipe:1)ÚstdinÚstdoutzFffmpeg was not found but is required to load audio files from filenamer   zMalformed soundfile)
Ú
subprocessÚPopenÚPIPEÚFileNotFoundErrorÚ
ValueErrorÚcommunicateÚnpÚ
frombufferÚfloat32Úshape)
r   r   ÚarÚacÚformat_for_conversionÚffmpeg_commandÚffmpeg_processÚoutput_streamÚ	out_bytesÚaudios
             úi/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/pipelines/audio_classification.pyÚffmpeg_readr+      sí   € ð Ð	€BØ	€BØ#ÐàØØØØ
ØØ
ØØØØØØð€Nð cÝ#Ô)¨.Å
ÄÕXbÔXgÐhÑhÔhˆˆøÝð cð cð cÝÐaÑbÔbÐbðcøøøà"×.Ò.¨xÑ8Ô8€MØ˜aÔ €IåŒM˜)¥R¤ZÑ0Ô0€EØ„{�1„~˜ÒÐÝÐ.Ñ/Ô/Ð/Ø€Ls   ˜+A ÁAT)Úhas_feature_extractorc            	       óž   ‡ — e Zd ZdZdZdZdZdZˆ fd„Zde	j
        ez  ez  ez  dedeeeef                  fˆ fd„Zdd
„Zd„ Zd„ Zdd„Zˆ xZS )ÚAudioClassificationPipelinea  
    Audio classification pipeline using any `AutoModelForAudioClassification`. This pipeline predicts the class of a
    raw waveform or an audio file. In case of an audio file, ffmpeg should be installed to support multiple audio
    formats.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> classifier = pipeline(model="superb/wav2vec2-base-superb-ks")
    >>> classifier("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/1.flac")
    [{'score': 0.997, 'label': '_unknown_'}, {'score': 0.002, 'label': 'left'}, {'score': 0.0, 'label': 'yes'}, {'score': 0.0, 'label': 'down'}, {'score': 0.0, 'label': 'stop'}]
    ```

    Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial)


    This pipeline can currently be loaded from [`pipeline`] using the following task identifier:
    `"audio-classification"`.

    See the list of available models on
    [huggingface.co/models](https://huggingface.co/models?filter=audio-classification).
    FTc                 ó¤   •— d|v r|d         €d |d<   n	d|vrd|d<    t          ¦   «         j        |i |¤Ž |                      t          ¦  «         d S )NÚtop_ké   )ÚsuperÚ__init__Úcheck_model_typer   )ÚselfÚargsÚkwargsÚ	__class__s      €r*   r3   z$AudioClassificationPipeline.__init__c   sm   ø€ à�fÐÐ ¨¤Ð!8Ø"ˆF�7‰OˆOØ˜FÐ"Ð"ØˆF�7‰OØ�‰ŒÔ˜$Ð) &Ð)Ð)Ð)à×ÒÕJÑKÔKÐKÐKÐKó    Úinputsr7   r   c                 ó8   •—  t          ¦   «         j        |fi |¤ŽS )a©  
        Classify the sequence(s) given as inputs. See the [`AutomaticSpeechRecognitionPipeline`] documentation for more
        information.

        Args:
            inputs (`np.ndarray` or `bytes` or `str` or `dict`):
                The inputs is either :
                    - `str` that is the filename of the audio file, the file will be read at the correct sampling rate
                      to get the waveform using *ffmpeg*. This requires *ffmpeg* to be installed on the system.
                    - `bytes` it is supposed to be the content of an audio file and is interpreted by *ffmpeg* in the
                      same way.
                    - (`np.ndarray` of shape (n, ) of type `np.float32` or `np.float64`)
                        Raw audio at the correct sampling rate (no further check will be done)
                    - `dict` form can be used to pass raw audio sampled at arbitrary `sampling_rate` and let this
                      pipeline do the resampling. The dict must be either be in the format `{"sampling_rate": int,
                      "raw": np.array}`, or `{"sampling_rate": int, "array": np.array}`, where the key `"raw"` or
                      `"array"` is used to denote the raw audio waveform.
            top_k (`int`, *optional*, defaults to None):
                The number of top labels that will be returned by the pipeline. If the provided number is `None` or
                higher than the number of labels available in the model configuration, it will default to the number of
                labels.
            function_to_apply (`str`, *optional*, defaults to "softmax"):
                The function to apply to the model output. By default, the pipeline will apply the softmax function to
                the output of the model. Valid options: ["softmax", "sigmoid", "none"]. Note that passing Python's
                built-in `None` will default to "softmax", so you need to pass the string "none" to disable any
                post-processing.

        Return:
            A list of `dict` with the following keys:

            - **label** (`str`) -- The label predicted.
            - **score** (`float`) -- The corresponding probability.
        )r2   Ú__call__)r5   r:   r7   r8   s      €r*   r<   z$AudioClassificationPipeline.__call__m   s%   ø€ ðD  �u‰wŒwÔ Ð1Ð1¨&Ð1Ð1Ð1r9   Nc                 óÜ   — i }|€| j         j        j        |d<   n+|| j         j        j        k    r| j         j        j        }||d<   |�|dvrt          d|› d�¦  «        ‚||d<   nd|d<   i i |fS )Nr0   )ÚsoftmaxÚsigmoidÚnonez'Invalid value for `function_to_apply`: z2. Valid options are ['softmax', 'sigmoid', 'none']Úfunction_to_applyr>   )ÚmodelÚconfigÚ
num_labelsr   )r5   r0   rA   r7   Úpostprocess_paramss        r*   Ú_sanitize_parametersz0AudioClassificationPipeline._sanitize_parameters‘   s»   € ØÐð ˆ=Ø*.¬*Ô*;Ô*FÐ˜wÑ'Ð'à�t”zÔ(Ô3Ò3Ð3Øœ
Ô)Ô4�Ø*/Ð˜wÑ'àÐ(Ø Ð(FÐFÐFÝ ðGÐ>Oð Gð Gð Gñô ð ð 7HÐÐ2Ñ3Ð3à6?ÐÐ2Ñ3Ø�2Ð)Ð)Ð)r9   c                 ór  — t          |t          ¦  «        r‚|                     d¦  «        s|                     d¦  «        rt          j        |d¬¦  «        j        }n<t          |d¦  «        5 }|                     ¦   «         }d d d ¦  «         n# 1 swxY w Y   t          |t          ¦  «        rt          || j
        j        ¦  «        }t          ¦   «         r?dd l}t          ||j        ¦  «        r&|                     ¦   «                              ¦   «         }t#          ¦   «         rGdd l}dd l}t          ||j        j        ¦  «        r%|                     ¦   «         }|j        }||j        dœ}t          |t0          ¦  «        �r$|                     ¦   «         }d|v rd	|v sd
|v st5          d¦  «        ‚|                     d	d ¦  «        }|€,|                     dd ¦  «         |                     d
d ¦  «        }|                     d¦  «        }|}|| j
        j        k    rŠdd l}t9          ¦   «         rddlm}	 nt?          d¦  «        ‚|	                      t          |tB          j"        ¦  «        r| #                    |¦  «        n||| j
        j        ¦  «                             ¦   «         }t          |tB          j"        ¦  «        stI          d¦  «        ‚tK          |j&        ¦  «        dk    rt5          d¦  «        ‚|  
                    || j
        j        d¬¦  «        }
| j'        �|
 (                    | j'        ¬¦  «        }
|
S )Nzhttp://zhttps://T)Úfollow_redirectsÚrbr   )Úarrayr   r   ÚrawrJ   zôWhen passing a dictionary to AudioClassificationPipeline, the dict needs to contain a "raw" key containing the numpy array or torch tensor representing the audio and a "sampling_rate" key, containing the sampling_rate associated with that arrayÚpath)Ú
functionalz›torchaudio is required to resample audio samples in AudioClassificationPipeline. The torchaudio package can be installed through: `pip install torchaudio`.z2We expect a numpy ndarray or torch tensor as inputr
   zFWe expect a single channel audio input for AudioClassificationPipelineÚpt)r   Úreturn_tensors)Údtype))Ú
isinstanceÚstrÚ
startswithÚhttpxÚgetÚcontentÚopenÚreadÚbytesr+   Úfeature_extractorr   r   ÚtorchÚTensorÚcpuÚnumpyr   Ú
torchcodecÚdecodersÚAudioDecoderÚget_all_samplesÚdataÚsample_rateÚdictÚcopyr   Úpopr   Ú
torchaudiorM   ÚImportErrorÚresampler   ÚndarrayÚ
from_numpyÚ	TypeErrorÚlenr!   rP   Úto)r5   r:   Úfr[   r_   Ú_audio_samplesÚ_arrayÚ_inputsÚin_sampling_rateÚFÚ	processeds              r*   Ú
preprocessz&AudioClassificationPipeline.preprocess§   s�  € Ý�f�cÑ"Ô"ð 	&Ø× Ò  Ñ+Ô+ð &¨v×/@Ò/@ÀÑ/LÔ/Lð &õ œ 6¸DÐAÑAÔAÔI��å˜& $Ñ'Ô'ð &¨1ØŸVšV™XœX�Fð&ð &ð &ñ &ô &ð &ð &ð &ð &ð &ð &øøøð &ð &ð &ð &õ �f�eÑ$Ô$ð 	OÝ  ¨Ô)?Ô)MÑNÔNˆFåÑÔð 	.ØˆLˆLˆLå˜& %¤,Ñ/Ô/ð .ØŸš™œ×+Ò+Ñ-Ô-�å"Ñ$Ô$ð 	XØˆLˆLˆLØÐÐÐå˜& *Ô"5Ô"BÑCÔCð XØ!'×!7Ò!7Ñ!9Ô!9�Ø'Ô,�Ø#)¸NÔ<VÐWÐW�å�f�dÑ#Ô#ñ !	Ø—[’[‘]”]ˆFð $ vÐ-Ð-°5¸F°?°?ÀgÐQWÐFWÐFWÝ ðNñô ð ð —j’j ¨Ñ-Ô-ˆGØˆà—
’
˜6 4Ñ(Ô(Ð(Ø Ÿ*š* W¨dÑ3Ô3�Ø%Ÿzšz¨/Ñ:Ô:ÐØˆFØ 4Ô#9Ô#GÒGÐGØ���å*Ñ,Ô,ð Ø:Ð:Ð:Ð:Ð:Ð:Ð:å%ðeñô ð ð
 ŸšÝ0:¸6Å2Ä:Ñ0NÔ0NÐZ�E×$Ò$ VÑ,Ô,Ð,ÐTZØ$ØÔ*Ô8ñô ÷ ’%‘'”'ð	 õ ˜&¥"¤*Ñ-Ô-ð 	RÝÐPÑQÔQÐQÝˆvŒ|ÑÔ Ò!Ð!ÝÐeÑfÔfÐfà×*Ò*Ø $Ô"8Ô"FÐW[ð +ñ 
ô 
ˆ	ð Œ:Ð!Ø!Ÿš¨4¬:˜Ñ6Ô6ˆIØÐs   Á,BÂBÂBc                 ó    —  | j         di |¤Ž}|S )N© )rB   )r5   Úmodel_inputsÚmodel_outputss      r*   Ú_forwardz$AudioClassificationPipeline._forwardò   s   € Ø"˜œ
Ð2Ð2 \Ð2Ð2ˆØÐr9   r1   r>   c                 ót  ‡ — |dk    r!|j         d                              d¦  «        }n3|dk    r |j         d                              ¦   «         }n|j         d         }|                     |¦  «        \  }}|                     ¦   «         }|                     ¦   «         }ˆ fd„t          ||¦  «        D ¦   «         }|S )Nr>   r   éÿÿÿÿr?   c                 óJ   •— g | ]\  }}|‰j         j        j        |         d œ‘Œ S ))ÚscoreÚlabel)rB   rC   Úid2label)Ú.0r€   Ú_idr5   s      €r*   ú
<listcomp>z;AudioClassificationPipeline.postprocess.<locals>.<listcomp>  s5   ø€ ÐpÐpÐpÑQ[ÐQVÐX[˜E¨D¬JÔ,=Ô,FÀsÔ,KÐLÐLÐpÐpÐpr9   )Úlogitsr>   r?   ÚtopkÚtolistÚzip)r5   r{   r0   rA   ÚprobsÚscoresÚidsÚlabelss   `       r*   Úpostprocessz'AudioClassificationPipeline.postprocessö   s¸   ø€ Ø 	Ò)Ð)Ø!Ô(¨Ô+×3Ò3°BÑ7Ô7ˆEˆEØ )Ò+Ð+Ø!Ô(¨Ô+×3Ò3Ñ5Ô5ˆEˆEà!Ô(¨Ô+ˆEØ—j’j Ñ'Ô'‰ˆ�à—’‘”ˆØ�jŠj‰lŒlˆàpÐpÐpÐpÕ_bÐciÐknÑ_oÔ_oÐpÑpÔpˆàˆr9   )NN)r1   r>   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr3   r   rk   rY   rR   re   r   Úlistr<   rF   rw   r|   rŽ   Ú__classcell__)r8   s   @r*   r.   r.   B   s  ø€ € € € € ðð ð2 €OØ!ÐØ"ÐØ€OðLð Lð Lð Lð Lð"2˜rœz¨EÑ1°CÑ7¸$Ñ>ð "2È#ð "2ÐRVÐW[Ð\_ÐadÐ\dÔWeÔRfð "2ð "2ð "2ð "2ð "2ð "2ðH*ð *ð *ð *ð,Ið Ið IðVð ð ðð ð ð ð ð ð ð r9   r.   )r   Útypingr   rT   r^   r   Úutilsr   r   r   r   r	   Úbaser   r   Úmodels.auto.modeling_autor   Ú
get_loggerr�   ÚloggerrY   Úintrk   r+   r.   ry   r9   r*   ú<module>r       sF  ðð Ð Ð Ð Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð à uÐ uÐ uÐ uÐ uÐ uÐ uÐ uÐ uÐ uÐ uÐ uÐ uÐ uØ 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4ð ÐÑÔð YØXÐXÐXÐXÐXÐXà	ˆÔ	˜HÑ	%Ô	%€ð!˜%ð !°ð !¸¼
ð !ð !ð !ð !ðH ÐÐ,Ð,À4ÐHÑHÔHÑIÔIðAð Að Að Að A (ñ Aô Añ JÔIðAð Að Ar9   