


import unittest

from transformers import load_tool
from transformers.tools.agent_types import AGENT_TYPE_MAPPING

from .test_tools_common import ToolTesterMixin, output_types

class      lowerCamelCase	(  unittest.TestCase,			SCREAMING_SNAKE_CASE     ):

       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : Union[str, Any]	  =      load_tool('''translation'''					)
        self.tool.setup()
        snake_case   : Optional[Any]	  =      load_tool('''translation'''	,						remote=a__					)

       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : Dict	  =      self.tool('''Hey, what\'s up?'''	,						src_lang='''English'''	,						tgt_lang='''French'''					)
        self.assertEqual(a__	,						'''- Hé, comment ça va?'''					)

       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : List[Any]	  =      self.remote_tool('''Hey, what\'s up?'''	,						src_lang='''English'''	,						tgt_lang='''French'''					)
        self.assertEqual(a__	,						'''- Hé, comment ça va?'''					)

       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : Optional[int]	  =      self.tool(text='''Hey, what\'s up?'''	,						src_lang='''English'''	,						tgt_lang='''French'''					)
        self.assertEqual(a__	,						'''- Hé, comment ça va?'''					)

       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : int	  =      self.remote_tool(text='''Hey, what\'s up?'''	,						src_lang='''English'''	,						tgt_lang='''French'''					)
        self.assertEqual(a__	,						'''- Hé, comment ça va?'''					)

       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : Optional[Any]	  =      ['''Hey, what\'s up?''', '''English''', '''Spanish''']
        snake_case   : int	  =      self.tool(*a__					)

        # There is a single output
        if len(self.tool.outputs					) == 1:
         snake_case   : Dict	  =      [outputs]

        self.assertListEqual(output_types(a__					)	,						self.tool.outputs					)

       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : str	  =      ['''Hey, what\'s up?''', '''English''', '''Spanish''']
        snake_case   : Union[str, Any]	  =      self.tool(*a__					)

        if not isinstance(a__	,						a__					):
         snake_case   : List[str]	  =      [outputs]

        self.assertEqual(len(a__					)	,						len(self.tool.outputs					)					)

        for output, output_type in zip(a__	,						self.tool.outputs					):
         snake_case   : Any	  =      AGENT_TYPE_MAPPING[output_type]
         self.assertTrue(isinstance(a__	,						a__					)					)



       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : Union[str, Any]	  =      ['''Hey, what\'s up?''', '''English''', '''Spanish''']

        snake_case   : Any	  =      []

        for _input, input_type in zip(a__	,						self.tool.inputs					):
         if isinstance(a__	,						a__					):
          _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input					) for _input_type in input_type]					)
         else:
          _inputs.append(AGENT_TYPE_MAPPING[input_type](_input					)					)

        # Should not raise an error
        snake_case   : Any	  =      self.tool(*a__					)

        if not isinstance(a__	,						a__					):
         snake_case   : Optional[int]	  =      [outputs]

        self.assertEqual(len(a__					)	,						len(self.tool.outputs					)					)










from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
  import torch

a				:							Union[str, Any]					=			logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
  from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):






  def    _UpperCAmelCase	(   cls       )		->str:


   '''simple docstring'''
   return cls






def    _UpperCAmelCase	(   __lowerCamelCase ,     __lowerCamelCase = None ,     __lowerCamelCase = None ,     __lowerCamelCase = None ,     __lowerCamelCase = None ,     )		->Union[str, Any]:


 '''simple docstring'''
 snake_case   : Union[str, Any]	  =      device
 snake_case   : Any	  =      shape[0]

 snake_case   : Any	  =      layout or torch.strided
 snake_case   : Union[str, Any]	  =      device or torch.device('''cpu'''       )

 if generator is not None:
  snake_case   : Any	  =      generator.device.type if not isinstance(__lowerCamelCase ,     __lowerCamelCase       ) else generator[0].device.type
  if gen_device_type != device.type and gen_device_type == "cpu":
   snake_case   : Optional[int]	  =      '''cpu'''
   if device != "mps":
    logger.info(
        f'''The passed generator was created on \'cpu\' even though a tensor on {device} was expected.'''
        f''' Tensors will be created on \'cpu\' and then moved to {device}. Note that one can probably'''
        f''' slighly speed up this function by passing a generator that was created on the {device} device.'''       )
  elif gen_device_type != device.type and gen_device_type == "cuda":
   raise ValueError(f'''Cannot generate a {device} tensor from a generator of type {gen_device_type}.'''       )

 if isinstance(__lowerCamelCase ,     __lowerCamelCase       ):
  snake_case   : Tuple	  =      (1,) + shape[1:]
  snake_case   : List[Any]	  =      [
      torch.randn(__lowerCamelCase ,     generator=generator[i] ,     device=__lowerCamelCase ,     dtype=__lowerCamelCase ,     layout=__lowerCamelCase       )
      for i in range(__lowerCamelCase       )
  ]
  snake_case   : int	  =      torch.cat(__lowerCamelCase ,     dim=0       ).to(__lowerCamelCase       )
 else:
  snake_case   : Union[str, Any]	  =      torch.randn(__lowerCamelCase ,     generator=__lowerCamelCase ,     device=__lowerCamelCase ,     dtype=__lowerCamelCase ,     layout=__lowerCamelCase       ).to(__lowerCamelCase       )

 return latents






def    _UpperCAmelCase	(   __lowerCamelCase       )		->Tuple:


 '''simple docstring'''
 if is_torch_version('''<''' ,     '''2.0.0'''       ) or not hasattr(__lowerCamelCase ,     '''_dynamo'''       ):
  return False
 return isinstance(__lowerCamelCase ,     torch._dynamo.eval_frame.OptimizedModule       )






