






'''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


lowerCamelCase_				: int    	=							logging.get_logger(__name__)
class    _SCREAMING_SNAKE_CASE							:


			'''simple docstring'''





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

									'''simple docstring'''







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

									UpperCamelCase__         = conversation_id
									UpperCamelCase__         = past_user_inputs
									UpperCamelCase__         = generated_responses
									UpperCamelCase__         = text






			def __eq__(   self							:						Union[str, Any] ,      lowercase							:						Union[str, Any]			)							->    Optional[int]:

									'''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			)							->    Tuple:

									'''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__         = 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__         = text






			def 				A      (   self							:						Tuple			)							->    int:

									'''simple docstring'''







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






			def 				A      (   self							:						str ,      lowercase							:						str			)							->    str:

									'''simple docstring'''







									self.generated_responses.append(_UpperCamelCase			)






			def 				A      (   self							:						str			)							->    Tuple:

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

									'''simple docstring'''







									UpperCamelCase__         = f"Conversation id: {self.uuid} \n"
									for is_user, text in self.iter_texts():
															UpperCamelCase__         = """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    _SCREAMING_SNAKE_CASE							(		__lowerCAmelCase	):


			'''simple docstring'''





			def __init__(   self							:						Tuple ,      *lowercase							:						str ,      **lowercase							:						Dict			)							->    Union[str, Any]:

									'''simple docstring'''







									super().__init__(*_UpperCamelCase ,      **_UpperCamelCase			)
									if self.tokenizer.pad_token_id is None:
															UpperCamelCase__         = 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]			)							->    int:

									'''simple docstring'''







									UpperCamelCase__         = {}
									UpperCamelCase__         = {}
									UpperCamelCase__         = {}

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

									if "max_length" in generate_kwargs:
															UpperCamelCase__         = 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__         = 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]			)							->    Any:

									'''simple docstring'''







									UpperCamelCase__         = 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=3_2			)							->    Dict[str, Any]:

									'''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__         = self.tokenizer._build_conversation_input_ids(_UpperCamelCase			)
									else:
															# If the tokenizer cannot handle conversations, we default to only the old version
															UpperCamelCase__         = self._legacy_parse_and_tokenize(_UpperCamelCase			)

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






			def 				A      (   self							:						Dict ,      lowercase							:						Union[str, Any] ,      lowercase							:						Tuple=1_0 ,      **lowercase							:						Optional[Any]			)							->    Tuple:

									'''simple docstring'''







									UpperCamelCase__         = generate_kwargs.get("""max_length""" ,      self.model.config.max_length			)

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






			def 				A      (   self							:						Union[str, Any] ,      lowercase							:						List[str] ,      lowercase							:						str=True			)							->    Union[str, Any]:

									'''simple docstring'''







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







			def 				A      (   self							:						Optional[int] ,      lowercase							:						Conversation			)							->    Dict:

									'''simple docstring'''







									UpperCamelCase__         = self.tokenizer.eos_token_id
									UpperCamelCase__         = []
									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__         = input_ids[-self.tokenizer.model_max_length :]
									return input_ids








'''simple docstring'''


from typing import Optional

import pyspark

from .. import Features, NamedSplit
from ..download import DownloadMode
from ..packaged_modules.spark.spark import Spark
from .abc import AbstractDatasetReader
class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





			def __init__(   self							:						Tuple ,      lowercase							:						pyspark.sql.DataFrame ,      lowercase							:						Optional[NamedSplit] = None ,      lowercase							:						Optional[Features] = None ,      lowercase							:						bool = True ,      lowercase							:						str = None ,      lowercase							:						bool = False ,      lowercase							:						str = None ,      lowercase							:						bool = True ,      lowercase							:						str = "arrow" ,      **lowercase							:						Any ,      )							->    Optional[int]:

									'''simple docstring'''







									super().__init__(
									    split=lowercase ,      features=lowercase ,      cache_dir=lowercase ,      keep_in_memory=lowercase ,      streaming=lowercase ,      **lowercase ,      )
									UpperCamelCase__         = load_from_cache_file
									UpperCamelCase__         = file_format
									UpperCamelCase__         = Spark(
									    df=lowercase ,      features=lowercase ,      cache_dir=lowercase ,      working_dir=lowercase ,      **lowercase ,      )







			def 				A      (   self							:						int			)							->    Any:

									'''simple docstring'''







									if self.streaming:
															return self.builder.as_streaming_dataset(split=self.split			)
									UpperCamelCase__         = None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD
									self.builder.download_and_prepare(
									    download_mode=lowercase ,      file_format=self._file_format ,      )
									return self.builder.as_dataset(split=self.split			)
