






'''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 CLIPSegProcessor, ViTImageProcessor
@require_vision
class UpperCAmelCase_       (     unittest.TestCase					):




      """simple docstring"""




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


       '''simple docstring'''

       lowercase       : Dict						=tempfile.mkdtemp()

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

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

       lowercase       : Dict						={
           '''do_resize''': True,
           '''size''': 20,
           '''do_center_crop''': True,
           '''crop_size''': 18,
           '''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],
       }
       lowercase       : int						=os.path.join(self.tmpdirname  ,   UpperCAmelCase		)
       with open(self.image_processor_file  ,   '''w'''  ,   encoding='''utf-8'''		) as fp:
        json.dump(UpperCAmelCase  ,   UpperCAmelCase		)






      def 					A__   (			self      :		Dict  ,   **UpperCAmelCase      :		List[str]		)	->	Union[str, Any]:


       '''simple docstring'''

       return CLIPTokenizer.from_pretrained(self.tmpdirname  ,   **UpperCAmelCase		)






      def 					A__   (			self      :		Optional[Any]  ,   **UpperCAmelCase      :		Tuple		)	->	List[Any]:


       '''simple docstring'''

       return CLIPTokenizerFast.from_pretrained(self.tmpdirname  ,   **UpperCAmelCase		)






      def 					A__   (			self      :		Tuple  ,   **UpperCAmelCase      :		Dict		)	->	int:


       '''simple docstring'''

       return ViTImageProcessor.from_pretrained(self.tmpdirname  ,   **UpperCAmelCase		)






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


       '''simple docstring'''

       shutil.rmtree(self.tmpdirname		)






      def 					A__   (			self      :		Tuple		)	->	Optional[Any]:


       '''simple docstring'''

       lowercase       : Dict						=[np.random.randint(255  ,   size=(3, 30, 400)  ,   dtype=np.uinta		)]

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

       return image_inputs






      def 					A__   (			self      :		int		)	->	List[Any]:


       '''simple docstring'''

       lowercase       : str						=self.get_tokenizer()
       lowercase       : List[str]						=self.get_rust_tokenizer()
       lowercase       : Union[str, Any]						=self.get_image_processor()

       lowercase       : str						=CLIPSegProcessor(tokenizer=UpperCAmelCase  ,   image_processor=UpperCAmelCase		)
       processor_slow.save_pretrained(self.tmpdirname		)
       lowercase       : List[Any]						=CLIPSegProcessor.from_pretrained(self.tmpdirname  ,   use_fast=UpperCAmelCase		)

       lowercase       : List[Any]						=CLIPSegProcessor(tokenizer=UpperCAmelCase  ,   image_processor=UpperCAmelCase		)
       processor_fast.save_pretrained(self.tmpdirname		)
       lowercase       : int						=CLIPSegProcessor.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  ,   UpperCAmelCase		)
       self.assertIsInstance(processor_fast.tokenizer  ,   UpperCAmelCase		)

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






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


       '''simple docstring'''

       lowercase       : Optional[int]						=CLIPSegProcessor(tokenizer=self.get_tokenizer()  ,   image_processor=self.get_image_processor()		)
       processor.save_pretrained(self.tmpdirname		)

       lowercase       : Dict						=self.get_tokenizer(bos_token='''(BOS)'''  ,   eos_token='''(EOS)'''		)
       lowercase       : Dict						=self.get_image_processor(do_normalize=UpperCAmelCase  ,   padding_value=1.0		)

       lowercase       : List[str]						=CLIPSegProcessor.from_pretrained(
           self.tmpdirname  ,   bos_token='''(BOS)'''  ,   eos_token='''(EOS)'''  ,   do_normalize=UpperCAmelCase  ,   padding_value=1.0		)

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

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






      def 					A__   (			self      :		Tuple		)	->	Optional[Any]:


       '''simple docstring'''

       lowercase       : str						=self.get_image_processor()
       lowercase       : Optional[int]						=self.get_tokenizer()

       lowercase       : str						=CLIPSegProcessor(tokenizer=UpperCAmelCase  ,   image_processor=UpperCAmelCase		)

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

       lowercase       : int						=image_processor(UpperCAmelCase  ,   return_tensors='''np'''		)
       lowercase       : Optional[int]						=processor(images=UpperCAmelCase  ,   return_tensors='''np'''		)

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






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


       '''simple docstring'''

       lowercase       : Optional[int]						=self.get_image_processor()
       lowercase       : Dict						=self.get_tokenizer()

       lowercase       : List[str]						=CLIPSegProcessor(tokenizer=UpperCAmelCase  ,   image_processor=UpperCAmelCase		)

       lowercase       : Any						='''lower newer'''

       lowercase       : int						=processor(text=UpperCAmelCase		)

       lowercase       : str						=tokenizer(UpperCAmelCase		)

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






      def 					A__   (			self      :		List[str]		)	->	List[Any]:


       '''simple docstring'''

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

       lowercase       : Any						=CLIPSegProcessor(tokenizer=UpperCAmelCase  ,   image_processor=UpperCAmelCase		)

       lowercase       : Optional[int]						='''lower newer'''
       lowercase       : Union[str, Any]						=self.prepare_image_inputs()

       lowercase       : Union[str, Any]						=processor(text=UpperCAmelCase  ,   images=UpperCAmelCase		)

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

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






      def 					A__   (			self      :		Optional[int]		)	->	Tuple:


       '''simple docstring'''

       lowercase       : int						=self.get_image_processor()
       lowercase       : List[Any]						=self.get_tokenizer()

       lowercase       : List[Any]						=CLIPSegProcessor(tokenizer=UpperCAmelCase  ,   image_processor=UpperCAmelCase		)

       lowercase       : str						=self.prepare_image_inputs()
       lowercase       : int						=self.prepare_image_inputs()

       lowercase       : Union[str, Any]						=processor(images=UpperCAmelCase  ,   visual_prompt=UpperCAmelCase		)

       self.assertListEqual(list(inputs.keys()		)  ,   ['''pixel_values''', '''conditional_pixel_values''']		)

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






      def 					A__   (			self      :		Optional[Any]		)	->	Dict:


       '''simple docstring'''

       lowercase       : Dict						=self.get_image_processor()
       lowercase       : Optional[Any]						=self.get_tokenizer()

       lowercase       : Union[str, Any]						=CLIPSegProcessor(tokenizer=UpperCAmelCase  ,   image_processor=UpperCAmelCase		)

       lowercase       : Union[str, Any]						=[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

       lowercase       : Union[str, Any]						=processor.batch_decode(UpperCAmelCase		)
       lowercase       : str						=tokenizer.batch_decode(UpperCAmelCase		)

       self.assertListEqual(UpperCAmelCase  ,   UpperCAmelCase		)











'''simple docstring'''



from ..utils import DummyObject, requires_backends
class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






      def __init__(			self      :		Dict  ,   *UpperCAmelCase      :		List[Any]  ,   **UpperCAmelCase      :		Union[str, Any]		)	->	List[Any]:


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Any  ,   *UpperCAmelCase      :		Dict  ,   **UpperCAmelCase      :		List[str]		)	->	List[Any]:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Dict  ,   *UpperCAmelCase      :		str  ,   **UpperCAmelCase      :		Any		)	->	Any:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)





