
    fTh(                         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
  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)Union   )add_end_docstringsis_torch_availableis_torchaudio_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/var/www/auris/envauris/lib/python3.13/site-packages/transformers/pipelines/audio_classification.pyffmpeg_readr*      s     ?B	B#

N c#)).
XbXgXgh #..x8Ma IMM)RZZ0E{{1~.//L  cabbcs   2B% %B;T)has_feature_extractorc                      ^  \ rS rSrSrU 4S jrS\\R                  \	\
4   4U 4S jjrSS jrS rS rSS	 jrS
rU =r$ )AudioClassificationPipelineB   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).
c                    > 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                  S:w  a  [        SU R                   S35      eU R                  [        5        g )Ntop_k   ptzThe z is only available in PyTorch.)super__init__	frameworkr   	__class__check_model_typer   )selfargskwargsr6   s      r)   r4   $AudioClassificationPipeline.__init__]   su    f!8"F7OF"F7O$)&)>>T!tDNN#33QRSSJK    inputsc                 &   > [         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__)r8   r=   r:   r6   s      r)   r?   $AudioClassificationPipeline.__call__j   s    L 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$ )Nr0   )softmaxsigmoidnonez'Invalid value for `function_to_apply`: z2. Valid options are ['softmax', 'sigmoid', 'none']function_to_applyrB   )modelconfig
num_labelsr   )r8   r0   rE   r:   postprocess_paramss        r)   _sanitize_parameters0AudioClassificationPipeline._sanitize_parameters   s     =*.***;*;*F*Fw'zz((333

))44*/w'( (FF =>O=P QG G  7H236?232)))r<   c                    [        U[        5      (       ar  UR                  S5      (       d  UR                  S5      (       a!  [        R                  " U5      R
                  nO%[        US5       nUR                  5       nS S S 5        [        U[        5      (       a  [        XR                  R                  5      n[        U[        5      (       a  UR                  5       nSU;   a  SU;   d  SU;   d  [        S5      eUR                  SS 5      nUc$  UR                  SS 5        UR                  SS 5      nUR                  S5      nUnX@R                  R                  :w  ah  S	S Kn[#        5       (       a  S	S
KJn  O[)        S5      eUR+                  UR-                  U5      X@R                  R                  5      R/                  5       n[        U[0        R2                  5      (       d  [5        S5      e[7        UR8                  5      S:w  a  [        S5      eU R                  XR                  R                  SS9nU R:                  b  UR=                  U R:                  S9nU$ ! , (       d  f       GN= f)Nzhttp://zhttps://rbr   rawarrayzWhen passing a dictionary to AudioClassificationPipeline, the dict needs to contain a "raw" key containing the numpy array representing the audio and a "sampling_rate" key, containing the sampling_rate associated with that arraypathr   )
functionalztorchaudio is required to resample audio samples in AudioClassificationPipeline. The torchaudio package can be installed through: `pip install torchaudio`.z"We expect a numpy ndarray as inputr	   zFWe expect a single channel audio input for AudioClassificationPipeliner2   )r   return_tensors)dtype)
isinstancestr
startswithrequestsgetcontentopenreadbytesr*   feature_extractorr   dictcopyr   poptorchr   
torchaudiorQ   ImportErrorresample
from_numpynumpyr   ndarray	TypeErrorlenr    torch_dtypeto)r8   r=   f_inputsin_sampling_ratera   F	processeds           r)   
preprocess&AudioClassificationPipeline.preprocess   s   fc""  ++v/@/@/L/L "f-55&$'1VVXF ( fe$$ )?)?)M)MNFfd##[[]F $v-5F?gQWFW N  jj-G

64( **Wd3%zz/:F#9#9#G#GG*,,:%e 
 $$V,.>@V@V@d@d%'  &"**--@AAv||!eff**"8"8"F"FW[ + 
	 '!4+;+;<Ic ('s   /I%%
I4c                 *    U R                   " S0 UD6nU$ )N )rF   )r8   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 )NrB   r   rC   )scorelabel)	logitsrB   rC   topktolistziprF   rG   id2label)
r8   rv   r0   rE   probsscoresidsr{   _idlabelss
             r)   postprocess'AudioClassificationPipeline.postprocess   s    	)!((+33B7E)+!((+335E!((+Ejj'jjl_bci_op_oQ[QVEJJ,=,=,F,Fs,KL_op qs   1Crt   )NN)r1   rB   )__name__
__module____qualname____firstlineno____doc__r4   r   r   rg   r\   rU   r?   rJ   rq   rw   r   __static_attributes____classcell__)r6   s   @r)   r-   r-   B   sF    2L&2bjj%,-&2P*,8t r<   r-   )r   typingr   rf   r   rW   utilsr   r   r   r   baser
   r   models.auto.modeling_autor   
get_loggerr   loggerr\   intrO   r*   r-   rt   r<   r)   <module>r      s        \ \ 4 X			H	%!% ! ! !H ,4HIq( q Jqr<   