ó
    ‰Ž¡j   ã                   ó²   • S SK Jr  S SKJr  S SKrS SKrSSKJrJ	r	  SSK
Jr  SSKJrJr  \	R                  " \5      r\" \" S	S	S
95       " S S\5      5       rg)é    )ÚUserDict)ÚAnyNé   )Úadd_end_docstringsÚloggingé   )Úffmpeg_read)ÚPipelineÚbuild_pipeline_init_argsT)Úhas_feature_extractorÚhas_tokenizerc            	       óª   ^ • \ 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
 rSS jrS rS rSrU =r$ )Ú#ZeroShotAudioClassificationPipelineé   a”  
Zero shot audio classification pipeline using `ClapModel`. This pipeline predicts the class of an audio when you
provide an audio and a set of `candidate_labels`.

<Tip warning={true}>

The default `hypothesis_template` is : `"This is a sound of {}."`. Make sure you update it for your usage.

</Tip>

Example:
```python
>>> from transformers import pipeline
>>> from datasets import load_dataset

>>> dataset = load_dataset("ashraq/esc50")
>>> audio = next(iter(dataset["train"]["audio"]))["array"]
>>> classifier = pipeline(task="zero-shot-audio-classification", model="laion/clap-htsat-unfused")
>>> classifier(audio, candidate_labels=["Sound of a dog", "Sound of vacuum cleaner"])
[{'score': 0.9996, 'label': 'Sound of a dog'}, {'score': 0.0004, 'label': 'Sound of vacuum cleaner'}]
```


Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial). This audio
classification pipeline can currently be loaded from [`pipeline`] using the following task identifier:
`"zero-shot-audio-classification"`. See the list of available models on
[huggingface.co/models](https://huggingface.co/models?filter=zero-shot-audio-classification).
FTc                 ó&   >• [         TU ]  " S0 UD6  g )N© )ÚsuperÚ__init__)ÚselfÚkwargsÚ	__class__s     €ÚÃC:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\transformers/pipelines/zero_shot_audio_classification.pyr   Ú,ZeroShotAudioClassificationPipeline.__init__C   s   ø€ Ü‰ÒÑ"˜6Ó"ó    Úaudiosr   Úreturnc                 ó&   >• [         TU ]  " U40 UD6$ )ah  
Assign labels to the audio(s) passed as inputs.

Args:
    audios (`str`, `list[str]`, `np.array` or `list[np.array]`):
        The pipeline handles three types of inputs:
        - A string containing a http link pointing to an audio
        - A string containing a local path to an audio
        - An audio loaded in numpy
    candidate_labels (`list[str]`):
        The candidate labels for this audio. They will be formatted using *hypothesis_template*.
    hypothesis_template (`str`, *optional*, defaults to `"This is a sound of {}"`):
        The format used in conjunction with *candidate_labels* to attempt the audio classification by
        replacing the placeholder with the candidate_labels. Pass "{}" if *candidate_labels* are
        already formatted.