class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






      def __init__(			self      :		Union[str, Any]  ,   *UpperCAmelCase      :		Optional[Any]  ,   **UpperCAmelCase      :		str		)	->	Optional[int]:


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Tuple  ,   *UpperCAmelCase      :		Any  ,   **UpperCAmelCase      :		List[Any]		)	->	int:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		List[Any]  ,   *UpperCAmelCase      :		Union[str, Any]  ,   **UpperCAmelCase      :		List[str]		)	->	List[Any]:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)





class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






      def __init__(			self      :		Any  ,   *UpperCAmelCase      :		int  ,   **UpperCAmelCase      :		Optional[Any]		)	->	Any:


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		str  ,   *UpperCAmelCase      :		Tuple  ,   **UpperCAmelCase      :		Union[str, Any]		)	->	Optional[int]:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Optional[int]  ,   *UpperCAmelCase      :		Dict  ,   **UpperCAmelCase      :		Optional[int]		)	->	List[str]:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)





class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






      def __init__(			self      :		Union[str, Any]  ,   *UpperCAmelCase      :		Optional[Any]  ,   **UpperCAmelCase      :		int		)	->	Optional[int]:


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Tuple  ,   *UpperCAmelCase      :		Any  ,   **UpperCAmelCase      :		Optional[int]		)	->	List[Any]:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		int  ,   *UpperCAmelCase      :		int  ,   **UpperCAmelCase      :		Dict		)	->	Optional[Any]:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)





