






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

  def __init__(				self      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE=7      ,		_SCREAMING_SNAKE_CASE=3      ,		_SCREAMING_SNAKE_CASE=18      ,		_SCREAMING_SNAKE_CASE=30      ,		_SCREAMING_SNAKE_CASE=400      ,		_SCREAMING_SNAKE_CASE=True      ,		_SCREAMING_SNAKE_CASE=None      ,		_SCREAMING_SNAKE_CASE=True      ,		_SCREAMING_SNAKE_CASE=False      ,		_SCREAMING_SNAKE_CASE=True      ,		_SCREAMING_SNAKE_CASE=True      ,		_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]      ,		_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]      ,		)  ->				Dict:



        """simple docstring"""






        UpperCamelCase      = parent
        UpperCamelCase      = batch_size
        UpperCamelCase      = num_channels
        UpperCamelCase      = image_size
        UpperCamelCase      = min_resolution
        UpperCamelCase      = max_resolution
        UpperCamelCase      = do_resize
        UpperCamelCase      = size if size is not None else {"""height""": 18, """width""": 20}
        UpperCamelCase      = do_thumbnail
        UpperCamelCase      = do_align_axis
        UpperCamelCase      = do_pad
        UpperCamelCase      = do_normalize
        UpperCamelCase      = image_mean
        UpperCamelCase      = image_std


  def   A__						(				self				)  ->				Optional[Any]:



        """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     a_						(      lowerCamelCase     ,	unittest.TestCase    ):
  lowercase							=				DonutImageProcessor if is_vision_available() else None

  def   A__						(				self				)  ->				Optional[int]:



        """simple docstring"""






        UpperCamelCase      = DonutImageProcessingTester(self				)

  @property
  def   A__						(				self				)  ->				Tuple:



        """simple docstring"""






        return self.image_processor_tester.prepare_image_processor_dict()

  def   A__						(				self				)  ->				Tuple:



        """simple docstring"""






        UpperCamelCase      = self.image_processing_class(**self.image_processor_dict				)
        self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""do_resize"""				)				)
        self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""size"""				)				)
        self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""do_thumbnail"""				)				)
        self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""do_align_long_axis"""				)				)
        self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""do_pad"""				)				)
        self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""do_normalize"""				)				)
        self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""image_mean"""				)				)
        self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""image_std"""				)				)

  def   A__						(				self				)  ->				List[str]:



        """simple docstring"""






        UpperCamelCase      = self.image_processing_class.from_dict(self.image_processor_dict				)
        self.assertEqual(image_processor.size      ,		{"""height""": 18, """width""": 20}				)

        UpperCamelCase      = 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
        UpperCamelCase      = self.image_processing_class.from_dict(self.image_processor_dict      ,		size=(42, 84)				)
        self.assertEqual(image_processor.size      ,		{"""height""": 84, """width""": 42}				)

  def   A__						(				self				)  ->				Dict:



        """simple docstring"""






        pass

  @is_flaky()
  def   A__						(				self				)  ->				List[Any]:



        """simple docstring"""






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

        # Test not batched input
        UpperCamelCase      = 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
        UpperCamelCase      = image_processing(_SCREAMING_SNAKE_CASE      ,		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   A__						(				self				)  ->				Optional[Any]:



        """simple docstring"""






        UpperCamelCase      = self.image_processing_class(**self.image_processor_dict				)
        # create random numpy tensors
        UpperCamelCase      = prepare_image_inputs(self.image_processor_tester      ,		equal_resolution=_SCREAMING_SNAKE_CASE      ,		numpify=_SCREAMING_SNAKE_CASE				)
        for image in image_inputs:
              self.assertIsInstance(_SCREAMING_SNAKE_CASE      ,		np.ndarray				)

        # Test not batched input
        UpperCamelCase      = 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
        UpperCamelCase      = image_processing(_SCREAMING_SNAKE_CASE      ,		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   A__						(				self				)  ->				Any:



        """simple docstring"""






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

        # Test not batched input
        UpperCamelCase      = 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
        UpperCamelCase      = image_processing(_SCREAMING_SNAKE_CASE      ,		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"""],
            )      ,		)








'''simple docstring'''

# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ..models.auto import AutoModelForVisionaSeq
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
     from PIL import Image






class     a_						(      lowerCamelCase    ):
  lowercase							=				"""Salesforce/blip-image-captioning-base"""
  lowercase							=				(
      """This is a tool that generates a description of an image. It takes an input named `image` which should be the """
      """image to caption, and returns a text that contains the description in English."""
  )
  lowercase							=				"""image_captioner"""
  lowercase							=				AutoModelForVisionaSeq

  lowercase							=				["""image"""]
  lowercase							=				["""text"""]

  def __init__(				self      ,		*_SCREAMING_SNAKE_CASE      ,		**_SCREAMING_SNAKE_CASE				)  ->				Any:



        """simple docstring"""






        requires_backends(self      ,		["""vision"""]				)
        super().__init__(*_SCREAMING_SNAKE_CASE      ,		**_SCREAMING_SNAKE_CASE				)

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE				)  ->				str:



        """simple docstring"""






        return self.pre_processor(images=_SCREAMING_SNAKE_CASE      ,		return_tensors="""pt"""				)

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE				)  ->				str:



        """simple docstring"""






        return self.model.generate(**_SCREAMING_SNAKE_CASE				)


  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE				)  ->				Union[str, Any]:



        """simple docstring"""






        return self.pre_processor.batch_decode(_SCREAMING_SNAKE_CASE      ,		skip_special_tokens=_SCREAMING_SNAKE_CASE				)[0].strip()
