§
    9²¸j   ã                   óÎ   — d dl mZ d dlmZ d dlZd dlZddlmZm	Z	 ddl
mZ ddlmZmZ  e	j        e¦  «        Z e ed	d	¬
¦  «        ¦  «         G d„ de¦  «        ¦   «         ZdS )é    )ÚUserDict)ÚAnyNé   )Úadd_end_docstringsÚloggingé   )Úffmpeg_read)ÚPipelineÚbuild_pipeline_init_argsT)Úhas_feature_extractorÚhas_tokenizerc            	       óœ   ‡ — 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	„ Zdd„Zd„ Zd„ Zˆ xZS )Ú#ZeroShotAudioClassificationPipelineaä  
    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          ¦   «         j        di |¤Ž d S )N© )ÚsuperÚ__init__)ÚselfÚkwargsÚ	__class__s     €ú…/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/pipelines/zero_shot_audio_classification.pyr   z,ZeroShotAudioClassificationPipeline.__init__C   s&   ø€ Ø�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ð"Ð"ó    Úaudiosr   Úreturnc                 ó8   •—  t          ¦   «         j        |fi |¤ŽS )a  
        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   z,ZeroShotAudioClassificationPipeline.__call__F   s$   ø€ ð.  �u‰wŒwÔ Ð1Ð1¨&Ð1Ð1Ð1r   c                 óL   — i }d|v r|d         |d<   d|v r|d         |d<   |i i fS )NÚcandidate_labelsÚhypothesis_templater   )r   r   Úpreprocess_paramss      r   Ú_sanitize_parametersz8ZeroShotAudioClassificationPipeline._sanitize_parameters_   sO   € ØÐØ Ð'Ð'Ø4:Ð;MÔ4NÐÐ0Ñ1Ø  FÐ*Ð*Ø7=Ð>SÔ7TÐÐ3Ñ4à  " bÐ(Ð(r   NúThis is a sound of {}.c                 ó  ‡— 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          |t          j        ¦  «        st          d¦  «        ‚t          |j        ¦  «        dk    rt#          d¦  «        ‚|  
                    |g| j
        j        d	¬
