



import unittest

from transformers import YosoConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import (
      YosoForMaskedLM,
      YosoForMultipleChoice,
      YosoForQuestionAnswering,
      YosoForSequenceClassification,
      YosoForTokenClassification,
      YosoModel,
  )
  from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST


class 	lowerCamelCase__ :


      def __init__( self						:  Union[str, Any]      ,       _a						:  Optional[Any]      ,       _a						:  Union[str, Any]=1_3      ,       _a						:  Optional[int]=7      ,       _a						:  int=True      ,       _a						:  Dict=True      ,       _a						:  Optional[int]=True      ,       _a						:  Optional[Any]=True      ,       _a						:  Tuple=9_9      ,       _a						:  Tuple=3_2      ,       _a						:  Optional[Any]=5      ,       _a						:  Dict=4      ,       _a						:  List[Any]=3_7      ,       _a						:  List[Any]="gelu"      ,       _a						:  List[str]=0.1      ,       _a						:  Tuple=0.1      ,       _a						:  Optional[Any]=5_1_2      ,       _a						:  Optional[Any]=1_6      ,       _a						:  Dict=2      ,       _a						:  str=0.0_2      ,       _a						:  Any=3      ,       _a						:  Union[str, Any]=4      ,       _a						:  Any=None      ,       ):
             a__:       Optional[int]											=parent
             a__:       int											=batch_size
             a__:       Optional[int]											=seq_length
             a__:       int											=is_training
             a__:       Optional[int]											=use_input_mask
             a__:       Optional[int]											=use_token_type_ids
             a__:       Dict											=use_labels
             a__:       Optional[Any]											=vocab_size
             a__:       List[str]											=hidden_size
             a__:       Union[str, Any]											=num_hidden_layers
             a__:       Tuple											=num_attention_heads
             a__:       List[Any]											=intermediate_size
             a__:       Tuple											=hidden_act
             a__:       Optional[Any]											=hidden_dropout_prob
             a__:       Any											=attention_probs_dropout_prob
             a__:       Any											=max_position_embeddings
             a__:       Union[str, Any]											=type_vocab_size
             a__:       Optional[int]											=type_sequence_label_size
             a__:       int											=initializer_range
             a__:       Optional[int]											=num_labels
             a__:       int											=num_choices
             a__:       int											=scope


      def   _lowerCamelCase			( self						:  str			):
             a__:       List[str]											=ids_tensor([self.batch_size, self.seq_length]      ,       self.vocab_size			)

             a__:       Optional[int]											=None
             if self.use_input_mask:
                    a__:       Optional[int]											=random_attention_mask([self.batch_size, self.seq_length]			)

             a__:       Any											=None
             if self.use_token_type_ids:
                    a__:       Any											=ids_tensor([self.batch_size, self.seq_length]      ,       self.type_vocab_size			)

             a__:       Union[str, Any]											=None
             a__:       Any											=None
             a__:       List[Any]											=None
             if self.use_labels:
                    a__:       str											=ids_tensor([self.batch_size]      ,       self.type_sequence_label_size			)
                    a__:       Dict											=ids_tensor([self.batch_size, self.seq_length]      ,       self.num_labels			)
                    a__:       Dict											=ids_tensor([self.batch_size]      ,       self.num_choices			)

             a__:       str											=self.get_config()

             return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


      def   _lowerCamelCase			( self						:  List[Any]			):
             return YosoConfig(
                 vocab_size=self.vocab_size      ,       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      ,       max_position_embeddings=self.max_position_embeddings      ,       type_vocab_size=self.type_vocab_size      ,       is_decoder=_a      ,       initializer_range=self.initializer_range      ,       )


      def   _lowerCamelCase			( self						:  str			):
             a__:       str											=self.get_config()
             a__:       Any											=3_0_0
             return config


      def   _lowerCamelCase			( self						:  Tuple			):
             (
                 a__			
             ):       Union[str, Any]											=self.prepare_config_and_inputs()

             a__:       Any											=True
             a__:       Union[str, Any]											=floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
             a__:       Optional[int]											=ids_tensor([self.batch_size, self.seq_length]      ,       vocab_size=2			)

             return (
                 config,
                 input_ids,
                 token_type_ids,
                 input_mask,
                 sequence_labels,
                 token_labels,
                 choice_labels,
                 encoder_hidden_states,
                 encoder_attention_mask,
             )


      def   _lowerCamelCase			( self						:  List[Any]      ,       _a						:  Tuple      ,       _a						:  Any      ,       _a						:  List[Any]      ,       _a						:  Any      ,       _a						:  Optional[int]      ,       _a						:  Any      ,       _a						:  Union[str, Any]			):
             a__:       Optional[Any]											=YosoModel(config=_a			)
             model.to(_a			)
             model.eval()
             a__:       List[Any]											=model(_a      ,       attention_mask=_a      ,       token_type_ids=_a			)
             a__:       Optional[int]											=model(_a      ,       token_type_ids=_a			)
             a__:       List[str]											=model(_a			)
             self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size)			)


      def   _lowerCamelCase			( self						:  Tuple      ,       _a						:  List[Any]      ,       _a						:  int      ,       _a						:  List[Any]      ,       _a						:  Optional[Any]      ,       _a						:  Optional[Any]      ,       _a						:  Tuple      ,       _a						:  Union[str, Any]      ,       _a						:  Union[str, Any]      ,       _a						:  int      ,       ):
             a__:       Tuple											=True
             a__:       Tuple											=YosoModel(_a			)
             model.to(_a			)
             model.eval()
             a__:       Optional[int]											=model(
                 _a      ,       attention_mask=_a      ,       token_type_ids=_a      ,       encoder_hidden_states=_a      ,       encoder_attention_mask=_a      ,       )
             a__:       List[str]											=model(
                 _a      ,       attention_mask=_a      ,       token_type_ids=_a      ,       encoder_hidden_states=_a      ,       )
             a__:       Optional[int]											=model(_a      ,       attention_mask=_a      ,       token_type_ids=_a			)
             self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size)			)


      def   _lowerCamelCase			( self						:  Optional[Any]      ,       _a						:  Union[str, Any]      ,       _a						:  Any      ,       _a						:  Any      ,       _a						:  Any      ,       _a						:  Union[str, Any]      ,       _a						:  str      ,       _a						:  Optional[int]			):
             a__:       Dict											=YosoForMaskedLM(config=_a			)
             model.to(_a			)
             model.eval()
             a__:       str											=model(_a      ,       attention_mask=_a      ,       token_type_ids=_a      ,       labels=_a			)
             self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.vocab_size)			)


      def   _lowerCamelCase			( self						:  Optional[Any]      ,       _a						:  List[Any]      ,       _a						:  str      ,       _a						:  Optional[Any]      ,       _a						:  List[Any]      ,       _a						:  Union[str, Any]      ,       _a						:  Optional[int]      ,       _a						:  Any			):
             a__:       Optional[Any]											=YosoForQuestionAnswering(config=_a			)
             model.to(_a			)
             model.eval()
             a__:       Optional[int]											=model(
                 _a      ,       attention_mask=_a      ,       token_type_ids=_a      ,       start_positions=_a      ,       end_positions=_a      ,       )
             self.parent.assertEqual(result.start_logits.shape      ,       (self.batch_size, self.seq_length)			)
             self.parent.assertEqual(result.end_logits.shape      ,       (self.batch_size, self.seq_length)			)


      def   _lowerCamelCase			( self						:  Tuple      ,       _a						:  Dict      ,       _a						:  List[str]      ,       _a						:  Tuple      ,       _a						:  Union[str, Any]      ,       _a						:  str      ,       _a						:  Tuple      ,       _a						:  Optional[int]			):
             a__:       List[str]											=self.num_labels
             a__:       Tuple											=YosoForSequenceClassification(_a			)
             model.to(_a			)
             model.eval()
             a__:       List[str]											=model(_a      ,       attention_mask=_a      ,       token_type_ids=_a      ,       labels=_a			)
             self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_labels)			)


      def   _lowerCamelCase			( self						:  List[Any]      ,       _a						:  str      ,       _a						:  List[str]      ,       _a						:  Any      ,       _a						:  int      ,       _a						:  List[Any]      ,       _a						:  Optional[int]      ,       _a						:  int			):
             a__:       int											=self.num_labels
             a__:       int											=YosoForTokenClassification(config=_a			)
             model.to(_a			)
             model.eval()
             a__:       Any											=model(_a      ,       attention_mask=_a      ,       token_type_ids=_a      ,       labels=_a			)
             self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.num_labels)			)


      def   _lowerCamelCase			( self						:  Optional[Any]      ,       _a						:  Union[str, Any]      ,       _a						:  str      ,       _a						:  Any      ,       _a						:  Optional[Any]      ,       _a						:  Optional[Any]      ,       _a						:  Optional[Any]      ,       _a						:  Tuple			):
             a__:       List[str]											=self.num_choices
             a__:       Optional[int]											=YosoForMultipleChoice(config=_a			)
             model.to(_a			)
             model.eval()
             a__:       str											=input_ids.unsqueeze(1			).expand(-1      ,       self.num_choices      ,       -1			).contiguous()
             a__:       Dict											=token_type_ids.unsqueeze(1			).expand(-1      ,       self.num_choices      ,       -1			).contiguous()
             a__:       Tuple											=input_mask.unsqueeze(1			).expand(-1      ,       self.num_choices      ,       -1			).contiguous()
             a__:       int											=model(
                 _a      ,       attention_mask=_a      ,       token_type_ids=_a      ,       labels=_a      ,       )
             self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_choices)			)







      def   _lowerCamelCase			( self						:  Dict			):
             a__:       List[Any]											=self.prepare_config_and_inputs()
             (
                 a__			
             ):       Union[str, Any]											=config_and_inputs
             a__:       Tuple											={"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
             return config, inputs_dict


@require_torch
class 	lowerCamelCase__ (		_a				,					_a				,					unittest.TestCase   ):
      _lowerCAmelCase								= (
          (
              YosoModel,
              YosoForMaskedLM,
              YosoForMultipleChoice,
              YosoForQuestionAnswering,
              YosoForSequenceClassification,
              YosoForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      _lowerCAmelCase								= False
      _lowerCAmelCase								= False
      _lowerCAmelCase								= False

      _lowerCAmelCase								= ()
      _lowerCAmelCase								= (
          {
              '''feature-extraction''': YosoModel,
              '''fill-mask''': YosoForMaskedLM,
              '''question-answering''': YosoForQuestionAnswering,
              '''text-classification''': YosoForSequenceClassification,
              '''token-classification''': YosoForTokenClassification,
              '''zero-shot''': YosoForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )


      def   _lowerCamelCase			( self						:  Optional[Any]			):
             a__:       str											=YosoModelTester(self			)
             a__:       Tuple											=ConfigTester(self      ,       config_class=_a      ,       hidden_size=3_7			)


      def   _lowerCamelCase			( self						:  Tuple			):
             self.config_tester.run_common_tests()


      def   _lowerCamelCase			( self						:  List[str]			):
             a__:       Tuple											=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*_a			)


      def   _lowerCamelCase			( self						:  List[Any]			):
             a__:       List[Any]											=self.model_tester.prepare_config_and_inputs()
             for type in ["absolute", "relative_key", "relative_key_query"]:
                    a__:       Optional[int]											=type
                    self.model_tester.create_and_check_model(*_a			)


      def   _lowerCamelCase			( self						:  str			):
             a__:       Optional[Any]											=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_lm(*_a			)


      def   _lowerCamelCase			( self						:  str			):
             a__:       Union[str, Any]											=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_multiple_choice(*_a			)


      def   _lowerCamelCase			( self						:  Dict			):
             a__:       Optional[int]											=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*_a			)


      def   _lowerCamelCase			( self						:  List[Any]			):
             a__:       Any											=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*_a			)


      def   _lowerCamelCase			( self						:  Dict			):
             a__:       Any											=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*_a			)


      @slow
      def   _lowerCamelCase			( self						:  List[Any]			):
             for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    a__:       List[str]											=YosoModel.from_pretrained(_a			)
                    self.assertIsNotNone(_a			)







      def   _lowerCamelCase			( self						:  List[str]			):
             return


@require_torch
class 	lowerCamelCase__ (		unittest.TestCase   ):


      @slow
      def   _lowerCamelCase			( self						:  List[Any]			):
             a__:       Optional[Any]											=YosoModel.from_pretrained("uw-madison/yoso-4096"			)
             a__:       List[str]											=torch.tensor([[0, 1, 2, 3, 4, 5]]			)

             with torch.no_grad():
                    a__:       Any											=model(_a			)[0]

             a__:       Union[str, Any]											=torch.Size((1, 6, 7_6_8)			)
             self.assertEqual(output.shape      ,       _a			)

             a__:       int											=torch.tensor(
                 [[[-0.0_6_1_1, 0.1_2_4_2, 0.0_8_4_0], [0.0_2_8_0, -0.0_0_4_8, 0.1_1_2_5], [0.0_1_0_6, 0.0_2_2_6, 0.0_7_5_1]]]			)

             self.assertTrue(torch.allclose(output[:, :3, :3]      ,       _a      ,       atol=1e-4			)			)


      @slow
      def   _lowerCamelCase			( self						:  str			):
             a__:       Union[str, Any]											=YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"			)
             a__:       Optional[Any]											=torch.tensor([[0, 1, 2, 3, 4, 5]]			)

             with torch.no_grad():
                    a__:       Optional[int]											=model(_a			)[0]

             a__:       List[Any]											=5_0_2_6_5

             a__:       str											=torch.Size((1, 6, vocab_size)			)
             self.assertEqual(output.shape      ,       _a			)

             a__:       int											=torch.tensor(
                 [[[-2.1_3_1_3, -3.7_2_8_5, -2.2_4_0_7], [-2.7_0_4_7, -3.3_3_1_4, -2.6_4_0_8], [0.0_6_2_9, -2.5_1_6_6, -0.3_3_5_6]]]			)

             self.assertTrue(torch.allclose(output[:, :3, :3]      ,       _a      ,       atol=1e-4			)			)







      @slow
      def   _lowerCamelCase			( self						:  int			):
             a__:       List[Any]											=YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"			)
             a__:       Optional[int]											=torch.arange(4_0_9_6			).unsqueeze(0			)

             with torch.no_grad():
                    a__:       Optional[int]											=model(_a			)[0]

             a__:       Tuple											=5_0_2_6_5

             a__:       Tuple											=torch.Size((1, 4_0_9_6, vocab_size)			)
             self.assertEqual(output.shape      ,       _a			)

             a__:       Optional[int]											=torch.tensor(
                 [[[-2.3_9_1_4, -4.3_7_4_2, -5.0_9_5_6], [-4.0_9_8_8, -4.2_3_8_4, -7.0_4_0_6], [-3.1_4_2_7, -3.7_1_9_2, -6.6_8_0_0]]]			)

             self.assertTrue(torch.allclose(output[:, :3, :3]      ,       _a      ,       atol=1e-4			)			)










