





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


  '''simple docstring'''







  def UpperCamelCase_ (      self      :					Any							):


    '''simple docstring'''





    _snake_case   :				Any														=	tempfile.mkdtemp()

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

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

    _snake_case   :				Dict														=	{
        'do_resize': True,
        'size': 20,
        'do_center_crop': True,
        'crop_size': 18,
        'do_normalize': True,
        'image_mean': [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],
        'image_std': [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],
    }
    _snake_case   :				List[str]														=	os.path.join(self.tmpdirname      ,				UpperCamelCase							)
    with open(self.image_processor_file      ,				'w'      ,				encoding='utf-8'							) as fp:
      json.dump(UpperCamelCase      ,				UpperCamelCase							)







  def UpperCamelCase_ (      self      :					Tuple      ,				**UpperCamelCase      :					Tuple							):


    '''simple docstring'''





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







  def UpperCamelCase_ (      self      :					List[str]      ,				**UpperCamelCase      :					Any							):


    '''simple docstring'''





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







  def UpperCamelCase_ (      self      :					List[Any]      ,				**UpperCamelCase      :					Any							):


    '''simple docstring'''





    return CLIPImageProcessor.from_pretrained(self.tmpdirname      ,				**UpperCamelCase							)







  def UpperCamelCase_ (      self      :					Optional[Any]							):


    '''simple docstring'''





    shutil.rmtree(self.tmpdirname							)







  def UpperCamelCase_ (      self      :					List[Any]							):


    '''simple docstring'''





    _snake_case   :				Optional[int]														=	[np.random.randint(2_55      ,				size=(3, 30, 4_00)      ,				dtype=np.uinta							)]

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

    return image_inputs







  def UpperCamelCase_ (      self      :					Optional[int]							):


    '''simple docstring'''





    _snake_case   :				List[str]														=	self.get_tokenizer()
    _snake_case   :				Dict														=	self.get_rust_tokenizer()
    _snake_case   :				Any														=	self.get_image_processor()

    _snake_case   :				Tuple														=	CLIPProcessor(tokenizer=UpperCamelCase      ,				image_processor=UpperCamelCase							)
    processor_slow.save_pretrained(self.tmpdirname							)
    _snake_case   :				Optional[Any]														=	CLIPProcessor.from_pretrained(self.tmpdirname      ,				use_fast=UpperCamelCase							)

    _snake_case   :				Any														=	CLIPProcessor(tokenizer=UpperCamelCase      ,				image_processor=UpperCamelCase							)
    processor_fast.save_pretrained(self.tmpdirname							)
    _snake_case   :				int														=	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 UpperCamelCase_ (      self      :					Any							):


    '''simple docstring'''





    _snake_case   :				List[str]														=	CLIPProcessor(tokenizer=self.get_tokenizer()      ,				image_processor=self.get_image_processor()							)
    processor.save_pretrained(self.tmpdirname							)

    _snake_case   :				Optional[int]														=	self.get_tokenizer(bos_token='(BOS)'      ,				eos_token='(EOS)'							)
    _snake_case   :				List[Any]														=	self.get_image_processor(do_normalize=UpperCamelCase      ,				padding_value=1.0							)

    _snake_case   :				Tuple														=	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 UpperCamelCase_ (      self      :					Optional[int]							):


    '''simple docstring'''





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

    _snake_case   :				Optional[Any]														=	CLIPProcessor(tokenizer=UpperCamelCase      ,				image_processor=UpperCamelCase							)

    _snake_case   :				Dict														=	self.prepare_image_inputs()

    _snake_case   :				str														=	image_processor(UpperCamelCase      ,				return_tensors='np'							)
    _snake_case   :				List[str]														=	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 UpperCamelCase_ (      self      :					Optional[int]							):


    '''simple docstring'''





    _snake_case   :				Optional[Any]														=	self.get_image_processor()
    _snake_case   :				List[Any]														=	self.get_tokenizer()

    _snake_case   :				Optional[Any]														=	CLIPProcessor(tokenizer=UpperCamelCase      ,				image_processor=UpperCamelCase							)

    _snake_case   :				Optional[Any]														=	'lower newer'

    _snake_case   :				List[Any]														=	processor(text=UpperCamelCase							)

    _snake_case   :				Tuple														=	tokenizer(UpperCamelCase							)

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







  def UpperCamelCase_ (      self      :					List[str]							):


    '''simple docstring'''





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

    _snake_case   :				Optional[Any]														=	CLIPProcessor(tokenizer=UpperCamelCase      ,				image_processor=UpperCamelCase							)

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

    _snake_case   :				Dict														=	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 UpperCamelCase_ (      self      :					Optional[Any]							):


    '''simple docstring'''





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

    _snake_case   :				List[str]														=	CLIPProcessor(tokenizer=UpperCamelCase      ,				image_processor=UpperCamelCase							)

    _snake_case   :				Tuple														=	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

    _snake_case   :				Optional[int]														=	processor.batch_decode(UpperCamelCase							)
    _snake_case   :				Dict														=	tokenizer.batch_decode(UpperCamelCase							)

    self.assertListEqual(UpperCamelCase      ,				UpperCamelCase							)







  def UpperCamelCase_ (      self      :					str							):


    '''simple docstring'''





    _snake_case   :				Dict														=	self.get_image_processor()
    _snake_case   :				str														=	self.get_tokenizer()

    _snake_case   :				Optional[Any]														=	CLIPProcessor(tokenizer=UpperCamelCase      ,				image_processor=UpperCamelCase							)

    _snake_case   :				Any														=	'lower newer'
    _snake_case   :				Tuple														=	self.prepare_image_inputs()

    _snake_case   :				Tuple														=	processor(text=UpperCamelCase      ,				images=UpperCamelCase							)

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