¦  «        }|                     | j        ¦  «        }||d<   ˆfd„|D ¦   «         }|                      |d	d¬¦  «        }|g|d<   |S )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   c                 ó:   •— g | ]}‰                      |¦  «        ‘ŒS r   )Úformat)Ú.0Úxr   s     €r   ú
<listcomp>zBZeroShotAudioClassificationPipeline.preprocess.<locals>.<listcomp>   s(   ø€ ÐMÐMÐM°qÐ(×/Ò/°Ñ2Ô2ÐMÐMÐMr   )r(   ÚpaddingÚtext_inputs)Ú
isinstanceÚstrÚ
startswithÚhttpxÚgetÚcontentÚopenÚreadÚbytesr	   Úfeature_extractorr'   ÚnpÚndarrayÚ	TypeErrorÚlenÚshapeÚ
ValueErrorÚtoÚdtypeÚ	tokenizer)r   Úaudior   r   ÚfÚinputsÚ	sequencesr/   s      `    r   Ú
preprocessz.ZeroShotAudioClassificationPipeline.preprocessh   sÈ  ø€ Ý�e�SÑ!Ô!ð 	%Ø×Ò 	Ñ*Ô*ð %¨e×.>Ò.>¸zÑ.JÔ.Jð %õ œ	 %¸$Ð?Ñ?Ô?ÔG��å˜% Ñ&Ô&ð %¨!ØŸFšF™HœH�Eð%ð %ð %ñ %ô %ð %ð %ð %ð %ð %ð %øøøð %ð %ð %ð %õ �e�UÑ#Ô#ð 	MÝ  tÔ'=Ô'KÑLÔLˆEå˜%¥¤Ñ,Ô,ð 	BÝÐ@ÑAÔAÐAÝˆuŒ{ÑÔ˜qÒ Ð ÝÐmÑnÔnÐnà×'Ò'ØˆG 4Ô#9Ô#GÐX\ð (ñ 
ô 
ˆð —’˜4œ:Ñ&Ô&ˆØ%5ˆÐ!Ñ"ØMÐMÐMÐMÐ<LÐMÑMÔMˆ	Ø—n’n Y¸tÈT�nÑRÔRˆØ!, ˆˆ}ÑØˆs   Á-BÂBÂBc                 óð   — |                      d¦  «        }|                      d¦  «        }t          |d         t          ¦  «        r	|d         }n|d         d         } | j        di |¤|¤Ž}||j        dœ}|S )Nr   r/   r   )r   Úlogitsr   )Úpopr0   r   ÚmodelÚlogits_per_audio)r   Úmodel_inputsr   r/   ÚoutputsÚmodel_outputss         r   Ú_forwardz,ZeroShotAudioClassificationPipeline._forward„   s–   € Ø'×+Ò+Ð,>Ñ?Ô?ÐØ"×&Ò& }Ñ5Ô5ˆÝ�k !”n¥hÑ/Ô/ð 	,Ø% aœ.ˆKˆKð & aœ.¨Ô+ˆKà�$”*Ð;Ð;˜{Ð;¨lÐ;Ð;ˆð !1ØÔ.ð
ð 
ˆð Ðr   c                 óô   — |                      d¦  «        }|d         d         }|                     d¬¦  «        }|                     ¦   «         }d„ t          t	          ||¦  «        d„ ¬¦  «        D ¦   «         }|S )Nr   rI   r   )Údimc                 ó   — g | ]
\  }}||d œ‘ŒS ))ÚscoreÚlabelr   )r+   rT   Úcandidate_labels      r   r-   zCZeroShotAudioClassificationPipeline.postprocess.<locals>.<listcomp>œ   s4   € ð 
ð 
ð 
á&��ð  oÐ6Ð6ð
ð 
ð 
r   c                 ó   — | d          S )Nr   r   )r,   s    r   ú<lambda>zAZeroShotAudioClassificationPipeline.postprocess.<locals>.<lambda>ž   s   € Ð_`ÐabÔ_cÐ^c€ r   )Úkey)rJ   ÚsoftmaxÚtolistÚsortedÚzip)r   rO   r   rI   ÚprobsÚscoresÚresults          r   Úpostprocessz/ZeroShotAudioClassificationPipeline.postprocess•   s‡   € Ø(×,Ò,Ð-?Ñ@Ô@ÐØ˜xÔ(¨Ô+ˆà—’ 1�Ñ%Ô%ˆØ—’‘”ˆð
ð 
å*0µ°VÐ=MÑ1NÔ1NÐTcÐTcÐ*dÑ*dÔ*dð
ñ 
ô 
ˆð ˆr   )Nr"   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr   r:   r;   r8   r1   Údictr   Úlistr   r!   rG   rP   ra   Ú__classcell__)r   s   @r   r   r      sò   ø€ € € € € ðð ð: €OØ!ÐØ"ÐØ€Oð#ð #ð #ð #ð #ð2˜rœz¨EÑ1°CÑ7¸$Ñ>ð 2È#ð 2ÐRVÐW[Ð\_ÐadÐ\dÔWeÔRfð 2ð 2ð 2ð 2ð 2ð 2ð2)ð )ð )ðð ð ð ð8ð ð ð"ð ð ð ð ð ð r   r   )Úcollectionsr   Útypingr   r3   Únumpyr:   Úutilsr   r   Úaudio_classificationr	   Úbaser
   r   Ú
get_loggerrb   Úloggerr   r   r   r   ú<module>ru      s  ðð !Ð  Ð  Ð  Ð  Ð  Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð ðð ð ð ð ð ð ð ð .Ð -Ð -Ð -Ð -Ð -Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4ð 
ˆÔ	˜HÑ	%Ô	%€ð ÐÐ,Ð,À4ÐW[Ð\Ñ\Ô\Ñ]Ô]ð@ð @ð @ð @ð @¨(ñ @ô @ñ ^Ô]ð@ð @ð @r   