



import uuid
from typing import Any, Dict, List, Optional, Union

from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
 import tensorflow as tf

if is_torch_available():
 import torch


__snake_case		:List[Any]   	=  logging.get_logger(__name__)

class 					_A :



       def __init__(	self							:  str  ,	__SCREAMING_SNAKE_CASE							:  Optional[int] = None  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any] = None  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]=None  ,	__SCREAMING_SNAKE_CASE							:  List[str]=None):


           '''simple docstring'''

           if not conversation_id:
               __a		 =       uuid.uuida()
           if past_user_inputs is None:
               __a		 =       []
           if generated_responses is None:
               __a		 =       []

           __a		 =       conversation_id
           __a		 =       past_user_inputs
           __a		 =       generated_responses
           __a		 =       text



       def __eq__(	self							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  Any):


           '''simple docstring'''

           if not isinstance(UpperCamelCase__  ,	UpperCamelCase__):
               return False
           if self.uuid == other.uuid:
               return True
           return (
               self.new_user_input == other.new_user_input
               and self.past_user_inputs == other.past_user_inputs
               and self.generated_responses == other.generated_responses
           )



       def 	_lowerCamelCase    (	self							:  Optional[Any]  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]  ,	__SCREAMING_SNAKE_CASE							:  Optional[int] = False):


           '''simple docstring'''

           if self.new_user_input:
               if overwrite:
                   logger.warning(
                       F'User input added while unprocessed input was existing: \"{self.new_user_input}\" was overwritten '
                       F'with: \"{text}\".')
                   __a		 =       text
               else:
                   logger.warning(
                       F'User input added while unprocessed input was existing: \"{self.new_user_input}\" new input '
                       F'ignored: \"{text}\". Set `overwrite` to True to overwrite unprocessed user input')
           else:
               __a		 =       text



       def 	_lowerCamelCase    (	self							:  Dict):


           '''simple docstring'''

           if self.new_user_input:
               self.past_user_inputs.append(self.new_user_input)
           __a		 =       None



       def 	_lowerCamelCase    (	self							:  Dict  ,	__SCREAMING_SNAKE_CASE							:  Any):


           '''simple docstring'''

           self.generated_responses.append(UpperCamelCase__)



       def 	_lowerCamelCase    (	self							:  Optional[int]):


           '''simple docstring'''

           for user_input, generated_response in zip(self.past_user_inputs  ,	self.generated_responses):
               yield True, user_input
               yield False, generated_response
           if self.new_user_input:
               yield True, self.new_user_input





       def __repr__(	self							:  Tuple):


           '''simple docstring'''

           __a		 =       F'Conversation id: {self.uuid} \n'
           for is_user, text in self.iter_texts():
               __a		 =       '''user''' if is_user else '''bot'''
               output += F'{name} >> {text} \n'
           return output


@add_end_docstrings(
    SCREAMING_SNAKE_CASE_   ,R'''\n        min_length_for_response (`int`, *optional*, defaults to 32):\n            The minimum length (in number of tokens) for a response.\n        minimum_tokens (`int`, *optional*, defaults to 10):\n            The minimum length of tokens to leave for a response.\n    '''   ,)
