import unittest

import numpy as np

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

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import MobileNetVaImageProcessor


class      lowerCamelCase    (unittest.TestCase    ):
  """simple docstring"""







  def __init__(							self       :				Optional[Any]     ,   __magic_name__       :				Tuple     ,   __magic_name__       :				Tuple=7     ,   __magic_name__       :				List[str]=3     ,   __magic_name__       :				Union[str, Any]=18     ,   __magic_name__       :				Optional[Any]=30     ,   __magic_name__       :				Tuple=400     ,   __magic_name__       :				Optional[Any]=True     ,   __magic_name__       :				Optional[int]=None     ,   __magic_name__       :				List[str]=True     ,   __magic_name__       :				Optional[int]=None     ,   )     ->		str:
        SCREAMING_SNAKE_CASE_      =     size if size is not None else {"shortest_edge": 20}
        SCREAMING_SNAKE_CASE_      =     crop_size if crop_size is not None else {"height": 18, "width": 18}
        SCREAMING_SNAKE_CASE_      =     parent
        SCREAMING_SNAKE_CASE_      =     batch_size
        SCREAMING_SNAKE_CASE_      =     num_channels
        SCREAMING_SNAKE_CASE_      =     image_size
        SCREAMING_SNAKE_CASE_      =     min_resolution
        SCREAMING_SNAKE_CASE_      =     max_resolution
        SCREAMING_SNAKE_CASE_      =     do_resize
        SCREAMING_SNAKE_CASE_      =     size
        SCREAMING_SNAKE_CASE_      =     do_center_crop
        SCREAMING_SNAKE_CASE_      =     crop_size







  def       __A   (							self       :				str      )     ->		Optional[int]:
        return {
            "do_resize": self.do_resize,
            "size": self.size,
            "do_center_crop": self.do_center_crop,
            "crop_size": self.crop_size,
        }



@require_torch
@require_vision
class      lowerCamelCase    (SCREAMING_SNAKE_CASE__     ,					unittest.TestCase    ):
  """simple docstring"""







  lowerCamelCase__   =  MobileNetVaImageProcessor if is_vision_available() else None







  def       __A   (							self       :				Optional[Any]      )     ->		Tuple:
        SCREAMING_SNAKE_CASE_      =     MobileNetVaImageProcessingTester(self      )







  @property
  def       __A   (							self       :				Union[str, Any]      )     ->		Dict:
        return self.image_processor_tester.prepare_image_processor_dict()







  def       __A   (							self       :				Any      )     ->		Optional[Any]:
        SCREAMING_SNAKE_CASE_      =     self.image_processing_class(**self.image_processor_dict      )
        self.assertTrue(hasattr(__magic_name__     ,   "do_resize"      )      )
        self.assertTrue(hasattr(__magic_name__     ,   "size"      )      )
        self.assertTrue(hasattr(__magic_name__     ,   "do_center_crop"      )      )
        self.assertTrue(hasattr(__magic_name__     ,   "crop_size"      )      )







  def       __A   (							self       :				Union[str, Any]      )     ->		List[Any]:
        SCREAMING_SNAKE_CASE_      =     self.image_processing_class.from_dict(self.image_processor_dict      )
        self.assertEqual(image_processor.size     ,   {"shortest_edge": 20}      )
        self.assertEqual(image_processor.crop_size     ,   {"height": 18, "width": 18}      )

        SCREAMING_SNAKE_CASE_      =     self.image_processing_class.from_dict(self.image_processor_dict     ,   size=42     ,   crop_size=84      )
        self.assertEqual(image_processor.size     ,   {"shortest_edge": 42}      )
        self.assertEqual(image_processor.crop_size     ,   {"height": 84, "width": 84}      )







  def       __A   (							self       :				int      )     ->		Optional[int]:
        pass







  def       __A   (							self       :				str      )     ->		Optional[int]:
        # Initialize image_processing
        SCREAMING_SNAKE_CASE_      =     self.image_processing_class(**self.image_processor_dict      )
        # create random PIL images
        SCREAMING_SNAKE_CASE_      =     prepare_image_inputs(self.image_processor_tester     ,   equal_resolution=__magic_name__      )
        for image in image_inputs:
              self.assertIsInstance(__magic_name__     ,   Image.Image      )

        # Test not batched input
        SCREAMING_SNAKE_CASE_      =     image_processing(image_inputs[0]     ,   return_tensors="pt"      ).pixel_values
        self.assertEqual(
            encoded_images.shape     ,   (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )     ,   )

        # Test batched
        SCREAMING_SNAKE_CASE_      =     image_processing(__magic_name__     ,   return_tensors="pt"      ).pixel_values
        self.assertEqual(
            encoded_images.shape     ,   (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )     ,   )







  def       __A   (							self       :				Optional[int]      )     ->		Union[str, Any]:
        # Initialize image_processing
        SCREAMING_SNAKE_CASE_      =     self.image_processing_class(**self.image_processor_dict      )
        # create random numpy tensors
        SCREAMING_SNAKE_CASE_      =     prepare_image_inputs(self.image_processor_tester     ,   equal_resolution=__magic_name__     ,   numpify=__magic_name__      )
        for image in image_inputs:
              self.assertIsInstance(__magic_name__     ,   np.ndarray      )

        # Test not batched input
        SCREAMING_SNAKE_CASE_      =     image_processing(image_inputs[0]     ,   return_tensors="pt"      ).pixel_values
        self.assertEqual(
            encoded_images.shape     ,   (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )     ,   )

        # Test batched
        SCREAMING_SNAKE_CASE_      =     image_processing(__magic_name__     ,   return_tensors="pt"      ).pixel_values
        self.assertEqual(
            encoded_images.shape     ,   (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )     ,   )







  def       __A   (							self       :				int      )     ->		List[str]:
        # Initialize image_processing
        SCREAMING_SNAKE_CASE_      =     self.image_processing_class(**self.image_processor_dict      )
        # create random PyTorch tensors
        SCREAMING_SNAKE_CASE_      =     prepare_image_inputs(self.image_processor_tester     ,   equal_resolution=__magic_name__     ,   torchify=__magic_name__      )
        for image in image_inputs:
              self.assertIsInstance(__magic_name__     ,   torch.Tensor      )

        # Test not batched input
        SCREAMING_SNAKE_CASE_      =     image_processing(image_inputs[0]     ,   return_tensors="pt"      ).pixel_values
        self.assertEqual(
            encoded_images.shape     ,   (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )     ,   )

        # Test batched
        SCREAMING_SNAKE_CASE_      =     image_processing(__magic_name__     ,   return_tensors="pt"      ).pixel_values
        self.assertEqual(
            encoded_images.shape     ,   (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )     ,   )



