





"""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 					__UpperCAmelCase							(						unittest.TestCase  ):
				def       UpperCAmelCase      (		self    :			Optional[Any]    )	->	Tuple:



							'''simple docstring'''



							a__  :	List[str]		 =			tempfile.mkdtemp()

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

							a__  :	Union[str, Any]		 =			os.path.join(self.tmpdirname	, VOCAB_FILES_NAMES["vocab_file"]    )
							a__  :	str		 =			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(a_    ) + "\n"    )
							with open(self.merges_file	, "w"	, encoding="utf-8"    ) as fp:
										fp.write("\n".join(a_    )    )

							a__  :	Any		 =			{
							    "do_resize": True,
							    "size": 20,
							    "do_center_crop": True,
							    "crop_size": 18,
							    "do_normalize": True,
							    "image_mean": [0.4814_5466, 0.457_8275, 0.4082_1073],
							    "image_std": [0.2686_2954, 0.2613_0258, 0.2757_7711],
							}
							a__  :	List[Any]		 =			os.path.join(self.tmpdirname	, a_    )
							with open(self.image_processor_file	, "w"	, encoding="utf-8"    ) as fp:
										json.dump(a_	, a_    )
				def       UpperCAmelCase      (		self    :			Any	, **a_    :			str    )	->	Tuple:



							'''simple docstring'''



							return CLIPTokenizer.from_pretrained(self.tmpdirname	, pad_token="!"	, **a_    )
				def       UpperCAmelCase      (		self    :			List[Any]	, **a_    :			Union[str, Any]    )	->	Union[str, Any]:



							'''simple docstring'''



							return CLIPTokenizerFast.from_pretrained(self.tmpdirname	, pad_token="!"	, **a_    )
				def       UpperCAmelCase      (		self    :			Optional[Any]	, **a_    :			int    )	->	Any:



							'''simple docstring'''



							return OwlViTImageProcessor.from_pretrained(self.tmpdirname	, **a_    )
				def       UpperCAmelCase      (		self    :			str    )	->	Dict:



							'''simple docstring'''



							shutil.rmtree(self.tmpdirname    )
				def       UpperCAmelCase      (		self    :			Optional[int]    )	->	Optional[Any]:



							'''simple docstring'''



							a__  :	Optional[int]		 =			[np.random.randint(2_55	, size=(3, 30, 4_00)	, dtype=np.uinta    )]

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

							return image_inputs
				def       UpperCAmelCase      (		self    :			Tuple    )	->	Optional[int]:



							'''simple docstring'''



							a__  :	Any		 =			self.get_tokenizer()
							a__  :	int		 =			self.get_rust_tokenizer()
							a__  :	int		 =			self.get_image_processor()

							a__  :	List[Any]		 =			OwlViTProcessor(tokenizer=a_	, image_processor=a_    )
							processor_slow.save_pretrained(self.tmpdirname    )
							a__  :	Tuple		 =			OwlViTProcessor.from_pretrained(self.tmpdirname	, use_fast=a_    )

							a__  :	Dict		 =			OwlViTProcessor(tokenizer=a_	, image_processor=a_    )
							processor_fast.save_pretrained(self.tmpdirname    )
							a__  :	Any		 =			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	, a_    )
							self.assertIsInstance(processor_fast.tokenizer	, a_    )

							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	, a_    )
							self.assertIsInstance(processor_fast.image_processor	, a_    )
				def       UpperCAmelCase      (		self    :			Optional[int]    )	->	int:



							'''simple docstring'''



							a__  :	Tuple		 =			OwlViTProcessor(tokenizer=self.get_tokenizer()	, image_processor=self.get_image_processor()    )
							processor.save_pretrained(self.tmpdirname    )

							a__  :	Union[str, Any]		 =			self.get_tokenizer(bos_token="(BOS)"	, eos_token="(EOS)"    )
							a__  :	List[Any]		 =			self.get_image_processor(do_normalize=a_    )

							a__  :	Union[str, Any]		 =			OwlViTProcessor.from_pretrained(
							    self.tmpdirname	, bos_token="(BOS)"	, eos_token="(EOS)"	, do_normalize=a_    )

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

							self.assertEqual(processor.image_processor.to_json_string()	, image_processor_add_kwargs.to_json_string()    )
							self.assertIsInstance(processor.image_processor	, a_    )
				def       UpperCAmelCase      (		self    :			Dict    )	->	Dict:



							'''simple docstring'''



							a__  :	Union[str, Any]		 =			self.get_image_processor()
							a__  :	Optional[Any]		 =			self.get_tokenizer()

							a__  :	Tuple		 =			OwlViTProcessor(tokenizer=a_	, image_processor=a_    )

							a__  :	Dict		 =			self.prepare_image_inputs()

							a__  :	Optional[Any]		 =			image_processor(a_	, return_tensors="np"    )
							a__  :	Optional[Any]		 =			processor(images=a_	, 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    :			int    )	->	Optional[Any]:



							'''simple docstring'''



							a__  :	Dict		 =			self.get_image_processor()
							a__  :	List[Any]		 =			self.get_tokenizer()

							a__  :	Union[str, Any]		 =			OwlViTProcessor(tokenizer=a_	, image_processor=a_    )

							a__  :	Tuple		 =			"lower newer"

							a__  :	Optional[int]		 =			processor(text=a_	, return_tensors="np"    )

							a__  :	Dict		 =			tokenizer(a_	, return_tensors="np"    )

							for key in encoded_tok.keys():
										self.assertListEqual(encoded_tok[key][0].tolist()	, encoded_processor[key][0].tolist()    )
				def       UpperCAmelCase      (		self    :			List[Any]    )	->	Tuple:



							'''simple docstring'''



							a__  :	Any		 =			self.get_image_processor()
							a__  :	Dict		 =			self.get_tokenizer()

							a__  :	Optional[Any]		 =			OwlViTProcessor(tokenizer=a_	, image_processor=a_    )

							a__  :	List[Any]		 =			"lower newer"
							a__  :	Dict		 =			self.prepare_image_inputs()

							a__  :	List[str]		 =			processor(text=a_	, images=a_    )

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

							# test if it raises when no input is passed
							with pytest.raises(a_    ):
										processor()
				def       UpperCAmelCase      (		self    :			Optional[int]    )	->	int:



							'''simple docstring'''



							a__  :	int		 =			"google/owlvit-base-patch32"
							a__  :	List[str]		 =			OwlViTProcessor.from_pretrained(a_    )

							a__  :	Optional[int]		 =			["cat", "nasa badge"]
							a__  :	Union[str, Any]		 =			processor(text=a_    )

							a__  :	Union[str, Any]		 =			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(a_    ):
										processor()
				def       UpperCAmelCase      (		self    :			Dict    )	->	List[Any]:



							'''simple docstring'''



							a__  :	Any		 =			"google/owlvit-base-patch32"
							a__  :	str		 =			OwlViTProcessor.from_pretrained(a_    )

							a__  :	str		 =			[["cat", "nasa badge"], ["person"]]
							a__  :	Optional[int]		 =			processor(text=a_    )

							a__  :	str		 =			16
							a__  :	Optional[int]		 =			len(a_    )
							a__  :	List[Any]		 =			max([len(a_    ) 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(a_    ):
										processor()
				def       UpperCAmelCase      (		self    :			int    )	->	Any:



							'''simple docstring'''



							a__  :	Optional[Any]		 =			"google/owlvit-base-patch32"
							a__  :	Dict		 =			OwlViTProcessor.from_pretrained(a_    )

							a__  :	str		 =			["cat", "nasa badge"]
							a__  :	Optional[int]		 =			processor(text=a_    )

							a__  :	Optional[int]		 =			16
							a__  :	List[Any]		 =			inputs["input_ids"]
							a__  :	Optional[Any]		 =			[
							    [4_94_06, 23_68, 4_94_07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
							    [4_94_06, 68_41, 1_13_01, 4_94_07, 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    :			Optional[Any]    )	->	Any:



							'''simple docstring'''



							a__  :	Dict		 =			self.get_image_processor()
							a__  :	Dict		 =			self.get_tokenizer()

							a__  :	Union[str, Any]		 =			OwlViTProcessor(tokenizer=a_	, image_processor=a_    )

							a__  :	List[Any]		 =			self.prepare_image_inputs()
							a__  :	int		 =			self.prepare_image_inputs()

							a__  :	Dict		 =			processor(images=a_	, query_images=a_    )

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

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






				def       UpperCAmelCase      (		self    :			Any    )	->	Union[str, Any]:



							'''simple docstring'''



							a__  :	int		 =			self.get_image_processor()
							a__  :	int		 =			self.get_tokenizer()

							a__  :	Dict		 =			OwlViTProcessor(tokenizer=a_	, image_processor=a_    )

							a__  :	str		 =			[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

							a__  :	List[str]		 =			processor.batch_decode(a_    )
							a__  :	Optional[int]		 =			tokenizer.batch_decode(a_    )

							self.assertListEqual(a_	, a_    )






"""simple docstring"""






import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin



class 					__UpperCAmelCase							(						unittest.TestCase						,  _UpperCamelCase  ):
				def       UpperCAmelCase      (		self    :			Dict    )	->	List[Any]:



							'''simple docstring'''



							a__  :	Any		 =			load_tool("text-classification"    )
							self.tool.setup()
							a__  :	List[Any]		 =			load_tool("text-classification"	, remote=a_    )
				def       UpperCAmelCase      (		self    :			Optional[int]    )	->	Optional[Any]:



							'''simple docstring'''



							a__  :	str		 =			self.tool("That's quite cool"	, ["positive", "negative"]    )
							self.assertEqual(a_	, "positive"    )
				def       UpperCAmelCase      (		self    :			Union[str, Any]    )	->	Optional[int]:



							'''simple docstring'''



							a__  :	List[str]		 =			self.remote_tool("That's quite cool"	, ["positive", "negative"]    )
							self.assertEqual(a_	, "positive"    )
				def       UpperCAmelCase      (		self    :			Dict    )	->	Any:



							'''simple docstring'''



							a__  :	List[str]		 =			self.tool(text="That's quite cool"	, labels=["positive", "negative"]    )
							self.assertEqual(a_	, "positive"    )






				def       UpperCAmelCase      (		self    :			List[Any]    )	->	List[Any]:



							'''simple docstring'''



							a__  :	Tuple		 =			self.remote_tool(text="That's quite cool"	, labels=["positive", "negative"]    )
							self.assertEqual(a_	, "positive"    )