





'''simple docstring'''






from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCamelCase					    =	logging.get_logger(__name__)

__UpperCamelCase					    =	{
    "microsoft/trocr-base-handwritten": (
        "https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json"
    ),
    # See all TrOCR models at https://huggingface.co/models?filter=trocr
}




class  _A (					__lowercase		):
		lowercase__:   Tuple				     =   '''trocr'''
		lowercase__:   Tuple				     =   ['''past_key_values''']
		lowercase__:   Tuple				     =   {
		    '''num_attention_heads''': '''decoder_attention_heads''',
		    '''hidden_size''': '''d_model''',
		    '''num_hidden_layers''': '''decoder_layers''',
		}

		def __init__(   self					:   Dict  ,      __magic_name__					:   List[Any]=5_02_65  ,      __magic_name__					:   Tuple=10_24  ,      __magic_name__					:   List[Any]=12  ,      __magic_name__					:   int=16  ,      __magic_name__					:   Optional[Any]=40_96  ,      __magic_name__					:   str="gelu"  ,      __magic_name__					:   Optional[int]=5_12  ,      __magic_name__					:   int=0.1  ,      __magic_name__					:   List[Any]=0.0  ,      __magic_name__					:   str=0.0  ,      __magic_name__					:   Dict=2  ,      __magic_name__					:   Dict=0.02  ,      __magic_name__					:   Optional[Any]=0.0  ,      __magic_name__					:   Optional[int]=True  ,      __magic_name__					:   Dict=False  ,      __magic_name__					:   Optional[Any]=True  ,      __magic_name__					:   Optional[int]=True  ,      __magic_name__					:   List[str]=1  ,      __magic_name__					:   str=0  ,      __magic_name__					:   Dict=2  ,      **__magic_name__					:   List[str]  ,      )					->    Any:


					"""simple docstring"""

					__snake_case					:      Optional[Any]							      =     vocab_size
					__snake_case					:      Union[str, Any]							      =     d_model
					__snake_case					:      List[Any]							      =     decoder_layers
					__snake_case					:      Optional[Any]							      =     decoder_attention_heads
					__snake_case					:      Optional[int]							      =     decoder_ffn_dim
					__snake_case					:      Optional[int]							      =     activation_function
					__snake_case					:      Dict							      =     max_position_embeddings
					__snake_case					:      Optional[int]							      =     dropout
					__snake_case					:      str							      =     attention_dropout
					__snake_case					:      List[str]							      =     activation_dropout
					__snake_case					:      str							      =     init_std
					__snake_case					:      List[str]							      =     decoder_layerdrop
					__snake_case					:      Any							      =     use_cache
					__snake_case					:      int							      =     scale_embedding
					__snake_case					:      List[Any]							      =     use_learned_position_embeddings
					__snake_case					:      int							      =     layernorm_embedding

					super().__init__(
					    pad_token_id=__magic_name__  ,      bos_token_id=__magic_name__  ,      eos_token_id=__magic_name__  ,      decoder_start_token_id=__magic_name__  ,      **__magic_name__  ,      )







'''simple docstring'''
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 A__       (	UpperCAmelCase_=""				):
						_UpperCamelCase   :						Any												=  tempfile.mkdtemp()
						return os.path.join(UpperCAmelCase_    ,    str(uuid.uuida()				) + suffix				)






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





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  torch.rand(12					,dtype=torch.floataa					) - 0.5
							_UpperCamelCase   :						Optional[int]												=  AgentAudio(lowerCamelCase__					)
							_UpperCamelCase   :						List[str]												=  str(agent_type.to_string()					)

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

							del agent_type

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

							# Ensure that the file contains the same value as the original tensor
							_UpperCamelCase							,     _UpperCamelCase   :						Union[str, Any]												=  sf.read(lowerCamelCase__					)
							self.assertTrue(torch.allclose(lowerCamelCase__					,torch.tensor(lowerCamelCase__					)					,atol=1E-4					)					)





	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  torch.rand(12					,dtype=torch.floataa					) - 0.5
							_UpperCamelCase   :						Any												=  get_new_path(suffix='.wav'					)
							sf.write(lowerCamelCase__					,lowerCamelCase__					,16000					)

							_UpperCamelCase   :						List[Any]												=  AgentAudio(lowerCamelCase__					)

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






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





	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						int												=  torch.randint(0					,256					,(64, 64, 3)					)
							_UpperCamelCase   :						Optional[Any]												=  AgentImage(lowerCamelCase__					)
							_UpperCamelCase   :						List[Any]												=  str(agent_type.to_string()					)

							# Ensure that the tensor and the agent_type's tensor are the same
							self.assertTrue(torch.allclose(lowerCamelCase__					,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(lowerCamelCase__					)					)





	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							_UpperCamelCase   :						str												=  Path(get_tests_dir('fixtures/tests_samples/COCO'					)					) / '000000039769.png'
							_UpperCamelCase   :						Tuple												=  Image.open(lowerCamelCase__					)
							_UpperCamelCase   :						Optional[Any]												=  AgentImage(lowerCamelCase__					)

							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(lowerCamelCase__					)					)





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  Path(get_tests_dir('fixtures/tests_samples/COCO'					)					) / '000000039769.png'
							_UpperCamelCase   :						Union[str, Any]												=  Image.open(lowerCamelCase__					)
							_UpperCamelCase   :						List[Any]												=  AgentImage(lowerCamelCase__					)

							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(lowerCamelCase__					)					)






class 				lowercase__      (				unittest.TestCase  ):





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  'Hey!'
							_UpperCamelCase   :						Optional[int]												=  AgentText(lowerCamelCase__					)

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


