Ë
    ÜÓ^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y)é    )Ú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        ‰| �  di |¤Ž y )N© )ÚsuperÚ__init__)ÚselfÚkwargsÚ	__class__s     €ú€/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/pipelines/zero_shot_audio_classification.pyr   z,ZeroShotAudioClassificationPipeline.__init__C   s   ø€ Ü‰ÑÑ"˜6Ó"ó    Úaudiosr   Úreturnc                 ó$   •— t        ‰| �  |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   ø€ ô. ‰wÑ Ñ1¨&Ñ1Ð1r   c                 ó@   — 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_   sI   € ØÐØ Ñ'Ø4:Ð;MÑ4NÐÐ0Ñ1Ø  FÑ*Ø7=Ð>SÑ7TÐÐ3Ñ4à  " bÐ(Ð(r   c                 ó  — t        |t        «      ri|j                  d«      s|j                  d«      r"t        j                  |d¬«      j
                  }n%t        |d«      5 }|j                  «       }d d d «       t        |t        «      r t        || j                  j                  «      }t        |t        j                  «      st        d«      ‚t        |j                   «      dk7  rt#        d«      ‚| j                  |g| j                  j                  d	¬
«      }|j%                  | j&                  «      }||d<   |D �cg c]  }|j)                  |«      ‘Œ }}| j+                  |d	d¬«      }|g|d<   |S # 1 sw Y   �ŒxY wc c}w )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   Ú
preprocessz.ZeroShotAudioClassificationPipeline.preprocessh   sY  € Ü�eœSÔ!Ø×Ñ 	Ô*¨e×.>Ñ.>¸zÔ.Jô Ÿ	™	 %¸$Ô?×GÑG‘ä˜% Ó&ð %¨!ØŸF™F›H�E÷%ô �eœUÔ#Ü  t×'=Ñ'=×'KÑ'KÓLˆEä˜%¤§¡Ô,ÜÐ@ÓAÐAÜˆu�{‰{Ó˜qÒ ÜÐmÓnÐnà×'Ñ'ØˆG 4×#9Ñ#9×#GÑ#GÐX\ð (ó 
ˆð —‘˜4Ÿ:™:Ó&ˆØ%5ˆÐ!Ñ"Ø<LÖM°qÐ(×/Ñ/°Õ2ÐMˆ	ÐMØ—n‘n Y¸tÈT�nÓRˆØ!, ˆˆ}ÑØˆ÷'%ñ %üò  Ns   Á!E5Ä?FÅ5E?c                 óÒ   — |j                  d«      }|j                  d«      }t        |d   t        «      r|d   }n|d   d   } | j                  di |¤|¤Ž}||j                  dœ}|S )Nr   r)   r   )r   Úlogitsr   )Úpopr*   r   ÚmodelÚlogits_per_audio)r   Úmodel_inputsr   r)   ÚoutputsÚmodel_outputss         r   Ú_forwardz,ZeroShotAudioClassificationPipeline._forward„   s€   € Ø'×+Ñ+Ð,>Ó?ÐØ"×&Ñ& }Ó5ˆÜ�k !‘n¤hÔ/Ø% a™.‰Kð & a™.¨Ñ+ˆKà�$—*‘*Ñ;˜{Ð;¨lÑ;ˆð !1Ø×.Ñ.ñ
ˆð Ðr   c                 óâ   — |j                  d«      }|d   d   }|j                  d¬«      }|j                  «       }t        t	        ||«      d„ ¬«      D ��cg c]
  \  }}||dœ‘Œ }}}|S c c}}w )Nr   rE   r   )Údimc                 ó   — | d    S )Nr   r   )rA   s    r   ú<lambda>zAZeroShotAudioClassificationPipeline.postprocess.<locals>.<lambda>ž   s   € Ð_`ÐabÑ_cÐ^c€ r   )Úkey)ÚscoreÚlabel)rF   ÚsoftmaxÚtolistÚsortedÚzip)	r   rK   r   rE   ÚprobsÚscoresrR   Úcandidate_labelÚresults	            r   Úpostprocessz/ZeroShotAudioClassificationPipeline.postprocess•   s‚   € Ø(×,Ñ,Ð-?Ó@ÐØ˜xÑ(¨Ñ+ˆà—‘ 1�Ó%ˆØ—‘“ˆô +1´°VÐ=MÓ1NÑTcÔ*d÷
á&��ð  oÓ6ð
ˆñ 
ð ˆùó	
s   ÁA+)NzThis is a sound of {}.)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr   r4   r5   r2   r+   Údictr   Úlistr   r!   rC   rL   r\   Ú__classcell__)r   s   @r   r   r      sy   ø„ ñð: €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   r-   Únumpyr4   Úutilsr   r   Úaudio_classificationr	   Úbaser
   r   Ú
get_loggerr]   Úloggerr   r   r   r   ú<module>rp      sa   ðõ !Ý ã Û ÷õ .ß 4ð 
ˆ×	Ñ	˜HÓ	%€ñ Ñ,À4ÐW[Ô\Ó]ô@¨(ó @ó ^ñ@r   