

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 	__magic_name__   (							self							:					Dict						):
       UpperCAmelCase	:							Tuple						=			tempfile.mkdtemp()

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

       UpperCAmelCase	:							Optional[int]						=			os.path.join(self.tmpdirname,		VOCAB_FILES_NAMES['''vocab_file''']						)
       UpperCAmelCase	:							Union[str, Any]						=			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						)						)

       UpperCAmelCase	:							Optional[Any]						=			{
           '''do_resize''': True,
           '''size''': 2_0,
           '''do_center_crop''': True,
           '''crop_size''': 1_8,
           '''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	:							List[str]						=			os.path.join(self.tmpdirname,		__A						)
       with open(self.image_processor_file,		'''w''',		encoding='''utf-8'''						) as fp:
         json.dump(__A,		__A						)



     def 	__magic_name__   (							self							:					List[Any],		**__A							:					Dict						):
       return CLIPTokenizer.from_pretrained(self.tmpdirname,		pad_token='''!''',		**__A						)



     def 	__magic_name__   (							self							:					int,		**__A							:					List[str]						):
       return CLIPTokenizerFast.from_pretrained(self.tmpdirname,		pad_token='''!''',		**__A						)



     def 	__magic_name__   (							self							:					Dict,		**__A							:					List[Any]						):
       return OwlViTImageProcessor.from_pretrained(self.tmpdirname,		**__A						)



     def 	__magic_name__   (							self							:					Union[str, Any]						):
       shutil.rmtree(self.tmpdirname						)



     def 	__magic_name__   (							self							:					Union[str, Any]						):
       UpperCAmelCase	:							List[str]						=			[np.random.randint(2_5_5,		size=(3, 3_0, 4_0_0),		dtype=np.uinta						)]

       UpperCAmelCase	:							Dict						=			[Image.fromarray(np.moveaxis(__A,		0,		-1						)						) for x in image_inputs]

       return image_inputs



     def 	__magic_name__   (							self							:					Optional[Any]						):
       UpperCAmelCase	:							Any						=			self.get_tokenizer()
       UpperCAmelCase	:							Optional[int]						=			self.get_rust_tokenizer()
       UpperCAmelCase	:							str						=			self.get_image_processor()

       UpperCAmelCase	:							Optional[int]						=			OwlViTProcessor(tokenizer=__A,		image_processor=__A						)
       processor_slow.save_pretrained(self.tmpdirname						)
       UpperCAmelCase	:							Optional[Any]						=			OwlViTProcessor.from_pretrained(self.tmpdirname,		use_fast=__A						)

       UpperCAmelCase	:							Dict						=			OwlViTProcessor(tokenizer=__A,		image_processor=__A						)
       processor_fast.save_pretrained(self.tmpdirname						)
       UpperCAmelCase	:							Union[str, 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 	__magic_name__   (							self							:					Tuple						):
       UpperCAmelCase	:							Dict						=			OwlViTProcessor(tokenizer=self.get_tokenizer(),		image_processor=self.get_image_processor()						)
       processor.save_pretrained(self.tmpdirname						)

       UpperCAmelCase	:							List[str]						=			self.get_tokenizer(bos_token='''(BOS)''',		eos_token='''(EOS)'''						)
       UpperCAmelCase	:							Dict						=			self.get_image_processor(do_normalize=__A						)

       UpperCAmelCase	:							str						=			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 	__magic_name__   (							self							:					List[Any]						):
       UpperCAmelCase	:							int						=			self.get_image_processor()
       UpperCAmelCase	:							Optional[Any]						=			self.get_tokenizer()

       UpperCAmelCase	:							Union[str, Any]						=			OwlViTProcessor(tokenizer=__A,		image_processor=__A						)

       UpperCAmelCase	:							Any						=			self.prepare_image_inputs()

       UpperCAmelCase	:							Optional[int]						=			image_processor(__A,		return_tensors='''np'''						)
       UpperCAmelCase	:							List[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 	__magic_name__   (							self							:					Union[str, Any]						):
       UpperCAmelCase	:							Tuple						=			self.get_image_processor()
       UpperCAmelCase	:							Any						=			self.get_tokenizer()

       UpperCAmelCase	:							Dict						=			OwlViTProcessor(tokenizer=__A,		image_processor=__A						)

       UpperCAmelCase	:							str						=			'''lower newer'''

       UpperCAmelCase	:							Optional[int]						=			processor(text=__A,		return_tensors='''np'''						)

       UpperCAmelCase	:							List[Any]						=			tokenizer(__A,		return_tensors='''np'''						)

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



     def 	__magic_name__   (							self							:					List[Any]						):
       UpperCAmelCase	:							Dict						=			self.get_image_processor()
       UpperCAmelCase	:							int						=			self.get_tokenizer()

       UpperCAmelCase	:							Union[str, Any]						=			OwlViTProcessor(tokenizer=__A,		image_processor=__A						)

       UpperCAmelCase	:							Tuple						=			'''lower newer'''
       UpperCAmelCase	:							Optional[int]						=			self.prepare_image_inputs()

       UpperCAmelCase	:							Optional[Any]						=			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 	__magic_name__   (							self							:					Union[str, Any]						):
       UpperCAmelCase	:							Dict						=			'''google/owlvit-base-patch32'''
       UpperCAmelCase	:							int						=			OwlViTProcessor.from_pretrained(__A						)

       UpperCAmelCase	:							int						=			['''cat''', '''nasa badge''']
       UpperCAmelCase	:							Any						=			processor(text=__A						)

       UpperCAmelCase	:							Optional[Any]						=			1_6
       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 	__magic_name__   (							self							:					Any						):
       UpperCAmelCase	:							List[Any]						=			'''google/owlvit-base-patch32'''
       UpperCAmelCase	:							Union[str, Any]						=			OwlViTProcessor.from_pretrained(__A						)

       UpperCAmelCase	:							Tuple						=			[['''cat''', '''nasa badge'''], ['''person''']]
       UpperCAmelCase	:							Optional[int]						=			processor(text=__A						)

       UpperCAmelCase	:							str						=			1_6
       UpperCAmelCase	:							Any						=			len(__A						)
       UpperCAmelCase	:							str						=			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 	__magic_name__   (							self							:					int						):
       UpperCAmelCase	:							Optional[Any]						=			'''google/owlvit-base-patch32'''
       UpperCAmelCase	:							Union[str, Any]						=			OwlViTProcessor.from_pretrained(__A						)

       UpperCAmelCase	:							Optional[int]						=			['''cat''', '''nasa badge''']
       UpperCAmelCase	:							Union[str, Any]						=			processor(text=__A						)

       UpperCAmelCase	:							List[Any]						=			1_6
       UpperCAmelCase	:							Tuple						=			inputs['''input_ids''']
       UpperCAmelCase	:							List[Any]						=			[
           [4_9_4_0_6, 2_3_6_8, 4_9_4_0_7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [4_9_4_0_6, 6_8_4_1, 1_1_3_0_1, 4_9_4_0_7, 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 	__magic_name__   (							self							:					Tuple						):
       UpperCAmelCase	:							Optional[Any]						=			self.get_image_processor()
       UpperCAmelCase	:							List[Any]						=			self.get_tokenizer()

       UpperCAmelCase	:							List[str]						=			OwlViTProcessor(tokenizer=__A,		image_processor=__A						)

       UpperCAmelCase	:							str						=			self.prepare_image_inputs()
       UpperCAmelCase	:							str						=			self.prepare_image_inputs()

       UpperCAmelCase	:							int						=			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 	__magic_name__   (							self							:					Optional[Any]						):
       UpperCAmelCase	:							int						=			self.get_image_processor()
       UpperCAmelCase	:							Any						=			self.get_tokenizer()

       UpperCAmelCase	:							Any						=			OwlViTProcessor(tokenizer=__A,		image_processor=__A						)

       UpperCAmelCase	:							Optional[Any]						=			[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

       UpperCAmelCase	:							Tuple						=			processor.batch_decode(__A						)
       UpperCAmelCase	:							Optional[Any]						=			tokenizer.batch_decode(__A						)

       self.assertListEqual(__A,		__A						)








# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from accelerate.utils import ComputeEnvironment

from .cluster import get_cluster_input
from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file  # noqa: F401
from .config_utils import _ask_field, _ask_options, _convert_compute_environment  # noqa: F401
from .sagemaker import get_sagemaker_input


_lowerCamelCase    :       Optional[Any]        =							"Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine"

def       a__					(		)  ->						List[Any]:
  UpperCAmelCase	:							Dict						=			_ask_options(
      '''In which compute environment are you running?'''    ,			['''This machine''', '''AWS (Amazon SageMaker)''']    ,			_convert_compute_environment    ,			)
  if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
    UpperCAmelCase	:							List[Any]						=			get_sagemaker_input()
  else:
    UpperCAmelCase	:							Optional[Any]						=			get_cluster_input()
  return config

def       a__					(		UpperCAmelCase							:      Union[str, Any]=None      )  ->						List[Any]:
  if subparsers is not None:
    UpperCAmelCase	:							Optional[Any]						=			subparsers.add_parser('''config'''    ,			description=UpperCAmelCase      )
  else:
    UpperCAmelCase	:							List[str]						=			argparse.ArgumentParser('''Accelerate config command'''    ,			description=UpperCAmelCase      )

  parser.add_argument(
      '''--config_file'''    ,			default=UpperCAmelCase    ,			help=(
          '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache '''
          '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '''
          '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '''
          '''with \'huggingface\'.'''
      )    ,			)

  if subparsers is not None:
    parser.set_defaults(func=UpperCAmelCase      )
  return parser

def       a__					(		UpperCAmelCase							:      List[Any]      )  ->						Optional[int]:
  UpperCAmelCase	:							str						=			get_user_input()
  if args.config_file is not None:
    UpperCAmelCase	:							Any						=			args.config_file
  else:
    if not os.path.isdir(UpperCAmelCase      ):
      os.makedirs(UpperCAmelCase      )
    UpperCAmelCase	:							List[str]						=			default_yaml_config_file

  if config_file.endswith('''.json'''      ):
    config.to_json_file(UpperCAmelCase      )
  else:
    config.to_yaml_file(UpperCAmelCase      )
  print(f'''accelerate configuration saved at {config_file}'''      )

def       a__					(		)  ->						Dict:
  UpperCAmelCase	:							str						=			config_command_parser()
  UpperCAmelCase	:							str						=			parser.parse_args()
  config_command(UpperCAmelCase      )


if __name__ == "__main__":
     main()





