





"""simple docstring"""





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


lowercase__				:int				   =       logging.get_logger(__name__)




class 							snake_case							:

      '''simple docstring'''



      def __init__(							self       :      str						,				__lowercase       :      str = None						,				__lowercase       :      uuid.UUID = None						,				__lowercase       :      Any=None						,				__lowercase       :      List[Any]=None ):


        '''simple docstring'''




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

        __UpperCAmelCase    :						uuid.UUID       =						conversation_id
        __UpperCAmelCase    :						List[str]       =						past_user_inputs
        __UpperCAmelCase    :						List[str]       =						generated_responses
        __UpperCAmelCase    :						Optional[str]       =						text

      def __eq__(							self       :      Union[str, Any]						,				__lowercase       :      Union[str, 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       A_					(							self       :      List[Any]						,				__lowercase       :      str						,				__lowercase       :      bool = 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}\".''' )
            __UpperCAmelCase    :						Optional[int]       =						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:
          __UpperCAmelCase    :						Union[str, Any]       =						text

      def       A_					(							self       :      Tuple ):


        '''simple docstring'''




        if self.new_user_input:
          self.past_user_inputs.append(self.new_user_input )
        __UpperCAmelCase    :						Optional[int]       =						None

      def       A_					(							self       :      str						,				__lowercase       :      str ):


        '''simple docstring'''




        self.generated_responses.append(_UpperCamelCase )

      def       A_					(							self       :      str ):


        '''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       :      Union[str, Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Union[str, Any]       =						f'''Conversation id: {self.uuid} \n'''
        for is_user, text in self.iter_texts():
          __UpperCAmelCase    :						str       =						"""user""" if is_user else """bot"""
          output += f'''{name} >> {text} \n'''
        return output




@add_end_docstrings(
    __lowerCAmelCase      ,  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 							snake_case							(			__lowerCAmelCase ):

      '''simple docstring'''



      def __init__(							self       :      Tuple						,				*__lowercase       :      str						,				**__lowercase       :      Dict ):


        '''simple docstring'''




        super().__init__(*_UpperCamelCase						,				**_UpperCamelCase )
        if self.tokenizer.pad_token_id is None:
          __UpperCAmelCase    :						Any       =						self.tokenizer.eos_token

      def       A_					(							self       :      int						,				__lowercase       :      Union[str, Any]=None						,				__lowercase       :      Optional[Any]=None						,				__lowercase       :      Union[str, Any]=None						,				**__lowercase       :      Union[str, Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Union[str, Any]       =						{}
        __UpperCAmelCase    :						Union[str, Any]       =						{}
        __UpperCAmelCase    :						List[Any]       =						{}

        if min_length_for_response is not None:
          __UpperCAmelCase    :						Dict       =						min_length_for_response
        if minimum_tokens is not None:
          __UpperCAmelCase    :						Dict       =						minimum_tokens

        if "max_length" in generate_kwargs:
          __UpperCAmelCase    :						Any       =						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:
          __UpperCAmelCase    :						int       =						clean_up_tokenization_spaces

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

      def __call__(							self       :      Any						,				__lowercase       :      Union[Conversation, List[Conversation]]						,				__lowercase       :      Optional[Any]=0						,				**__lowercase       :      List[Any] ):


        '''simple docstring'''




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

      def       A_					(							self       :      Tuple						,				__lowercase       :      Conversation						,				__lowercase       :      Tuple=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''' ):
          __UpperCAmelCase    :						List[str]       =						self.tokenizer._build_conversation_input_ids(_UpperCamelCase )
        else:
          # If the tokenizer cannot handle conversations, we default to only the old version
          __UpperCAmelCase    :						Optional[int]       =						self._legacy_parse_and_tokenize(_UpperCamelCase )

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

      def       A_					(							self       :      Dict						,				__lowercase       :      Union[str, Any]						,				__lowercase       :      Tuple=10						,				**__lowercase       :      Optional[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Dict       =						generate_kwargs.get('''max_length'''						,				self.model.config.max_length )

        __UpperCAmelCase    :						Union[str, Any]       =						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})''' )
          __UpperCAmelCase    :						Optional[Any]       =						max_length - minimum_tokens
          __UpperCAmelCase    :						Tuple       =						model_inputs["""input_ids"""][:, -trim:]
          if "attention_mask" in model_inputs:
            __UpperCAmelCase    :						List[str]       =						model_inputs["""attention_mask"""][:, -trim:]
        __UpperCAmelCase    :						List[Any]       =						model_inputs.pop('''conversation''' )
        __UpperCAmelCase    :						Any       =						max_length
        __UpperCAmelCase    :						str       =						self.model.generate(**_UpperCamelCase						,				**_UpperCamelCase )
        if self.model.config.is_encoder_decoder:
          __UpperCAmelCase    :						Optional[Any]       =						1
        else:
          __UpperCAmelCase    :						List[Any]       =						n
        return {"output_ids": output_ids[:, start_position:], "conversation": conversation}

      def       A_					(							self       :      Union[str, Any]						,				__lowercase       :      List[str]						,				__lowercase       :      str=True ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[str]       =						model_outputs["""output_ids"""]
        __UpperCAmelCase    :						Dict       =						self.tokenizer.decode(
            output_ids[0]						,				skip_special_tokens=_UpperCamelCase						,				clean_up_tokenization_spaces=_UpperCamelCase						,				)
        __UpperCAmelCase    :						Tuple       =						model_outputs["""conversation"""]
        conversation.mark_processed()
        conversation.append_response(_UpperCamelCase )
        return conversation







      def       A_					(							self       :      Optional[int]						,				__lowercase       :      Conversation ):


        '''simple docstring'''




        __UpperCAmelCase    :						str       =						self.tokenizer.eos_token_id
        __UpperCAmelCase    :						int       =						[]
        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:
          __UpperCAmelCase    :						List[str]       =						input_ids[-self.tokenizer.model_max_length :]
        return input_ids










"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


lowercase__				:Tuple				   =       {
    'configuration_xlm': ['XLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLMConfig', 'XLMOnnxConfig'],
    'tokenization_xlm': ['XLMTokenizer'],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase__				:Optional[Any]				   =       [
      'XLM_PRETRAINED_MODEL_ARCHIVE_LIST',
      'XLMForMultipleChoice',
      'XLMForQuestionAnswering',
      'XLMForQuestionAnsweringSimple',
      'XLMForSequenceClassification',
      'XLMForTokenClassification',
      'XLMModel',
      'XLMPreTrainedModel',
      'XLMWithLMHeadModel',
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase__				:List[Any]				   =       [
      'TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST',
      'TFXLMForMultipleChoice',
      'TFXLMForQuestionAnsweringSimple',
      'TFXLMForSequenceClassification',
      'TFXLMForTokenClassification',
      'TFXLMMainLayer',
      'TFXLMModel',
      'TFXLMPreTrainedModel',
      'TFXLMWithLMHeadModel',
  ]


if TYPE_CHECKING:
  from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig
  from .tokenization_xlm import XLMTokenizer

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_xlm import (
        XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
        XLMForMultipleChoice,
        XLMForQuestionAnswering,
        XLMForQuestionAnsweringSimple,
        XLMForSequenceClassification,
        XLMForTokenClassification,
        XLMModel,
        XLMPreTrainedModel,
        XLMWithLMHeadModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_xlm import (
        TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFXLMForMultipleChoice,
        TFXLMForQuestionAnsweringSimple,
        TFXLMForSequenceClassification,
        TFXLMForTokenClassification,
        TFXLMMainLayer,
        TFXLMModel,
        TFXLMPreTrainedModel,
        TFXLMWithLMHeadModel,
    )

else:
  import sys

  lowercase__				:List[Any]				   =       _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)