class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






      def __init__(			self      :		Optional[int]  ,   *UpperCAmelCase      :		Dict  ,   **UpperCAmelCase      :		Union[str, Any]		)	->	List[str]:


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		str  ,   *UpperCAmelCase      :		Optional[int]  ,   **UpperCAmelCase      :		List[str]		)	->	str:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Dict  ,   *UpperCAmelCase      :		Union[str, Any]  ,   **UpperCAmelCase      :		List[Any]		)	->	Dict:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)





class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






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


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Optional[Any]  ,   *UpperCAmelCase      :		Optional[Any]  ,   **UpperCAmelCase      :		Optional[Any]		)	->	str:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Optional[int]  ,   *UpperCAmelCase      :		Union[str, Any]  ,   **UpperCAmelCase      :		Dict		)	->	Dict:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)





class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






      def __init__(			self      :		int  ,   *UpperCAmelCase      :		List[Any]  ,   **UpperCAmelCase      :		Tuple		)	->	Union[str, Any]:


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Any  ,   *UpperCAmelCase      :		int  ,   **UpperCAmelCase      :		Optional[Any]		)	->	str:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Union[str, Any]  ,   *UpperCAmelCase      :		List[str]  ,   **UpperCAmelCase      :		List[str]		)	->	Optional[Any]:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)





class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






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


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Optional[Any]  ,   *UpperCAmelCase      :		Optional[Any]  ,   **UpperCAmelCase      :		Any		)	->	List[Any]:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Dict  ,   *UpperCAmelCase      :		Any  ,   **UpperCAmelCase      :		Optional[Any]		)	->	List[Any]:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)





class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






      def __init__(			self      :		Optional[int]  ,   *UpperCAmelCase      :		Optional[int]  ,   **UpperCAmelCase      :		Any		)	->	Any:


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Tuple  ,   *UpperCAmelCase      :		List[str]  ,   **UpperCAmelCase      :		Optional[int]		)	->	List[str]:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Union[str, Any]  ,   *UpperCAmelCase      :		Any  ,   **UpperCAmelCase      :		int		)	->	Tuple:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)





class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






      def __init__(			self      :		Optional[int]  ,   *UpperCAmelCase      :		Union[str, Any]  ,   **UpperCAmelCase      :		Dict		)	->	int:


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		List[str]  ,   *UpperCAmelCase      :		Dict  ,   **UpperCAmelCase      :		Dict		)	->	List[Any]:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		str  ,   *UpperCAmelCase      :		Optional[Any]  ,   **UpperCAmelCase      :		str		)	->	int:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)





class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






      def __init__(			self      :		Optional[int]  ,   *UpperCAmelCase      :		Any  ,   **UpperCAmelCase      :		Optional[int]		)	->	Optional[int]:


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Optional[int]  ,   *UpperCAmelCase      :		Dict  ,   **UpperCAmelCase      :		Optional[Any]		)	->	Dict:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		str  ,   *UpperCAmelCase      :		str  ,   **UpperCAmelCase      :		Any		)	->	int:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)





class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






      def __init__(			self      :		Optional[int]  ,   *UpperCAmelCase      :		str  ,   **UpperCAmelCase      :		Any		)	->	Optional[int]:


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Optional[int]  ,   *UpperCAmelCase      :		Tuple  ,   **UpperCAmelCase      :		Any		)	->	Dict:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Optional[int]  ,   *UpperCAmelCase      :		Optional[int]  ,   **UpperCAmelCase      :		Optional[Any]		)	->	Dict:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)





class UpperCAmelCase_       (     metaclass=__A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''flax''']






      def __init__(			self      :		Optional[Any]  ,   *UpperCAmelCase      :		str  ,   **UpperCAmelCase      :		int		)	->	Tuple:


       '''simple docstring'''

       requires_backends(self  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Dict  ,   *UpperCAmelCase      :		List[Any]  ,   **UpperCAmelCase      :		Optional[int]		)	->	str:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)






      @classmethod
      def 					A__   (			cls      :		Union[str, Any]  ,   *UpperCAmelCase      :		List[Any]  ,   **UpperCAmelCase      :		Union[str, Any]		)	->	Union[str, Any]:


       '''simple docstring'''

       requires_backends(cls  ,   ['''flax''']		)



