


import unittest

import numpy as np

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
		from transformers.models.bert.modeling_flax_bert import (
		    FlaxBertForMaskedLM,
		    FlaxBertForMultipleChoice,
		    FlaxBertForNextSentencePrediction,
		    FlaxBertForPreTraining,
		    FlaxBertForQuestionAnswering,
		    FlaxBertForSequenceClassification,
		    FlaxBertForTokenClassification,
		    FlaxBertModel,
		)



class 						_UpperCAmelCase      (     unittest.TestCase							):






		def __init__(						self				:     Tuple   ,  UpperCAmelCase				:     List[str]   ,  UpperCAmelCase				:     int=13   ,  UpperCAmelCase				:     Optional[Any]=7   ,  UpperCAmelCase				:     Dict=True   ,  UpperCAmelCase				:     Tuple=True   ,  UpperCAmelCase				:     Optional[Any]=True   ,  UpperCAmelCase				:     int=True   ,  UpperCAmelCase				:     Union[str, Any]=99   ,  UpperCAmelCase				:     Tuple=32   ,  UpperCAmelCase				:     Union[str, Any]=5   ,  UpperCAmelCase				:     int=4   ,  UpperCAmelCase				:     Optional[Any]=37   ,  UpperCAmelCase				:     Optional[Any]="gelu"   ,  UpperCAmelCase				:     List[Any]=0.1   ,  UpperCAmelCase				:     int=0.1   ,  UpperCAmelCase				:     Tuple=5_12   ,  UpperCAmelCase				:     Dict=16   ,  UpperCAmelCase				:     Union[str, Any]=2   ,  UpperCAmelCase				:     Any=0.02   ,  UpperCAmelCase				:     List[Any]=4   ,  ):
				SCREAMING_SNAKE_CASE_				:List[str]				     =						parent
				SCREAMING_SNAKE_CASE_				:int				     =						batch_size
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						seq_length
				SCREAMING_SNAKE_CASE_				:str				     =						is_training
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						use_attention_mask
				SCREAMING_SNAKE_CASE_				:int				     =						use_token_type_ids
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						use_labels
				SCREAMING_SNAKE_CASE_				:int				     =						vocab_size
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						hidden_size
				SCREAMING_SNAKE_CASE_				:List[str]				     =						num_hidden_layers
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						num_attention_heads
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						intermediate_size
				SCREAMING_SNAKE_CASE_				:str				     =						hidden_act
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						hidden_dropout_prob
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						attention_probs_dropout_prob
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						max_position_embeddings
				SCREAMING_SNAKE_CASE_				:Any				     =						type_vocab_size
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						type_sequence_label_size
				SCREAMING_SNAKE_CASE_				:Any				     =						initializer_range
				SCREAMING_SNAKE_CASE_				:List[str]				     =						num_choices






		def _snake_case		(						self				:     List[Any]):
				SCREAMING_SNAKE_CASE_				:Tuple				     =						ids_tensor([self.batch_size, self.seq_length]   ,  self.vocab_size)

				SCREAMING_SNAKE_CASE_				:Any				     =						None
				if self.use_attention_mask:
						SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						random_attention_mask([self.batch_size, self.seq_length])

				SCREAMING_SNAKE_CASE_				:Dict				     =						None
				if self.use_token_type_ids:
						SCREAMING_SNAKE_CASE_				:Tuple				     =						ids_tensor([self.batch_size, self.seq_length]   ,  self.type_vocab_size)

				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						BertConfig(
				    vocab_size=self.vocab_size   ,  hidden_size=self.hidden_size   ,  num_hidden_layers=self.num_hidden_layers   ,  num_attention_heads=self.num_attention_heads   ,  intermediate_size=self.intermediate_size   ,  hidden_act=self.hidden_act   ,  hidden_dropout_prob=self.hidden_dropout_prob   ,  attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,  max_position_embeddings=self.max_position_embeddings   ,  type_vocab_size=self.type_vocab_size   ,  is_decoder=lowerCamelCase__   ,  initializer_range=self.initializer_range   ,  )

				return config, input_ids, token_type_ids, attention_mask






		def _snake_case		(						self				:     int):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						self.prepare_config_and_inputs()
				SCREAMING_SNAKE_CASE_				:List[str]				     =						config_and_inputs
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask}
				return config, inputs_dict







		def _snake_case		(						self				:     Tuple):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						self.prepare_config_and_inputs()
				SCREAMING_SNAKE_CASE_				:Any				     =						config_and_inputs

				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						True
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						floats_tensor([self.batch_size, self.seq_length, self.hidden_size])
				SCREAMING_SNAKE_CASE_				:Dict				     =						ids_tensor([self.batch_size, self.seq_length]   ,  vocab_size=2)

				return (
				    config,
				    input_ids,
				    attention_mask,
				    encoder_hidden_states,
				    encoder_attention_mask,
				)




@require_flax
class 						_UpperCAmelCase      (     lowercase		,		unittest.TestCase							):
		lowerCamelCase_	:			Any       			=  True

		lowerCamelCase_	:			str       			=  (
		    (
		        FlaxBertModel,
		        FlaxBertForPreTraining,
		        FlaxBertForMaskedLM,
		        FlaxBertForMultipleChoice,
		        FlaxBertForQuestionAnswering,
		        FlaxBertForNextSentencePrediction,
		        FlaxBertForSequenceClassification,
		        FlaxBertForTokenClassification,
		        FlaxBertForQuestionAnswering,
		    )
		    if is_flax_available()
		    else ()
		)






		def _snake_case		(						self				:     Optional[Any]):
				SCREAMING_SNAKE_CASE_				:Tuple				     =						FlaxBertModelTester(self)







		@slow
		def _snake_case		(						self				:     Optional[int]):
				SCREAMING_SNAKE_CASE_				:List[str]				     =						FlaxBertModel.from_pretrained("bert-base-cased")
				SCREAMING_SNAKE_CASE_				:str				     =						model(np.ones((1, 1)))
				self.assertIsNotNone(lowerCamelCase__)











'''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__					)


