


import importlib
import inspect
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
A							:					List[Any]     						=     'src/transformers'


# This is to make sure the transformers module imported is the one in the repo.
A							:					int     						=     importlib.util.spec_from_file_location(
    'transformers',
    os.path.join(PATH_TO_TRANSFORMERS, '__init__.py'),
    submodule_search_locations=[PATH_TO_TRANSFORMERS],
)
A							:					Optional[int]     						=     spec.loader.load_module()

A							:					Union[str, Any]     						=     transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
A							:					Dict     						=     re.compile('\[(.+?)\]\((https://huggingface\.co/.+?)\)')


A							:					Optional[int]     						=     {
    'CLIPConfigMixin',
    'DecisionTransformerConfigMixin',
    'EncoderDecoderConfigMixin',
    'RagConfigMixin',
    'SpeechEncoderDecoderConfigMixin',
    'VisionEncoderDecoderConfigMixin',
    'VisionTextDualEncoderConfigMixin',
}





def 	_lowerCAmelCase       (    )						->	Optional[int]:


						'''simple docstring'''






						__snake_case				=		[]

						for config_class in list(CONFIG_MAPPING.values() ):
												__snake_case				=		False

												# source code of `config_class`
												__snake_case				=		inspect.getsource(_lowerCAmelCase )
												__snake_case				=		_re_checkpoint.findall(_lowerCAmelCase )

												for checkpoint in checkpoints:
																		# Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
																		# For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
																		__snake_case		,					__snake_case				=		checkpoint

																		# verify the checkpoint name corresponds to the checkpoint link
																		__snake_case				=		F'''https://huggingface.co/{ckpt_name}'''
																		if ckpt_link == ckpt_link_from_name:
																								__snake_case				=		True
																								break

												__snake_case				=		config_class.__name__
												if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
																		configs_without_checkpoint.append(_lowerCAmelCase )

						if len(_lowerCAmelCase ) > 0:
												__snake_case				=		"\n".join(sorted(_lowerCAmelCase ) )
												raise ValueError(F'''The following configurations don\'t contain any valid checkpoint:\n{message}''' )


if __name__ == "__main__":
	check_config_docstrings_have_checkpoints()









import os
import tempfile
import unittest
import uuid
from pathlib import Path

from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision
from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText
from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available


if is_torch_available():
	import torch

if is_soundfile_availble():
	import soundfile as sf

if is_vision_available():
	from PIL import Image





def 	_lowerCAmelCase       (    _lowerCAmelCase="" )						->	str:


						'''simple docstring'''






						__snake_case				=		tempfile.mkdtemp()
						return os.path.join(_lowerCAmelCase      ,	str(uuid.uuida() ) + suffix )






@require_soundfile
@require_torch
class     UpperCamelCase(  unittest.TestCase   ):


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Union[str, Any]						)			->	Optional[int]:




											'''simple docstring'''

											__snake_case				=		torch.rand(1_2     ,    dtype=torch.floataa						) - 0.5
											__snake_case				=		AgentAudio(SCREAMING_SNAKE_CASE						)
											__snake_case				=		str(agent_type.to_string()						)

											# Ensure that the tensor and the agent_type's tensor are the same
											self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE     ,    agent_type.to_raw()     ,    atol=1e-4						)						)

											del agent_type

											# Ensure the path remains even after the object deletion
											self.assertTrue(os.path.exists(SCREAMING_SNAKE_CASE						)						)

											# Ensure that the file contains the same value as the original tensor
											__snake_case		,					__snake_case				=		sf.read(SCREAMING_SNAKE_CASE						)
											self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE     ,    torch.tensor(SCREAMING_SNAKE_CASE						)     ,    atol=1e-4						)						)






					def 							SCREAMING_SNAKE_CASE_  (						self	:		Dict						)			->	int:




											'''simple docstring'''

											__snake_case				=		torch.rand(1_2     ,    dtype=torch.floataa						) - 0.5
											__snake_case				=		get_new_path(suffix=".wav"						)
											sf.write(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE     ,    1_6_0_0_0						)

											__snake_case				=		AgentAudio(SCREAMING_SNAKE_CASE						)

											self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE     ,    agent_type.to_raw()     ,    atol=1e-4						)						)
											self.assertEqual(agent_type.to_string()     ,    SCREAMING_SNAKE_CASE						)






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


					def 							SCREAMING_SNAKE_CASE_  (						self	:		str						)			->	Optional[int]:




											'''simple docstring'''

											__snake_case				=		torch.randint(0     ,    2_5_6     ,    (6_4, 6_4, 3)						)
											__snake_case				=		AgentImage(SCREAMING_SNAKE_CASE						)
											__snake_case				=		str(agent_type.to_string()						)

											# Ensure that the tensor and the agent_type's tensor are the same
											self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE     ,    agent_type._tensor     ,    atol=1e-4						)						)

											self.assertIsInstance(agent_type.to_raw()     ,    Image.Image						)

											# Ensure the path remains even after the object deletion
											del agent_type
											self.assertTrue(os.path.exists(SCREAMING_SNAKE_CASE						)						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple						)			->	Any:




											'''simple docstring'''

											__snake_case				=		Path(get_tests_dir("fixtures/tests_samples/COCO"						)						) / "000000039769.png"
											__snake_case				=		Image.open(SCREAMING_SNAKE_CASE						)
											__snake_case				=		AgentImage(SCREAMING_SNAKE_CASE						)

											self.assertTrue(path.samefile(agent_type.to_string()						)						)
											self.assertTrue(image == agent_type.to_raw()						)

											# Ensure the path remains even after the object deletion
											del agent_type
											self.assertTrue(os.path.exists(SCREAMING_SNAKE_CASE						)						)






					def 							SCREAMING_SNAKE_CASE_  (						self	:		Union[str, Any]						)			->	Union[str, Any]:




											'''simple docstring'''

											__snake_case				=		Path(get_tests_dir("fixtures/tests_samples/COCO"						)						) / "000000039769.png"
											__snake_case				=		Image.open(SCREAMING_SNAKE_CASE						)
											__snake_case				=		AgentImage(SCREAMING_SNAKE_CASE						)

											self.assertFalse(path.samefile(agent_type.to_string()						)						)
											self.assertTrue(image == agent_type.to_raw()						)

											# Ensure the path remains even after the object deletion
											del agent_type
											self.assertTrue(os.path.exists(SCREAMING_SNAKE_CASE						)						)






class     UpperCamelCase(  unittest.TestCase   ):


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Any						)			->	Any:




											'''simple docstring'''

											__snake_case				=		"Hey!"
											__snake_case				=		AgentText(SCREAMING_SNAKE_CASE						)

											self.assertEqual(SCREAMING_SNAKE_CASE     ,    agent_type.to_string()						)
											self.assertEqual(SCREAMING_SNAKE_CASE     ,    agent_type.to_raw()						)
											self.assertEqual(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)