class 					_A (			SCREAMING_SNAKE_CASE_					):



       def __init__(	self							:  Any  ,	*__SCREAMING_SNAKE_CASE							:  List[Any]  ,	**__SCREAMING_SNAKE_CASE							:  Tuple):


           '''simple docstring'''

           super().__init__(*UpperCamelCase__  ,	**UpperCamelCase__)
           if self.tokenizer.pad_token_id is None:
               __a		 =       self.tokenizer.eos_token



       def 	_lowerCamelCase    (	self							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=None  ,	__SCREAMING_SNAKE_CASE							:  Tuple=None  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=None  ,	**__SCREAMING_SNAKE_CASE							:  int):


           '''simple docstring'''

           __a		 =       {}
           __a		 =       {}
           __a		 =       {}

           if min_length_for_response is not None:
               __a		 =       min_length_for_response
           if minimum_tokens is not None:
               __a		 =       minimum_tokens

           if "max_length" in generate_kwargs:
               __a		 =       generate_kwargs['''max_length''']
               # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length)
           if clean_up_tokenization_spaces is not None:
               __a		 =       clean_up_tokenization_spaces

           if generate_kwargs:
               forward_params.update(UpperCamelCase__)
           return preprocess_params, forward_params, postprocess_params



       def __call__(	self							:  str  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]  ,	__SCREAMING_SNAKE_CASE							:  Dict=0  ,	**__SCREAMING_SNAKE_CASE							:  Dict):


           '''simple docstring'''

           __a		 =       super().__call__(UpperCamelCase__  ,	num_workers=UpperCamelCase__  ,	**UpperCamelCase__)
           if isinstance(UpperCamelCase__  ,	UpperCamelCase__) and len(UpperCamelCase__) == 1:
               return outputs[0]
           return outputs



       def 	_lowerCamelCase    (	self							:  int  ,	__SCREAMING_SNAKE_CASE							:  List[Any]  ,	__SCREAMING_SNAKE_CASE							:  Any=32):


           '''simple docstring'''

           if not isinstance(UpperCamelCase__  ,	UpperCamelCase__):
               raise ValueError('''ConversationalPipeline, expects Conversation as inputs''')
           if conversation.new_user_input is None:
               raise ValueError(
                   F'Conversation with UUID {type(conversation.uuid)} does not contain new user input to process. '
                   '''Add user inputs with the conversation\'s `add_user_input` method''')
           if hasattr(self.tokenizer  ,	'''_build_conversation_input_ids'''):
               __a		 =       self.tokenizer._build_conversation_input_ids(UpperCamelCase__)
           else:
               # If the tokenizer cannot handle conversations, we default to only the old version
               __a		 =       self._legacy_parse_and_tokenize(UpperCamelCase__)

           if self.framework == "pt":
               __a		 =       torch.LongTensor([input_ids])
           elif self.framework == "tf":
               __a		 =       tf.constant([input_ids])
           return {"input_ids": input_ids, "conversation": conversation}



       def 	_lowerCamelCase    (	self							:  str  ,	__SCREAMING_SNAKE_CASE							:  Any  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=10  ,	**__SCREAMING_SNAKE_CASE							:  str):


           '''simple docstring'''

           __a		 =       generate_kwargs.get('''max_length'''  ,	self.model.config.max_length)

           __a		 =       model_inputs['''input_ids'''].shape[1]
           if max_length - minimum_tokens < n:
               logger.warning(F'Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})')
               __a		 =       max_length - minimum_tokens
               __a		 =       model_inputs['''input_ids'''][:, -trim:]
               if "attention_mask" in model_inputs:
                   __a		 =       model_inputs['''attention_mask'''][:, -trim:]
           __a		 =       model_inputs.pop('''conversation''')
           __a		 =       max_length
           __a		 =       self.model.generate(**UpperCamelCase__  ,	**UpperCamelCase__)
           if self.model.config.is_encoder_decoder:
               __a		 =       1
           else:
               __a		 =       n
           return {"output_ids": output_ids[:, start_position:], "conversation": conversation}



       def 	_lowerCamelCase    (	self							:  Any  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]  ,	__SCREAMING_SNAKE_CASE							:  str=True):


           '''simple docstring'''

           __a		 =       model_outputs['''output_ids''']
           __a		 =       self.tokenizer.decode(
               output_ids[0]  ,	skip_special_tokens=UpperCamelCase__  ,	clean_up_tokenization_spaces=UpperCamelCase__  ,	)
           __a		 =       model_outputs['''conversation''']
           conversation.mark_processed()
           conversation.append_response(UpperCamelCase__)
           return conversation





       def 	_lowerCamelCase    (	self							:  Dict  ,	__SCREAMING_SNAKE_CASE							:  int):


           '''simple docstring'''

           __a		 =       self.tokenizer.eos_token_id
           __a		 =       []
           for is_user, text in conversation.iter_texts():
               if eos_token_id is not None:
                   input_ids.extend(self.tokenizer.encode(UpperCamelCase__  ,	add_special_tokens=UpperCamelCase__) + [eos_token_id])
               else:
                   input_ids.extend(self.tokenizer.encode(UpperCamelCase__  ,	add_special_tokens=UpperCamelCase__))

           if len(UpperCamelCase__) > self.tokenizer.model_max_length:
               __a		 =       input_ids[-self.tokenizer.model_max_length :]
           return input_ids










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

import numpy as np
import pyarrow as pa

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


if TYPE_CHECKING:
 import jax
 import jaxlib

__snake_case		:str   	=  get_logger()

__snake_case		:Optional[dict]   	=  None

