
'''simple docstring'''

from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
    from PIL import Image

    from ..image_utils import load_image

if is_tf_available():
    from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING

if is_torch_available():
    import torch

    from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING

__snake_case:  int								=			logging.get_logger(__name__)


@add_end_docstrings(lowerCAmelCase__    )
class  _UpperCAmelCase ( lowerCAmelCase__    ):

     """simple docstring"""

     def __init__(      self							,		*lowerCAmelCase_							,		**lowerCAmelCase_ ):

       '''simple docstring'''

       super().__init__(*lowerCAmelCase_							,		**lowerCAmelCase_ )
       requires_backends(self							,		"""vision""" )
       self.check_model_type(
           TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == """tf""" else MODEL_FOR_VISION_2_SEQ_MAPPING )


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_=None							,		lowerCAmelCase_=None							,		lowerCAmelCase_=None ):

       '''simple docstring'''

       a_      :	List[Any]      =							{}
       a_      :	Dict      =							{}

       if prompt is not None:
         a_      :	Union[str, Any]      =							prompt

       if generate_kwargs is not None:
         a_      :	List[str]      =							generate_kwargs
       if max_new_tokens is not None:
         if "generate_kwargs" not in forward_kwargs:
           a_      :	Optional[Any]      =							{}
         if "max_new_tokens" in forward_kwargs["generate_kwargs"]:
           raise ValueError(
               """'max_new_tokens' is defined twice, once in 'generate_kwargs' and once as a direct parameter,"""
               """ please use only one""" )
         a_      :	Union[str, Any]      =							max_new_tokens
       return preprocess_params, forward_kwargs, {}


     def __call__(      self							,		lowerCAmelCase_							,		**lowerCAmelCase_ ):

       '''simple docstring'''

       return super().__call__(lowerCAmelCase_							,		**lowerCAmelCase_ )


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_=None ):

       '''simple docstring'''

       a_      :	Optional[Any]      =							load_image(lowerCAmelCase_ )

       if prompt is not None:
         if not isinstance(lowerCAmelCase_							,		lowerCAmelCase_ ):
           raise ValueError(
               f'''Received an invalid text input, got - {type(lowerCAmelCase_ )} - but expected a single string. '''
               """Note also that one single text can be provided for conditional image to text generation.""" )

         a_      :	Tuple      =							self.model.config.model_type

         if model_type == "git":
           a_      :	int      =							self.image_processor(images=lowerCAmelCase_							,		return_tensors=self.framework )
           a_      :	Tuple      =							self.tokenizer(text=lowerCAmelCase_							,		add_special_tokens=lowerCAmelCase_ ).input_ids
           a_      :	List[Any]      =							[self.tokenizer.cls_token_id] + input_ids
           a_      :	str      =							torch.tensor(lowerCAmelCase_ ).unsqueeze(0 )
           model_inputs.update({"""input_ids""": input_ids} )

         elif model_type == "pix2struct":
           a_      :	List[Any]      =							self.image_processor(images=lowerCAmelCase_							,		header_text=lowerCAmelCase_							,		return_tensors=self.framework )

         elif model_type != "vision-encoder-decoder":
           # vision-encoder-decoder does not support conditional generation
           a_      :	List[str]      =							self.image_processor(images=lowerCAmelCase_							,		return_tensors=self.framework )
           a_      :	str      =							self.tokenizer(lowerCAmelCase_							,		return_tensors=self.framework )
           model_inputs.update(lowerCAmelCase_ )

         else:
           raise ValueError(f'''Model type {model_type} does not support conditional text generation''' )

       else:
         a_      :	int      =							self.image_processor(images=lowerCAmelCase_							,		return_tensors=self.framework )

       if self.model.config.model_type == "git" and prompt is None:
         a_      :	Union[str, Any]      =							None

       return model_inputs


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_=None ):

       '''simple docstring'''

       if (
           "input_ids" in model_inputs
           and isinstance(model_inputs["""input_ids"""]							,		lowerCAmelCase_ )
           and all(x is None for x in model_inputs["""input_ids"""] )
       ):
         a_      :	Optional[int]      =							None

       if generate_kwargs is None:
         a_      :	Any      =							{}
       # FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py`
       #  parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas
       #  the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name`
       #  in the `_prepare_model_inputs` method.
       a_      :	Optional[Any]      =							model_inputs.pop(self.model.main_input_name )
       a_      :	Any      =							self.model.generate(lowerCAmelCase_							,		**lowerCAmelCase_							,		**lowerCAmelCase_ )
       return model_outputs






     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_ ):

       '''simple docstring'''

       a_      :	int      =							[]
       for output_ids in model_outputs:
         a_      :	Union[str, Any]      =							{
             """generated_text""": self.tokenizer.decode(
                 lowerCAmelCase_							,		skip_special_tokens=lowerCAmelCase_							,		)
         }
         records.append(lowerCAmelCase_ )
       return records




