


"""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_			     =  logging.get_logger(__name__)
class 					__lowerCAmelCase					:


		'''simple docstring'''



		def __init__(  self							, _a = None							, _a = None							, _a=None							, _a=None      ):
				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							, _a      ):
				if not isinstance(_a							, _a      ):
						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 			__UpperCAmelCase       (  self							, _a							, _a = False      ):
				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 			__UpperCAmelCase       (  self      ):
				if self.new_user_input:
						self.past_user_inputs.append(self.new_user_input      )
				__a           =			None

		def 			__UpperCAmelCase       (  self							, _a      ):
				self.generated_responses.append(_a      )

		def 			__UpperCAmelCase       (  self      ):
				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      ):
				__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 					__lowerCAmelCase					(   __SCREAMING_SNAKE_CASE       ):


		'''simple docstring'''



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

		def 			__UpperCAmelCase       (  self							, _a=None							, _a=None							, _a=None							, **_a      ):
				__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(_a      )
				return preprocess_params, forward_params, postprocess_params

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

		def 			__UpperCAmelCase       (  self							, _a							, _a=32      ):
				if not isinstance(_a							, _a      ):
						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(_a      )
				else:
						# If the tokenizer cannot handle conversations, we default to only the old version
						__a           =			self._legacy_parse_and_tokenize(_a      )

				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 			__UpperCAmelCase       (  self							, _a							, _a=10							, **_a      ):
				__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(**_a							, **_a      )
				if self.model.config.is_encoder_decoder:
						__a           =			1
				else:
						__a           =			n
				return {"output_ids": output_ids[:, start_position:], "conversation": conversation}

		def 			__UpperCAmelCase       (  self							, _a							, _a=True      ):
				__a           =			model_outputs['''output_ids''']
				__a           =			self.tokenizer.decode(
				    output_ids[0]							, skip_special_tokens=_a							, clean_up_tokenization_spaces=_a							, )
				__a           =			model_outputs['''conversation''']
				conversation.mark_processed()
				conversation.append_response(_a      )
				return conversation





		def 			__UpperCAmelCase       (  self							, _a      ):
				__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(_a							, add_special_tokens=_a      ) + [eos_token_id]      )
						else:
								input_ids.extend(self.tokenizer.encode(_a							, add_special_tokens=_a      )      )

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










"""simple docstring"""




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowercase_			     =  {"configuration_xglm": ["XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XGLMConfig"]}

try:
				if not is_sentencepiece_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_			     =  ["XGLMTokenizer"]

try:
				if not is_tokenizers_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_			     =  ["XGLMTokenizerFast"]

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_			     =  [
				    "XGLM_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "XGLMForCausalLM",
				    "XGLMModel",
				    "XGLMPreTrainedModel",
				]


try:
				if not is_flax_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_			     =  [
				    "FlaxXGLMForCausalLM",
				    "FlaxXGLMModel",
				    "FlaxXGLMPreTrainedModel",
				]


try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_			     =  [
				    "TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "TFXGLMForCausalLM",
				    "TFXGLMModel",
				    "TFXGLMPreTrainedModel",
				]


if TYPE_CHECKING:
				from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig

				try:
								if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_xglm import XGLMTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_xglm_fast import XGLMTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel

				try:
								if not is_flax_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_xglm import (
								    TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFXGLMForCausalLM,
								    TFXGLMModel,
								    TFXGLMPreTrainedModel,
								)


else:
				import sys

				lowercase_			     =  _LazyModule(__name__, globals()["__file__"], _import_structure)






