






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 				__A     :





											"""simple docstring"""







											@staticmethod
											def       __lowercase							( *lowerCamelCase__							,  **lowerCamelCase__    ):




																"""simple docstring"""

																pass




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





				"""simple docstring"""







				UpperCamelCase__ :      str			 =MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING


				def       __lowercase							( self							,  lowerCamelCase__							,  lowerCamelCase__							,  lowerCamelCase__    ):




									"""simple docstring"""

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

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


				def       __lowercase							( self							,  lowerCamelCase__							,  lowerCamelCase__    ):




									"""simple docstring"""

									__UpperCamelCase      :					Optional[Any]       		=object_detector(examples[0]							,  threshold=0.0    )

									__UpperCamelCase      :					List[Any]       		=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       __lowercase							( self    ):




									"""simple docstring"""

									pass


				@require_torch
				def       __lowercase							( self    ):




									"""simple docstring"""

									__UpperCamelCase      :					Union[str, Any]       		=pipeline(
									    'zero-shot-object-detection'							,  model='hf-internal-testing/tiny-random-owlvit-object-detection'    )

									__UpperCamelCase      :					List[str]       		=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}},
									    ]							,  )

									__UpperCamelCase      :					str       		=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       __lowercase							( self    ):




									"""simple docstring"""

									__UpperCamelCase      :					Tuple       		=pipeline('zero-shot-object-detection'    )

									__UpperCamelCase      :					Dict       		=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}},
									    ]							,  )

									__UpperCamelCase      :					Optional[Any]       		=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       __lowercase							( self    ):




									"""simple docstring"""

									pass


				@require_torch
				@slow
				def       __lowercase							( self    ):




									"""simple docstring"""

									__UpperCamelCase      :					int       		=0.2
									__UpperCamelCase      :					Optional[int]       		=pipeline('zero-shot-object-detection'    )

									__UpperCamelCase      :					Tuple       		=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       __lowercase							( self    ):




									"""simple docstring"""

									__UpperCamelCase      :					List[Any]       		=2
									__UpperCamelCase      :					str       		=pipeline('zero-shot-object-detection'    )

									__UpperCamelCase      :					List[Any]       		=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}},
									    ]							,  )




"""simple docstring"""





from __future__ import annotations

import unittest

from transformers import XGLMConfig, XGLMTokenizer, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers.models.xglm.modeling_tf_xglm import (
       TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
       TFXGLMForCausalLM,
       TFXGLMModel,
   )




