


"""simple docstring"""
import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
 import jax

 from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel




class 	lowercase__				(					unittest.TestCase				):





   def __init__(							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE=13      ,   SCREAMING_SNAKE_CASE=30      ,   SCREAMING_SNAKE_CASE=2      ,   SCREAMING_SNAKE_CASE=3      ,   SCREAMING_SNAKE_CASE=True      ,   SCREAMING_SNAKE_CASE=True      ,   SCREAMING_SNAKE_CASE=32      ,   SCREAMING_SNAKE_CASE=5      ,   SCREAMING_SNAKE_CASE=4      ,   SCREAMING_SNAKE_CASE=37      ,   SCREAMING_SNAKE_CASE="gelu"      ,   SCREAMING_SNAKE_CASE=0.1      ,   SCREAMING_SNAKE_CASE=0.1      ,   SCREAMING_SNAKE_CASE=10      ,   SCREAMING_SNAKE_CASE=0.02      ,   )  ->  Dict:
      _lowerCamelCase	:					int   =		parent
      _lowerCamelCase	:					List[Any]   =		batch_size
      _lowerCamelCase	:					Tuple   =		image_size
      _lowerCamelCase	:					Dict   =		patch_size
      _lowerCamelCase	:					Union[str, Any]   =		num_channels
      _lowerCamelCase	:					List[Any]   =		is_training
      _lowerCamelCase	:					Any   =		use_labels
      _lowerCamelCase	:					List[Any]   =		hidden_size
      _lowerCamelCase	:					Any   =		num_hidden_layers
      _lowerCamelCase	:					Optional[int]   =		num_attention_heads
      _lowerCamelCase	:					Union[str, Any]   =		intermediate_size
      _lowerCamelCase	:					Any   =		hidden_act
      _lowerCamelCase	:					Optional[Any]   =		hidden_dropout_prob
      _lowerCamelCase	:					Optional[Any]   =		attention_probs_dropout_prob
      _lowerCamelCase	:					str   =		type_sequence_label_size
      _lowerCamelCase	:					List[str]   =		initializer_range

      # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
      _lowerCamelCase	:					Optional[Any]   =		(image_size // patch_size) ** 2
      _lowerCamelCase	:					Any   =		num_patches + 1





   def 		UpperCamelCase_  (							self)  ->  Dict:
      _lowerCamelCase	:					str   =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

      _lowerCamelCase	:					int   =		ViTConfig(
          image_size=self.image_size      ,   patch_size=self.patch_size      ,   num_channels=self.num_channels      ,   hidden_size=self.hidden_size      ,   num_hidden_layers=self.num_hidden_layers      ,   num_attention_heads=self.num_attention_heads      ,   intermediate_size=self.intermediate_size      ,   hidden_act=self.hidden_act      ,   hidden_dropout_prob=self.hidden_dropout_prob      ,   attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,   is_decoder=SCREAMING_SNAKE_CASE      ,   initializer_range=self.initializer_range      ,   )

      return config, pixel_values





   def 		UpperCamelCase_  (							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)  ->  Optional[int]:
      _lowerCamelCase	:					Dict   =		FlaxViTModel(config=SCREAMING_SNAKE_CASE)
      _lowerCamelCase	:					List[Any]   =		model(SCREAMING_SNAKE_CASE)
      # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
      _lowerCamelCase	:					Dict   =		(self.image_size, self.image_size)
      _lowerCamelCase	:					int   =		(self.patch_size, self.patch_size)
      _lowerCamelCase	:					Union[str, Any]   =		(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
      self.parent.assertEqual(result.last_hidden_state.shape      ,   (self.batch_size, num_patches + 1, self.hidden_size))





   def 		UpperCamelCase_  (							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)  ->  List[str]:
      _lowerCamelCase	:					str   =		self.type_sequence_label_size
      _lowerCamelCase	:					List[Any]   =		FlaxViTForImageClassification(config=SCREAMING_SNAKE_CASE)
      _lowerCamelCase	:					List[Any]   =		model(SCREAMING_SNAKE_CASE)
      self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.type_sequence_label_size))

      # test greyscale images
      _lowerCamelCase	:					Dict   =		1
      _lowerCamelCase	:					int   =		FlaxViTForImageClassification(SCREAMING_SNAKE_CASE)

      _lowerCamelCase	:					Union[str, Any]   =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
      _lowerCamelCase	:					Union[str, Any]   =		model(SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Union[str, Any]:
      _lowerCamelCase	:					Optional[Any]   =		self.prepare_config_and_inputs()
      (
          (
          _lowerCamelCase
      )   ,    (
          _lowerCamelCase
      )   ,    
      )	:					str   =		config_and_inputs
      _lowerCamelCase	:					Union[str, Any]   =		{"""pixel_values""": pixel_values}
      return config, inputs_dict







@require_flax
class 	lowercase__				(					A_ ,unittest.TestCase				):
   __UpperCAmelCase							       =					(FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()





   def 		UpperCamelCase_  (							self)  ->  None:
      _lowerCamelCase	:					Dict   =		FlaxViTModelTester(self)
      _lowerCamelCase	:					Any   =		ConfigTester(self      ,   config_class=SCREAMING_SNAKE_CASE      ,   has_text_modality=SCREAMING_SNAKE_CASE      ,   hidden_size=37)





   def 		UpperCamelCase_  (							self)  ->  int:
      self.config_tester.run_common_tests()





   def 		UpperCamelCase_  (							self)  ->  List[str]:
      _lowerCamelCase	:					Optional[int]   =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Any:
      _lowerCamelCase	:					Optional[Any]   =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Dict:
      _lowerCamelCase   ,    _lowerCamelCase	:					Optional[Any]   =		self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         _lowerCamelCase	:					int   =		model_class(SCREAMING_SNAKE_CASE)
         _lowerCamelCase	:					Optional[Any]   =		inspect.signature(model.__call__)
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         _lowerCamelCase	:					Optional[int]   =		[*signature.parameters.keys()]

         _lowerCamelCase	:					Tuple   =		["""pixel_values"""]
         self.assertListEqual(arg_names[:1]      ,   SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  List[str]:
      _lowerCamelCase   ,    _lowerCamelCase	:					str   =		self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         with self.subTest(model_class.__name__):
            _lowerCamelCase	:					List[Any]   =		self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)
            _lowerCamelCase	:					List[str]   =		model_class(SCREAMING_SNAKE_CASE)

            @jax.jit
            def model_jitted(SCREAMING_SNAKE_CASE      ,   **SCREAMING_SNAKE_CASE):
               return model(pixel_values=SCREAMING_SNAKE_CASE      ,   **SCREAMING_SNAKE_CASE)

            with self.subTest("""JIT Enabled"""):
               _lowerCamelCase	:					List[str]   =		model_jitted(**SCREAMING_SNAKE_CASE).to_tuple()

            with self.subTest("""JIT Disabled"""):
               with jax.disable_jit():
                  _lowerCamelCase	:					List[Any]   =		model_jitted(**SCREAMING_SNAKE_CASE).to_tuple()

            self.assertEqual(len(SCREAMING_SNAKE_CASE)      ,   len(SCREAMING_SNAKE_CASE))
            for jitted_output, output in zip(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE):
               self.assertEqual(jitted_output.shape      ,   output.shape)





   @slow
   def 		UpperCamelCase_  (							self)  ->  List[str]:
      for model_class_name in self.all_model_classes:
         _lowerCamelCase	:					Tuple   =		model_class_name.from_pretrained("""google/vit-base-patch16-224""")
         _lowerCamelCase	:					List[Any]   =		model(np.ones((1, 3, 224, 224)))
         self.assertIsNotNone(SCREAMING_SNAKE_CASE)






'''simple docstring'''
from collections import UserDict
from typing import Union

import numpy as np
import requests

from ..utils import (
    add_end_docstrings,
    logging,
)
from .audio_classification import ffmpeg_read
from .base import PIPELINE_INIT_ARGS, Pipeline


snake_case_					:      List[Any]    			=    logging.get_logger(__name__)






@add_end_docstrings(lowercase  )
class 				lowercase__      (				lowercase  ):





	def __init__(					self   :							Tuple					,**lowerCamelCase__   :							Union[str, Any]					):







							'''simple docstring'''
							super().__init__(**lowerCamelCase__					)

							if self.framework != "pt":
													raise ValueError(F'The {self.__class__} is only available in PyTorch.'					)
							# No specific FOR_XXX available yet





	def __call__(					self   :							Dict					,lowerCamelCase__   :							Union[np.ndarray, bytes, str]					,**lowerCamelCase__   :							Any					):







							'''simple docstring'''
							return super().__call__(lowerCamelCase__					,**lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							int					,**lowerCamelCase__   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  {}
							if "candidate_labels" in kwargs:
													_UpperCamelCase   :						Any												=  kwargs['candidate_labels']
							if "hypothesis_template" in kwargs:
													_UpperCamelCase   :						Any												=  kwargs['hypothesis_template']

							return preprocess_params, {}, {}





	def       UpperCamelCase_    (					self   :							Any					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							str=None					,lowerCamelCase__   :							List[Any]="This is a sound of {}."					):







							'''simple docstring'''
							if isinstance(lowerCamelCase__					,lowerCamelCase__					):
													if audio.startswith('http://'					) or audio.startswith('https://'					):
																			# We need to actually check for a real protocol, otherwise it's impossible to use a local file
																			# like http_huggingface_co.png
																			_UpperCamelCase   :						Optional[Any]												=  requests.get(lowerCamelCase__					).content
													else:
																			with open(lowerCamelCase__					,'rb'					) as f:
																									_UpperCamelCase   :						List[str]												=  f.read()

							if isinstance(lowerCamelCase__					,lowerCamelCase__					):
													_UpperCamelCase   :						int												=  ffmpeg_read(lowerCamelCase__					,self.feature_extractor.sampling_rate					)

							if not isinstance(lowerCamelCase__					,np.ndarray					):
													raise ValueError('We expect a numpy ndarray as input'					)
							if len(audio.shape					) != 1:
													raise ValueError('We expect a single channel audio input for ZeroShotAudioClassificationPipeline'					)

							_UpperCamelCase   :						Dict												=  self.feature_extractor(
							    [audio]					,sampling_rate=self.feature_extractor.sampling_rate					,return_tensors='pt'					)
							_UpperCamelCase   :						List[Any]												=  candidate_labels
							_UpperCamelCase   :						Tuple												=  [hypothesis_template.format(lowerCamelCase__					) for x in candidate_labels]
							_UpperCamelCase   :						Dict												=  self.tokenizer(lowerCamelCase__					,return_tensors=self.framework					,padding=lowerCamelCase__					)
							_UpperCamelCase   :						Optional[Any]												=  [text_inputs]
							return inputs





	def       UpperCamelCase_    (					self   :							str					,lowerCamelCase__   :							Tuple					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  model_inputs.pop('candidate_labels'					)
							_UpperCamelCase   :						str												=  model_inputs.pop('text_inputs'					)
							if isinstance(text_inputs[0]					,lowerCamelCase__					):
													_UpperCamelCase   :						Union[str, Any]												=  text_inputs[0]
							else:
													# Batching case.
													_UpperCamelCase   :						int												=  text_inputs[0][0]

							_UpperCamelCase   :						List[str]												=  self.model(**lowerCamelCase__					,**lowerCamelCase__					)

							_UpperCamelCase   :						Optional[Any]												=  {
							    'candidate_labels': candidate_labels,
							    'logits': outputs.logits_per_audio,
							}
							return model_outputs





	def       UpperCamelCase_    (					self   :							int					,lowerCamelCase__   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  model_outputs.pop('candidate_labels'					)
							_UpperCamelCase   :						int												=  model_outputs['logits'][0]

							if self.framework == "pt":
													_UpperCamelCase   :						Tuple												=  logits.softmax(dim=0					)
													_UpperCamelCase   :						str												=  probs.tolist()
							else:
													raise ValueError('`tf` framework not supported.'					)

							_UpperCamelCase   :						Union[str, Any]												=  [
							    {'score': score, 'label': candidate_label}
							    for score, candidate_label in sorted(zip(lowerCamelCase__					,lowerCamelCase__					)					,key=lambda lowerCamelCase__					: -x[0]					)
							]
							return result