from random import randint, random


def     lowerCamelCase_	(      lowerCAmelCase:	int    ,							lowerCAmelCase:	int    ,							lowerCAmelCase:	int    ,							lowerCAmelCase:	bool = False    ,							lowerCAmelCase:	bool = False    ,							lowerCAmelCase:	int = 5    ,							)->		list:
  _snake_case   :				Dict														=	[[-1] * number_of_cells]  # Create a highway without any car
  _snake_case   :				List[str]														=	0
  _snake_case   :				List[str]														=	max(lowerCAmelCase    ,							0				)
  while i < number_of_cells:
    _snake_case   :				Optional[Any]														=	(
        randint(0    ,							lowerCAmelCase				) if random_speed else initial_speed
    )  # Place the cars
    i += (
        randint(1    ,							max_speed * 2				) if random_frequency else frequency
    )  # Arbitrary number, may need tuning
  return highway


def     lowerCamelCase_	(      lowerCAmelCase:	list    ,							lowerCAmelCase:	int				)->		int:
  _snake_case   :				Dict														=	0
  _snake_case   :				Optional[Any]														=	highway_now[car_index + 1 :]
  for cell in range(len(lowerCAmelCase				)				):  # May need a better name for this
    if cells[cell] != -1:  # If the cell is not empty then
      return distance  # we have the distance we wanted
    distance += 1
  # Here if the car is near the end of the highway
  return distance + get_distance(lowerCAmelCase    ,							-1				)


def     lowerCamelCase_	(      lowerCAmelCase:	list    ,							lowerCAmelCase:	float    ,							lowerCAmelCase:	int				)->		list:
  _snake_case   :				List[Any]														=	len(lowerCAmelCase				)
  # Beforce calculations, the highway is empty
  _snake_case   :				List[Any]														=	[-1] * number_of_cells

  for car_index in range(lowerCAmelCase				):
    if highway_now[car_index] != -1:
      # Add 1 to the current speed of the car and cap the speed
      _snake_case   :				int														=	min(highway_now[car_index] + 1    ,							lowerCAmelCase				)
      # Number of empty cell before the next car
      _snake_case   :				Tuple														=	get_distance(lowerCAmelCase    ,							lowerCAmelCase				) - 1
      # We can't have the car causing an accident
      _snake_case   :				Union[str, Any]														=	min(next_highway[car_index]    ,							lowerCAmelCase				)
      if random() < probability:
        # Randomly, a driver will slow down
        _snake_case   :				int														=	max(next_highway[car_index] - 1    ,							0				)
  return next_highway


def     lowerCamelCase_	(      lowerCAmelCase:	list    ,							lowerCAmelCase:	int    ,							lowerCAmelCase:	float    ,							lowerCAmelCase:	int				)->		list:
  _snake_case   :				Dict														=	len(highway[0]				)

  for i in range(lowerCAmelCase				):
    _snake_case   :				Any														=	update(highway[i]    ,							lowerCAmelCase    ,							lowerCAmelCase				)
    _snake_case   :				Tuple														=	[-1] * number_of_cells

    for car_index in range(lowerCAmelCase				):
      _snake_case   :				Union[str, Any]														=	next_speeds_calculated[car_index]
      if speed != -1:
        # Change the position based on the speed (with % to create the loop)
        _snake_case   :				Union[str, Any]														=	(car_index + speed) % number_of_cells
        # Commit the change of position
        _snake_case   :				Tuple														=	speed
    highway.append(lowerCAmelCase				)

  return highway


if __name__ == "__main__":
      import doctest

      doctest.testmod()




