






"""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_	:				Optional[Any]										= logging.get_logger(__name__)


class      _lowercase :


      def __init__(    self				:	Any							,   a				:	str = None							,   a				:	uuid.UUID = None							,   a				:	int=None							,   a				:	Optional[int]=None						):





            """simple docstring"""







            if not conversation_id:
                  __snake_case      :    Any  			=uuid.uuida()
            if past_user_inputs is None:
                  __snake_case      :    Any  			=[]
            if generated_responses is None:
                  __snake_case      :    Optional[int]  			=[]

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


      def __eq__(    self				:	Any							,   a				:	Optional[int]						):





            """simple docstring"""







            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				:	Optional[Any]							,   a				:	str							,   a				:	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}".'''						)
                        __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:
                  __snake_case      :    str  			=text


      def _UpperCamelCase						(    self				:	Optional[int]						):





            """simple docstring"""







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


      def _UpperCamelCase						(    self				:	Dict							,   a				:	str						):





            """simple docstring"""







            self.generated_responses.append(a						)


      def _UpperCamelCase						(    self				:	Any						):





            """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				:	Optional[int]						):





            """simple docstring"""







            __snake_case      :    List[str]  			=f'''Conversation id: {self.uuid} \n'''
            for is_user, text in self.iter_texts():
                  __snake_case      :    List[str]  			='''user''' if is_user else '''bot'''
                  output += f'''{name} >> {text} \n'''
            return output



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


      def __init__(    self				:	List[str]							,   *a				:	Tuple							,   **a				:	List[str]						):





            """simple docstring"""







            super().__init__(*a							,   **a						)
            if self.tokenizer.pad_token_id is None:
                  __snake_case      :    Union[str, Any]  			=self.tokenizer.eos_token


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





            """simple docstring"""







            __snake_case      :    Optional[int]  			={}
            __snake_case      :    Union[str, Any]  			={}
            __snake_case      :    Tuple  			={}

            if min_length_for_response is not None:
                  __snake_case      :    List[Any]  			=min_length_for_response
            if minimum_tokens is not None:
                  __snake_case      :    Dict  			=minimum_tokens

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

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


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





            """simple docstring"""







            __snake_case      :    Union[str, Any]  			=super().__call__(a							,   num_workers=a							,   **a						)
            if isinstance(a							,   a						) and len(a						) == 1:
                  return outputs[0]
            return outputs


      def _UpperCamelCase						(    self				:	List[str]							,   a				:	Conversation							,   a				:	Any=3_2						):





            """simple docstring"""







            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'''						):
                  __snake_case      :    int  			=self.tokenizer._build_conversation_input_ids(a						)
            else:
                  # If the tokenizer cannot handle conversations, we default to only the old version
                  __snake_case      :    Optional[Any]  			=self._legacy_parse_and_tokenize(a						)

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


      def _UpperCamelCase						(    self				:	str							,   a				:	int							,   a				:	Union[str, Any]=1_0							,   **a				:	int						):





            """simple docstring"""







            __snake_case      :    Tuple  			=generate_kwargs.get('''max_length'''							,   self.model.config.max_length						)

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


      def _UpperCamelCase						(    self				:	int							,   a				:	Tuple							,   a				:	Optional[int]=True						):





            """simple docstring"""







            __snake_case      :    Dict  			=model_outputs['''output_ids''']
            __snake_case      :    Tuple  			=self.tokenizer.decode(
                output_ids[0]							,   skip_special_tokens=a							,   clean_up_tokenization_spaces=a							,   )
            __snake_case      :    Any  			=model_outputs['''conversation''']
            conversation.mark_processed()
            conversation.append_response(a						)
            return conversation







      def _UpperCamelCase						(    self				:	Optional[int]							,   a				:	Conversation						):





            """simple docstring"""







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










"""simple docstring"""





import unittest

import numpy as np

from transformers.testing_utils import is_flaky, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import DonutImageProcessor


class      _lowercase (     unittest.TestCase						):


      def __init__(    self				:	List[Any]							,   a				:	int							,   a				:	Dict=7							,   a				:	int=3							,   a				:	int=1_8							,   a				:	Dict=3_0							,   a				:	Dict=4_0_0							,   a				:	Optional[Any]=True							,   a				:	Dict=None							,   a				:	int=True							,   a				:	Dict=False							,   a				:	int=True							,   a				:	str=True							,   a				:	List[str]=[0.5, 0.5, 0.5]							,   a				:	Optional[Any]=[0.5, 0.5, 0.5]							,   ):





            """simple docstring"""







            __snake_case      :    List[Any]  			=parent
            __snake_case      :    List[Any]  			=batch_size
            __snake_case      :    str  			=num_channels
            __snake_case      :    Dict  			=image_size
            __snake_case      :    str  			=min_resolution
            __snake_case      :    Tuple  			=max_resolution
            __snake_case      :    str  			=do_resize
            __snake_case      :    Any  			=size if size is not None else {'''height''': 1_8, '''width''': 2_0}
            __snake_case      :    List[Any]  			=do_thumbnail
            __snake_case      :    Tuple  			=do_align_axis
            __snake_case      :    Any  			=do_pad
            __snake_case      :    Dict  			=do_normalize
            __snake_case      :    List[Any]  			=image_mean
            __snake_case      :    Any  			=image_std







      def _UpperCamelCase						(    self				:	str						):





            """simple docstring"""







            return {
                "do_resize": self.do_resize,
                "size": self.size,
                "do_thumbnail": self.do_thumbnail,
                "do_align_long_axis": self.do_align_axis,
                "do_pad": self.do_pad,
                "do_normalize": self.do_normalize,
                "image_mean": self.image_mean,
                "image_std": self.image_std,
            }



@require_torch
@require_vision
class      _lowercase (     lowerCAmelCase		,  unittest.TestCase						):
      _a   :     str								=   DonutImageProcessor if is_vision_available() else None


      def _UpperCamelCase						(    self				:	Optional[int]						):





            """simple docstring"""







            __snake_case      :    Optional[Any]  			=DonutImageProcessingTester(self						)


      @property
      def _UpperCamelCase						(    self				:	Optional[int]						):





            """simple docstring"""







            return self.image_processor_tester.prepare_image_processor_dict()


      def _UpperCamelCase						(    self				:	List[str]						):





            """simple docstring"""







            __snake_case      :    Optional[Any]  			=self.image_processing_class(**self.image_processor_dict						)
            self.assertTrue(hasattr(a							,   '''do_resize'''						)						)
            self.assertTrue(hasattr(a							,   '''size'''						)						)
            self.assertTrue(hasattr(a							,   '''do_thumbnail'''						)						)
            self.assertTrue(hasattr(a							,   '''do_align_long_axis'''						)						)
            self.assertTrue(hasattr(a							,   '''do_pad'''						)						)
            self.assertTrue(hasattr(a							,   '''do_normalize'''						)						)
            self.assertTrue(hasattr(a							,   '''image_mean'''						)						)
            self.assertTrue(hasattr(a							,   '''image_std'''						)						)


      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            __snake_case      :    Dict  			=self.image_processing_class.from_dict(self.image_processor_dict						)
            self.assertEqual(image_processor.size							,   {'''height''': 1_8, '''width''': 2_0}						)

            __snake_case      :    List[Any]  			=self.image_processing_class.from_dict(self.image_processor_dict							,   size=4_2						)
            self.assertEqual(image_processor.size							,   {'''height''': 4_2, '''width''': 4_2}						)

            # Previous config had dimensions in (width, height) order
            __snake_case      :    Tuple  			=self.image_processing_class.from_dict(self.image_processor_dict							,   size=(4_2, 8_4)						)
            self.assertEqual(image_processor.size							,   {'''height''': 8_4, '''width''': 4_2}						)


      def _UpperCamelCase						(    self				:	Any						):





            """simple docstring"""







            pass


      @is_flaky()
      def _UpperCamelCase						(    self				:	Union[str, Any]						):





            """simple docstring"""







            __snake_case      :    List[Any]  			=self.image_processing_class(**self.image_processor_dict						)
            # create random PIL images
            __snake_case      :    List[Any]  			=prepare_image_inputs(self.image_processor_tester							,   equal_resolution=a						)
            for image in image_inputs:
                  self.assertIsInstance(a							,   Image.Image						)

            # Test not batched input
            __snake_case      :    str  			=image_processing(image_inputs[0]							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    Optional[int]  			=image_processing(a							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )


      @is_flaky()
      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            __snake_case      :    Tuple  			=self.image_processing_class(**self.image_processor_dict						)
            # create random numpy tensors
            __snake_case      :    Optional[int]  			=prepare_image_inputs(self.image_processor_tester							,   equal_resolution=a							,   numpify=a						)
            for image in image_inputs:
                  self.assertIsInstance(a							,   np.ndarray						)

            # Test not batched input
            __snake_case      :    List[Any]  			=image_processing(image_inputs[0]							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    int  			=image_processing(a							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )







      @is_flaky()
      def _UpperCamelCase						(    self				:	List[str]						):





            """simple docstring"""







            __snake_case      :    Dict  			=self.image_processing_class(**self.image_processor_dict						)
            # create random PyTorch tensors
            __snake_case      :    Dict  			=prepare_image_inputs(self.image_processor_tester							,   equal_resolution=a							,   torchify=a						)
            for image in image_inputs:
                  self.assertIsInstance(a							,   torch.Tensor						)

            # Test not batched input
            __snake_case      :    Dict  			=image_processing(image_inputs[0]							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    Optional[int]  			=image_processing(a							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )


