


'''simple docstring'''

import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
     from PIL import Image
else:





     class      snake_case    :





       """simple docstring"""




       @staticmethod
       def   snake_case		(    *UpperCamelCase			,  **UpperCamelCase     ):







        """simple docstring"""
        pass





@is_pipeline_test
@require_vision
@require_torch
class      snake_case    (    unittest.TestCase			):





  """simple docstring"""




  _lowerCamelCase		    =  MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  pipeline(
       "zero-shot-object-detection"			,  model="hf-internal-testing/tiny-random-owlvit-object-detection"     )

   lowerCamelCase_      =  [
       {
           "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
           "candidate_labels": ["cat", "remote", "couch"],
       }
   ]
   return object_detector, examples






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  object_detector(examples[0]			,  threshold=0.0     )

   lowerCamelCase_      =  len(UpperCamelCase     )
   self.assertGreater(UpperCamelCase			,  0     )
   self.assertEqual(
       UpperCamelCase			,  [
           {
               "score": ANY(UpperCamelCase     ),
               "label": ANY(UpperCamelCase     ),
               "box": {"xmin": ANY(UpperCamelCase     ), "ymin": ANY(UpperCamelCase     ), "xmax": ANY(UpperCamelCase     ), "ymax": ANY(UpperCamelCase     )},
           }
           for i in range(UpperCamelCase     )
       ]			,  )






  @require_tf
  @unittest.skip("Zero Shot Object Detection not implemented in TF"     )
  def   snake_case		(    self     ):







   """simple docstring"""
   pass






  @require_torch
  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  pipeline(
       "zero-shot-object-detection"			,  model="hf-internal-testing/tiny-random-owlvit-object-detection"     )

   lowerCamelCase_      =  object_detector(
       "./tests/fixtures/tests_samples/COCO/000000039769.png"			,  candidate_labels=["cat", "remote", "couch"]			,  threshold=0.64			,  )

   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.7_235, "label": "cat", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
           {"score": 0.7_218, "label": "remote", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
           {"score": 0.7_184, "label": "couch", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
           {"score": 0.6_748, "label": "remote", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
           {"score": 0.6_656, "label": "cat", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
           {"score": 0.6_614, "label": "couch", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
           {"score": 0.6_456, "label": "remote", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
           {"score": 0.642, "label": "remote", "box": {"xmin": 67, "ymin": 274, "xmax": 93, "ymax": 297}},
           {"score": 0.6_419, "label": "cat", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
       ]			,  )

   lowerCamelCase_      =  object_detector(
       [
           {
               "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
               "candidate_labels": ["cat", "remote", "couch"],
           }
       ]			,  threshold=0.64			,  )

   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           [
               {"score": 0.7_235, "label": "cat", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
               {"score": 0.7_218, "label": "remote", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
               {"score": 0.7_184, "label": "couch", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
               {"score": 0.6_748, "label": "remote", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
               {"score": 0.6_656, "label": "cat", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
               {"score": 0.6_614, "label": "couch", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
               {"score": 0.6_456, "label": "remote", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
               {"score": 0.642, "label": "remote", "box": {"xmin": 67, "ymin": 274, "xmax": 93, "ymax": 297}},
               {"score": 0.6_419, "label": "cat", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
           ]
       ]			,  )






  @require_torch
  @slow
  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  pipeline("zero-shot-object-detection"     )

   lowerCamelCase_      =  object_detector(
       "http://images.cocodataset.org/val2017/000000039769.jpg"			,  candidate_labels=["cat", "remote", "couch"]			,  )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.2_868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
           {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
           {"score": 0.2_537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
           {"score": 0.1_474, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
           {"score": 0.1_208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
       ]			,  )

   lowerCamelCase_      =  object_detector(
       [
           {
               "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
               "candidate_labels": ["cat", "remote", "couch"],
           },
           {
               "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
               "candidate_labels": ["cat", "remote", "couch"],
           },
       ]			,  )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           [
               {"score": 0.2_868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
               {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
               {"score": 0.2_537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
               {"score": 0.1_474, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
               {"score": 0.1_208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
           ],
           [
               {"score": 0.2_868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
               {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
               {"score": 0.2_537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
               {"score": 0.1_474, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
               {"score": 0.1_208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
           ],
       ]			,  )






  @require_tf
  @unittest.skip("Zero Shot Object Detection not implemented in TF"     )
  def   snake_case		(    self     ):







   """simple docstring"""
   pass






  @require_torch
  @slow
  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  0.2
   lowerCamelCase_      =  pipeline("zero-shot-object-detection"     )

   lowerCamelCase_      =  object_detector(
       "http://images.cocodataset.org/val2017/000000039769.jpg"			,  candidate_labels=["cat", "remote", "couch"]			,  threshold=UpperCamelCase			,  )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.2_868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
           {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
           {"score": 0.2_537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
       ]			,  )







  @require_torch
  @slow
  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  2
   lowerCamelCase_      =  pipeline("zero-shot-object-detection"     )

   lowerCamelCase_      =  object_detector(
       "http://images.cocodataset.org/val2017/000000039769.jpg"			,  candidate_labels=["cat", "remote", "couch"]			,  top_k=UpperCamelCase			,  )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.2_868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
           {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
       ]			,  )









from ...processing_utils import ProcessorMixin


class 				UpperCamelCase_    ( UpperCAmelCase__     ):

    '''simple docstring'''






    UpperCAmelCase__							  =      '''SpeechT5FeatureExtractor'''
    UpperCAmelCase__							  =      '''SpeechT5Tokenizer'''


    def __init__(			self      :	Any					, UpperCAmelCase__      :	Dict					, UpperCAmelCase__      :	Tuple)						->Union[str, Any]:





           '''simple docstring'''

           super().__init__(UpperCAmelCase__					, UpperCAmelCase__)


    def __call__(			self      :	Dict					, *UpperCAmelCase__      :	List[str]					, **UpperCAmelCase__      :	Any)						->Optional[Any]:





           '''simple docstring'''

           A__       =    kwargs.pop('''audio'''					, UpperCAmelCase__)
           A__       =    kwargs.pop('''text'''					, UpperCAmelCase__)
           A__       =    kwargs.pop('''text_target'''					, UpperCAmelCase__)
           A__       =    kwargs.pop('''audio_target'''					, UpperCAmelCase__)
           A__       =    kwargs.pop('''sampling_rate'''					, UpperCAmelCase__)

           if audio is not None and text is not None:
                  raise ValueError(
                      '''Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?''')
           if audio_target is not None and text_target is not None:
                  raise ValueError(
                      '''Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?''')
           if audio is None and audio_target is None and text is None and text_target is None:
                  raise ValueError(
                      '''You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process.''')

           if audio is not None:
                  A__       =    self.feature_extractor(UpperCAmelCase__					, *UpperCAmelCase__					, sampling_rate=UpperCAmelCase__					, **UpperCAmelCase__)
           elif text is not None:
                  A__       =    self.tokenizer(UpperCAmelCase__					, **UpperCAmelCase__)
           else:
                  A__       =    None

           if audio_target is not None:
                  A__       =    self.feature_extractor(audio_target=UpperCAmelCase__					, *UpperCAmelCase__					, sampling_rate=UpperCAmelCase__					, **UpperCAmelCase__)
                  A__       =    targets['''input_values''']
           elif text_target is not None:
                  A__       =    self.tokenizer(UpperCAmelCase__					, **UpperCAmelCase__)
                  A__       =    targets['''input_ids''']
           else:
                  A__       =    None

           if inputs is None:
                  return targets

           if targets is not None:
                  A__       =    labels

                  A__       =    targets.get('''attention_mask''')
                  if decoder_attention_mask is not None:
                         A__       =    decoder_attention_mask

           return inputs


    def        SCREAMING_SNAKE_CASE							(			self      :	Optional[Any]					, *UpperCAmelCase__      :	Tuple					, **UpperCAmelCase__      :	int)						->Optional[int]:





           '''simple docstring'''

           A__       =    kwargs.pop('''input_values'''					, UpperCAmelCase__)
           A__       =    kwargs.pop('''input_ids'''					, UpperCAmelCase__)
           A__       =    kwargs.pop('''labels'''					, UpperCAmelCase__)

           if input_values is not None and input_ids is not None:
                  raise ValueError('''Cannot process both `input_values` and `input_ids` inputs.''')
           if input_values is None and input_ids is None and labels is None:
                  raise ValueError(
                      '''You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded.''')

           if input_values is not None:
                  A__       =    self.feature_extractor.pad(UpperCAmelCase__					, *UpperCAmelCase__					, **UpperCAmelCase__)
           elif input_ids is not None:
                  A__       =    self.tokenizer.pad(UpperCAmelCase__					, **UpperCAmelCase__)
           else:
                  A__       =    None

           if labels is not None:
                  if "input_ids" in labels or (isinstance(UpperCAmelCase__					, UpperCAmelCase__) and "input_ids" in labels[0]):
                         A__       =    self.tokenizer.pad(UpperCAmelCase__					, **UpperCAmelCase__)
                         A__       =    targets['''input_ids''']
                  else:
                         A__       =    self.feature_extractor.feature_size
                         A__       =    self.feature_extractor.num_mel_bins
                         A__       =    self.feature_extractor.pad(UpperCAmelCase__					, *UpperCAmelCase__					, **UpperCAmelCase__)
                         A__       =    feature_size_hack
                         A__       =    targets['''input_values''']
           else:
                  A__       =    None

           if inputs is None:
                  return targets

           if targets is not None:
                  A__       =    labels

                  A__       =    targets.get('''attention_mask''')
                  if decoder_attention_mask is not None:
                         A__       =    decoder_attention_mask

           return inputs


    def        SCREAMING_SNAKE_CASE							(			self      :	Any					, *UpperCAmelCase__      :	Dict					, **UpperCAmelCase__      :	Optional[Any])						->Optional[Any]:





           '''simple docstring'''

           return self.tokenizer.batch_decode(*UpperCAmelCase__					, **UpperCAmelCase__)





    def        SCREAMING_SNAKE_CASE							(			self      :	Optional[Any]					, *UpperCAmelCase__      :	List[Any]					, **UpperCAmelCase__      :	Union[str, Any])						->Dict:





           '''simple docstring'''

           return self.tokenizer.decode(*UpperCAmelCase__					, **UpperCAmelCase__)


