
"""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 CLIPImageProcessor, CLIPProcessor



@require_vision
class   __a  (      unittest.TestCase	):






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]			)->  Optional[Any]:







              """simple docstring"""
              UpperCamelCase					 =		tempfile.mkdtemp()

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

              UpperCamelCase					 =		os.path.join(self.tmpdirname       ,      VOCAB_FILES_NAMES["vocab_file"]			)
              UpperCamelCase					 =		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_			)			)

              UpperCamelCase					 =		{
                  "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],
              }
              UpperCamelCase					 =		os.path.join(self.tmpdirname       ,      UpperCAmelCase_			)
              with open(self.image_processor_file       ,      "w"       ,      encoding="utf-8"			) as fp:
                     json.dump(UpperCAmelCase_       ,      UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Tuple       ,      **UpperCAmelCase_ :							int			)->  Tuple:







              """simple docstring"""
              return CLIPTokenizer.from_pretrained(self.tmpdirname       ,      **UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Dict       ,      **UpperCAmelCase_ :							str			)->  Tuple:







              """simple docstring"""
              return CLIPTokenizerFast.from_pretrained(self.tmpdirname       ,      **UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[int]       ,      **UpperCAmelCase_ :							Tuple			)->  List[str]:







              """simple docstring"""
              return CLIPImageProcessor.from_pretrained(self.tmpdirname       ,      **UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							str			)->  Optional[int]:







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






       def 	_SCREAMING_SNAKE_CASE       (  self :							Tuple			)->  int:







              """simple docstring"""
              UpperCamelCase					 =		[np.random.randint(255       ,      size=(3, 30, 400)       ,      dtype=np.uinta			)]

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

              return image_inputs






       def 	_SCREAMING_SNAKE_CASE       (  self :							Tuple			)->  str:







              """simple docstring"""
              UpperCamelCase					 =		self.get_tokenizer()
              UpperCamelCase					 =		self.get_rust_tokenizer()
              UpperCamelCase					 =		self.get_image_processor()

              UpperCamelCase					 =		CLIPProcessor(tokenizer=UpperCAmelCase_       ,      image_processor=UpperCAmelCase_			)
              processor_slow.save_pretrained(self.tmpdirname			)
              UpperCamelCase					 =		CLIPProcessor.from_pretrained(self.tmpdirname       ,      use_fast=UpperCAmelCase_			)

              UpperCamelCase					 =		CLIPProcessor(tokenizer=UpperCAmelCase_       ,      image_processor=UpperCAmelCase_			)
              processor_fast.save_pretrained(self.tmpdirname			)
              UpperCamelCase					 =		CLIPProcessor.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 	_SCREAMING_SNAKE_CASE       (  self :							Union[str, Any]			)->  str:







              """simple docstring"""
              UpperCamelCase					 =		CLIPProcessor(tokenizer=self.get_tokenizer()       ,      image_processor=self.get_image_processor()			)
              processor.save_pretrained(self.tmpdirname			)

              UpperCamelCase					 =		self.get_tokenizer(bos_token="(BOS)"       ,      eos_token="(EOS)"			)
              UpperCamelCase					 =		self.get_image_processor(do_normalize=UpperCAmelCase_       ,      padding_value=1.0			)

              UpperCamelCase					 =		CLIPProcessor.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 	_SCREAMING_SNAKE_CASE       (  self :							Union[str, Any]			)->  str:







              """simple docstring"""
              UpperCamelCase					 =		self.get_image_processor()
              UpperCamelCase					 =		self.get_tokenizer()

              UpperCamelCase					 =		CLIPProcessor(tokenizer=UpperCAmelCase_       ,      image_processor=UpperCAmelCase_			)

              UpperCamelCase					 =		self.prepare_image_inputs()

              UpperCamelCase					 =		image_processor(UpperCAmelCase_       ,      return_tensors="np"			)
              UpperCamelCase					 =		processor(images=UpperCAmelCase_       ,      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 	_SCREAMING_SNAKE_CASE       (  self :							List[Any]			)->  int:







              """simple docstring"""
              UpperCamelCase					 =		self.get_image_processor()
              UpperCamelCase					 =		self.get_tokenizer()

              UpperCamelCase					 =		CLIPProcessor(tokenizer=UpperCAmelCase_       ,      image_processor=UpperCAmelCase_			)

              UpperCamelCase					 =		"lower newer"

              UpperCamelCase					 =		processor(text=UpperCAmelCase_			)

              UpperCamelCase					 =		tokenizer(UpperCAmelCase_			)

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






       def 	_SCREAMING_SNAKE_CASE       (  self :							Tuple			)->  Any:







              """simple docstring"""
              UpperCamelCase					 =		self.get_image_processor()
              UpperCamelCase					 =		self.get_tokenizer()

              UpperCamelCase					 =		CLIPProcessor(tokenizer=UpperCAmelCase_       ,      image_processor=UpperCAmelCase_			)

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

              UpperCamelCase					 =		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 	_SCREAMING_SNAKE_CASE       (  self :							str			)->  int:







              """simple docstring"""
              UpperCamelCase					 =		self.get_image_processor()
              UpperCamelCase					 =		self.get_tokenizer()

              UpperCamelCase					 =		CLIPProcessor(tokenizer=UpperCAmelCase_       ,      image_processor=UpperCAmelCase_			)

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

              UpperCamelCase					 =		processor.batch_decode(UpperCAmelCase_			)
              UpperCamelCase					 =		tokenizer.batch_decode(UpperCAmelCase_			)

              self.assertListEqual(UpperCAmelCase_       ,      UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]			)->  Any:







              """simple docstring"""
              UpperCamelCase					 =		self.get_image_processor()
              UpperCamelCase					 =		self.get_tokenizer()

              UpperCamelCase					 =		CLIPProcessor(tokenizer=UpperCAmelCase_       ,      image_processor=UpperCAmelCase_			)

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

              UpperCamelCase					 =		processor(text=UpperCAmelCase_       ,      images=UpperCAmelCase_			)

              self.assertListEqual(list(inputs.keys()			)       ,      processor.model_input_names			)







"""simple docstring"""


from __future__ import annotations

from typing import Generic, TypeVar

SCREAMING_SNAKE_CASE      =     TypeVar("""T""")



class   __a  (      Generic[T]	):






       def __init__(  self :							List[Any]       ,      UpperCAmelCase_ :							T			)->  None:







              """simple docstring"""
              UpperCamelCase					 =		data
              UpperCamelCase					 =		self
              UpperCamelCase					 =		0





class   __a  (      Generic[T]	):






       def __init__(  self :							int			)->  None:







              """simple docstring"""
              # map from node name to the node object
              UpperCamelCase					 =		{}






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[int]       ,      UpperCAmelCase_ :							T			)->  None:







              """simple docstring"""
              # create a new set with x as its member
              UpperCamelCase					 =		DisjointSetTreeNode(UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]       ,      UpperCAmelCase_ :							T			)->  DisjointSetTreeNode[T]:







              """simple docstring"""
              # find the set x belongs to (with path-compression)
              UpperCamelCase					 =		self.map[data]
              if elem_ref != elem_ref.parent:
                     UpperCamelCase					 =		self.find_set(elem_ref.parent.data			)
              return elem_ref.parent






       def 	_SCREAMING_SNAKE_CASE       (  self :							Any       ,      UpperCAmelCase_ :							DisjointSetTreeNode[T]       ,      UpperCAmelCase_ :							DisjointSetTreeNode[T]			)->  None:







              """simple docstring"""
              # helper function for union operation
              if nodea.rank > nodea.rank:
                     UpperCamelCase					 =		nodea
              else:
                     UpperCamelCase					 =		nodea
                     if nodea.rank == nodea.rank:
                            nodea.rank += 1






       def 	_SCREAMING_SNAKE_CASE       (  self :							int       ,      UpperCAmelCase_ :							T       ,      UpperCAmelCase_ :							T			)->  None:







              """simple docstring"""
              # merge 2 disjoint sets
              self.link(self.find_set(UpperCAmelCase_			)       ,      self.find_set(UpperCAmelCase_			)			)





class   __a  (      Generic[T]	):






       def __init__(  self :							Dict			)->  None:







              """simple docstring"""
              # connections: map from the node to the neighbouring nodes (with weights)
              UpperCamelCase					 =		{}






       def 	_SCREAMING_SNAKE_CASE       (  self :							Any       ,      UpperCAmelCase_ :							T			)->  None:







              """simple docstring"""
              # add a node ONLY if its not present in the graph
              if node not in self.connections:
                     UpperCamelCase					 =		{}






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]       ,      UpperCAmelCase_ :							T       ,      UpperCAmelCase_ :							T       ,      UpperCAmelCase_ :							int			)->  None:







              """simple docstring"""
              # add an edge with the given weight
              self.add_node(UpperCAmelCase_			)
              self.add_node(UpperCAmelCase_			)
              UpperCamelCase					 =		weight
              UpperCamelCase					 =		weight






       def 	_SCREAMING_SNAKE_CASE       (  self :							Tuple			)->  GraphUndirectedWeighted[T]:







              """simple docstring"""
              UpperCamelCase					 =		[]
              UpperCamelCase					 =		set()
              for start in self.connections:
                     for end in self.connections[start]:
                            if (start, end) not in seen:
                                   seen.add((end, start)			)
                                   edges.append((start, end, self.connections[start][end])			)
              edges.sort(key=lambda UpperCAmelCase_			: x[2]			)

              # creating the disjoint set
              UpperCamelCase					 =		DisjointSetTree[T]()
              for node in self.connections:
                     disjoint_set.make_set(UpperCAmelCase_			)

              # MST generation
              UpperCamelCase					 =		0
              UpperCamelCase					 =		0
              UpperCamelCase					 =		GraphUndirectedWeighted[T]()
              while num_edges < len(self.connections			) - 1:
                     UpperCamelCase ,       UpperCamelCase ,       UpperCamelCase					 =		edges[index]
                     index += 1
                     UpperCamelCase					 =		disjoint_set.find_set(UpperCAmelCase_			)
                     UpperCamelCase					 =		disjoint_set.find_set(UpperCAmelCase_			)
                     if parent_u != parent_v:
                            num_edges += 1
                            graph.add_edge(UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_			)
                            disjoint_set.union(UpperCAmelCase_       ,      UpperCAmelCase_			)
              return graph





