
# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
		import tensorflow as tf


class 		__magic_name__   (						TensorFormatter[Mapping, "tf.Tensor", Mapping]    ):





		def __init__(							self    :	List[Any]						,  lowerCamelCase__    :	Dict=None						,  **lowerCamelCase__    :	Optional[int]				):
							super().__init__(features=lowerCamelCase__				)
							lowerCAmelCase		:					Any  	=     tf_tensor_kwargs
							import tensorflow as tf  # noqa: import tf at initialization





		def       _A							(							self    :	Dict						,  lowerCamelCase__    :	List[str]				):
							import tensorflow as tf

							if isinstance(lowerCamelCase__						,  lowerCamelCase__				) and column:
												if all(
												    isinstance(lowerCamelCase__						,  tf.Tensor				) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column				):
																	return tf.stack(lowerCamelCase__				)
												elif all(
												    isinstance(lowerCamelCase__						,  (tf.Tensor, tf.RaggedTensor)				) and x.ndim == 1 and x.dtype == column[0].dtype
												    for x in column				):
																	# only rag 1-D tensors, otherwise some dimensions become ragged even though they were consolidated
																	return tf.ragged.stack(lowerCamelCase__				)

							return column





		def       _A							(							self    :	str						,  lowerCamelCase__    :	Tuple				):
							import tensorflow as tf

							if value is None:
												return value

							lowerCAmelCase		:					Tuple  	=     {}

							if isinstance(lowerCamelCase__						,  (np.number, np.ndarray)				) and np.issubdtype(value.dtype						,  np.integer				):
												lowerCAmelCase		:					int  	=     {'''dtype''': tf.intaa}
							elif isinstance(lowerCamelCase__						,  (np.number, np.ndarray)				) and np.issubdtype(value.dtype						,  np.floating				):
												lowerCAmelCase		:					Any  	=     {'''dtype''': tf.floataa}
							elif config.PIL_AVAILABLE and "PIL" in sys.modules:
												import PIL.Image

												if isinstance(lowerCamelCase__						,  PIL.Image.Image				):
																	lowerCAmelCase		:					List[str]  	=     np.asarray(lowerCamelCase__				)

							return tf.convert_to_tensor(lowerCamelCase__						,  **{**default_dtype, **self.tf_tensor_kwargs}				)





		def       _A							(							self    :	Any						,  lowerCamelCase__    :	str				):
							import tensorflow as tf

							# support for torch, tf, jax etc.
							if config.TORCH_AVAILABLE and "torch" in sys.modules:
												import torch

												if isinstance(lowerCamelCase__						,  torch.Tensor				):
																	return self._tensorize(data_struct.detach().cpu().numpy()[()]				)
							if hasattr(lowerCamelCase__						,  '''__array__'''				) and not isinstance(lowerCamelCase__						,  tf.Tensor				):
												lowerCAmelCase		:					List[str]  	=     data_struct.__array__()
							# support for nested types like struct of list of struct
							if isinstance(lowerCamelCase__						,  np.ndarray				):
												if data_struct.dtype == object:  # tf tensors cannot be instantied from an array of objects
																	return self._consolidate([self.recursive_tensorize(lowerCamelCase__				) for substruct in data_struct]				)
							elif isinstance(lowerCamelCase__						,  (list, tuple)				):
												return self._consolidate([self.recursive_tensorize(lowerCamelCase__				) for substruct in data_struct]				)
							return self._tensorize(lowerCamelCase__				)





		def       _A							(							self    :	Dict						,  lowerCamelCase__    :	dict				):
							return map_nested(self._recursive_tensorize						,  lowerCamelCase__						,  map_list=lowerCamelCase__				)





		def       _A							(							self    :	Dict						,  lowerCamelCase__    :	pa.Table				):
							lowerCAmelCase		:					List[Any]  	=     self.numpy_arrow_extractor().extract_row(lowerCamelCase__				)
							lowerCAmelCase		:					List[str]  	=     self.python_features_decoder.decode_row(lowerCamelCase__				)
							return self.recursive_tensorize(lowerCamelCase__				)





		def       _A							(							self    :	Any						,  lowerCamelCase__    :	pa.Table				):
							lowerCAmelCase		:					Any  	=     self.numpy_arrow_extractor().extract_column(lowerCamelCase__				)
							lowerCAmelCase		:					Union[str, Any]  	=     self.python_features_decoder.decode_column(lowerCamelCase__						,  pa_table.column_names[0]				)
							lowerCAmelCase		:					Any  	=     self.recursive_tensorize(lowerCamelCase__				)
							lowerCAmelCase		:					List[Any]  	=     self._consolidate(lowerCamelCase__				)
							return column





		def       _A							(							self    :	Dict						,  lowerCamelCase__    :	pa.Table				):
							lowerCAmelCase		:					Union[str, Any]  	=     self.numpy_arrow_extractor().extract_batch(lowerCamelCase__				)
							lowerCAmelCase		:					str  	=     self.python_features_decoder.decode_batch(lowerCamelCase__				)
							lowerCAmelCase		:					Tuple  	=     self.recursive_tensorize(lowerCamelCase__				)
							for column_name in batch:
												lowerCAmelCase		:					Optional[Any]  	=     self._consolidate(batch[column_name]				)
							return batch