import subprocess
from typing import Union

import numpy as np
import requests

from ..utils import add_end_docstrings, is_torch_available, is_torchaudio_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_torch_available():
		from ..models.auto.modeling_auto import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

__UpperCAmelCase						  =     logging.get_logger(__name__)





def 						__lowerCamelCase       (      __magic_name__	:  bytes     , __magic_name__	:  int						):
							a__:       Optional[int]											=F"{sampling_rate}"
							a__:       Union[str, Any]											="1"
							a__:       List[str]											="f32le"
							a__:       Dict											=[
							    "ffmpeg",
							    "-i",
							    "pipe:0",
							    "-ac",
							    ac,
							    "-ar",
							    ar,
							    "-f",
							    format_for_conversion,
							    "-hide_banner",
							    "-loglevel",
							    "quiet",
							    "pipe:1",
							]

							try:
														a__:       List[Any]											=subprocess.Popen(__magic_name__     , stdin=subprocess.PIPE     , stdout=subprocess.PIPE						)
							except FileNotFoundError:
														raise ValueError("ffmpeg was not found but is required to load audio files from filename"						)
							a__:       int											=ffmpeg_process.communicate(__magic_name__						)
							a__:       List[str]											=output_stream[0]

							a__:       str											=np.frombuffer(__magic_name__     , np.floataa						)
							if audio.shape[0] == 0:
														raise ValueError("Malformed soundfile"						)
							return audio







