
# 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   __UpperCamelCase				(    TensorFormatter[Mapping, '''tf.Tensor''', Mapping]   ):






     """simple docstring"""







     def __init__(							self    :    Optional[Any]      ,		_A    :    List[str]=None      ,		**_A    :    Optional[Any]				):







       """simple docstring"""




       super().__init__(features=_A				)
       __SCREAMING_SNAKE_CASE     :					Optional[int]			=						tf_tensor_kwargs
       import tensorflow as tf  # noqa: import tf at initialization



     def 				UpperCAmelCase__    (							self    :    Dict      ,		_A    :    List[str]				):







       """simple docstring"""




       import tensorflow as tf

       if isinstance(_A      ,		_A				) and column:
         if all(
             isinstance(_A      ,		tf.Tensor				) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column				):
           return tf.stack(_A				)
         elif all(
             isinstance(_A      ,		(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(_A				)

       return column



     def 				UpperCAmelCase__    (							self    :    List[str]      ,		_A    :    List[Any]				):







       """simple docstring"""




       import tensorflow as tf

       if value is None:
         return value

       __SCREAMING_SNAKE_CASE     :					str			=						{}

       if isinstance(_A      ,		(np.number, np.ndarray)				) and np.issubdtype(value.dtype      ,		np.integer				):
         __SCREAMING_SNAKE_CASE     :					Dict			=						{'''dtype''': tf.intaa}
       elif isinstance(_A      ,		(np.number, np.ndarray)				) and np.issubdtype(value.dtype      ,		np.floating				):
         __SCREAMING_SNAKE_CASE     :					Union[str, Any]			=						{'''dtype''': tf.floataa}
       elif config.PIL_AVAILABLE and "PIL" in sys.modules:
         import PIL.Image

         if isinstance(_A      ,		PIL.Image.Image				):
           __SCREAMING_SNAKE_CASE     :					Union[str, Any]			=						np.asarray(_A				)

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



     def 				UpperCAmelCase__    (							self    :    Optional[Any]      ,		_A    :    Tuple				):







       """simple docstring"""




       import tensorflow as tf

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

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



     def 				UpperCAmelCase__    (							self    :    str      ,		_A    :    dict				):







       """simple docstring"""




       return map_nested(self._recursive_tensorize      ,		_A      ,		map_list=_A				)



     def 				UpperCAmelCase__    (							self    :    List[str]      ,		_A    :    pa.Table				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Optional[int]			=						self.numpy_arrow_extractor().extract_row(_A				)
       __SCREAMING_SNAKE_CASE     :					Tuple			=						self.python_features_decoder.decode_row(_A				)
       return self.recursive_tensorize(_A				)



     def 				UpperCAmelCase__    (							self    :    List[str]      ,		_A    :    pa.Table				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Union[str, Any]			=						self.numpy_arrow_extractor().extract_column(_A				)
       __SCREAMING_SNAKE_CASE     :					Union[str, Any]			=						self.python_features_decoder.decode_column(_A      ,		pa_table.column_names[0]				)
       __SCREAMING_SNAKE_CASE     :					Dict			=						self.recursive_tensorize(_A				)
       __SCREAMING_SNAKE_CASE     :					str			=						self._consolidate(_A				)
       return column




     def 				UpperCAmelCase__    (							self    :    List[str]      ,		_A    :    pa.Table				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Any			=						self.numpy_arrow_extractor().extract_batch(_A				)
       __SCREAMING_SNAKE_CASE     :					Union[str, Any]			=						self.python_features_decoder.decode_batch(_A				)
       __SCREAMING_SNAKE_CASE     :					str			=						self.recursive_tensorize(_A				)
       for column_name in batch:
         __SCREAMING_SNAKE_CASE     :					str			=						self._consolidate(batch[column_name]				)
       return batch


import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


lowercase_											=	"""
Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.

In March 2021, Hugging Face raised $40 million in a Series B funding round.[3]

On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]
"""





class   __UpperCamelCase				(    unittest.TestCase				,		lowerCAmelCase__   ):






     """simple docstring"""







     def 				UpperCAmelCase__    (							self    :    Optional[Any]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					int			=						load_tool('''summarization'''				)
       self.tool.setup()
       __SCREAMING_SNAKE_CASE     :					Any			=						load_tool('''summarization'''      ,		remote=_A				)



     def 				UpperCAmelCase__    (							self    :    Tuple				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Dict			=						self.tool(_A				)
       self.assertEqual(
           _A      ,		'''Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM.'''      ,		)



     def 				UpperCAmelCase__    (							self    :    List[str]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						self.remote_tool(_A				)
       self.assertEqual(
           _A      ,		'''Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM.'''      ,		)



     def 				UpperCAmelCase__    (							self    :    List[str]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Dict			=						self.tool(text=_A				)
       self.assertEqual(
           _A      ,		'''Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM.'''      ,		)




     def 				UpperCAmelCase__    (							self    :    Optional[int]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					str			=						self.remote_tool(text=_A				)
       self.assertEqual(
           _A      ,		'''Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM.'''      ,		)
