ó
    ‰Ž¡jS+  ã                   óú   • S SK r S SKJr  S SKrS SKrSSKJrJrJ	r	J
r
Jr  SSKJrJr  \" 5       (       a  SSKJr  \R"                  " \5      rS\S	\S
\R,                  4S jr\" \" SS95       " S S\5      5       rg)é    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                 ó|  • U nSnSnSSSSUSUSUS	S
SS/n [         R                  " U[         R                  [         R                  S9nUR                  U 5      nUS   n[        R                  " U[        R                  5      n	U	R                  S   S:X  a  [	        S5      eU	$ ! [         a    [	        S5      ef = f)z7
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
             Ú¹C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\transformers/pipelines/audio_classification.pyÚffmpeg_readr+      sÙ   € ð ˆ?€BØ	€BØ#ÐàØØØØ
ØØ
ØØØØØØð€Nð cÜ#×)Ò)¨.Ä
ÇÁÔXb×XgÑXgÑhˆð #×.Ñ.¨xÓ8€MØ˜aÑ €Iä�MŠM˜)¤R§Z¡ZÓ0€EØ‡{�{�1�~˜ÓÜÐ.Ó/Ð/Ø€Løô ó cÜÐaÓbÐbðcús   ˜2B% Â%B;T)Úhas_feature_extractorc            	       ó®   ^ • \ rS rSrSrSrSrSrSrU 4S jr	S\
R                  \-  \-  \-  S\S\\\\4      4U 4S	 jjrSS
 jrS rS rSS jrSrU =r$ )ÚAudioClassificationPipelineéB   aÄ  
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                 óŠ   >• SU;   a  US   c  S US'   OSU;  a  SUS'   [         TU ]  " U0 UD6  U R                  [        5        g )NÚtop_ké   )ÚsuperÚ__init__Úcheck_model_typer   )ÚselfÚargsÚkwargsÚ	__class__s      €r*   r4   Ú$AudioClassificationPipeline.__init__c   sO   ø€ à�fÓ ¨¡Ñ!8Ø"ˆF�7ŠOØ˜FÓ"ØˆF�7‰OÜ‰Ò˜$Ð) &Ò)à×ÑÔJÕKó    Úinputsr8   r   c                 ó&   >• [         TU ]  " U40 UD6$ )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.
)r3   Ú__call__)r6   r<   r8   r9   s      €r*   r>   Ú$AudioClassificationPipeline.__call__m   s   ø€ ôD ‰wÒ Ñ1¨&Ñ1Ð1r;   c                 ó0  • 0 nUc$  U R                   R                  R                  US'   OGXR                   R                  R                  :”  a   U R                   R                  R                  nXS'   Ub  US;  a  [        SU S35      eX$S'   OSUS'   0 0 U4$ )Nr1   )ÚsoftmaxÚsigmoidÚnonez'Invalid value for `function_to_apply`: z2. Valid options are ['softmax', 'sigmoid', 'none']Úfunction_to_applyrA   )ÚmodelÚconfigÚ
num_labelsr   )r6   r1   rD   r8   Úpostprocess_paramss        r*   Ú_sanitize_parametersÚ0AudioClassificationPipeline._sanitize_parameters‘   sµ   € ØÐð ‰=Ø*.¯*©*×*;Ñ*;×*FÑ*FÐ˜wÒ'à—z‘z×(Ñ(×3Ñ3Ó3ØŸ
™
×)Ñ)×4Ñ4�Ø*/˜wÑ'àÑ(Ø Ð(FÓFÜ Ø=Ð>OÐ=Pð QGð Góð ð 7HÐ2Ò3à6?ÐÐ2Ñ3Ø�2Ð)Ð)Ð)r;   c                 ó’  • [        U[        5      (       aq  UR                  S5      (       d  UR                  S5      (       a   [        R                  " USS9R
                  nO%[        US5       nUR                  5       nS S S 5        [        U[        5      (       a  [        XR                  R                  5      n[        5       (       a<  SS Kn[        XR                  5      (       a  UR                  5       R!                  5       n[#        5       (       aV  SS KnSS Kn[        XR&                  R(                  5      (       a*  UR+                  5       nUR,                  nXeR.                  S.n[        U[0        5      (       Ga  UR3                  5       nSU;   a  S	U;   d  S
U;   d  [5        S5      eUR7                  S	S 5      nUc$  UR7                  SS 5        UR7                  S
S 5      nUR7                  S5      nUnX€R                  R                  :w  aŠ  SS Kn[9        5       (       a  SSKJn	  O[?        S5      eU	RA                  [        U[B        RD                  5      (       a  URG                  U5      OUUU R                  R                  5      R!                  5       n[        U[B        RD                  5      (       d  [I        S5      e[K        URL                  5      S:w  a  [5        S5      eU R                  XR                  R                  SS9n
U RN                  b  U
RQ                  U RN                  S9n
U
$ ! , (       d  f       GN¿= f)Nzhttp://zhttps://T)Úfollow_redirectsÚrbr   )Úarrayr   r   ÚrawrN   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   Ú
torchaudiorQ   ÚImportErrorÚresampler   ÚndarrayÚ
from_numpyÚ	TypeErrorÚlenr!   rT   Úto)r6   r<   Úfr_   rc   Ú_audio_samplesÚ_arrayÚ_inputsÚin_sampling_rateÚFÚ	processeds              r*   Ú
preprocessÚ&AudioClassificationPipeline.preprocess§   s§  € Ü�fœc×"Ñ"Ø× Ñ  ×+Ñ+¨v×/@Ñ/@À×/LÑ/Lô Ÿš 6¸DÑA×IÑI‘ä˜& $Ô'¨1ØŸV™V›X�F÷ (ô �fœe×$Ñ$Ü  ×)?Ñ)?×)MÑ)MÓNˆFä×ÑÛä˜&§,¡,×/Ñ/ØŸ™›×+Ñ+Ó-�ä"×$Ñ$ÛÛä˜&×"5Ñ"5×"BÑ"B×CÑCØ!'×!7Ñ!7Ó!9�Ø'×,Ñ,�Ø#)×<VÑ<VÑW�ä�fœd×#Ò#Ø—[‘[“]ˆFð $ vÓ-°5¸F³?ÀgÐQWÓFWÜ ðNóð ð —j‘j ¨Ó-ˆGØ‰à—
‘
˜6 4Ô(Ø Ÿ*™* W¨dÓ3�Ø%Ÿz™z¨/Ó:ÐØˆFØ×#9Ñ#9×#GÑ#GÓGÛä*×,Ñ,Þ:ä%ðeóð ð
 Ÿ™Ü0:¸6Ä2Ç:Á:×0NÑ0N�E×$Ñ$ VÔ,ÐTZØ$Ø×*Ñ*×8Ñ8ó÷ ‘%“'ð	 ô ˜&¤"§*¡*×-Ñ-ÜÐPÓQÐQÜˆv�|‰|Ó Ó!ÜÐeÓfÐfà×*Ñ*Ø×"8Ñ"8×"FÑ"FÐW[ð +ð 
ˆ	ð �:‰:Ñ!Ø!Ÿ™¨4¯:©:˜Ð6ˆIØÐ÷E (Ö'ús   Á.L7Ì7
Mc                 ó*   • U R                   " S0 UD6nU$ )N© )rE   )r6   Úmodel_inputsÚmodel_outputss      r*   Ú_forwardÚ$AudioClassificationPipeline._forwardò   s   € ØŸ
š
Ñ2 \Ñ2ˆØÐr;   c                 ó¬  • US:X  a  UR                   S   R                  S5      nO3US:X  a  UR                   S   R                  5       nOUR                   S   nUR                  U5      u  pVUR	                  5       nUR	                  5       n[        XV5       VVs/ s H*  u  pxXpR                  R                  R                  U   S.PM,     n	nnU	$ s  snnf )NrA   r   éÿÿÿÿrB   )ÚscoreÚlabel)	ÚlogitsrA   rB   ÚtopkÚtolistÚziprE   rF   Úid2label)
r6   r€   r1   rD   ÚprobsÚscoresÚidsr…   Ú_idÚlabelss
             r*   ÚpostprocessÚ'AudioClassificationPipeline.postprocessö   s¿   € Ø 	Ó)Ø!×(Ñ(¨Ñ+×3Ñ3°BÓ7‰EØ )Ó+Ø!×(Ñ(¨Ñ+×3Ñ3Ó5‰Eà!×(Ñ(¨Ñ+ˆEØ—j‘j Ó'‰ˆà—‘“ˆØ�j‰j‹lˆä_bÐciÔ_oÔpÒ_oÑQ[ÐQV˜E¯J©J×,=Ñ,=×,FÑ,FÀsÑ,KÔLÑ_oˆÑpàˆùó qs   Â1Cr~   )NN)r2   rA   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr4   r   ro   r]   rV   ri   r   Úlistr>   rI   r{   r�   r‘   Ú__static_attributes__Ú__classcell__)r9   s   @r*   r.   r.   B   s‚   ø† ñð2 €OØ!ÐØ"ÐØ€OõLð"2˜rŸz™z¨EÑ1°CÑ7¸$Ñ>ð "2È#ð "2ÐRVÐW[Ð\_ÐadÐ\dÑWeÑRf÷ "2ôH*ò,IòV÷ò r;   r.   )r   Útypingr   rX   rb   r   Úutilsr   r   r   r   r	   Úbaser   r   Úmodels.auto.modeling_autor   Ú
get_loggerr“   Úloggerr]   Úintro   r+   r.   r~   r;   r*   Ú<module>r¦      sƒ   ðó Ý ã Û ç uÕ uß 4ñ ×ÑÝXà	×	Ò	˜HÓ	%€ð!˜%ð !°ð !¸¿
¹
ô !ñH Ñ,À4ÑHÓIôA (ó Aó JñAr;   