@require_tf
class 					_lowerCAmelCase  :







 """simple docstring"""







 __UpperCAmelCase	:					Tuple   		=	XGLMConfig
 __UpperCAmelCase	:					Optional[Any]   		=	{}
 __UpperCAmelCase	:					Union[str, Any]   		=	"gelu"


 def __init__(			self							:			Optional[int],					UpperCAmelCase__							:			List[str],					UpperCAmelCase__							:			Optional[int]=1_4,					UpperCAmelCase__							:			str=7,					UpperCAmelCase__							:			Optional[Any]=True,					UpperCAmelCase__							:			List[Any]=True,					UpperCAmelCase__							:			int=True,					UpperCAmelCase__							:			List[str]=9_9,					UpperCAmelCase__							:			Union[str, Any]=3_2,					UpperCAmelCase__							:			Union[str, Any]=2,					UpperCAmelCase__							:			Union[str, Any]=4,					UpperCAmelCase__							:			Tuple=3_7,					UpperCAmelCase__							:			List[Any]="gelu",					UpperCAmelCase__							:			List[str]=0.1,					UpperCAmelCase__							:			Optional[int]=0.1,					UpperCAmelCase__							:			Tuple=5_1_2,					UpperCAmelCase__							:			Optional[Any]=0.02,					):
    __lowercase								=						parent
    __lowercase								=						batch_size
    __lowercase								=						seq_length
    __lowercase								=						is_training
    __lowercase								=						use_input_mask
    __lowercase								=						use_labels
    __lowercase								=						vocab_size
    __lowercase								=						d_model
    __lowercase								=						num_hidden_layers
    __lowercase								=						num_attention_heads
    __lowercase								=						ffn_dim
    __lowercase								=						activation_function
    __lowercase								=						activation_dropout
    __lowercase								=						attention_dropout
    __lowercase								=						max_position_embeddings
    __lowercase								=						initializer_range
    __lowercase								=						None
    __lowercase								=						0
    __lowercase								=						2
    __lowercase								=						1


 def 					_lowercase (			self							:			Union[str, Any]	):
    return XGLMConfig.from_pretrained("facebook/xglm-564M"	)


 def 					_lowercase (			self							:			Tuple	):
    __lowercase								=						tf.clip_by_value(
        ids_tensor([self.batch_size, self.seq_length],					self.vocab_size	),					clip_value_min=0,					clip_value_max=3	)

    __lowercase								=						None
    if self.use_input_mask:
       __lowercase								=						random_attention_mask([self.batch_size, self.seq_length]	)

    __lowercase								=						self.get_config()

    __lowercase								=						floats_tensor([self.num_hidden_layers, self.num_attention_heads],					2	)

    return (
        config,
        input_ids,
        input_mask,
        head_mask,
    )


 def 					_lowercase (			self							:			List[Any]	):
    return XGLMConfig(
        vocab_size=self.vocab_size,					d_model=self.hidden_size,					num_layers=self.num_hidden_layers,					attention_heads=self.num_attention_heads,					ffn_dim=self.ffn_dim,					activation_function=self.activation_function,					activation_dropout=self.activation_dropout,					attention_dropout=self.attention_dropout,					max_position_embeddings=self.max_position_embeddings,					initializer_range=self.initializer_range,					use_cache=UpperCAmelCase__,					bos_token_id=self.bos_token_id,					eos_token_id=self.eos_token_id,					pad_token_id=self.pad_token_id,					return_dict=UpperCAmelCase__,					)



 def 					_lowercase (			self							:			Dict	):
    __lowercase								=						self.prepare_config_and_inputs()

    (
        (
        __lowercase
    )		,(
        __lowercase
    )		,(
        __lowercase
    )		,(
        __lowercase
    )		,
    )								=						config_and_inputs

    __lowercase								=						{
        "input_ids": input_ids,
        "head_mask": head_mask,
    }

    return config, inputs_dict