@add_end_docstrings(_a   )
class 	lowerCamelCase__ (		_a   ):


						def __init__( self						:  Union[str, Any]      ,       *_a						:  Dict      ,       **_a						:  Tuple			):
													# Default, might be overriden by the model.config.
													a__:       List[Any]											=5
													super().__init__(*_a      ,       **_a			)

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

													self.check_model_type(_a			)


						def __call__( self						:  Tuple      ,       _a						:  Union[np.ndarray, bytes, str]      ,       **_a						:  Union[str, Any]      ,       ):
													return super().__call__(_a      ,       **_a			)


						def   _lowerCamelCase			( self						:  int      ,       _a						:  Tuple=None      ,       **_a						:  int			):
													# No parameters on this pipeline right now
													a__:       Optional[int]											={}
													if top_k is not None:
																				if top_k > self.model.config.num_labels:
																											a__:       Union[str, Any]											=self.model.config.num_labels
																				a__:       Union[str, Any]											=top_k
													return {}, {}, postprocess_params


						def   _lowerCamelCase			( self						:  Any      ,       _a						:  Union[str, Any]			):
													if isinstance(_a      ,       _a			):
																				if inputs.startswith("http://"			) or inputs.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
																											a__:       List[str]											=requests.get(_a			).content
																				else:
																											with open(_a      ,       "rb"			) as f:
																																		a__:       Dict											=f.read()

													if isinstance(_a      ,       _a			):
																				a__:       Tuple											=ffmpeg_read(_a      ,       self.feature_extractor.sampling_rate			)

													if isinstance(_a      ,       _a			):
																				# Accepting `"array"` which is the key defined in `datasets` for
																				# better integration
																				if not ("sampling_rate" in inputs and ("raw" in inputs or "array" in inputs)):
																											raise ValueError(
																											    "When 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"			)

																				a__:       Tuple											=inputs.pop("raw"      ,       _a			)
																				if _inputs is None:
																											# Remove path which will not be used from `datasets`.
																											inputs.pop("path"      ,       _a			)
																											a__:       int											=inputs.pop("array"      ,       _a			)
																				a__:       Optional[Any]											=inputs.pop("sampling_rate"			)
																				a__:       Optional[int]											=_inputs
																				if in_sampling_rate != self.feature_extractor.sampling_rate:
																											import torch

																											if is_torchaudio_available():
																																		from torchaudio import functional as F
																											else:
																																		raise ImportError(
																																		    "torchaudio is required to resample audio samples in AudioClassificationPipeline. "
																																		    "The torchaudio package can be installed through: `pip install torchaudio`."			)

																											a__:       Union[str, Any]											=F.resample(
																											    torch.from_numpy(_a			)      ,       _a      ,       self.feature_extractor.sampling_rate			).numpy()

													if not isinstance(_a      ,       np.ndarray			):
																				raise ValueError("We expect a numpy ndarray as input"			)
													if len(inputs.shape			) != 1:
																				raise ValueError("We expect a single channel audio input for AudioClassificationPipeline"			)

													a__:       Union[str, Any]											=self.feature_extractor(
													    _a      ,       sampling_rate=self.feature_extractor.sampling_rate      ,       return_tensors="pt"			)
													return processed


						def   _lowerCamelCase			( self						:  Dict      ,       _a						:  Tuple			):
													a__:       str											=self.model(**_a			)
													return model_outputs







						def   _lowerCamelCase			( self						:  Optional[Any]      ,       _a						:  Tuple      ,       _a						:  Tuple=5			):
													a__:       Tuple											=model_outputs.logits[0].softmax(-1			)
													a__							,			a__:       Any											=probs.topk(_a			)

													a__:       Union[str, Any]											=scores.tolist()
													a__:       Dict											=ids.tolist()

													a__:       Optional[int]											=[{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_a      ,       _a			)]

													return labels