class 					_A (			TensorFormatter[Mapping, '''jax.Array''', Mapping]					):



       def __init__(	self							:  str  ,	__SCREAMING_SNAKE_CASE							:  int=None  ,	__SCREAMING_SNAKE_CASE							:  List[Any]=None  ,	**__SCREAMING_SNAKE_CASE							:  Any):


           '''simple docstring'''

           super().__init__(features=__SCREAMING_SNAKE_CASE)
           import jax
           from jaxlib.xla_client import Device

           if isinstance(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE):
               raise ValueError(
                   F'Expected {device} to be a `str` not {type(__SCREAMING_SNAKE_CASE)}, as `jaxlib.xla_extension.Device` '
                   '''is not serializable neither with `pickle` nor with `dill`. Instead you can surround '''
                   '''the device with `str()` to get its string identifier that will be internally mapped '''
                   '''to the actual `jaxlib.xla_extension.Device`.''')
           __a		 =       device if isinstance(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE) else str(jax.devices()[0])
           # using global variable since `jaxlib.xla_extension.Device` is not serializable neither
           # with `pickle` nor with `dill`, so we need to use a global variable instead
           global DEVICE_MAPPING
           if DEVICE_MAPPING is None:
               __a		 =       self._map_devices_to_str()
           if self.device not in list(DEVICE_MAPPING.keys()):
               logger.warning(
                   F'Device with string identifier {self.device} not listed among the available '
                   F'devices: {list(DEVICE_MAPPING.keys())}, so falling back to the default '
                   F'device: {str(jax.devices()[0])}.')
               __a		 =       str(jax.devices()[0])
           __a		 =       jnp_array_kwargs



       @staticmethod
       def 	_lowerCamelCase    (	):


           '''simple docstring'''

           import jax

           return {str(__SCREAMING_SNAKE_CASE): device for device in jax.devices()}



       def 	_lowerCamelCase    (	self							:  Union[str, Any]  ,	__SCREAMING_SNAKE_CASE							:  Tuple):


           '''simple docstring'''

           import jax
           import jax.numpy as jnp

           if isinstance(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE) and column:
               if all(
                   isinstance(__SCREAMING_SNAKE_CASE  ,	jax.Array) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column):
                   return jnp.stack(__SCREAMING_SNAKE_CASE  ,	axis=0)
           return column



       def 	_lowerCamelCase    (	self							:  str  ,	__SCREAMING_SNAKE_CASE							:  str):


           '''simple docstring'''

           import jax
           import jax.numpy as jnp

           if isinstance(__SCREAMING_SNAKE_CASE  ,	(str, bytes, type(__SCREAMING_SNAKE_CASE))):
               return value
           elif isinstance(__SCREAMING_SNAKE_CASE  ,	(np.character, np.ndarray)) and np.issubdtype(value.dtype  ,	np.character):
               return value.tolist()

           __a		 =       {}

           if isinstance(__SCREAMING_SNAKE_CASE  ,	(np.number, np.ndarray)) and np.issubdtype(value.dtype  ,	np.integer):
               # the default int precision depends on the jax config
               # see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision
               if jax.config.jax_enable_xaa:
                   __a		 =       {'''dtype''': jnp.intaa}
               else:
                   __a		 =       {'''dtype''': jnp.intaa}
           elif isinstance(__SCREAMING_SNAKE_CASE  ,	(np.number, np.ndarray)) and np.issubdtype(value.dtype  ,	np.floating):
               __a		 =       {'''dtype''': jnp.floataa}
           elif config.PIL_AVAILABLE and "PIL" in sys.modules:
               import PIL.Image

               if isinstance(__SCREAMING_SNAKE_CASE  ,	PIL.Image.Image):
                   __a		 =       np.asarray(__SCREAMING_SNAKE_CASE)

        # using global variable since `jaxlib.xla_extension.Device` is not serializable neither
        # with `pickle` nor with `dill`, so we need to use a global variable instead
           global DEVICE_MAPPING
           if DEVICE_MAPPING is None:
               __a		 =       self._map_devices_to_str()

           with jax.default_device(DEVICE_MAPPING[self.device]):
               # calling jnp.array on a np.ndarray does copy the data
               # see https://github.com/google/jax/issues/4486
               return jnp.array(__SCREAMING_SNAKE_CASE  ,	**{**default_dtype, **self.jnp_array_kwargs})



       def 	_lowerCamelCase    (	self							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  str):


           '''simple docstring'''

           import jax

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

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



       def 	_lowerCamelCase    (	self							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  dict):


           '''simple docstring'''

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



       def 	_lowerCamelCase    (	self							:  str  ,	__SCREAMING_SNAKE_CASE							:  pa.Table):


           '''simple docstring'''

           __a		 =       self.numpy_arrow_extractor().extract_row(__SCREAMING_SNAKE_CASE)
           __a		 =       self.python_features_decoder.decode_row(__SCREAMING_SNAKE_CASE)
           return self.recursive_tensorize(__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  pa.Table):


           '''simple docstring'''

           __a		 =       self.numpy_arrow_extractor().extract_column(__SCREAMING_SNAKE_CASE)
           __a		 =       self.python_features_decoder.decode_column(__SCREAMING_SNAKE_CASE  ,	pa_table.column_names[0])
           __a		 =       self.recursive_tensorize(__SCREAMING_SNAKE_CASE)
           __a		 =       self._consolidate(__SCREAMING_SNAKE_CASE)
           return column





       def 	_lowerCamelCase    (	self							:  Union[str, Any]  ,	__SCREAMING_SNAKE_CASE							:  pa.Table):


           '''simple docstring'''

           __a		 =       self.numpy_arrow_extractor().extract_batch(__SCREAMING_SNAKE_CASE)
           __a		 =       self.python_features_decoder.decode_batch(__SCREAMING_SNAKE_CASE)
           __a		 =       self.recursive_tensorize(__SCREAMING_SNAKE_CASE)
           for column_name in batch:
               __a		 =       self._consolidate(batch[column_name])
           return batch





