





import json
import os
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import ImageGPTImageProcessor

class 		SCREAMING_SNAKE_CASE__    (	unittest.TestCase  ):


      def __init__(self	:				Optional[int]    ,      a__	:				List[str]    ,      a__	:				List[Any]=7    ,      a__	:				List[Any]=3    ,      a__	:				List[Any]=18    ,      a__	:				Any=30    ,      a__	:				Tuple=400    ,      a__	:				Any=True    ,      a__	:				Optional[Any]=None    ,      a__	:				List[Any]=True    ,      ):





         """simple docstring"""



         __snake_case								=       size if size is not None else {'''height''': 18, '''width''': 18}
         __snake_case								=       parent
         __snake_case								=       batch_size
         __snake_case								=       num_channels
         __snake_case								=       image_size
         __snake_case								=       min_resolution
         __snake_case								=       max_resolution
         __snake_case								=       do_resize
         __snake_case								=       size
         __snake_case								=       do_normalize


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





         """simple docstring"""



         return {
             # here we create 2 clusters for the sake of simplicity
             "clusters": np.asarray(
                 [
                     [0.8_8_6_6_4_4_3_6_3_4_0_3_3_2_0_3, 0.6_6_1_8_8_2_9_3_6_9_5_4_4_9_8_3, 0.3_8_9_1_7_4_6_4_0_1_7_8_6_8_0_4],
                     [-0.6_0_4_2_5_5_9_1_4_6_8_8_1_1_0_4, -0.0_2_2_9_5_0_0_8_8_6_0_5_2_8_4_6_9, 0.5_4_2_3_7_9_7_3_6_9_0_0_3_2_9_6],
                 ]				),
             "do_resize": self.do_resize,
             "size": self.size,
             "do_normalize": self.do_normalize,
         }






@require_torch
@require_vision
class 		SCREAMING_SNAKE_CASE__    (	_UpperCAmelCase						,				unittest.TestCase  ):
      A_						:    Any							=   ImageGPTImageProcessor if is_vision_available() else None


      def        a			(self	:				Optional[int]				):





         """simple docstring"""



         __snake_case								=       ImageGPTImageProcessingTester(self				)


      @property
      def        a			(self	:				Tuple				):





         """simple docstring"""



         return self.image_processor_tester.prepare_image_processor_dict()


      def        a			(self	:				List[Any]				):





         """simple docstring"""



         __snake_case								=       self.image_processing_class(**self.image_processor_dict				)
         self.assertTrue(hasattr(a__    ,      '''clusters'''				)				)
         self.assertTrue(hasattr(a__    ,      '''do_resize'''				)				)
         self.assertTrue(hasattr(a__    ,      '''size'''				)				)
         self.assertTrue(hasattr(a__    ,      '''do_normalize'''				)				)


      def        a			(self	:				Optional[int]				):





         """simple docstring"""



         __snake_case								=       self.image_processing_class.from_dict(self.image_processor_dict				)
         self.assertEqual(image_processor.size    ,      {'''height''': 18, '''width''': 18}				)

         __snake_case								=       self.image_processing_class.from_dict(self.image_processor_dict    ,      size=42				)
         self.assertEqual(image_processor.size    ,      {'''height''': 42, '''width''': 42}				)


      def        a			(self	:				List[Any]				):





         """simple docstring"""



         __snake_case								=       self.image_processing_class(**self.image_processor_dict				)
         __snake_case								=       json.loads(image_processor.to_json_string()				)
         for key, value in self.image_processor_dict.items():
            if key == "clusters":
               self.assertTrue(np.array_equal(a__    ,      obj[key]				)				)
            else:
               self.assertEqual(obj[key]    ,      a__				)


      def        a			(self	:				int				):





         """simple docstring"""



         __snake_case								=       self.image_processing_class(**self.image_processor_dict				)

         with tempfile.TemporaryDirectory() as tmpdirname:
            __snake_case								=       os.path.join(a__    ,      '''image_processor.json'''				)
            image_processor_first.to_json_file(a__				)
            __snake_case								=       self.image_processing_class.from_json_file(a__				).to_dict()

         __snake_case								=       image_processor_first.to_dict()
         for key, value in image_processor_first.items():
            if key == "clusters":
               self.assertTrue(np.array_equal(a__    ,      image_processor_second[key]				)				)
            else:
               self.assertEqual(image_processor_first[key]    ,      a__				)


      def        a			(self	:				Optional[int]				):





         """simple docstring"""



         __snake_case								=       self.image_processing_class(**self.image_processor_dict				)

         with tempfile.TemporaryDirectory() as tmpdirname:
            image_processor_first.save_pretrained(a__				)
            __snake_case								=       self.image_processing_class.from_pretrained(a__				).to_dict()

         __snake_case								=       image_processor_first.to_dict()
         for key, value in image_processor_first.items():
            if key == "clusters":
               self.assertTrue(np.array_equal(a__    ,      image_processor_second[key]				)				)
            else:
               self.assertEqual(image_processor_first[key]    ,      a__				)


      @unittest.skip('''ImageGPT requires clusters at initialization'''				)
      def        a			(self	:				Union[str, Any]				):





         """simple docstring"""



         pass






