





"""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__				:Dict				   =       logging.get_logger(__name__)




class 							snake_case							:

      '''simple docstring'''



      def __init__(							self       :      Dict						,				__lowercase       :      int = None						,				__lowercase       :      Optional[Any] = None						,				__lowercase       :      int=None						,				__lowercase       :      Tuple=None ):


        '''simple docstring'''




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

        __UpperCAmelCase    :						str       =						conversation_id
        __UpperCAmelCase    :						List[str]       =						past_user_inputs
        __UpperCAmelCase    :						Dict       =						generated_responses
        __UpperCAmelCase    :						Any       =						text

      def __eq__(							self       :      Dict						,				__lowercase       :      Optional[Any] ):


        '''simple docstring'''




        if not isinstance(_lowerCamelCase						,				_lowerCamelCase ):
          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       :      str						,				__lowercase       :      Dict						,				__lowercase       :      List[Any] = 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    :						str       =						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    :						Any       =						text

      def       A_					(							self       :      List[str] ):


        '''simple docstring'''




        if self.new_user_input:
          self.past_user_inputs.append(self.new_user_input )
        __UpperCAmelCase    :						Tuple       =						None

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


        '''simple docstring'''




        self.generated_responses.append(_lowerCamelCase )

      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       :      List[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[Any]       =						f'''Conversation id: {self.uuid} \n'''
        for is_user, text in self.iter_texts():
          __UpperCAmelCase    :						Union[str, Any]       =						'''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       :      List[str]						,				*__lowercase       :      Tuple						,				**__lowercase       :      Union[str, Any] ):


        '''simple docstring'''




        super().__init__(*_lowerCamelCase						,				**_lowerCamelCase )
        if self.tokenizer.pad_token_id is None:
          __UpperCAmelCase    :						List[Any]       =						self.tokenizer.eos_token

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


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[Any]       =						{}
        __UpperCAmelCase    :						Optional[Any]       =						{}
        __UpperCAmelCase    :						Optional[Any]       =						{}

        if min_length_for_response is not None:
          __UpperCAmelCase    :						str       =						min_length_for_response
        if minimum_tokens is not None:
          __UpperCAmelCase    :						Optional[Any]       =						minimum_tokens

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

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

      def __call__(							self       :      Tuple						,				__lowercase       :      Any						,				__lowercase       :      Dict=0						,				**__lowercase       :      List[Any] ):


        '''simple docstring'''




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

      def       A_					(							self       :      List[str]						,				__lowercase       :      List[str]						,				__lowercase       :      str=32 ):


        '''simple docstring'''




        if not isinstance(_lowerCamelCase						,				_lowerCamelCase ):
          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[Any]       =						self.tokenizer._build_conversation_input_ids(_lowerCamelCase )
        else:
          # If the tokenizer cannot handle conversations, we default to only the old version
          __UpperCAmelCase    :						List[str]       =						self._legacy_parse_and_tokenize(_lowerCamelCase )

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

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


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						generate_kwargs.get('''max_length'''						,				self.model.config.max_length )

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

      def       A_					(							self       :      Optional[Any]						,				__lowercase       :      int						,				__lowercase       :      Optional[int]=True ):


        '''simple docstring'''




        __UpperCAmelCase    :						Any       =						model_outputs['''output_ids''']
        __UpperCAmelCase    :						Optional[Any]       =						self.tokenizer.decode(
            output_ids[0]						,				skip_special_tokens=_lowerCamelCase						,				clean_up_tokenization_spaces=_lowerCamelCase						,				)
        __UpperCAmelCase    :						Any       =						model_outputs['''conversation''']
        conversation.mark_processed()
        conversation.append_response(_lowerCamelCase )
        return conversation







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


        '''simple docstring'''




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

        if len(_lowerCamelCase ) > self.tokenizer.model_max_length:
          __UpperCAmelCase    :						Optional[Any]       =						input_ids[-self.tokenizer.model_max_length :]
        return input_ids






"""simple docstring"""





# coding=utf-8
# Copyright 2020 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# this script dumps information about the environment

import os
import sys

import transformers


lowercase__				:int				   =       '3'

print('Python version:', sys.version)
print('transformers version:', transformers.__version__)

try:
  import torch

  print('Torch version:', torch.__version__)
  print('Cuda available:', torch.cuda.is_available())
  print('Cuda version:', torch.version.cuda)
  print('CuDNN version:', torch.backends.cudnn.version())
  print('Number of GPUs available:', torch.cuda.device_count())
  print('NCCL version:', torch.cuda.nccl.version())
except ImportError:
  print('Torch version:', None)

try:
  import deepspeed

  print('DeepSpeed version:', deepspeed.__version__)
except ImportError:
  print('DeepSpeed version:', None)

try:
  import tensorflow as tf

  print('TensorFlow version:', tf.__version__)
  print('TF GPUs available:', bool(tf.config.list_physical_devices('GPU')))
  print('Number of TF GPUs available:', len(tf.config.list_physical_devices('GPU')))
except ImportError:
  print('TensorFlow version:', None)