"""simple docstring"""





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

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
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


lowerCAmelCase_     			=						get_tests_dir('''fixtures/dummy-config.json''')




class 			_snake_case		(     unittest.TestCase			):







				"""simple docstring"""






				def 		_lowerCAmelCase		(   self				:				str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:    Dict      	=     0





				def 		_lowerCAmelCase		(   self				:				Dict):



											"""simple docstring"""







											self.assertIsNotNone(transformers.models.auto.__spec__)
											self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""))





				def 		_lowerCAmelCase		(   self				:				str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:    List[Any]      	=     AutoConfig.from_pretrained("""bert-base-uncased""")
											self.assertIsInstance(__lowerCamelCase   ,     __lowerCamelCase)





				def 		_lowerCAmelCase		(   self				:				Optional[int]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:    Any      	=     AutoConfig.from_pretrained(__lowerCamelCase)
											self.assertIsInstance(__lowerCamelCase   ,     __lowerCamelCase)





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



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:    int      	=     AutoConfig.from_pretrained(__lowerCamelCase)
											self.assertIsInstance(__lowerCamelCase   ,     __lowerCamelCase)





				def 		_lowerCAmelCase		(   self				:				Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:    List[Any]      	=     AutoConfig.for_model("""roberta""")
											self.assertIsInstance(__lowerCamelCase   ,     __lowerCamelCase)





				def 		_lowerCAmelCase		(   self				:				Optional[int]):



											"""simple docstring"""







											with tempfile.TemporaryDirectory() as tmp_dir:
																		# This model name contains bert and roberta, but roberta ends up being picked.
																		_SCREAMING_SNAKE_CASE		:    Optional[Any]      	=     os.path.join(__lowerCamelCase   ,     """fake-roberta""")
																		os.makedirs(__lowerCamelCase   ,     exist_ok=__lowerCamelCase)
																		with open(os.path.join(__lowerCamelCase   ,     """config.json""")   ,     """w""") as f:
																									f.write(json.dumps({}))
																		_SCREAMING_SNAKE_CASE		:    Tuple      	=     AutoConfig.from_pretrained(__lowerCamelCase)
																		self.assertEqual(type(__lowerCamelCase)   ,     __lowerCamelCase)





				def 		_lowerCAmelCase		(   self				:				Dict):



											"""simple docstring"""







											try:
																		AutoConfig.register("""custom"""   ,     __lowerCamelCase)
																		# Wrong model type will raise an error
																		with self.assertRaises(__lowerCamelCase):
																									AutoConfig.register("""model"""   ,     __lowerCamelCase)
																		# Trying to register something existing in the Transformers library will raise an error
																		with self.assertRaises(__lowerCamelCase):
																									AutoConfig.register("""bert"""   ,     __lowerCamelCase)

																		# Now that the config is registered, it can be used as any other config with the auto-API
																		_SCREAMING_SNAKE_CASE		:    List[Any]      	=     CustomConfig()
																		with tempfile.TemporaryDirectory() as tmp_dir:
																									config.save_pretrained(__lowerCamelCase)
																									_SCREAMING_SNAKE_CASE		:    int      	=     AutoConfig.from_pretrained(__lowerCamelCase)
																									self.assertIsInstance(__lowerCamelCase   ,     __lowerCamelCase)

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





				def 		_lowerCAmelCase		(   self				:				List[Any]):



											"""simple docstring"""







											with self.assertRaisesRegex(
											    __lowerCamelCase   ,     """bert-base is not a local folder and is not a valid model identifier"""):
																		_SCREAMING_SNAKE_CASE		:    Optional[Any]      	=     AutoConfig.from_pretrained("""bert-base""")





				def 		_lowerCAmelCase		(   self				:				Optional[int]):



											"""simple docstring"""







											with self.assertRaisesRegex(
											    __lowerCamelCase   ,     r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""):
																		_SCREAMING_SNAKE_CASE		:    Optional[Any]      	=     AutoConfig.from_pretrained(__lowerCamelCase   ,     revision="""aaaaaa""")





				def 		_lowerCAmelCase		(   self				:				Dict):



											"""simple docstring"""







											with self.assertRaisesRegex(
											    __lowerCamelCase   ,     """hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""   ,     ):
																		_SCREAMING_SNAKE_CASE		:    Optional[Any]      	=     AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo""")





				def 		_lowerCAmelCase		(   self				:				Tuple):



											"""simple docstring"""







											with self.assertRaises(__lowerCamelCase):
																		_SCREAMING_SNAKE_CASE		:    List[Any]      	=     AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model""")
											# If remote code is disabled, we can't load this config.
											with self.assertRaises(__lowerCamelCase):
																		_SCREAMING_SNAKE_CASE		:    Tuple      	=     AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""   ,     trust_remote_code=__lowerCamelCase)

											_SCREAMING_SNAKE_CASE		:    Optional[int]      	=     AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""   ,     trust_remote_code=__lowerCamelCase)
											self.assertEqual(config.__class__.__name__   ,     """NewModelConfig""")

											# Test config can be reloaded.
											with tempfile.TemporaryDirectory() as tmp_dir:
																		config.save_pretrained(__lowerCamelCase)
																		_SCREAMING_SNAKE_CASE		:    List[Any]      	=     AutoConfig.from_pretrained(__lowerCamelCase   ,     trust_remote_code=__lowerCamelCase)
											self.assertEqual(reloaded_config.__class__.__name__   ,     """NewModelConfig""")





				def 		_lowerCAmelCase		(   self				:				Optional[Any]):



											"""simple docstring"""







											class 			_snake_case		(     UpperCamelCase__			):







															"""simple docstring"""






															a      =    "new-model"

											try:
																		AutoConfig.register("""new-model"""   ,     __lowerCamelCase)
																		# If remote code is not set, the default is to use local
																		_SCREAMING_SNAKE_CASE		:    str      	=     AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model""")
																		self.assertEqual(config.__class__.__name__   ,     """NewModelConfigLocal""")

																		# If remote code is disabled, we load the local one.
																		_SCREAMING_SNAKE_CASE		:    Optional[int]      	=     AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""   ,     trust_remote_code=__lowerCamelCase)
																		self.assertEqual(config.__class__.__name__   ,     """NewModelConfigLocal""")

																		# If remote is enabled, we load from the Hub
																		_SCREAMING_SNAKE_CASE		:    Optional[int]      	=     AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""   ,     trust_remote_code=__lowerCamelCase)
																		self.assertEqual(config.__class__.__name__   ,     """NewModelConfig""")

											finally:
																		if "new-model" in CONFIG_MAPPING._extra_content:
																									del CONFIG_MAPPING._extra_content["new-model"]









