






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  _lowercase   (   unittest.TestCase						):
      _a							:						Any										=							MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
      _a							:						str										=							TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
      def 						lowercase__					(     self				,			a				,			a				,			a   ):
       snake_case__  :    str							       =AudioClassificationPipeline(model=a				,			feature_extractor=a   )

       # test with a raw waveform
       snake_case__  :    Dict							       =np.zeros((3_4_0_0_0,)   )
       snake_case__  :    int							       =np.zeros((1_4_0_0_0,)   )
       return audio_classifier, [audioa, audio]
      def 						lowercase__					(     self				,			a				,			a   ):
       snake_case__       ,   snake_case__  :    Optional[Any]							       =examples
       snake_case__  :    List[Any]							       =audio_classifier(a   )
       # by default a model is initialized with num_labels=2
       self.assertEqual(
           a				,			[
               {"""score""": ANY(a   ), """label""": ANY(a   )},
               {"""score""": ANY(a   ), """label""": ANY(a   )},
           ]				,			)
       snake_case__  :    str							       =audio_classifier(a				,			top_k=1   )
       self.assertEqual(
           a				,			[
               {"""score""": ANY(a   ), """label""": ANY(a   )},
           ]				,			)

       self.run_torchaudio(a   )
      @require_torchaudio
      def 						lowercase__					(     self				,			a   ):
       import datasets

       # test with a local file
       snake_case__  :    List[Any]							       =datasets.load_dataset("""hf-internal-testing/librispeech_asr_dummy"""				,			"""clean"""				,			split="""validation"""   )
       snake_case__  :    str							       =dataset[0]["""audio"""]["""array"""]
       snake_case__  :    List[str]							       =audio_classifier(a   )
       self.assertEqual(
           a				,			[
               {"""score""": ANY(a   ), """label""": ANY(a   )},
               {"""score""": ANY(a   ), """label""": ANY(a   )},
           ]				,			)
      @require_torch
      def 						lowercase__					(     self   ):
       snake_case__  :    List[Any]							       ="""anton-l/wav2vec2-random-tiny-classifier"""

       snake_case__  :    List[Any]							       =pipeline("""audio-classification"""				,			model=a   )

       snake_case__  :    List[str]							       =np.ones((8_0_0_0,)   )
       snake_case__  :    Any							       =audio_classifier(a				,			top_k=4   )

       snake_case__  :    str							       =[
           {"""score""": 0.0842, """label""": """no"""},
           {"""score""": 0.0838, """label""": """up"""},
           {"""score""": 0.0837, """label""": """go"""},
           {"""score""": 0.0834, """label""": """right"""},
       ]
       snake_case__  :    int							       =[
           {"""score""": 0.0845, """label""": """stop"""},
           {"""score""": 0.0844, """label""": """on"""},
           {"""score""": 0.0841, """label""": """right"""},
           {"""score""": 0.0834, """label""": """left"""},
       ]
       self.assertIn(nested_simplify(a				,			decimals=4   )				,			[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]   )

       snake_case__  :    Union[str, Any]							       ={"""array""": np.ones((8_0_0_0,)   ), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate}
       snake_case__  :    str							       =audio_classifier(a				,			top_k=4   )
       self.assertIn(nested_simplify(a				,			decimals=4   )				,			[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]   )
      @require_torch
      @slow
      def 						lowercase__					(     self   ):
       import datasets

       snake_case__  :    Optional[Any]							       ="""superb/wav2vec2-base-superb-ks"""

       snake_case__  :    str							       =pipeline("""audio-classification"""				,			model=a   )
       snake_case__  :    List[Any]							       =datasets.load_dataset("""anton-l/superb_dummy"""				,			"""ks"""				,			split="""test"""   )

       snake_case__  :    str							       =np.array(dataset[3]["""speech"""]				,			dtype=np.floataa   )
       snake_case__  :    Any							       =audio_classifier(a				,			top_k=4   )
       self.assertEqual(
           nested_simplify(a				,			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 						lowercase__					(     self   ):
       pass













from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
       from PIL import Image

       from ..image_utils import load_image

if is_torch_available():
       import torch

       from transformers.modeling_outputs import BaseModelOutput

       from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

__lowerCamelCase	:		Dict										=   logging.get_logger(__name__)







@add_end_docstrings(_A						)
class  _lowercase   (   _A						):
      def __init__(     self				,			**a   ):
       super().__init__(**a   )

       if self.framework == "tf":
        raise ValueError(F"The {self.__class__} is only available in PyTorch."   )

       requires_backends(self				,			"""vision"""   )
       self.check_model_type(a   )
      def __call__(     self				,			a				,			a = None				,			**a				,			):
       if "text_queries" in kwargs:
        snake_case__  :    Any							       =kwargs.pop("""text_queries"""   )

       if isinstance(a				,			(str, Image.Image)   ):
        snake_case__  :    Union[str, Any]							       ={"""image""": image, """candidate_labels""": candidate_labels}
       else:
        snake_case__  :    str							       =image
       snake_case__  :    Union[str, Any]							       =super().__call__(a				,			**a   )
       return results
      def 						lowercase__					(     self				,			**a   ):
       snake_case__  :    Union[str, Any]							       ={}
       if "threshold" in kwargs:
        snake_case__  :    Optional[Any]							       =kwargs["""threshold"""]
       if "top_k" in kwargs:
        snake_case__  :    Dict							       =kwargs["""top_k"""]
       return {}, {}, postprocess_params
      def 						lowercase__					(     self				,			a   ):
       snake_case__  :    List[Any]							       =load_image(inputs["""image"""]   )
       snake_case__  :    Dict							       =inputs["""candidate_labels"""]
       if isinstance(a				,			a   ):
        snake_case__  :    str							       =candidate_labels.split(""","""   )

       snake_case__  :    Optional[Any]							       =torch.tensor([[image.height, image.width]]				,			dtype=torch.intaa   )
       for i, candidate_label in enumerate(a   ):
        snake_case__  :    Tuple							       =self.tokenizer(a				,			return_tensors=self.framework   )
        snake_case__  :    int							       =self.image_processor(a				,			return_tensors=self.framework   )
        yield {
            "is_last": i == len(a   ) - 1,
            "target_size": target_size,
            "candidate_label": candidate_label,
            **text_inputs,
            **image_features,
        }
      def 						lowercase__					(     self				,			a   ):
       snake_case__  :    Tuple							       =model_inputs.pop("""target_size"""   )
       snake_case__  :    Tuple							       =model_inputs.pop("""candidate_label"""   )
       snake_case__  :    Dict							       =model_inputs.pop("""is_last"""   )

       snake_case__  :    Any							       =self.model(**a   )

       snake_case__  :    List[Any]							       ={"""target_size""": target_size, """candidate_label""": candidate_label, """is_last""": is_last, **outputs}
       return model_outputs
      def 						lowercase__					(     self				,			a				,			a=0.1				,			a=None   ):
       snake_case__  :    Optional[int]							       =[]
       for model_output in model_outputs:
        snake_case__  :    Tuple							       =model_output["""candidate_label"""]
        snake_case__  :    Optional[int]							       =BaseModelOutput(a   )
        snake_case__  :    int							       =self.image_processor.post_process_object_detection(
            outputs=a				,			threshold=a				,			target_sizes=model_output["""target_size"""]   )[0]

        for index in outputs["scores"].nonzero():
         snake_case__  :    int							       =outputs["""scores"""][index].item()
         snake_case__  :    Tuple							       =self._get_bounding_box(outputs["""boxes"""][index][0]   )

         snake_case__  :    Tuple							       ={"""score""": score, """label""": label, """box""": box}
         results.append(a   )

       snake_case__  :    Tuple							       =sorted(a				,			key=lambda a   : x["score"]				,			reverse=a   )
       if top_k:
        snake_case__  :    Union[str, Any]							       =results[:top_k]

       return results



      def 						lowercase__					(     self				,			a   ):
       if self.framework != "pt":
        raise ValueError("""The ZeroShotObjectDetectionPipeline is only available in PyTorch."""   )
       snake_case__       ,   snake_case__       ,   snake_case__       ,   snake_case__  :    Dict							       =box.int().tolist()
       snake_case__  :    int							       ={
           """xmin""": xmin,
           """ymin""": ymin,
           """xmax""": xmax,
           """ymax""": ymax,
       }
       return bbox





