






"""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 OwlViTImageProcessor, OwlViTProcessor







@require_vision
class    lowercase__			(							unittest.TestCase   ):


			def 	UpperCAmelCase__						(      self     :  Optional[Any]					):
										lowerCamelCase_       :							List[Any]									=tempfile.mkdtemp()

										# fmt: off
										lowerCamelCase_       :							Dict									=["""""", """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
										lowerCamelCase_       :							List[Any]									=dict(zip(snake_case__   ,       range(len(snake_case__					)					)					)					)
										lowerCamelCase_       :							List[Any]									=["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
										lowerCamelCase_       :							Union[str, Any]									={"""unk_token""": """<unk>"""}

										lowerCamelCase_       :							Any									=os.path.join(self.tmpdirname   ,       VOCAB_FILES_NAMES["vocab_file"]					)
										lowerCamelCase_       :							Dict									=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__					)					)

										lowerCamelCase_       :							Union[str, Any]									={
										    """do_resize""": True,
										    """size""": 20,
										    """do_center_crop""": True,
										    """crop_size""": 18,
										    """do_normalize""": True,
										    """image_mean""": [0.48_145_466, 0.4_578_275, 0.40_821_073],
										    """image_std""": [0.26_862_954, 0.26_130_258, 0.27_577_711],
										}
										lowerCamelCase_       :							str									=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 	UpperCAmelCase__						(      self     :  List[Any]   ,       **snake_case__     :  int					):
										return CLIPTokenizer.from_pretrained(self.tmpdirname   ,       pad_token="!"   ,       **snake_case__					)


			def 	UpperCAmelCase__						(      self     :  Optional[int]   ,       **snake_case__     :  Any					):
										return CLIPTokenizerFast.from_pretrained(self.tmpdirname   ,       pad_token="!"   ,       **snake_case__					)


			def 	UpperCAmelCase__						(      self     :  Optional[int]   ,       **snake_case__     :  Optional[Any]					):
										return OwlViTImageProcessor.from_pretrained(self.tmpdirname   ,       **snake_case__					)


			def 	UpperCAmelCase__						(      self     :  Dict					):
										shutil.rmtree(self.tmpdirname					)


			def 	UpperCAmelCase__						(      self     :  Dict					):
										lowerCamelCase_       :							str									=[np.random.randint(255   ,       size=(3, 30, 400)   ,       dtype=np.uinta					)]

										lowerCamelCase_       :							List[str]									=[Image.fromarray(np.moveaxis(snake_case__   ,       0   ,       -1					)					) for x in image_inputs]

										return image_inputs


			def 	UpperCAmelCase__						(      self     :  Dict					):
										lowerCamelCase_       :							Any									=self.get_tokenizer()
										lowerCamelCase_       :							Any									=self.get_rust_tokenizer()
										lowerCamelCase_       :							int									=self.get_image_processor()

										lowerCamelCase_       :							Union[str, Any]									=OwlViTProcessor(tokenizer=snake_case__   ,       image_processor=snake_case__					)
										processor_slow.save_pretrained(self.tmpdirname					)
										lowerCamelCase_       :							Dict									=OwlViTProcessor.from_pretrained(self.tmpdirname   ,       use_fast=snake_case__					)

										lowerCamelCase_       :							Tuple									=OwlViTProcessor(tokenizer=snake_case__   ,       image_processor=snake_case__					)
										processor_fast.save_pretrained(self.tmpdirname					)
										lowerCamelCase_       :							Dict									=OwlViTProcessor.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 	UpperCAmelCase__						(      self     :  Optional[int]					):
										lowerCamelCase_       :							Tuple									=OwlViTProcessor(tokenizer=self.get_tokenizer()   ,       image_processor=self.get_image_processor()					)
										processor.save_pretrained(self.tmpdirname					)

										lowerCamelCase_       :							Tuple									=self.get_tokenizer(bos_token="(BOS)"   ,       eos_token="(EOS)"					)
										lowerCamelCase_       :							Union[str, Any]									=self.get_image_processor(do_normalize=snake_case__					)

										lowerCamelCase_       :							Optional[Any]									=OwlViTProcessor.from_pretrained(
										    self.tmpdirname   ,       bos_token="(BOS)"   ,       eos_token="(EOS)"   ,       do_normalize=snake_case__					)

										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 	UpperCAmelCase__						(      self     :  Union[str, Any]					):
										lowerCamelCase_       :							Optional[Any]									=self.get_image_processor()
										lowerCamelCase_       :							List[str]									=self.get_tokenizer()

										lowerCamelCase_       :							Any									=OwlViTProcessor(tokenizer=snake_case__   ,       image_processor=snake_case__					)

										lowerCamelCase_       :							Union[str, Any]									=self.prepare_image_inputs()

										lowerCamelCase_       :							List[Any]									=image_processor(snake_case__   ,       return_tensors="np"					)
										lowerCamelCase_       :							Dict									=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 	UpperCAmelCase__						(      self     :  Any					):
										lowerCamelCase_       :							Tuple									=self.get_image_processor()
										lowerCamelCase_       :							Optional[int]									=self.get_tokenizer()

										lowerCamelCase_       :							Tuple									=OwlViTProcessor(tokenizer=snake_case__   ,       image_processor=snake_case__					)

										lowerCamelCase_       :							Dict									="""lower newer"""

										lowerCamelCase_       :							List[Any]									=processor(text=snake_case__   ,       return_tensors="np"					)

										lowerCamelCase_       :							Tuple									=tokenizer(snake_case__   ,       return_tensors="np"					)

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


			def 	UpperCAmelCase__						(      self     :  Tuple					):
										lowerCamelCase_       :							Tuple									=self.get_image_processor()
										lowerCamelCase_       :							List[Any]									=self.get_tokenizer()

										lowerCamelCase_       :							List[str]									=OwlViTProcessor(tokenizer=snake_case__   ,       image_processor=snake_case__					)

										lowerCamelCase_       :							List[str]									="""lower newer"""
										lowerCamelCase_       :							int									=self.prepare_image_inputs()

										lowerCamelCase_       :							List[str]									=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 	UpperCAmelCase__						(      self     :  Union[str, Any]					):
										lowerCamelCase_       :							str									="""google/owlvit-base-patch32"""
										lowerCamelCase_       :							Union[str, Any]									=OwlViTProcessor.from_pretrained(snake_case__					)

										lowerCamelCase_       :							Optional[int]									=["""cat""", """nasa badge"""]
										lowerCamelCase_       :							Optional[Any]									=processor(text=snake_case__					)

										lowerCamelCase_       :							int									=16
										self.assertListEqual(list(inputs.keys()					)   ,       ["input_ids", "attention_mask"]					)
										self.assertEqual(inputs["input_ids"].shape   ,       (2, seq_length)					)

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


			def 	UpperCAmelCase__						(      self     :  Optional[int]					):
										lowerCamelCase_       :							List[Any]									="""google/owlvit-base-patch32"""
										lowerCamelCase_       :							Optional[int]									=OwlViTProcessor.from_pretrained(snake_case__					)

										lowerCamelCase_       :							int									=[["""cat""", """nasa badge"""], ["""person"""]]
										lowerCamelCase_       :							Tuple									=processor(text=snake_case__					)

										lowerCamelCase_       :							List[Any]									=16
										lowerCamelCase_       :							Dict									=len(snake_case__					)
										lowerCamelCase_       :							str									=max([len(snake_case__					) for texts in input_texts]					)

										self.assertListEqual(list(inputs.keys()					)   ,       ["input_ids", "attention_mask"]					)
										self.assertEqual(inputs["input_ids"].shape   ,       (batch_size * num_max_text_queries, seq_length)					)

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


			def 	UpperCAmelCase__						(      self     :  Optional[Any]					):
										lowerCamelCase_       :							int									="""google/owlvit-base-patch32"""
										lowerCamelCase_       :							Any									=OwlViTProcessor.from_pretrained(snake_case__					)

										lowerCamelCase_       :							Optional[Any]									=["""cat""", """nasa badge"""]
										lowerCamelCase_       :							List[Any]									=processor(text=snake_case__					)

										lowerCamelCase_       :							Union[str, Any]									=16
										lowerCamelCase_       :							str									=inputs["""input_ids"""]
										lowerCamelCase_       :							Optional[int]									=[
										    [4_9406, 2368, 4_9407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
										    [4_9406, 6841, 1_1301, 4_9407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
										]

										self.assertListEqual(list(inputs.keys()					)   ,       ["input_ids", "attention_mask"]					)
										self.assertEqual(inputs["input_ids"].shape   ,       (2, seq_length)					)
										self.assertListEqual(list(input_ids[0]					)   ,       predicted_ids[0]					)
										self.assertListEqual(list(input_ids[1]					)   ,       predicted_ids[1]					)


			def 	UpperCAmelCase__						(      self     :  Union[str, Any]					):
										lowerCamelCase_       :							Tuple									=self.get_image_processor()
										lowerCamelCase_       :							Optional[Any]									=self.get_tokenizer()

										lowerCamelCase_       :							Optional[Any]									=OwlViTProcessor(tokenizer=snake_case__   ,       image_processor=snake_case__					)

										lowerCamelCase_       :							Union[str, Any]									=self.prepare_image_inputs()
										lowerCamelCase_       :							Any									=self.prepare_image_inputs()

										lowerCamelCase_       :							List[str]									=processor(images=snake_case__   ,       query_images=snake_case__					)

										self.assertListEqual(list(inputs.keys()					)   ,       ["query_pixel_values", "pixel_values"]					)

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


			def 	UpperCAmelCase__						(      self     :  Dict					):
										lowerCamelCase_       :							int									=self.get_image_processor()
										lowerCamelCase_       :							Optional[int]									=self.get_tokenizer()

										lowerCamelCase_       :							Dict									=OwlViTProcessor(tokenizer=snake_case__   ,       image_processor=snake_case__					)

										lowerCamelCase_       :							Tuple									=[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

										lowerCamelCase_       :							Optional[Any]									=processor.batch_decode(snake_case__					)
										lowerCamelCase_       :							Union[str, Any]									=tokenizer.batch_decode(snake_case__					)

										self.assertListEqual(snake_case__   ,       snake_case__					)







"""simple docstring"""






from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
							if not (is_transformers_available() and is_torch_available()):
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from ...utils.dummy_torch_and_transformers_objects import (
							    ImageTextPipelineOutput,
							    UniDiffuserPipeline,
							)
else:
							from .modeling_text_decoder import UniDiffuserTextDecoder
							from .modeling_uvit import UniDiffuserModel, UTransformeraDModel
							from .pipeline_unidiffuser import ImageTextPipelineOutput, UniDiffuserPipeline