def lowerCamelCase__   (							)				->    Optional[Any]:
   __snake_case								=       load_dataset('''hf-internal-testing/fixtures_image_utils'''	,				split='''test'''	)

   __snake_case								=       Image.open(dataset[4]['''file''']	)
   __snake_case								=       Image.open(dataset[5]['''file''']	)

   __snake_case								=       [imagea, imagea]

   return images







@require_vision
@require_torch
class 		SCREAMING_SNAKE_CASE__    (	unittest.TestCase  ):


      @slow
      def        a			(self	:				List[Any]				):





         """simple docstring"""



         __snake_case								=       ImageGPTImageProcessor.from_pretrained('''openai/imagegpt-small'''				)

         __snake_case								=       prepare_images()

         # test non-batched
         __snake_case								=       image_processing(images[0]    ,      return_tensors='''pt'''				)

         self.assertIsInstance(encoding.input_ids    ,      torch.LongTensor				)
         self.assertEqual(encoding.input_ids.shape    ,      (1, 1024)				)

         __snake_case								=       [306, 191, 191]
         self.assertEqual(encoding.input_ids[0, :3].tolist()    ,      a__				)

         # test batched
         __snake_case								=       image_processing(a__    ,      return_tensors='''pt'''				)

         self.assertIsInstance(encoding.input_ids    ,      torch.LongTensor				)
         self.assertEqual(encoding.input_ids.shape    ,      (2, 1024)				)

         __snake_case								=       [303, 13, 13]
         self.assertEqual(encoding.input_ids[1, -3:].tolist()    ,      a__				)








import re
import string

import numpy as np

import datasets


snake_case_           =      '\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'

snake_case_           =      '\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'

snake_case_           =      '\n'

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


      def        a			(self	:				Optional[Any]				):





         """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        a			(self	:				List[Any]    ,      a__	:				int    ,      a__	:				Optional[int]    ,      a__	:				Optional[Any]=None    ,      a__	:				Any=False    ,      a__	:				Dict=False    ,      a__	:				Tuple=False    ,      ):





         """simple docstring"""



         if regexes_to_ignore is not None:
            for s in regexes_to_ignore:
               __snake_case								=       np.array([re.sub(a__    ,      ''''''    ,      a__				) for x in predictions]				)
               __snake_case								=       np.array([re.sub(a__    ,      ''''''    ,      a__				) for x in references]				)
         else:
            __snake_case								=       np.asarray(a__				)
            __snake_case								=       np.asarray(a__				)

         if ignore_case:
            __snake_case								=       np.char.lower(a__				)
            __snake_case								=       np.char.lower(a__				)

         if ignore_punctuation:
            __snake_case								=       string.punctuation.maketrans(''''''    ,      ''''''    ,      string.punctuation				)
            __snake_case								=       np.char.translate(a__    ,      table=a__				)
            __snake_case								=       np.char.translate(a__    ,      table=a__				)

         if ignore_numbers:
            __snake_case								=       string.digits.maketrans(''''''    ,      ''''''    ,      string.digits				)
            __snake_case								=       np.char.translate(a__    ,      table=a__				)
            __snake_case								=       np.char.translate(a__    ,      table=a__				)

         __snake_case								=       predictions == references

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

