

"""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   __lowercase      (     unittest.TestCase       ):

  def  __lowercase							(	self		:      str     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       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__       :	Dict        =       dict(zip(A       ,range(len(A     )     )     )     )
   UpperCAmelCase__       :	Tuple        =       ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
   UpperCAmelCase__       :	Dict        =       {"""unk_token""": """<unk>"""}

   UpperCAmelCase__       :	Union[str, Any]        =       os.path.join(self.tmpdirname       ,VOCAB_FILES_NAMES["""vocab_file"""]     )
   UpperCAmelCase__       :	int        =       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[int]        =       {
       """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],
   }
   UpperCAmelCase__       :	str        =       os.path.join(self.tmpdirname       ,A     )
   with open(self.image_processor_file       ,"""w"""       ,encoding="""utf-8"""     ) as fp:
    json.dump(A       ,A     )

  def  __lowercase							(	self		:      Any       ,**A		:      List[str]     ):






   '''simple docstring'''
   return CLIPTokenizer.from_pretrained(self.tmpdirname       ,**A     )

  def  __lowercase							(	self		:      Tuple       ,**A		:      Union[str, Any]     ):






   '''simple docstring'''
   return CLIPTokenizerFast.from_pretrained(self.tmpdirname       ,**A     )

  def  __lowercase							(	self		:      Tuple       ,**A		:      Tuple     ):






   '''simple docstring'''
   return ViTImageProcessor.from_pretrained(self.tmpdirname       ,**A     )

  def  __lowercase							(	self		:      List[Any]     ):






   '''simple docstring'''
   shutil.rmtree(self.tmpdirname     )

  def  __lowercase							(	self		:      Union[str, Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Union[str, Any]        =       [np.random.randint(255       ,size=(3, 30, 400)       ,dtype=np.uinta     )]

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

   return image_inputs

  def  __lowercase							(	self		:      Union[str, Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	str        =       self.get_tokenizer()
   UpperCAmelCase__       :	Union[str, Any]        =       self.get_rust_tokenizer()
   UpperCAmelCase__       :	List[str]        =       self.get_image_processor()

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

   UpperCAmelCase__       :	Tuple        =       CLIPSegProcessor(tokenizer=A       ,image_processor=A     )
   processor_fast.save_pretrained(self.tmpdirname     )
   UpperCAmelCase__       :	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       ,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  __lowercase							(	self		:      List[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[int]        =       CLIPSegProcessor(tokenizer=self.get_tokenizer()       ,image_processor=self.get_image_processor()     )
   processor.save_pretrained(self.tmpdirname     )

   UpperCAmelCase__       :	Optional[int]        =       self.get_tokenizer(bos_token="""(BOS)"""       ,eos_token="""(EOS)"""     )
   UpperCAmelCase__       :	Optional[int]        =       self.get_image_processor(do_normalize=A       ,padding_value=1.0     )

   UpperCAmelCase__       :	Tuple        =       CLIPSegProcessor.from_pretrained(
       self.tmpdirname       ,bos_token="""(BOS)"""       ,eos_token="""(EOS)"""       ,do_normalize=A       ,padding_value=1.0     )

   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  __lowercase							(	self		:      List[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Union[str, Any]        =       self.get_image_processor()
   UpperCAmelCase__       :	int        =       self.get_tokenizer()

   UpperCAmelCase__       :	str        =       CLIPSegProcessor(tokenizer=A       ,image_processor=A     )

   UpperCAmelCase__       :	str        =       self.prepare_image_inputs()

   UpperCAmelCase__       :	Union[str, Any]        =       image_processor(A       ,return_tensors="""np"""     )
   UpperCAmelCase__       :	int        =       processor(images=A       ,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  __lowercase							(	self		:      str     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Union[str, Any]        =       self.get_image_processor()
   UpperCAmelCase__       :	Any        =       self.get_tokenizer()

   UpperCAmelCase__       :	Any        =       CLIPSegProcessor(tokenizer=A       ,image_processor=A     )

   UpperCAmelCase__       :	Any        =       """lower newer"""

   UpperCAmelCase__       :	Optional[Any]        =       processor(text=A     )

   UpperCAmelCase__       :	Optional[Any]        =       tokenizer(A     )

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

  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   UpperCAmelCase__       :	List[str]        =       self.get_image_processor()
   UpperCAmelCase__       :	int        =       self.get_tokenizer()

   UpperCAmelCase__       :	Optional[int]        =       CLIPSegProcessor(tokenizer=A       ,image_processor=A     )

   UpperCAmelCase__       :	Union[str, Any]        =       """lower newer"""
   UpperCAmelCase__       :	List[str]        =       self.prepare_image_inputs()

   UpperCAmelCase__       :	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  __lowercase							(	self		:      int     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[int]        =       self.get_image_processor()
   UpperCAmelCase__       :	Union[str, Any]        =       self.get_tokenizer()

   UpperCAmelCase__       :	Dict        =       CLIPSegProcessor(tokenizer=A       ,image_processor=A     )

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

   UpperCAmelCase__       :	str        =       processor(images=A       ,visual_prompt=A     )

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

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




  def  __lowercase							(	self		:      List[str]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Tuple        =       self.get_image_processor()
   UpperCAmelCase__       :	int        =       self.get_tokenizer()

   UpperCAmelCase__       :	Optional[Any]        =       CLIPSegProcessor(tokenizer=A       ,image_processor=A     )

   UpperCAmelCase__       :	Optional[int]        =       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

   UpperCAmelCase__       :	Tuple        =       processor.batch_decode(A     )
   UpperCAmelCase__       :	Union[str, Any]        =       tokenizer.batch_decode(A     )

   self.assertListEqual(A       ,A     )






'''simple docstring'''






import unittest

from diffusers.pipelines.pipeline_utils import is_safetensors_compatible







class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   def 	__UpperCAmelCase   (      self      :			Optional[Any]     )  ->			Dict:
        _A    		=  [
            'safety_checker/pytorch_model.bin',
            'safety_checker/model.safetensors',
            'vae/diffusion_pytorch_model.bin',
            'vae/diffusion_pytorch_model.safetensors',
            'text_encoder/pytorch_model.bin',
            'text_encoder/model.safetensors',
            'unet/diffusion_pytorch_model.bin',
            'unet/diffusion_pytorch_model.safetensors',
        ]
        self.assertTrue(is_safetensors_compatible(UpperCamelCase__     )     )
   def 	__UpperCAmelCase   (      self      :			Union[str, Any]     )  ->			Optional[Any]:
        _A    		=  [
            'unet/diffusion_pytorch_model.bin',
            'unet/diffusion_pytorch_model.safetensors',
        ]
        self.assertTrue(is_safetensors_compatible(UpperCamelCase__     )     )
   def 	__UpperCAmelCase   (      self      :			str     )  ->			Union[str, Any]:
        _A    		=  [
            'safety_checker/pytorch_model.bin',
            'safety_checker/model.safetensors',
            'vae/diffusion_pytorch_model.bin',
            'vae/diffusion_pytorch_model.safetensors',
            'text_encoder/pytorch_model.bin',
            'text_encoder/model.safetensors',
            'unet/diffusion_pytorch_model.bin',
            # Removed: 'unet/diffusion_pytorch_model.safetensors',
        ]
        self.assertFalse(is_safetensors_compatible(UpperCamelCase__     )     )
   def 	__UpperCAmelCase   (      self      :			int     )  ->			Dict:
        _A    		=  [
            'text_encoder/pytorch_model.bin',
            'text_encoder/model.safetensors',
        ]
        self.assertTrue(is_safetensors_compatible(UpperCamelCase__     )     )
   def 	__UpperCAmelCase   (      self      :			Union[str, Any]     )  ->			str:
        _A    		=  [
            'safety_checker/pytorch_model.bin',
            'safety_checker/model.safetensors',
            'vae/diffusion_pytorch_model.bin',
            'vae/diffusion_pytorch_model.safetensors',
            'text_encoder/pytorch_model.bin',
            # Removed: 'text_encoder/model.safetensors',
            'unet/diffusion_pytorch_model.bin',
            'unet/diffusion_pytorch_model.safetensors',
        ]
        self.assertFalse(is_safetensors_compatible(UpperCamelCase__     )     )
   def 	__UpperCAmelCase   (      self      :			str     )  ->			Dict:
        _A    		=  [
            'safety_checker/pytorch_model.fp16.bin',
            'safety_checker/model.fp16.safetensors',
            'vae/diffusion_pytorch_model.fp16.bin',
            'vae/diffusion_pytorch_model.fp16.safetensors',
            'text_encoder/pytorch_model.fp16.bin',
            'text_encoder/model.fp16.safetensors',
            'unet/diffusion_pytorch_model.fp16.bin',
            'unet/diffusion_pytorch_model.fp16.safetensors',
        ]
        _A    		=  'fp16'
        self.assertTrue(is_safetensors_compatible(UpperCamelCase__, variant=UpperCamelCase__     )     )
   def 	__UpperCAmelCase   (      self      :			Optional[int]     )  ->			List[str]:
        _A    		=  [
            'unet/diffusion_pytorch_model.fp16.bin',
            'unet/diffusion_pytorch_model.fp16.safetensors',
        ]
        _A    		=  'fp16'
        self.assertTrue(is_safetensors_compatible(UpperCamelCase__, variant=UpperCamelCase__     )     )
   def 	__UpperCAmelCase   (      self      :			List[str]     )  ->			List[str]:
        # pass variant but use the non-variant filenames
        _A    		=  [
            'unet/diffusion_pytorch_model.bin',
            'unet/diffusion_pytorch_model.safetensors',
        ]
        _A    		=  'fp16'
        self.assertTrue(is_safetensors_compatible(UpperCamelCase__, variant=UpperCamelCase__     )     )
   def 	__UpperCAmelCase   (      self      :			List[Any]     )  ->			Optional[Any]:
        _A    		=  [
            'safety_checker/pytorch_model.fp16.bin',
            'safety_checker/model.fp16.safetensors',
            'vae/diffusion_pytorch_model.fp16.bin',
            'vae/diffusion_pytorch_model.fp16.safetensors',
            'text_encoder/pytorch_model.fp16.bin',
            'text_encoder/model.fp16.safetensors',
            'unet/diffusion_pytorch_model.fp16.bin',
            # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors',
        ]
        _A    		=  'fp16'
        self.assertFalse(is_safetensors_compatible(UpperCamelCase__, variant=UpperCamelCase__     )     )
   def 	__UpperCAmelCase   (      self      :			Dict     )  ->			Union[str, Any]:
        _A    		=  [
            'text_encoder/pytorch_model.fp16.bin',
            'text_encoder/model.fp16.safetensors',
        ]
        _A    		=  'fp16'
        self.assertTrue(is_safetensors_compatible(UpperCamelCase__, variant=UpperCamelCase__     )     )
   def 	__UpperCAmelCase   (      self      :			Tuple     )  ->			str:
        # pass variant but use the non-variant filenames
        _A    		=  [
            'text_encoder/pytorch_model.bin',
            'text_encoder/model.safetensors',
        ]
        _A    		=  'fp16'
        self.assertTrue(is_safetensors_compatible(UpperCamelCase__, variant=UpperCamelCase__     )     )

   def 	__UpperCAmelCase   (      self      :			List[Any]     )  ->			int:
        _A    		=  [
            'safety_checker/pytorch_model.fp16.bin',
            'safety_checker/model.fp16.safetensors',
            'vae/diffusion_pytorch_model.fp16.bin',
            'vae/diffusion_pytorch_model.fp16.safetensors',
            'text_encoder/pytorch_model.fp16.bin',
            # 'text_encoder/model.fp16.safetensors',
            'unet/diffusion_pytorch_model.fp16.bin',
            'unet/diffusion_pytorch_model.fp16.safetensors',
        ]
        _A    		=  'fp16'
        self.assertFalse(is_safetensors_compatible(UpperCamelCase__, variant=UpperCamelCase__     )     )