import json
import os
import tempfile
import unittest

from transformers.modelcard import ModelCard


class 		__magic_name__   (						unittest.TestCase    ):





		def       _A							(							self    :	Dict				):
							lowerCAmelCase		:					List[Any]  	=     {
							    '''model_details''': {
							        '''Organization''': '''testing''',
							        '''Model date''': '''today''',
							        '''Model version''': '''v2.1, Developed by Test Corp in 2019.''',
							        '''Architecture''': '''Convolutional Neural Network.''',
							    },
							    '''metrics''': '''BLEU and ROUGE-1''',
							    '''evaluation_data''': {
							        '''Datasets''': {'''BLEU''': '''My-great-dataset-v1''', '''ROUGE-1''': '''My-short-dataset-v2.1'''},
							        '''Preprocessing''': '''See details on https://arxiv.org/pdf/1810.03993.pdf''',
							    },
							    '''training_data''': {
							        '''Dataset''': '''English Wikipedia dump dated 2018-12-01''',
							        '''Preprocessing''': (
							            '''Using SentencePiece vocabulary of size 52k tokens. See details on'''
							            ''' https://arxiv.org/pdf/1810.03993.pdf'''
							        ),
							    },
							    '''quantitative_analyses''': {'''BLEU''': 5_5.1, '''ROUGE-1''': 7_6},
							}





		def       _A							(							self    :	str				):
							lowerCAmelCase		:					Dict  	=     ModelCard.from_dict(self.inputs_dict				)
							self.assertTrue(hasattr(lowerCamelCase__						,  '''model_details'''				)				)
							self.assertTrue(hasattr(lowerCamelCase__						,  '''intended_use'''				)				)
							self.assertTrue(hasattr(lowerCamelCase__						,  '''factors'''				)				)
							self.assertTrue(hasattr(lowerCamelCase__						,  '''metrics'''				)				)
							self.assertTrue(hasattr(lowerCamelCase__						,  '''evaluation_data'''				)				)
							self.assertTrue(hasattr(lowerCamelCase__						,  '''training_data'''				)				)
							self.assertTrue(hasattr(lowerCamelCase__						,  '''quantitative_analyses'''				)				)
							self.assertTrue(hasattr(lowerCamelCase__						,  '''ethical_considerations'''				)				)
							self.assertTrue(hasattr(lowerCamelCase__						,  '''caveats_and_recommendations'''				)				)





		def       _A							(							self    :	Optional[Any]				):
							lowerCAmelCase		:					Tuple  	=     ModelCard.from_dict(self.inputs_dict				)
							lowerCAmelCase		:					Dict  	=     json.loads(modelcard.to_json_string()				)
							for key, value in self.inputs_dict.items():
												self.assertEqual(obj[key]						,  lowerCamelCase__				)





		def       _A							(							self    :	Tuple				):
							lowerCAmelCase		:					Tuple  	=     ModelCard.from_dict(self.inputs_dict				)

							with tempfile.TemporaryDirectory() as tmpdirname:
												lowerCAmelCase		:					str  	=     os.path.join(lowerCamelCase__						,  '''modelcard.json'''				)
												model_card_first.to_json_file(lowerCamelCase__				)
												lowerCAmelCase		:					Dict  	=     ModelCard.from_json_file(lowerCamelCase__				)

							self.assertEqual(model_card_second.to_dict()						,  model_card_first.to_dict()				)





		def       _A							(							self    :	Union[str, Any]				):
							lowerCAmelCase		:					int  	=     ModelCard.from_dict(self.inputs_dict				)

							with tempfile.TemporaryDirectory() as tmpdirname:
												model_card_first.save_pretrained(lowerCamelCase__				)
												lowerCAmelCase		:					Any  	=     ModelCard.from_pretrained(lowerCamelCase__				)

							self.assertEqual(model_card_second.to_dict()						,  model_card_first.to_dict()				)