'''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  _UpperCAmelCase ( unittest.TestCase    ):

     """simple docstring"""

     def __init__(      self							,		lowerCAmelCase_							,		lowerCAmelCase_=7							,		lowerCAmelCase_=3							,		lowerCAmelCase_=18							,		lowerCAmelCase_=30							,		lowerCAmelCase_=4_00							,		lowerCAmelCase_=True							,		lowerCAmelCase_=None							,		lowerCAmelCase_=True							,		lowerCAmelCase_=False							,		lowerCAmelCase_=True							,		lowerCAmelCase_=True							,		lowerCAmelCase_=[0.5, 0.5, 0.5]							,		lowerCAmelCase_=[0.5, 0.5, 0.5]							,		):

       '''simple docstring'''

       a_      :	Optional[int]      =							parent
       a_      :	Union[str, Any]      =							batch_size
       a_      :	Any      =							num_channels
       a_      :	Tuple      =							image_size
       a_      :	Any      =							min_resolution
       a_      :	Dict      =							max_resolution
       a_      :	Optional[Any]      =							do_resize
       a_      :	Tuple      =							size if size is not None else {"""height""": 18, """width""": 20}
       a_      :	int      =							do_thumbnail
       a_      :	Optional[int]      =							do_align_axis
       a_      :	Tuple      =							do_pad
       a_      :	int      =							do_normalize
       a_      :	Tuple      =							image_mean
       a_      :	Optional[Any]      =							image_std






     def 							_lowerCAmelCase  (      self ):

       '''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  _UpperCAmelCase ( lowerCAmelCase__	,unittest.TestCase    ):

     """simple docstring"""

     a_    	=					DonutImageProcessor if is_vision_available() else None


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Optional[int]      =							DonutImageProcessingTester(self )


     @property
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       return self.image_processor_tester.prepare_image_processor_dict()


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Any      =							self.image_processing_class(**self.image_processor_dict )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""do_resize""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""size""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""do_thumbnail""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""do_align_long_axis""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""do_pad""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""do_normalize""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""image_mean""" ) )
       self.assertTrue(hasattr(lowerCAmelCase_							,		"""image_std""" ) )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	List[str]      =							self.image_processing_class.from_dict(self.image_processor_dict )
       self.assertEqual(image_processor.size							,		{"""height""": 18, """width""": 20} )

       a_      :	str      =							self.image_processing_class.from_dict(self.image_processor_dict							,		size=42 )
       self.assertEqual(image_processor.size							,		{"""height""": 42, """width""": 42} )

       # Previous config had dimensions in (width, height) order
       a_      :	Any      =							self.image_processing_class.from_dict(self.image_processor_dict							,		size=(42, 84) )
       self.assertEqual(image_processor.size							,		{"""height""": 84, """width""": 42} )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       pass


     @is_flaky()
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Dict      =							self.image_processing_class(**self.image_processor_dict )
       # create random PIL images
       a_      :	str      =							prepare_image_inputs(self.image_processor_tester							,		equal_resolution=lowerCAmelCase_ )
       for image in image_inputs:
         self.assertIsInstance(lowerCAmelCase_							,		Image.Image )

       # Test not batched input
       a_      :	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
       a_      :	int      =							image_processing(lowerCAmelCase_							,		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 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	List[str]      =							self.image_processing_class(**self.image_processor_dict )
       # create random numpy tensors
       a_      :	int      =							prepare_image_inputs(self.image_processor_tester							,		equal_resolution=lowerCAmelCase_							,		numpify=lowerCAmelCase_ )
       for image in image_inputs:
         self.assertIsInstance(lowerCAmelCase_							,		np.ndarray )

       # Test not batched input
       a_      :	Optional[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
       a_      :	Dict      =							image_processing(lowerCAmelCase_							,		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 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Union[str, Any]      =							self.image_processing_class(**self.image_processor_dict )
       # create random PyTorch tensors
       a_      :	Optional[Any]      =							prepare_image_inputs(self.image_processor_tester							,		equal_resolution=lowerCAmelCase_							,		torchify=lowerCAmelCase_ )
       for image in image_inputs:
         self.assertIsInstance(lowerCAmelCase_							,		torch.Tensor )

       # Test not batched input
       a_      :	Optional[int]      =							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
       a_      :	Any      =							image_processing(lowerCAmelCase_							,		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"""],
           )							,		)


