


'''simple docstring'''
import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available


if is_vision_available():
  from PIL import Image

  from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor




@require_vision
class      lowerCamelCase_  (      unittest.TestCase ):






      def lowercase_				(   self							:       Optional[Any]					):


          '''simple docstring'''


          UpperCAmelCase__     :						List[Any]						=							tempfile.mkdtemp()

          UpperCAmelCase__     :						Optional[int]						=							[
              '''[UNK]''',
              '''[CLS]''',
              '''[SEP]''',
              '''[PAD]''',
              '''[MASK]''',
              '''的''',
              '''价''',
              '''格''',
              '''是''',
              '''15''',
              '''便''',
              '''alex''',
              '''##andra''',
              '''，''',
              '''。''',
              '''-''',
              '''t''',
              '''shirt''',
          ]
          UpperCAmelCase__     :						int						=							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]					)					)

          UpperCAmelCase__     :						Any						=							{
              '''do_resize''': True,
              '''size''': {'''height''': 224, '''width''': 224},
              '''do_center_crop''': True,
              '''crop_size''': {'''height''': 18, '''width''': 18},
              '''do_normalize''': True,
              '''image_mean''': [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
              '''image_std''': [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
              '''do_convert_rgb''': True,
          }
          UpperCAmelCase__     :						int						=							os.path.join(self.tmpdirname		,		_A					)
          with open(self.image_processor_file		,		'''w'''		,		encoding='''utf-8'''					) as fp:
              json.dump(_A		,		_A					)






      def lowercase_				(   self							:       Optional[Any]		,		**_A							:       Optional[Any]					):


          '''simple docstring'''


          return BertTokenizer.from_pretrained(self.tmpdirname		,		**_A					)






      def lowercase_				(   self							:       int		,		**_A							:       str					):


          '''simple docstring'''


          return BertTokenizerFast.from_pretrained(self.tmpdirname		,		**_A					)






      def lowercase_				(   self							:       Dict		,		**_A							:       Any					):


          '''simple docstring'''


          return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname		,		**_A					)






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


          '''simple docstring'''


          shutil.rmtree(self.tmpdirname					)






      def lowercase_				(   self							:       Optional[int]					):


          '''simple docstring'''


          UpperCAmelCase__     :						Dict						=							[np.random.randint(255		,		size=(3, 30, 400)		,		dtype=np.uinta					)]

          UpperCAmelCase__     :						str						=							[Image.fromarray(np.moveaxis(_A		,		0		,		-1					)					) for x in image_inputs]

          return image_inputs






      def lowercase_				(   self							:       Tuple					):


          '''simple docstring'''


          UpperCAmelCase__     :						Any						=							self.get_tokenizer()
          UpperCAmelCase__     :						Tuple						=							self.get_rust_tokenizer()
          UpperCAmelCase__     :						Tuple						=							self.get_image_processor()

          UpperCAmelCase__     :						List[Any]						=							ChineseCLIPProcessor(tokenizer=_A		,		image_processor=_A					)
          processor_slow.save_pretrained(self.tmpdirname					)
          UpperCAmelCase__     :						Dict						=							ChineseCLIPProcessor.from_pretrained(self.tmpdirname		,		use_fast=_A					)

          UpperCAmelCase__     :						List[Any]						=							ChineseCLIPProcessor(tokenizer=_A		,		image_processor=_A					)
          processor_fast.save_pretrained(self.tmpdirname					)
          UpperCAmelCase__     :						Dict						=							ChineseCLIPProcessor.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 lowercase_				(   self							:       Optional[int]					):


          '''simple docstring'''


          UpperCAmelCase__     :						Dict						=							ChineseCLIPProcessor(tokenizer=self.get_tokenizer()		,		image_processor=self.get_image_processor()					)
          processor.save_pretrained(self.tmpdirname					)

          UpperCAmelCase__     :						Optional[Any]						=							self.get_tokenizer(cls_token='''(CLS)'''		,		sep_token='''(SEP)'''					)
          UpperCAmelCase__     :						Any						=							self.get_image_processor(do_normalize=_A					)

          UpperCAmelCase__     :						str						=							ChineseCLIPProcessor.from_pretrained(
              self.tmpdirname		,		cls_token='''(CLS)'''		,		sep_token='''(SEP)'''		,		do_normalize=_A					)

          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 lowercase_				(   self							:       Tuple					):


          '''simple docstring'''


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

          UpperCAmelCase__     :						str						=							ChineseCLIPProcessor(tokenizer=_A		,		image_processor=_A					)

          UpperCAmelCase__     :						Any						=							self.prepare_image_inputs()

          UpperCAmelCase__     :						Optional[int]						=							image_processor(_A		,		return_tensors='''np'''					)
          UpperCAmelCase__     :						Optional[int]						=							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 lowercase_				(   self							:       Optional[Any]					):


          '''simple docstring'''


          UpperCAmelCase__     :						Any						=							self.get_image_processor()
          UpperCAmelCase__     :						Any						=							self.get_tokenizer()

          UpperCAmelCase__     :						Tuple						=							ChineseCLIPProcessor(tokenizer=_A		,		image_processor=_A					)

          UpperCAmelCase__     :						str						=							'''Alexandra，T-shirt的价格是15便士。'''

          UpperCAmelCase__     :						List[str]						=							processor(text=_A					)

          UpperCAmelCase__     :						Any						=							tokenizer(_A					)

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






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


          '''simple docstring'''


          UpperCAmelCase__     :						int						=							self.get_image_processor()
          UpperCAmelCase__     :						List[Any]						=							self.get_tokenizer()

          UpperCAmelCase__     :						int						=							ChineseCLIPProcessor(tokenizer=_A		,		image_processor=_A					)

          UpperCAmelCase__     :						Any						=							'''Alexandra，T-shirt的价格是15便士。'''
          UpperCAmelCase__     :						int						=							self.prepare_image_inputs()

          UpperCAmelCase__     :						Tuple						=							processor(text=_A		,		images=_A					)

          self.assertListEqual(list(inputs.keys()					)		,		['''input_ids''', '''token_type_ids''', '''attention_mask''', '''pixel_values''']					)

          # test if it raises when no input is passed
          with pytest.raises(_A					):
              processor()






      def lowercase_				(   self							:       int					):


          '''simple docstring'''


          UpperCAmelCase__     :						Any						=							self.get_image_processor()
          UpperCAmelCase__     :						List[str]						=							self.get_tokenizer()

          UpperCAmelCase__     :						Tuple						=							ChineseCLIPProcessor(tokenizer=_A		,		image_processor=_A					)

          UpperCAmelCase__     :						Dict						=							[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

          UpperCAmelCase__     :						List[Any]						=							processor.batch_decode(_A					)
          UpperCAmelCase__     :						Tuple						=							tokenizer.batch_decode(_A					)

          self.assertListEqual(_A		,		_A					)






      def lowercase_				(   self							:       Tuple					):


          '''simple docstring'''


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

          UpperCAmelCase__     :						Tuple						=							ChineseCLIPProcessor(tokenizer=_A		,		image_processor=_A					)

          UpperCAmelCase__     :						int						=							'''Alexandra，T-shirt的价格是15便士。'''
          UpperCAmelCase__     :						Optional[Any]						=							self.prepare_image_inputs()

          UpperCAmelCase__     :						Tuple						=							processor(text=_A		,		images=_A					)

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

import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils"""))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


_SCREAMING_SNAKE_CASE 		=      get_tests_dir("""fixtures""")
_SCREAMING_SNAKE_CASE 		=      get_tests_dir("""fixtures/dummy_feature_extractor_config.json""")
_SCREAMING_SNAKE_CASE 		=      get_tests_dir("""fixtures/dummy-config.json""")
class SCREAMING_SNAKE_CASE_		(      unittest.TestCase		):







   def 						lowerCamelCase_	(     self						:						Tuple				):





    """simple docstring"""


    UpperCamelCase			  =      0







   def 						lowerCamelCase_	(     self						:						int				):





    """simple docstring"""


    UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h"""				)
    self.assertIsInstance(lowerCamelCase_   ,   lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						Optional[Any]				):





    """simple docstring"""


    UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_				)
    self.assertIsInstance(lowerCamelCase_   ,   lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						Tuple				):





    """simple docstring"""


    with tempfile.TemporaryDirectory() as tmpdirname:
     UpperCamelCase			  =      WavaVecaConfig()

     # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
     UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_				).to_dict()

     config_dict.pop("""feature_extractor_type"""				)
     UpperCamelCase			  =      WavaVecaFeatureExtractor(**lowerCamelCase_				)

     # save in new folder
     model_config.save_pretrained(lowerCamelCase_				)
     config.save_pretrained(lowerCamelCase_				)

     UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_				)

     # make sure private variable is not incorrectly saved
     UpperCamelCase			  =      json.loads(config.to_json_string()				)
     self.assertTrue("""_processor_class""" not in dict_as_saved				)

    self.assertIsInstance(lowerCamelCase_   ,   lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						List[str]				):





    """simple docstring"""


    UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_				)
    self.assertIsInstance(lowerCamelCase_   ,   lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						int				):





    """simple docstring"""


    with self.assertRaisesRegex(
        lowerCamelCase_   ,   """bert-base is not a local folder and is not a valid model identifier"""				):
     UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained("""bert-base"""				)







   def 						lowerCamelCase_	(     self						:						str				):





    """simple docstring"""


    with self.assertRaisesRegex(
        lowerCamelCase_   ,   R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""				):
     UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_   ,   revision="""aaaaaa"""				)







   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    with self.assertRaisesRegex(
        lowerCamelCase_   ,   """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."""   ,   ):
     UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model"""				)







   def 						lowerCamelCase_	(     self						:						List[Any]				):





    """simple docstring"""


    with self.assertRaises(lowerCamelCase_				):
     UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(
         """hf-internal-testing/test_dynamic_feature_extractor"""				)
    # If remote code is disabled, we can't load this config.
    with self.assertRaises(lowerCamelCase_				):
     UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(
         """hf-internal-testing/test_dynamic_feature_extractor"""   ,   trust_remote_code=lowerCamelCase_				)

    UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(
        """hf-internal-testing/test_dynamic_feature_extractor"""   ,   trust_remote_code=lowerCamelCase_				)
    self.assertEqual(feature_extractor.__class__.__name__   ,   """NewFeatureExtractor"""				)

    # Test feature extractor can be reloaded.
    with tempfile.TemporaryDirectory() as tmp_dir:
     feature_extractor.save_pretrained(lowerCamelCase_				)
     UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_   ,   trust_remote_code=lowerCamelCase_				)
    self.assertEqual(reloaded_feature_extractor.__class__.__name__   ,   """NewFeatureExtractor"""				)







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





    """simple docstring"""


    try:
     AutoConfig.register("""custom"""   ,   lowerCamelCase_				)
     AutoFeatureExtractor.register(lowerCamelCase_   ,   lowerCamelCase_				)
     # Trying to register something existing in the Transformers library will raise an error
     with self.assertRaises(lowerCamelCase_				):
      AutoFeatureExtractor.register(lowerCamelCase_   ,   lowerCamelCase_				)

     # Now that the config is registered, it can be used as any other config with the auto-API
     UpperCamelCase			  =      CustomFeatureExtractor.from_pretrained(lowerCamelCase_				)
     with tempfile.TemporaryDirectory() as tmp_dir:
      feature_extractor.save_pretrained(lowerCamelCase_				)
      UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_				)
      self.assertIsInstance(lowerCamelCase_   ,   lowerCamelCase_				)

    finally:
     if "custom" in CONFIG_MAPPING._extra_content:
      del CONFIG_MAPPING._extra_content["custom"]
     if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
      del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]







   def 						lowerCamelCase_	(     self						:						Dict				):





    """simple docstring"""


    class SCREAMING_SNAKE_CASE_		(      __lowerCAmelCase		):
       __lowerCAmelCase           =				True

    try:
     AutoConfig.register("""custom"""   ,   lowerCamelCase_				)
     AutoFeatureExtractor.register(lowerCamelCase_   ,   lowerCamelCase_				)
     # If remote code is not set, the default is to use local
     UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(
         """hf-internal-testing/test_dynamic_feature_extractor"""				)
     self.assertEqual(feature_extractor.__class__.__name__   ,   """NewFeatureExtractor"""				)
     self.assertTrue(feature_extractor.is_local				)

     # If remote code is disabled, we load the local one.
     UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(
         """hf-internal-testing/test_dynamic_feature_extractor"""   ,   trust_remote_code=lowerCamelCase_				)
     self.assertEqual(feature_extractor.__class__.__name__   ,   """NewFeatureExtractor"""				)
     self.assertTrue(feature_extractor.is_local				)

     # If remote is enabled, we load from the Hub
     UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(
         """hf-internal-testing/test_dynamic_feature_extractor"""   ,   trust_remote_code=lowerCamelCase_				)
     self.assertEqual(feature_extractor.__class__.__name__   ,   """NewFeatureExtractor"""				)
     self.assertTrue(not hasattr(lowerCamelCase_   ,   """is_local"""				)				)

    finally:
     if "custom" in CONFIG_MAPPING._extra_content:
      del CONFIG_MAPPING._extra_content["custom"]
     if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
      del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
