

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     a							(					self       ):
											snake_case_							=    tempfile.mkdtemp()

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

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

											snake_case_							=    {
											    '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_							=    os.path.join(self.tmpdirname      ,   __a       )
											with open(self.image_processor_file      ,   'w'      ,   encoding='utf-8'       ) as fp:
																		json.dump(__a      ,   __a       )




				def     a							(					self      ,   **snake_case       ):
											return CLIPTokenizer.from_pretrained(self.tmpdirname      ,   **__a       )




				def     a							(					self      ,   **snake_case       ):
											return CLIPTokenizerFast.from_pretrained(self.tmpdirname      ,   **__a       )




				def     a							(					self      ,   **snake_case       ):
											return ViTImageProcessor.from_pretrained(self.tmpdirname      ,   **__a       )




				def     a							(					self       ):
											shutil.rmtree(self.tmpdirname       )




				def     a							(					self       ):
											snake_case_							=    [np.random.randint(255      ,   size=(3, 30, 400)      ,   dtype=np.uinta       )]

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

											return image_inputs




				def     a							(					self       ):
											snake_case_							=    self.get_tokenizer()
											snake_case_							=    self.get_rust_tokenizer()
											snake_case_							=    self.get_image_processor()

											snake_case_							=    CLIPSegProcessor(tokenizer=__a      ,   image_processor=__a       )
											processor_slow.save_pretrained(self.tmpdirname       )
											snake_case_							=    CLIPSegProcessor.from_pretrained(self.tmpdirname      ,   use_fast=__a       )

											snake_case_							=    CLIPSegProcessor(tokenizer=__a      ,   image_processor=__a       )
											processor_fast.save_pretrained(self.tmpdirname       )
											snake_case_							=    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     a							(					self       ):
											snake_case_							=    CLIPSegProcessor(tokenizer=self.get_tokenizer()      ,   image_processor=self.get_image_processor()       )
											processor.save_pretrained(self.tmpdirname       )

											snake_case_							=    self.get_tokenizer(bos_token='(BOS)'      ,   eos_token='(EOS)'       )
											snake_case_							=    self.get_image_processor(do_normalize=__a      ,   padding_value=1.0       )

											snake_case_							=    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     a							(					self       ):
											snake_case_							=    self.get_image_processor()
											snake_case_							=    self.get_tokenizer()

											snake_case_							=    CLIPSegProcessor(tokenizer=__a      ,   image_processor=__a       )

											snake_case_							=    self.prepare_image_inputs()

											snake_case_							=    image_processor(__a      ,   return_tensors='np'       )
											snake_case_							=    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     a							(					self       ):
											snake_case_							=    self.get_image_processor()
											snake_case_							=    self.get_tokenizer()

											snake_case_							=    CLIPSegProcessor(tokenizer=__a      ,   image_processor=__a       )

											snake_case_							=    'lower newer'

											snake_case_							=    processor(text=__a       )

											snake_case_							=    tokenizer(__a       )

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




				def     a							(					self       ):
											snake_case_							=    self.get_image_processor()
											snake_case_							=    self.get_tokenizer()

											snake_case_							=    CLIPSegProcessor(tokenizer=__a      ,   image_processor=__a       )

											snake_case_							=    'lower newer'
											snake_case_							=    self.prepare_image_inputs()

											snake_case_							=    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     a							(					self       ):
											snake_case_							=    self.get_image_processor()
											snake_case_							=    self.get_tokenizer()

											snake_case_							=    CLIPSegProcessor(tokenizer=__a      ,   image_processor=__a       )

											snake_case_							=    self.prepare_image_inputs()
											snake_case_							=    self.prepare_image_inputs()

											snake_case_							=    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     a							(					self       ):
											snake_case_							=    self.get_image_processor()
											snake_case_							=    self.get_tokenizer()

											snake_case_							=    CLIPSegProcessor(tokenizer=__a      ,   image_processor=__a       )

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

											snake_case_							=    processor.batch_decode(__a       )
											snake_case_							=    tokenizer.batch_decode(__a       )

											self.assertListEqual(__a      ,   __a       )








'''simple docstring'''



import re
import string

import numpy as np

import datasets


__lowercase	:						Tuple       =	'\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n'

__lowercase	:						List[str]       =	'\nArgs:\n    predictions: List of predicted texts.\n    references: List of reference texts.\n    regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n        ignore when calculating the exact matches. Note: these regexes are removed\n        from the input data before the changes based on the options below (e.g. ignore_case,\n        ignore_punctuation, ignore_numbers) are applied.\n    ignore_case: Boolean, defaults to False. If true, turns everything\n        to lowercase so that capitalization differences are ignored.\n    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\n    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\nReturns:\n    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    25.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    50.0\n\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    75.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n    >>> print(round(results["exact_match"], 1))\n    100.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n    >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    33.3\n\n'

__lowercase	:						Any       =	'\n'





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION		,      _KWARGS_DESCRIPTION					)
class 							__UpperCamelCase				(		datasets.Metric					):




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return datasets.MetricInfo(
						    description=_DESCRIPTION						,				citation=_CITATION						,				inputs_description=_KWARGS_DESCRIPTION						,				features=datasets.Features(
						        {
						            'predictions': datasets.Value('string'						,				id='sequence'							),
						            'references': datasets.Value('string'						,				id='sequence'							),
						        }							)						,				reference_urls=[]						,				)





	def       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a=None						,				__a=False						,				__a=False						,				__a=False						,				):

						'''simple docstring'''





						if regexes_to_ignore is not None:
											for s in regexes_to_ignore:
																__a			:       Tuple					    =  np.array([re.sub(__a						,				''						,				__a							) for x in predictions]							)
																__a			:       List[Any]					    =  np.array([re.sub(__a						,				''						,				__a							) for x in references]							)
						else:
											__a			:       int					    =  np.asarray(__a							)
											__a			:       str					    =  np.asarray(__a							)

						if ignore_case:
											__a			:       Dict					    =  np.char.lower(__a							)
											__a			:       List[str]					    =  np.char.lower(__a							)

						if ignore_punctuation:
											__a			:       Dict					    =  string.punctuation.maketrans(''						,				''						,				string.punctuation							)
											__a			:       Tuple					    =  np.char.translate(__a						,				table=__a							)
											__a			:       Dict					    =  np.char.translate(__a						,				table=__a							)

						if ignore_numbers:
											__a			:       Optional[int]					    =  string.digits.maketrans(''						,				''						,				string.digits							)
											__a			:       Tuple					    =  np.char.translate(__a						,				table=__a							)
											__a			:       Optional[int]					    =  np.char.translate(__a						,				table=__a							)

						__a			:       Any					    =  predictions == references

						return {"exact_match": np.mean(__a							) * 100}