Return:
    A list of dictionaries containing one entry per proposed label. Each dictionary contains the
    following keys:
    - **label** (`str`) -- One of the suggested *candidate_labels*.
    - **score** (`float`) -- The score attributed by the model to that label. It is a value between
        0 and 1, computed as the `softmax` of `logits_per_audio`.
)r   Ú__call__)r   r   r   r   s      €r   r   Ú,ZeroShotAudioClassificationPipeline.__call__F   s   ø€ ô. ‰wÒ Ñ1¨&Ñ1Ð1r   c                 óH   • 0 nSU;   a  US   US'   SU;   a  US   US'   U0 0 4$ )NÚcandidate_labelsÚhypothesis_templater   )r   r   Úpreprocess_paramss      r   Ú_sanitize_parametersÚ8ZeroShotAudioClassificationPipeline._sanitize_parameters_   sI   € ØÐØ Ó'Ø4:Ð;MÑ4NÐÐ0Ñ1Ø  FÓ*Ø7=Ð>SÑ7TÐÐ3Ñ4à  " bÐ(Ð(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[        U[        R                  5      (       d  [        S5      e[        UR                   5      S:w  a  [#        S5      eU R                  U/U R                  R                  S	S
9nUR%                  U R&                  5      nX%S'   U Vs/ s H  ocR)                  U5      PM     nnU R+                  US	SS9nU/US'   U$ ! , (       d  f       GN= fs  snf )Nzhttp://zhttps://T)Úfollow_redirectsÚrbz"We expect a numpy ndarray as inputr   zNWe expect a single channel audio input for ZeroShotAudioClassificationPipelineÚpt)Úsampling_rateÚreturn_tensorsr!   )r+   ÚpaddingÚtext_inputs)Ú
isinstanceÚstrÚ
startswithÚhttpxÚgetÚcontentÚopenÚreadÚbytesr	   Úfeature_extractorr*   ÚnpÚndarrayÚ	TypeErrorÚlenÚshapeÚ
ValueErrorÚtoÚdtypeÚformatÚ	tokenizer)	r   Úaudior!   r"   ÚfÚinputsÚxÚ	sequencesr-   s	            r   Ú
preprocessÚ.ZeroShotAudioClassificationPipeline.preprocessh   s]  € Ü�eœS×!Ñ!Ø×Ñ 	×*Ñ*¨e×.>Ñ.>¸z×.JÑ.Jô Ÿ	š	 %¸$Ñ?×GÑG‘ä˜% Ô&¨!ØŸF™F›H�E÷ 'ô �eœU×#Ñ#Ü ×'=Ñ'=×'KÑ'KÓLˆEä˜%¤§¡×,Ñ,ÜÐ@ÓAÐAÜˆu�{‰{Ó˜qÓ ÜÐmÓnÐnà×'Ñ'ØˆG 4×#9Ñ#9×#GÑ#GÐX\ð (ð 
ˆð —‘˜4Ÿ:™:Ó&ˆØ%5Ð!Ñ"Ù<LÓMÒ<L°q×/Ñ/°Ö2Ñ<Lˆ	ÐMØ—n‘n Y¸tÈT�nÐRˆØ!, ˆˆ}ÑØˆ÷' 'Ö&üò  Ns   Á.FÅFÆ
Fc                 óÜ   • UR                  S5      nUR                  S5      n[        US   [        5      (       a  US   nOUS   S   nU R                  " S0 UDUD6nUUR                  S.nU$ )Nr!   r-   r   )r!   Úlogitsr   )Úpopr.   r   ÚmodelÚlogits_per_audio)r   Úmodel_inputsr!   r-   ÚoutputsÚmodel_outputss         r   Ú_forwardÚ,ZeroShotAudioClassificationPipeline._forward„   s�   € Ø'×+Ñ+Ð,>Ó?ÐØ"×&Ñ& }Ó5ˆÜ�k !‘n¤h×/Ñ/Ø% a™.‰Kð & a™.¨Ñ+ˆKà—*’*Ñ;˜{Ð;¨lÑ;ˆð !1Ø×.Ñ.ñ
ˆð Ðr   c                 óÖ   • UR                  S5      nUS   S   nUR                  SS9nUR                  5       n[        [	        XR5      S S9 VVs/ s H	  u  pgXgS.PM     nnnU$ s  snnf )Nr!   rJ   r   )Údimc                 ó   • U S   * $ )Nr   r   )rE   s    r   Ú<lambda>ÚAZeroShotAudioClassificationPipeline.postprocess.<locals>.<lambda>ž   s   € Ð_`ÐabÑ_cÑ^cr   )Úkey)ÚscoreÚlabel)rK   ÚsoftmaxÚtolistÚsortedÚzip)	r   rP   r!   rJ   ÚprobsÚscoresrY   Úcandidate_labelÚresults	            r   ÚpostprocessÚ/ZeroShotAudioClassificationPipeline.postprocess•   s   € Ø(×,Ñ,Ð-?Ó@ÐØ˜xÑ(¨Ñ+ˆà—‘ 1�Ð%ˆØ—‘“ˆô +1´°VÓ1NÑTcÒ*dô
â*dÑ&�ð Ô6Ù*dð 	ñ 
ð ˆùó	
s   ÁA%r   )NzThis is a sound of {}.)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr   r8   r9   r6   r/   Údictr   Úlistr   r$   rG   rQ   rc   Ú__static_attributes__Ú__classcell__)r   s   @r   r   r      s~   ø† ñð: €OØ!ÐØ"ÐØ€Oõ#ð2˜rŸz™z¨EÑ1°CÑ7¸$Ñ>ð 2È#ð 2ÐRVÐW[Ð\_ÐadÐ\dÑWeÑRf÷ 2ò2)ôò8÷"ð r   r   )Úcollectionsr   Útypingr   r1   Únumpyr8   Úutilsr   r   Úaudio_classificationr	   Úbaser
   r   Ú
get_loggerre   Úloggerr   r   r   r   Ú<module>rz      s_   ðõ !Ý ã Û ÷õ .ß 4ð 
×	Ò	˜HÓ	%€ñ Ñ,À4ÐW[Ñ\Ó]ô@¨(ó @ó ^ñ@r   