





'''simple docstring'''




import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
    from PIL import Image

    from transformers import CLIPImageProcessor, CLIPProcessor






@require_vision
class UpperCAmelCase				(		unittest.TestCase				):



    def        __lowerCamelCase    (	self       ):
     __UpperCAmelCase       = tempfile.mkdtemp()

     # fmt: off
     __UpperCAmelCase       = ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>']
     # fmt: on
     __UpperCAmelCase       = dict(zip(_snake_case  ,      range(len(_snake_case       )       )       )       )
     __UpperCAmelCase       = ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>', '']
     __UpperCAmelCase       = {'unk_token': '<unk>'}

     __UpperCAmelCase       = os.path.join(self.tmpdirname  ,      VOCAB_FILES_NAMES['vocab_file']       )
     __UpperCAmelCase       = os.path.join(self.tmpdirname  ,      VOCAB_FILES_NAMES['merges_file']       )
     with open(self.vocab_file  ,      'w'  ,      encoding='utf-8'       ) as fp:
      fp.write(json.dumps(_snake_case       ) + '\n'       )
     with open(self.merges_file  ,      'w'  ,      encoding='utf-8'       ) as fp:
      fp.write('\n'.join(_snake_case       )       )

     __UpperCAmelCase       = {
         'do_resize': True,
         'size': 20,
         'do_center_crop': True,
         'crop_size': 18,
         'do_normalize': True,
         'image_mean': [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
         'image_std': [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
     }
     __UpperCAmelCase       = os.path.join(self.tmpdirname  ,      _snake_case       )
     with open(self.image_processor_file  ,      'w'  ,      encoding='utf-8'       ) as fp:
      json.dump(_snake_case  ,      _snake_case       )



    def        __lowerCamelCase    (	self  ,      **__A       ):
     return CLIPTokenizer.from_pretrained(self.tmpdirname  ,      **_snake_case       )



    def        __lowerCamelCase    (	self  ,      **__A       ):
     return CLIPTokenizerFast.from_pretrained(self.tmpdirname  ,      **_snake_case       )



    def        __lowerCamelCase    (	self  ,      **__A       ):
     return CLIPImageProcessor.from_pretrained(self.tmpdirname  ,      **_snake_case       )



    def        __lowerCamelCase    (	self       ):
     shutil.rmtree(self.tmpdirname       )



    def        __lowerCamelCase    (	self       ):
     __UpperCAmelCase       = [np.random.randint(255  ,      size=(3, 30, 400)  ,      dtype=np.uinta       )]

     __UpperCAmelCase       = [Image.fromarray(np.moveaxis(_snake_case  ,      0  ,      -1       )       ) for x in image_inputs]

     return image_inputs



    def        __lowerCamelCase    (	self       ):
     __UpperCAmelCase       = self.get_tokenizer()
     __UpperCAmelCase       = self.get_rust_tokenizer()
     __UpperCAmelCase       = self.get_image_processor()

     __UpperCAmelCase       = CLIPProcessor(tokenizer=_snake_case  ,      image_processor=_snake_case       )
     processor_slow.save_pretrained(self.tmpdirname       )
     __UpperCAmelCase       = CLIPProcessor.from_pretrained(self.tmpdirname  ,      use_fast=_snake_case       )

     __UpperCAmelCase       = CLIPProcessor(tokenizer=_snake_case  ,      image_processor=_snake_case       )
     processor_fast.save_pretrained(self.tmpdirname       )
     __UpperCAmelCase       = CLIPProcessor.from_pretrained(self.tmpdirname       )

     self.assertEqual(processor_slow.tokenizer.get_vocab()  ,      tokenizer_slow.get_vocab()       )
     self.assertEqual(processor_fast.tokenizer.get_vocab()  ,      tokenizer_fast.get_vocab()       )
     self.assertEqual(tokenizer_slow.get_vocab()  ,      tokenizer_fast.get_vocab()       )
     self.assertIsInstance(processor_slow.tokenizer  ,      _snake_case       )
     self.assertIsInstance(processor_fast.tokenizer  ,      _snake_case       )

     self.assertEqual(processor_slow.image_processor.to_json_string()  ,      image_processor.to_json_string()       )
     self.assertEqual(processor_fast.image_processor.to_json_string()  ,      image_processor.to_json_string()       )
     self.assertIsInstance(processor_slow.image_processor  ,      _snake_case       )
     self.assertIsInstance(processor_fast.image_processor  ,      _snake_case       )



    def        __lowerCamelCase    (	self       ):
     __UpperCAmelCase       = CLIPProcessor(tokenizer=self.get_tokenizer()  ,      image_processor=self.get_image_processor()       )
     processor.save_pretrained(self.tmpdirname       )

     __UpperCAmelCase       = self.get_tokenizer(bos_token='(BOS)'  ,      eos_token='(EOS)'       )
     __UpperCAmelCase       = self.get_image_processor(do_normalize=_snake_case  ,      padding_value=1.0       )

     __UpperCAmelCase       = CLIPProcessor.from_pretrained(
         self.tmpdirname  ,      bos_token='(BOS)'  ,      eos_token='(EOS)'  ,      do_normalize=_snake_case  ,      padding_value=1.0       )

     self.assertEqual(processor.tokenizer.get_vocab()  ,      tokenizer_add_kwargs.get_vocab()       )
     self.assertIsInstance(processor.tokenizer  ,      _snake_case       )

     self.assertEqual(processor.image_processor.to_json_string()  ,      image_processor_add_kwargs.to_json_string()       )
     self.assertIsInstance(processor.image_processor  ,      _snake_case       )



    def        __lowerCamelCase    (	self       ):
     __UpperCAmelCase       = self.get_image_processor()
     __UpperCAmelCase       = self.get_tokenizer()

     __UpperCAmelCase       = CLIPProcessor(tokenizer=_snake_case  ,      image_processor=_snake_case       )

     __UpperCAmelCase       = self.prepare_image_inputs()

     __UpperCAmelCase       = image_processor(_snake_case  ,      return_tensors='np'       )
     __UpperCAmelCase       = processor(images=_snake_case  ,      return_tensors='np'       )

     for key in input_image_proc.keys():
      self.assertAlmostEqual(input_image_proc[key].sum()  ,      input_processor[key].sum()  ,      delta=1E-2       )



    def        __lowerCamelCase    (	self       ):
     __UpperCAmelCase       = self.get_image_processor()
     __UpperCAmelCase       = self.get_tokenizer()

     __UpperCAmelCase       = CLIPProcessor(tokenizer=_snake_case  ,      image_processor=_snake_case       )

     __UpperCAmelCase       = 'lower newer'

     __UpperCAmelCase       = processor(text=_snake_case       )

     __UpperCAmelCase       = tokenizer(_snake_case       )

     for key in encoded_tok.keys():
      self.assertListEqual(encoded_tok[key]  ,      encoded_processor[key]       )



    def        __lowerCamelCase    (	self       ):
     __UpperCAmelCase       = self.get_image_processor()
     __UpperCAmelCase       = self.get_tokenizer()

     __UpperCAmelCase       = CLIPProcessor(tokenizer=_snake_case  ,      image_processor=_snake_case       )

     __UpperCAmelCase       = 'lower newer'
     __UpperCAmelCase       = self.prepare_image_inputs()

     __UpperCAmelCase       = processor(text=_snake_case  ,      images=_snake_case       )

     self.assertListEqual(list(inputs.keys()       )  ,      ['input_ids', 'attention_mask', 'pixel_values']       )

     # test if it raises when no input is passed
     with pytest.raises(_snake_case       ):
      processor()



    def        __lowerCamelCase    (	self       ):
     __UpperCAmelCase       = self.get_image_processor()
     __UpperCAmelCase       = self.get_tokenizer()

     __UpperCAmelCase       = CLIPProcessor(tokenizer=_snake_case  ,      image_processor=_snake_case       )

     __UpperCAmelCase       = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     __UpperCAmelCase       = processor.batch_decode(_snake_case       )
     __UpperCAmelCase       = tokenizer.batch_decode(_snake_case       )

     self.assertListEqual(_snake_case  ,      _snake_case       )




    def        __lowerCamelCase    (	self       ):
     __UpperCAmelCase       = self.get_image_processor()
     __UpperCAmelCase       = self.get_tokenizer()

     __UpperCAmelCase       = CLIPProcessor(tokenizer=_snake_case  ,      image_processor=_snake_case       )

     __UpperCAmelCase       = 'lower newer'
     __UpperCAmelCase       = self.prepare_image_inputs()

     __UpperCAmelCase       = processor(text=_snake_case  ,      images=_snake_case       )

     self.assertListEqual(list(inputs.keys()       )  ,      processor.model_input_names       )










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   __lowerCAmelCase  :
												"""simple docstring"""






												@staticmethod
												def      _a				(  *_snake_case       :							int					,       **_snake_case       :							List[str]       ):






															"""simple docstring"""


															pass


@is_pipeline_test
@require_vision
@require_torch
class   __lowerCAmelCase  (		unittest.TestCase   ):
					"""simple docstring"""






					A__					:		List[str]      							=     MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING



					def      _a				(  self       :							Any					,       _snake_case       :							Union[str, Any]					,       _snake_case       :							Tuple					,       _snake_case       :							Optional[Any]       ):






								"""simple docstring"""


								A__	  =     pipeline(
								    'zero-shot-object-detection'					,       model='hf-internal-testing/tiny-random-owlvit-object-detection'       )

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



					def      _a				(  self       :							int					,       _snake_case       :							int					,       _snake_case       :							List[str]       ):






								"""simple docstring"""


								A__	  =     object_detector(examples[0]					,       threshold=0.0       )

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



					@require_tf
					@unittest.skip('Zero Shot Object Detection not implemented in TF'       )
					def      _a				(  self       :							List[str]       ):






								"""simple docstring"""


								pass



					@require_torch
					def      _a				(  self       :							Optional[int]       ):






								"""simple docstring"""


								A__	  =     pipeline(
								    'zero-shot-object-detection'					,       model='hf-internal-testing/tiny-random-owlvit-object-detection'       )

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

								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [
								        {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								        {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
								        {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								    ]					,       )

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

								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [
								        [
								            {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								            {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
								            {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								        ]
								    ]					,       )



					@require_torch
					@slow
					def      _a				(  self       :							int       ):






								"""simple docstring"""


								A__	  =     pipeline('zero-shot-object-detection'       )

								A__	  =     object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,       candidate_labels=['cat', 'remote', 'couch']					,       )
								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [
								        {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								        {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								        {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								        {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								    ]					,       )

								A__	  =     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(_snake_case					,       decimals=4       )					,       [
								        [
								            {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								            {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								            {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								            {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								            {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								        ],
								        [
								            {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								            {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								            {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								            {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								            {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								        ],
								    ]					,       )



					@require_tf
					@unittest.skip('Zero Shot Object Detection not implemented in TF'       )
					def      _a				(  self       :							int       ):






								"""simple docstring"""


								pass



					@require_torch
					@slow
					def      _a				(  self       :							str       ):






								"""simple docstring"""


								A__	  =     0.2
								A__	  =     pipeline('zero-shot-object-detection'       )

								A__	  =     object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,       candidate_labels=['cat', 'remote', 'couch']					,       threshold=_snake_case					,       )
								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [
								        {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								        {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								    ]					,       )



					@require_torch
					@slow
					def      _a				(  self       :							Any       ):






								"""simple docstring"""


								A__	  =     2
								A__	  =     pipeline('zero-shot-object-detection'       )

								A__	  =     object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,       candidate_labels=['cat', 'remote', 'couch']					,       top_k=_snake_case					,       )
								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [
								        {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								    ]					,       )




