





'''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    lowerCAmelCase_		(		unittest.TestCase		):




 def 							_snake_case   (	self		)    ->							List[Any]:
     _lowerCAmelCase				=      tempfile.mkdtemp()

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

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

     _lowerCAmelCase				=      {
         "do_resize": True,
         "size": 20,
         "do_center_crop": True,
         "crop_size": 18,
         "do_normalize": True,
         "image_mean": [0.48145466, 0.4578275, 0.40821073],
         "image_std": [0.26862954, 0.26130258, 0.27577711],
     }
     _lowerCAmelCase				=      os.path.join(self.tmpdirname			,     _lowerCAmelCase		)
     with open(self.image_processor_file			,     "w"			,     encoding="utf-8"		) as fp:
         json.dump(_lowerCAmelCase			,     _lowerCAmelCase		)




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							Dict:
     return CLIPTokenizer.from_pretrained(self.tmpdirname			,     pad_token="!"			,     **_lowerCAmelCase		)




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




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							Optional[Any]:
     return OwlViTImageProcessor.from_pretrained(self.tmpdirname			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Dict:
     shutil.rmtree(self.tmpdirname		)




 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      [np.random.randint(255			,     size=(3, 30, 400)			,     dtype=np.uinta		)]

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

     return image_inputs




 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      self.get_tokenizer()
     _lowerCAmelCase				=      self.get_rust_tokenizer()
     _lowerCAmelCase				=      self.get_image_processor()

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)
     processor_slow.save_pretrained(self.tmpdirname		)
     _lowerCAmelCase				=      OwlViTProcessor.from_pretrained(self.tmpdirname			,     use_fast=_lowerCAmelCase		)

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)
     processor_fast.save_pretrained(self.tmpdirname		)
     _lowerCAmelCase				=      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			,     _lowerCAmelCase		)
     self.assertIsInstance(processor_fast.tokenizer			,     _lowerCAmelCase		)

     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			,     _lowerCAmelCase		)
     self.assertIsInstance(processor_fast.image_processor			,     _lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=self.get_tokenizer()			,     image_processor=self.get_image_processor()		)
     processor.save_pretrained(self.tmpdirname		)

     _lowerCAmelCase				=      self.get_tokenizer(bos_token="(BOS)"			,     eos_token="(EOS)"		)
     _lowerCAmelCase				=      self.get_image_processor(do_normalize=_lowerCAmelCase		)

     _lowerCAmelCase				=      OwlViTProcessor.from_pretrained(
         self.tmpdirname			,     bos_token="(BOS)"			,     eos_token="(EOS)"			,     do_normalize=_lowerCAmelCase		)

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

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




 def 							_snake_case   (	self		)    ->							Tuple:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_tokenizer()

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)

     _lowerCAmelCase				=      self.prepare_image_inputs()

     _lowerCAmelCase				=      image_processor(_lowerCAmelCase			,     return_tensors="np"		)
     _lowerCAmelCase				=      processor(images=_lowerCAmelCase			,     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 							_snake_case   (	self		)    ->							Optional[Any]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_tokenizer()

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)

     _lowerCAmelCase				=      "lower newer"

     _lowerCAmelCase				=      processor(text=_lowerCAmelCase			,     return_tensors="np"		)

     _lowerCAmelCase				=      tokenizer(_lowerCAmelCase			,     return_tensors="np"		)

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




 def 							_snake_case   (	self		)    ->							List[str]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_tokenizer()

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)

     _lowerCAmelCase				=      "lower newer"
     _lowerCAmelCase				=      self.prepare_image_inputs()

     _lowerCAmelCase				=      processor(text=_lowerCAmelCase			,     images=_lowerCAmelCase		)

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

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




 def 							_snake_case   (	self		)    ->							int:
     _lowerCAmelCase				=      "google/owlvit-base-patch32"
     _lowerCAmelCase				=      OwlViTProcessor.from_pretrained(_lowerCAmelCase		)

     _lowerCAmelCase				=      ["cat", "nasa badge"]
     _lowerCAmelCase				=      processor(text=_lowerCAmelCase		)

     _lowerCAmelCase				=      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(_lowerCAmelCase		):
         processor()




 def 							_snake_case   (	self		)    ->							Optional[Any]:
     _lowerCAmelCase				=      "google/owlvit-base-patch32"
     _lowerCAmelCase				=      OwlViTProcessor.from_pretrained(_lowerCAmelCase		)

     _lowerCAmelCase				=      [["cat", "nasa badge"], ["person"]]
     _lowerCAmelCase				=      processor(text=_lowerCAmelCase		)

     _lowerCAmelCase				=      16
     _lowerCAmelCase				=      len(_lowerCAmelCase		)
     _lowerCAmelCase				=      max([len(_lowerCAmelCase		) 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(_lowerCAmelCase		):
         processor()




 def 							_snake_case   (	self		)    ->							Any:
     _lowerCAmelCase				=      "google/owlvit-base-patch32"
     _lowerCAmelCase				=      OwlViTProcessor.from_pretrained(_lowerCAmelCase		)

     _lowerCAmelCase				=      ["cat", "nasa badge"]
     _lowerCAmelCase				=      processor(text=_lowerCAmelCase		)

     _lowerCAmelCase				=      16
     _lowerCAmelCase				=      inputs["input_ids"]
     _lowerCAmelCase				=      [
         [49406, 2368, 49407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [49406, 6841, 11301, 49407, 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 							_snake_case   (	self		)    ->							Optional[Any]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_tokenizer()

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)

     _lowerCAmelCase				=      self.prepare_image_inputs()
     _lowerCAmelCase				=      self.prepare_image_inputs()

     _lowerCAmelCase				=      processor(images=_lowerCAmelCase			,     query_images=_lowerCAmelCase		)

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

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





 def 							_snake_case   (	self		)    ->							Optional[int]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_tokenizer()

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)

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

     _lowerCAmelCase				=      processor.batch_decode(_lowerCAmelCase		)
     _lowerCAmelCase				=      tokenizer.batch_decode(_lowerCAmelCase		)

     self.assertListEqual(_lowerCAmelCase			,     _lowerCAmelCase		)









'''simple docstring'''


import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
       from transformers import TvltImageProcessor

if is_speech_available():
       from transformers import TvltFeatureExtractor

from transformers import TvltProcessor

@require_torch
class    lowerCAmelCase_		(		unittest.TestCase		):




 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      "ZinengTang/tvlt-base"
     _lowerCAmelCase				=      tempfile.mkdtemp()




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							Any:
     return TvltImageProcessor.from_pretrained(self.checkpoint			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							str:
     return TvltFeatureExtractor.from_pretrained(self.checkpoint			,     **_lowerCAmelCase		)




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




 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)
     processor.save_pretrained(self.tmpdirname		)
     _lowerCAmelCase				=      TvltProcessor.from_pretrained(self.tmpdirname		)

     self.assertIsInstance(processor.feature_extractor			,     _lowerCAmelCase		)
     self.assertIsInstance(processor.image_processor			,     _lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Any:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([12000]		)

     _lowerCAmelCase				=      feature_extractor(_lowerCAmelCase			,     return_tensors="np"		)
     _lowerCAmelCase				=      processor(audio=_lowerCAmelCase			,     return_tensors="np"		)

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




 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([3, 224, 224]		)

     _lowerCAmelCase				=      image_processor(_lowerCAmelCase			,     return_tensors="np"		)
     _lowerCAmelCase				=      processor(images=_lowerCAmelCase			,     return_tensors="np"		)

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




 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([12000]		)
     _lowerCAmelCase				=      np.ones([3, 224, 224]		)

     _lowerCAmelCase				=      processor(audio=_lowerCAmelCase			,     images=_lowerCAmelCase		)

     self.assertListEqual(list(inputs.keys()		)			,     ["audio_values", "audio_mask", "pixel_values", "pixel_mask"]		)

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





 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     self.assertListEqual(
         processor.model_input_names			,     image_processor.model_input_names + feature_extractor.model_input_names			,     msg="`processor` and `image_processor`+`feature_extractor` model input names do not match"			,     )


