





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


__UpperCAmelCase            =       logging.get_logger(__name__)




class      UpperCamelCase__					:




					"""simple docstring"""


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

							'''simple docstring'''



							if not conversation_id:
									SCREAMING_SNAKE_CASE :				Union[str, Any]		=			uuid.uuida()
							if past_user_inputs is None:
									SCREAMING_SNAKE_CASE :				Optional[int]		=			[]
							if generated_responses is None:
									SCREAMING_SNAKE_CASE :				Tuple		=			[]

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


					def __eq__(						self				:			List[str]      ,						lowerCamelCase_				:			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 lowerCamelCase_			(						self				:			Tuple      ,						lowerCamelCase_				:			str      ,						lowerCamelCase_				:			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}".'''						)
											SCREAMING_SNAKE_CASE :				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:
									SCREAMING_SNAKE_CASE :				str		=			text


					def lowerCamelCase_			(						self				:			Tuple						):

							'''simple docstring'''



							if self.new_user_input:
									self.past_user_inputs.append(self.new_user_input						)
							SCREAMING_SNAKE_CASE :				int		=			None


					def lowerCamelCase_			(						self				:			Dict      ,						lowerCamelCase_				:			str						):

							'''simple docstring'''



							self.generated_responses.append(lowerCamelCase_						)


					def lowerCamelCase_			(						self				:			Dict						):

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

							'''simple docstring'''



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







@add_end_docstrings(
    lowercase_						,					R'''
        min_length_for_response (`int`, *optional*, defaults to 32):
            The minimum length (in number of tokens) for a response.
        minimum_tokens (`int`, *optional*, defaults to 10):
            The minimum length of tokens to leave for a response.
    '''						,					)
class      UpperCamelCase__					(					lowercase_			):




					"""simple docstring"""


					def __init__(						self				:			Optional[int]      ,						*lowerCamelCase_				:			Dict      ,						**lowerCamelCase_				:			Any						):

							'''simple docstring'''



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


					def lowerCamelCase_			(						self				:			Optional[int]      ,						lowerCamelCase_				:			Union[str, Any]=None      ,						lowerCamelCase_				:			Dict=None      ,						lowerCamelCase_				:			Union[str, Any]=None      ,						**lowerCamelCase_				:			List[Any]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Optional[Any]		=			{}
							SCREAMING_SNAKE_CASE :				List[Any]		=			{}
							SCREAMING_SNAKE_CASE :				str		=			{}

							if min_length_for_response is not None:
									SCREAMING_SNAKE_CASE :				Dict		=			min_length_for_response
							if minimum_tokens is not None:
									SCREAMING_SNAKE_CASE :				Tuple		=			minimum_tokens

							if "max_length" in generate_kwargs:
									SCREAMING_SNAKE_CASE :				Dict		=			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:
									SCREAMING_SNAKE_CASE :				Union[str, Any]		=			clean_up_tokenization_spaces

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


					def __call__(						self				:			Union[str, Any]      ,						lowerCamelCase_				:			Union[Conversation, List[Conversation]]      ,						lowerCamelCase_				:			List[Any]=0      ,						**lowerCamelCase_				:			int						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Tuple		=			super().__call__(lowerCamelCase_      ,						num_workers=lowerCamelCase_      ,						**lowerCamelCase_						)
							if isinstance(lowerCamelCase_      ,						lowerCamelCase_						) and len(lowerCamelCase_						) == 1:
									return outputs[0]
							return outputs


					def lowerCamelCase_			(						self				:			Optional[Any]      ,						lowerCamelCase_				:			Conversation      ,						lowerCamelCase_				:			Any=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"""						):
									SCREAMING_SNAKE_CASE :				Union[str, Any]		=			self.tokenizer._build_conversation_input_ids(lowerCamelCase_						)
							else:
									# If the tokenizer cannot handle conversations, we default to only the old version
									SCREAMING_SNAKE_CASE :				Dict		=			self._legacy_parse_and_tokenize(lowerCamelCase_						)

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


					def lowerCamelCase_			(						self				:			List[str]      ,						lowerCamelCase_				:			Optional[Any]      ,						lowerCamelCase_				:			Tuple=10      ,						**lowerCamelCase_				:			Dict						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				str		=			generate_kwargs.get("""max_length"""      ,						self.model.config.max_length						)

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


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

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Tuple		=			model_outputs["""output_ids"""]
							SCREAMING_SNAKE_CASE :				Optional[int]		=			self.tokenizer.decode(
							    output_ids[0]      ,						skip_special_tokens=lowerCamelCase_      ,						clean_up_tokenization_spaces=lowerCamelCase_      ,						)
							SCREAMING_SNAKE_CASE :				List[Any]		=			model_outputs["""conversation"""]
							conversation.mark_processed()
							conversation.append_response(lowerCamelCase_						)
							return conversation



					def lowerCamelCase_			(						self				:			Dict      ,						lowerCamelCase_				:			Conversation						):

							'''simple docstring'''



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










'''simple docstring'''




import os
import sys
import unittest


__UpperCAmelCase            =       os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, """utils"""))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
__UpperCAmelCase            =       os.path.join(git_repo_path, """src""", """transformers""")

__UpperCAmelCase            =       """
{0} = None
"""

__UpperCAmelCase            =       """
class {0}(metaclass=DummyObject):
    _backends = {1}

    def __init__(self, *args, **kwargs):
        requires_backends(self, {1})
"""


__UpperCAmelCase            =       """
def {0}(*args, **kwargs):
    requires_backends({0}, {1})
"""




class      UpperCamelCase__					(					unittest.TestCase			):




					"""simple docstring"""


					def lowerCamelCase_			(						self				:			List[str]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Dict		=			find_backend("""    _import_structure[\"models.albert\"].append(\"AlbertTokenizerFast\")"""						)
							self.assertIsNone(lowerCamelCase_						)

							SCREAMING_SNAKE_CASE :				str		=			find_backend("""    if not is_tokenizers_available():"""						)
							self.assertEqual(lowerCamelCase_      ,						"""tokenizers"""						)

							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			find_backend("""    if not is_tensorflow_text_available():"""						)
							self.assertEqual(lowerCamelCase_      ,						"""tensorflow_text"""						)

							SCREAMING_SNAKE_CASE :				List[str]		=			find_backend("""    if not (is_sentencepiece_available() and is_tokenizers_available()):"""						)
							self.assertEqual(lowerCamelCase_      ,						"""sentencepiece_and_tokenizers"""						)

							SCREAMING_SNAKE_CASE :				Tuple		=			find_backend(
							    """    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"""						)
							self.assertEqual(lowerCamelCase_      ,						"""sentencepiece_and_tensorflow_text"""						)

							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			find_backend(
							    """    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):"""						)
							self.assertEqual(lowerCamelCase_      ,						"""sentencepiece_and_tokenizers_and_vision"""						)


					def lowerCamelCase_			(						self				:			Any						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Optional[Any]		=			read_init()
							# We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
							self.assertIn("""torch"""      ,						lowerCamelCase_						)
							self.assertIn("""tensorflow_text"""      ,						lowerCamelCase_						)
							self.assertIn("""sentencepiece_and_tokenizers"""      ,						lowerCamelCase_						)

							# Likewise, we can't assert on the exact content of a key
							self.assertIn("""BertModel"""      ,						objects["""torch"""]						)
							self.assertIn("""TFBertModel"""      ,						objects["""tf"""]						)
							self.assertIn("""FlaxBertModel"""      ,						objects["""flax"""]						)
							self.assertIn("""BertModel"""      ,						objects["""torch"""]						)
							self.assertIn("""TFBertTokenizer"""      ,						objects["""tensorflow_text"""]						)
							self.assertIn("""convert_slow_tokenizer"""      ,						objects["""sentencepiece_and_tokenizers"""]						)


					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				str		=			create_dummy_object("""CONSTANT"""      ,						"""'torch'"""						)
							self.assertEqual(lowerCamelCase_      ,						"""\nCONSTANT = None\n"""						)

							SCREAMING_SNAKE_CASE :				List[Any]		=			create_dummy_object("""function"""      ,						"""'torch'"""						)
							self.assertEqual(
							    lowerCamelCase_      ,						"""\ndef function(*args, **kwargs):\n    requires_backends(function, 'torch')\n"""						)

							SCREAMING_SNAKE_CASE :				str		=			"""
class FakeClass(metaclass=DummyObject):
    _backends = 'torch'

    def __init__(self, *args, **kwargs):
        requires_backends(self, 'torch')
"""
							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			create_dummy_object("""FakeClass"""      ,						"""'torch'"""						)
							self.assertEqual(lowerCamelCase_      ,						lowerCamelCase_						)



					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			"""# This file is autogenerated by the command `make fix-copies`, do not edit.
from ..utils import DummyObject, requires_backends


CONSTANT = None


def function(*args, **kwargs):
    requires_backends(function, [\"torch\"])


class FakeClass(metaclass=DummyObject):
    _backends = [\"torch\"]

    def __init__(self, *args, **kwargs):
        requires_backends(self, [\"torch\"])
"""
							SCREAMING_SNAKE_CASE :				List[Any]		=			create_dummy_files({"""torch""": ["""CONSTANT""", """function""", """FakeClass"""]}						)
							self.assertEqual(dummy_files["""torch"""]      ,						lowerCamelCase_						)



