
'''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							:   Any					=     logging.get_logger(__name__)

class 	__lowercase				:

			def __init__(self			,   A = None			,   A = None			,   A=None			,   A=None      ):
								if not conversation_id:
													lowerCamelCase_					:      Any     =		uuid.uuida()
								if past_user_inputs is None:
													lowerCamelCase_					:      Optional[Any]     =		[]
								if generated_responses is None:
													lowerCamelCase_					:      Optional[Any]     =		[]

								lowerCamelCase_					:      uuid.UUID     =		conversation_id
								lowerCamelCase_					:      List[str]     =		past_user_inputs
								lowerCamelCase_					:      List[str]     =		generated_responses
								lowerCamelCase_					:      Optional[str]     =		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}\"."""      )
																		lowerCamelCase_					:      Optional[Any]     =		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:
													lowerCamelCase_					:      Tuple     =		text

			def        UpperCAmelCase__ (self      ):
								if self.new_user_input:
													self.past_user_inputs.append(self.new_user_input      )
								lowerCamelCase_					:      Tuple     =		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      ):
								lowerCamelCase_					:      str     =		F"""Conversation id: {self.uuid} \n"""
								for is_user, text in self.iter_texts():
													lowerCamelCase_					:      str     =		'''user''' if is_user else '''bot'''
													output += F"""{name} >> {text} \n"""
								return output

@add_end_docstrings(
    _lowercase      ,     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 	__lowercase				(  _lowercase    ):

			def __init__(self			,   *A			,   **A      ):
								super().__init__(*A			,   **A      )
								if self.tokenizer.pad_token_id is None:
													lowerCamelCase_					:      Optional[Any]     =		self.tokenizer.eos_token

			def        UpperCAmelCase__ (self			,   A=None			,   A=None			,   A=None			,   **A      ):
								lowerCamelCase_					:      Dict     =		{}
								lowerCamelCase_					:      int     =		{}
								lowerCamelCase_					:      Any     =		{}

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

								if "max_length" in generate_kwargs:
													lowerCamelCase_					:      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:
													lowerCamelCase_					:      str     =		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      ):
								lowerCamelCase_					:      Optional[Any]     =		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=3_2      ):
								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'''      ):
													lowerCamelCase_					:      int     =		self.tokenizer._build_conversation_input_ids(A      )
								else:
													# If the tokenizer cannot handle conversations, we default to only the old version
													lowerCamelCase_					:      int     =		self._legacy_parse_and_tokenize(A      )

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

			def        UpperCAmelCase__ (self			,   A			,   A=1_0			,   **A      ):
								lowerCamelCase_					:      Dict     =		generate_kwargs.get('''max_length'''			,   self.model.config.max_length      )

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

			def        UpperCAmelCase__ (self			,   A			,   A=True      ):
								lowerCamelCase_					:      int     =		model_outputs['''output_ids''']
								lowerCamelCase_					:      Optional[int]     =		self.tokenizer.decode(
								    output_ids[0]			,   skip_special_tokens=A			,   clean_up_tokenization_spaces=A			,   )
								lowerCamelCase_					:      str     =		model_outputs['''conversation''']
								conversation.mark_processed()
								conversation.append_response(A      )
								return conversation






			def        UpperCAmelCase__ (self			,   A      ):
								lowerCamelCase_					:      Optional[int]     =		self.tokenizer.eos_token_id
								lowerCamelCase_					:      Tuple     =		[]
								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:
													lowerCamelCase_					:      Optional[int]     =		input_ids[-self.tokenizer.model_max_length :]
								return input_ids




'''simple docstring'''


from typing import TYPE_CHECKING

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


__lowercase							:   str					=     {
    '''configuration_mctct''': ['''MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MCTCTConfig'''],
    '''feature_extraction_mctct''': ['''MCTCTFeatureExtractor'''],
    '''processing_mctct''': ['''MCTCTProcessor'''],
}


try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowercase							:   str					=     [
					    '''MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''MCTCTForCTC''',
					    '''MCTCTModel''',
					    '''MCTCTPreTrainedModel''',
					]


if TYPE_CHECKING:
					from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig
					from .feature_extraction_mctct import MCTCTFeatureExtractor
					from .processing_mctct import MCTCTProcessor

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel

else:
					import sys

					__lowercase							:   Union[str, Any]					=     _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