import unittest

from huggingface_hub import hf_hub_download

from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor
from transformers.pipelines import VideoClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_decord,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
)

from .test_pipelines_common import ANY




@is_pipeline_test
@require_torch_or_tf
@require_vision
@require_decord
class        SCREAMING_SNAKE_CASE__					(		unittest.TestCase ):
 __SCREAMING_SNAKE_CASE							=      MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING



 def 							UpperCamelCase     (				self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase					):
        A__		   =		hf_hub_download(
            repo_id='''nateraw/video-demo''',filename='''archery.mp4''',repo_type='''dataset'''					)
        A__		   =		VideoClassificationPipeline(model=__lowerCamelCase,image_processor=__lowerCamelCase,top_k=2					)
        A__		   =		[
            example_video_filepath,
            '''https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4''',
        ]
        return video_classifier, examples



 def 							UpperCamelCase     (				self,__lowerCamelCase,__lowerCamelCase					):
        for example in examples:
               A__		   =		video_classifier(__lowerCamelCase					)

               self.assertEqual(
                   __lowerCamelCase,[
                       {'''score''': ANY(__lowerCamelCase					), '''label''': ANY(__lowerCamelCase					)},
                       {'''score''': ANY(__lowerCamelCase					), '''label''': ANY(__lowerCamelCase					)},
                   ],)



 @require_torch
 def 							UpperCamelCase     (				self					):
        A__		   =		'''hf-internal-testing/tiny-random-VideoMAEForVideoClassification'''
        A__		   =		VideoMAEFeatureExtractor(
            size={'''shortest_edge''': 10},crop_size={'''height''': 10, '''width''': 10}					)
        A__		   =		pipeline(
            '''video-classification''',model=__lowerCamelCase,feature_extractor=__lowerCamelCase,frame_sampling_rate=4					)

        A__		   =		hf_hub_download(repo_id='''nateraw/video-demo''',filename='''archery.mp4''',repo_type='''dataset'''					)
        A__		   =		video_classifier(__lowerCamelCase,top_k=2					)
        self.assertEqual(
            nested_simplify(__lowerCamelCase,decimals=4					),[{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}],)

        A__		   =		video_classifier(
            [
                video_file_path,
                video_file_path,
            ],top_k=2,)
        self.assertEqual(
            nested_simplify(__lowerCamelCase,decimals=4					),[
                [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}],
                [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}],
            ],)




 @require_tf
 def 							UpperCamelCase     (				self					):
        pass