@require_tf
class 					_lowerCAmelCase  (	lowercase     ,lowercase     ,unittest.TestCase   ):







 """simple docstring"""







 __UpperCAmelCase	:					Union[str, Any]   		=	(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
 __UpperCAmelCase	:					List[str]   		=	(TFXGLMForCausalLM,) if is_tf_available() else ()
 __UpperCAmelCase	:					Any   		=	(
     {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {}
 )
 __UpperCAmelCase	:					Optional[Any]   		=	False
 __UpperCAmelCase	:					List[str]   		=	False
 __UpperCAmelCase	:					int   		=	False


 def 					_lowercase (			self							:			Optional[Any]	):
    __lowercase								=						TFXGLMModelTester(self	)
    __lowercase								=						ConfigTester(self,					config_class=UpperCAmelCase__,					n_embd=3_7	)


 def 					_lowercase (			self							:			Any	):
    self.config_tester.run_common_tests()


 @slow
 def 					_lowercase (			self							:			List[str]	):
    for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowercase								=						TFXGLMModel.from_pretrained(UpperCAmelCase__	)
       self.assertIsNotNone(UpperCAmelCase__	)



 @unittest.skip(reason="Currently, model embeddings are going to undergo a major refactor."	)
 def 					_lowercase (			self							:			int	):
    super().test_resize_token_embeddings()







@require_tf
class 					_lowerCAmelCase  (	unittest.TestCase   ):







 """simple docstring"""







 @slow
 def 					_lowercase (			self							:			Dict,					UpperCAmelCase__							:			Optional[int]=True	):
    __lowercase								=						TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"	)
    __lowercase								=						tf.convert_to_tensor([[2, 2_6_8, 9_8_6_5]],					dtype=tf.intaa	)  # The dog
    # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other
    # fmt: off
    __lowercase								=						[2, 2_6_8, 9_8_6_5, 6_7, 1_1, 1_9_8_8, 5_7_2_5_2, 9_8_6_5, 5, 9_8_4, 6_7, 1_9_8_8, 2_1_3_8_3_8, 1_6_5_8, 5_3, 7_0_4_4_6, 3_3, 6_6_5_7, 2_7_8, 1_5_8_1]
    # fmt: on
    __lowercase								=						model.generate(UpperCAmelCase__,					do_sample=UpperCAmelCase__,					num_beams=1	)
    if verify_outputs:
       self.assertListEqual(output_ids[0].numpy().tolist(),					UpperCAmelCase__	)


 @slow
 def 					_lowercase (			self							:			List[Any]	):
    __lowercase								=						XGLMTokenizer.from_pretrained("facebook/xglm-564M"	)
    __lowercase								=						TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"	)

    tf.random.set_seed(0	)
    __lowercase								=						tokenizer("Today is a nice day and",					return_tensors="tf"	)
    __lowercase								=						tokenized.input_ids
    # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices)
    with tf.device(":/CPU:0"	):
       __lowercase								=						model.generate(UpperCAmelCase__,					do_sample=UpperCAmelCase__,					seed=[7, 0]	)
    __lowercase								=						tokenizer.decode(output_ids[0],					skip_special_tokens=UpperCAmelCase__	)

    __lowercase								=						(
        "Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due"
    )
    self.assertEqual(UpperCAmelCase__,					UpperCAmelCase__	)



 @slow
 def 					_lowercase (			self							:			Dict	):
    __lowercase								=						TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"	)
    __lowercase								=						XGLMTokenizer.from_pretrained("facebook/xglm-564M"	)

    __lowercase								=						"left"

    # use different length sentences to test batching
    __lowercase								=						[
        "This is an extremelly long sentence that only exists to test the ability of the model to cope with "
        "left-padding, such as in batched generation. The output for the sequence below should be the same "
        "regardless of whether left padding is applied or not. When",
        "Hello, my dog is a little",
    ]

    __lowercase								=						tokenizer(UpperCAmelCase__,					return_tensors="tf",					padding=UpperCAmelCase__	)
    __lowercase								=						inputs["input_ids"]

    __lowercase								=						model.generate(input_ids=UpperCAmelCase__,					attention_mask=inputs["attention_mask"],					max_new_tokens=1_2	)

    __lowercase								=						tokenizer(sentences[0],					return_tensors="tf"	).input_ids
    __lowercase								=						model.generate(input_ids=UpperCAmelCase__,					max_new_tokens=1_2	)

    __lowercase								=						tokenizer(sentences[1],					return_tensors="tf"	).input_ids
    __lowercase								=						model.generate(input_ids=UpperCAmelCase__,					max_new_tokens=1_2	)

    __lowercase								=						tokenizer.batch_decode(UpperCAmelCase__,					skip_special_tokens=UpperCAmelCase__	)
    __lowercase								=						tokenizer.decode(output_non_padded[0],					skip_special_tokens=UpperCAmelCase__	)
    __lowercase								=						tokenizer.decode(output_padded[0],					skip_special_tokens=UpperCAmelCase__	)

    __lowercase								=						[
        "This is an extremelly long sentence that only exists to test the ability of the model to cope with "
        "left-padding, such as in batched generation. The output for the sequence below should be the same "
        "regardless of whether left padding is applied or not. When left padding is applied, the sequence will be "
        "a single",
        "Hello, my dog is a little bit of a shy one, but he is very friendly",
    ]
    self.assertListEqual(UpperCAmelCase__,					UpperCAmelCase__	)
    self.assertListEqual(UpperCAmelCase__,					[non_padded_sentence, padded_sentence]	)




