import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


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

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


__lowercase											=      get_tests_dir('''fixtures''')


class 			lowerCamelCase_  (     unittest.TestCase				):






    '''simple docstring'''






    def     UpperCamelCase__     (			self) ->			str:
        # A mock response for an HTTP head request to emulate server down
        __UpperCamelCase  :Optional[Any]    						=	mock.Mock()
        __UpperCamelCase  :Optional[Any]    						=	500
        __UpperCamelCase  :Optional[int]    						=	{}
        __UpperCamelCase  :int    						=	HTTPError
        __UpperCamelCase  :Any    						=	{}

        # Download this model to make sure it's in the cache.
        __UpperCamelCase  :Tuple    						=	WavaVecaFeatureExtractor.from_pretrained('''hf-internal-testing/tiny-random-wav2vec2''')
        # Under the mock environment we get a 500 error when trying to reach the model.
        with mock.patch('''requests.Session.request'''				,				return_value=__lowercase) as mock_head:
            __UpperCamelCase  :List[Any]    						=	WavaVecaFeatureExtractor.from_pretrained('''hf-internal-testing/tiny-random-wav2vec2''')
            # This check we did call the fake head request
            mock_head.assert_called()







    def     UpperCamelCase__     (			self) ->			Dict:
        # This test is for deprecated behavior and can be removed in v5
        __UpperCamelCase  :Optional[int]    						=	WavaVecaFeatureExtractor.from_pretrained(
            '''https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json''')


@is_staging_test
class 			lowerCamelCase_  (     unittest.TestCase				):






    '''simple docstring'''






    @classmethod
    def     UpperCamelCase__     (			cls) ->			Any:
        __UpperCamelCase  :List[Any]    						=	TOKEN
        HfFolder.save_token(__lowercase)







    @classmethod
    def     UpperCamelCase__     (			cls) ->			List[str]:
        try:
            delete_repo(token=cls._token				,				repo_id='''test-feature-extractor''')
        except HTTPError:
            pass

        try:
            delete_repo(token=cls._token				,				repo_id='''valid_org/test-feature-extractor-org''')
        except HTTPError:
            pass

        try:
            delete_repo(token=cls._token				,				repo_id='''test-dynamic-feature-extractor''')
        except HTTPError:
            pass







    def     UpperCamelCase__     (			self) ->			int:
        __UpperCamelCase  :Union[str, Any]    						=	WavaVecaFeatureExtractor.from_pretrained(__lowercase)
        feature_extractor.push_to_hub('''test-feature-extractor'''				,				use_auth_token=self._token)

        __UpperCamelCase  :List[Any]    						=	WavaVecaFeatureExtractor.from_pretrained(f"""{USER}/test-feature-extractor""")
        for k, v in feature_extractor.__dict__.items():
            self.assertEqual(__lowercase				,				getattr(__lowercase				,				__lowercase))

        # Reset repo
        delete_repo(token=self._token				,				repo_id='''test-feature-extractor''')

        # Push to hub via save_pretrained
        with tempfile.TemporaryDirectory() as tmp_dir:
            feature_extractor.save_pretrained(
                __lowercase				,				repo_id='''test-feature-extractor'''				,				push_to_hub=__lowercase				,				use_auth_token=self._token)

        __UpperCamelCase  :Tuple    						=	WavaVecaFeatureExtractor.from_pretrained(f"""{USER}/test-feature-extractor""")
        for k, v in feature_extractor.__dict__.items():
            self.assertEqual(__lowercase				,				getattr(__lowercase				,				__lowercase))







    def     UpperCamelCase__     (			self) ->			Any:
        __UpperCamelCase  :Tuple    						=	WavaVecaFeatureExtractor.from_pretrained(__lowercase)
        feature_extractor.push_to_hub('''valid_org/test-feature-extractor'''				,				use_auth_token=self._token)

        __UpperCamelCase  :Any    						=	WavaVecaFeatureExtractor.from_pretrained('''valid_org/test-feature-extractor''')
        for k, v in feature_extractor.__dict__.items():
            self.assertEqual(__lowercase				,				getattr(__lowercase				,				__lowercase))

        # Reset repo
        delete_repo(token=self._token				,				repo_id='''valid_org/test-feature-extractor''')

        # Push to hub via save_pretrained
        with tempfile.TemporaryDirectory() as tmp_dir:
            feature_extractor.save_pretrained(
                __lowercase				,				repo_id='''valid_org/test-feature-extractor-org'''				,				push_to_hub=__lowercase				,				use_auth_token=self._token)

        __UpperCamelCase  :int    						=	WavaVecaFeatureExtractor.from_pretrained('''valid_org/test-feature-extractor-org''')
        for k, v in feature_extractor.__dict__.items():
            self.assertEqual(__lowercase				,				getattr(__lowercase				,				__lowercase))







    def     UpperCamelCase__     (			self) ->			Union[str, Any]:
        CustomFeatureExtractor.register_for_auto_class()
        __UpperCamelCase  :Union[str, Any]    						=	CustomFeatureExtractor.from_pretrained(__lowercase)

        feature_extractor.push_to_hub('''test-dynamic-feature-extractor'''				,				use_auth_token=self._token)

        # This has added the proper auto_map field to the config
        self.assertDictEqual(
            feature_extractor.auto_map				,				{'''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor'''}				,				)

        __UpperCamelCase  :Optional[int]    						=	AutoFeatureExtractor.from_pretrained(
            f"""{USER}/test-dynamic-feature-extractor"""				,				trust_remote_code=__lowercase)
        # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
        self.assertEqual(new_feature_extractor.__class__.__name__				,				'''CustomFeatureExtractor''')







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_  (     UpperCAmelCase_		,			unittest.TestCase				):






    '''simple docstring'''






    a__			:		Any      			=   CpmAntTokenizer
    a__			:		Optional[Any]      			=   False







    def     UpperCamelCase__     (			self) ->			Any:
        super().setUp()

        __UpperCamelCase  :Optional[int]    						=	[
            '''<d>''',
            '''</d>''',
            '''<s>''',
            '''</s>''',
            '''</_>''',
            '''<unk>''',
            '''<pad>''',
            '''</n>''',
            '''我''',
            '''是''',
            '''C''',
            '''P''',
            '''M''',
            '''A''',
            '''n''',
            '''t''',
        ]
        __UpperCamelCase  :Optional[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]))







    @tooslow
    def     UpperCamelCase__     (			self) ->			Dict:
        __UpperCamelCase  :Tuple    						=	CpmAntTokenizer.from_pretrained('''openbmb/cpm-ant-10b''')
        __UpperCamelCase  :Dict    						=	'''今天天气真好！'''
        __UpperCamelCase  :Tuple    						=	['''今天''', '''天气''', '''真''', '''好''', '''！''']
        __UpperCamelCase  :Optional[Any]    						=	tokenizer.tokenize(__lowercase)
        self.assertListEqual(__lowercase				,				__lowercase)
        __UpperCamelCase  :int    						=	'''今天天气真好！'''
        __UpperCamelCase  :List[str]    						=	[tokenizer.bos_token] + tokens

        __UpperCamelCase  :List[str]    						=	[6, 9_802, 14_962, 2_082, 831, 244]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowercase)				,				__lowercase)

        __UpperCamelCase  :Dict    						=	tokenizer.decode(__lowercase)
        self.assertEqual(__lowercase				,				__lowercase)





