





import unittest

import numpy as np

from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
from transformers.pipelines import AudioClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torchaudio,
    slow,
)

from .test_pipelines_common import ANY




@is_pipeline_test
class 					_lowerCamelCase			(     unittest.TestCase     ):



      """simple docstring"""






      UpperCAmelCase_    :   List[str]      =MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
      UpperCAmelCase_    :   Any      =TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING







      def     UpperCAmelCase		(			self					,    UpperCAmelCase					,    UpperCAmelCase					,    UpperCAmelCase )							-> Any:






        '''simple docstring'''






        __snake_case :					List[str]          =							AudioClassificationPipeline(model=__lowercase					,    feature_extractor=__lowercase )

        # test with a raw waveform
        __snake_case :					int          =							np.zeros((34000,) )
        __snake_case :					str          =							np.zeros((14000,) )
        return audio_classifier, [audioa, audio]







      def     UpperCAmelCase		(			self					,    UpperCAmelCase					,    UpperCAmelCase )							-> Optional[int]:






        '''simple docstring'''






        __snake_case :					Tuple          =							examples
        __snake_case :					Optional[int]          =							audio_classifier(__lowercase )
        # by default a model is initialized with num_labels=2
        self.assertEqual(
            __lowercase					,    [
                {"score": ANY(__lowercase ), "label": ANY(__lowercase )},
                {"score": ANY(__lowercase ), "label": ANY(__lowercase )},
            ]					,    )
        __snake_case :					List[Any]          =							audio_classifier(__lowercase					,    top_k=1 )
        self.assertEqual(
            __lowercase					,    [
                {"score": ANY(__lowercase ), "label": ANY(__lowercase )},
            ]					,    )

        self.run_torchaudio(__lowercase )







      @require_torchaudio
      def     UpperCAmelCase		(			self					,    UpperCAmelCase )							-> List[Any]:






        '''simple docstring'''






        import datasets

        # test with a local file
        __snake_case :					Optional[Any]          =							datasets.load_dataset("hf-internal-testing/librispeech_asr_dummy"					,    "clean"					,    split="validation" )
        __snake_case :					int          =							dataset[0]['audio']['array']
        __snake_case :					List[Any]          =							audio_classifier(__lowercase )
        self.assertEqual(
            __lowercase					,    [
                {"score": ANY(__lowercase ), "label": ANY(__lowercase )},
                {"score": ANY(__lowercase ), "label": ANY(__lowercase )},
            ]					,    )







      @require_torch
      def     UpperCAmelCase		(			self )							-> Union[str, Any]:






        '''simple docstring'''






        __snake_case :					Union[str, Any]          =							'anton-l/wav2vec2-random-tiny-classifier'

        __snake_case :					Dict          =							pipeline("audio-classification"					,    model=__lowercase )

        __snake_case :					Union[str, Any]          =							np.ones((8000,) )
        __snake_case :					Union[str, Any]          =							audio_classifier(__lowercase					,    top_k=4 )

        __snake_case :					Tuple          =							[
            {'score': 0.0_842, 'label': 'no'},
            {'score': 0.0_838, 'label': 'up'},
            {'score': 0.0_837, 'label': 'go'},
            {'score': 0.0_834, 'label': 'right'},
        ]
        __snake_case :					Optional[int]          =							[
            {'score': 0.0_845, 'label': 'stop'},
            {'score': 0.0_844, 'label': 'on'},
            {'score': 0.0_841, 'label': 'right'},
            {'score': 0.0_834, 'label': 'left'},
        ]
        self.assertIn(nested_simplify(__lowercase					,    decimals=4 )					,    [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] )

        __snake_case :					List[Any]          =							{'array': np.ones((8000,) ), 'sampling_rate': audio_classifier.feature_extractor.sampling_rate}
        __snake_case :					Tuple          =							audio_classifier(__lowercase					,    top_k=4 )
        self.assertIn(nested_simplify(__lowercase					,    decimals=4 )					,    [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] )







      @require_torch
      @slow
      def     UpperCAmelCase		(			self )							-> List[str]:






        '''simple docstring'''






        import datasets

        __snake_case :					Optional[int]          =							'superb/wav2vec2-base-superb-ks'

        __snake_case :					Dict          =							pipeline("audio-classification"					,    model=__lowercase )
        __snake_case :					str          =							datasets.load_dataset("anton-l/superb_dummy"					,    "ks"					,    split="test" )

        __snake_case :					List[str]          =							np.array(dataset[3]["speech"]					,    dtype=np.floataa )
        __snake_case :					Union[str, Any]          =							audio_classifier(__lowercase					,    top_k=4 )
        self.assertEqual(
            nested_simplify(__lowercase					,    decimals=3 )					,    [
                {"score": 0.981, "label": "go"},
                {"score": 0.007, "label": "up"},
                {"score": 0.006, "label": "_unknown_"},
                {"score": 0.001, "label": "down"},
            ]					,    )







      @require_tf
      @unittest.skip("Audio classification is not implemented for TF" )
      def     UpperCAmelCase		(			self )							-> int:






        '''simple docstring'''






        pass





'''simple docstring'''
# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

import torch

from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
 from PIL import Image

class     _lowerCamelCase       (     snake_case_					):




 '''simple docstring'''
 __lowercase					:		int     				=    '''dandelin/vilt-b32-finetuned-vqa'''
 __lowercase					:		int     				=    (
     '''This is a tool that answers a question about an image. It takes an input named `image` which should be the '''
     '''image containing the information, as well as a `question` which should be the question in English. It '''
     '''returns a text that is the answer to the question.'''
 )
 __lowercase					:		Union[str, Any]     				=    '''image_qa'''
 __lowercase					:		Optional[Any]     				=    AutoProcessor
 __lowercase					:		Tuple     				=    AutoModelForVisualQuestionAnswering

 __lowercase					:		Tuple     				=    ['''image''', '''text''']
 __lowercase					:		List[str]     				=    ['''text''']


 def __init__(   self   ,   *__lowercase   ,   **__lowercase    ):

  """simple docstring"""







  requires_backends(self   ,   ['vision']    )
  super().__init__(*__lowercase   ,   **__lowercase    )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  return self.pre_processor(__lowercase   ,   __lowercase   ,   return_tensors='pt'    )


 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  with torch.no_grad():
   return self.model(**__lowercase    ).logits







 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  __A	:		List[str]	  =      outputs.argmax(-1    ).item()
  return self.model.config.idalabel[idx]