import os
import unittest

from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer
from transformers.testing_utils import require_jieba, tooslow

from ...test_tokenization_common import TokenizerTesterMixin


@require_jieba
class      lowerCamelCase    (SCREAMING_SNAKE_CASE__     ,					unittest.TestCase    ):
  """simple docstring"""







  lowerCamelCase__   =  CpmAntTokenizer
  lowerCamelCase__   =  False







  def       __A   (							self       :				List[str]      )     ->		str:
        super().setUp()

        SCREAMING_SNAKE_CASE_      =     [
            "<d>",
            "</d>",
            "<s>",
            "</s>",
            "</_>",
            "<unk>",
            "<pad>",
            "</n>",
            "我",
            "是",
            "C",
            "P",
            "M",
            "A",
            "n",
            "t",
        ]
        SCREAMING_SNAKE_CASE_      =     os.path.join(self.tmpdirname     ,   VOCAB_FILES_NAMES["vocab_file"]      )
        with open(self.vocab_file     ,   "w"     ,   encoding="utf-8"      ) as vocab_writer:
              vocab_writer.write("".join([x + "\n" for x in vocab_tokens]      )      )







  @tooslow
  def       __A   (							self       :				int      )     ->		Union[str, Any]:
        SCREAMING_SNAKE_CASE_      =     CpmAntTokenizer.from_pretrained("openbmb/cpm-ant-10b"      )
        SCREAMING_SNAKE_CASE_      =     "今天天气真好！"
        SCREAMING_SNAKE_CASE_      =     ["今天", "天气", "真", "好", "！"]
        SCREAMING_SNAKE_CASE_      =     tokenizer.tokenize(__magic_name__      )
        self.assertListEqual(__magic_name__     ,   __magic_name__      )
        SCREAMING_SNAKE_CASE_      =     "今天天气真好！"
        SCREAMING_SNAKE_CASE_      =     [tokenizer.bos_token] + tokens

        SCREAMING_SNAKE_CASE_      =     [6, 9_802, 14_962, 2_082, 831, 244]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(__magic_name__      )     ,   __magic_name__      )

        SCREAMING_SNAKE_CASE_      =     tokenizer.decode(__magic_name__      )
        self.assertEqual(__magic_name__     ,   __magic_name__      )


