

"""simple docstring"""

import unittest

import numpy as np

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

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import ChineseCLIPImageProcessor






class 			__a					(						unittest.TestCase       ):





       """simple docstring"""


       def __init__(   self			,					snake_case			,					snake_case=7			,					snake_case=3			,					snake_case=18			,					snake_case=30			,					snake_case=400			,					snake_case=True			,					snake_case=None			,					snake_case=True			,					snake_case=None			,					snake_case=True			,					snake_case=[0.48_145_466, 0.4_578_275, 0.40_821_073]			,					snake_case=[0.26_862_954, 0.26_130_258, 0.27_577_711]			,					snake_case=True			,					):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				size if size is not None else {"height": 224, "width": 224}
        lowerCAmelCase__   :				Any 			=				crop_size if crop_size is not None else {"height": 18, "width": 18}
        lowerCAmelCase__   :				List[Any] 			=				parent
        lowerCAmelCase__   :				Optional[int] 			=				batch_size
        lowerCAmelCase__   :				Dict 			=				num_channels
        lowerCAmelCase__   :				Dict 			=				image_size
        lowerCAmelCase__   :				Optional[int] 			=				min_resolution
        lowerCAmelCase__   :				Optional[Any] 			=				max_resolution
        lowerCAmelCase__   :				Union[str, Any] 			=				do_resize
        lowerCAmelCase__   :				Optional[Any] 			=				size
        lowerCAmelCase__   :				Any 			=				do_center_crop
        lowerCAmelCase__   :				int 			=				crop_size
        lowerCAmelCase__   :				int 			=				do_normalize
        lowerCAmelCase__   :				Union[str, Any] 			=				image_mean
        lowerCAmelCase__   :				str 			=				image_std
        lowerCAmelCase__   :				Union[str, Any] 			=				do_convert_rgb






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        return {
            "do_resize": self.do_resize,
            "size": self.size,
            "do_center_crop": self.do_center_crop,
            "crop_size": self.crop_size,
            "do_normalize": self.do_normalize,
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_convert_rgb": self.do_convert_rgb,
        }






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case=False			,					snake_case=False			,					snake_case=False   ):





        """simple docstring"""

        assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

        if equal_resolution:
         lowerCAmelCase__   :				Union[str, Any] 			=				[]
         for i in range(self.batch_size   ):
          image_inputs.append(
              np.random.randint(
                  255			,					size=(self.num_channels, self.max_resolution, self.max_resolution)			,					dtype=np.uinta   )   )
        else:
         lowerCAmelCase__   :				Tuple 			=				[]
         for i in range(self.batch_size   ):
          lowerCAmelCase__			,						lowerCAmelCase__   :				Tuple 			=				np.random.choice(np.arange(self.min_resolution			,					self.max_resolution   )			,					2   )
          image_inputs.append(np.random.randint(255			,					size=(self.num_channels, width, height)			,					dtype=np.uinta   )   )

        if not numpify and not torchify:
         # PIL expects the channel dimension as last dimension
         lowerCAmelCase__   :				Optional[Any] 			=				[Image.fromarray(np.moveaxis(snake_case			,					0			,					-1   )   ) for x in image_inputs]

        if torchify:
         lowerCAmelCase__   :				Any 			=				[torch.from_numpy(snake_case   ) for x in image_inputs]

        return image_inputs






@require_torch
@require_vision
class 			__a					(						__magic_name__       ,	unittest.TestCase       ):





       """simple docstring"""


       __UpperCamelCase					:      str    =						ChineseCLIPImageProcessor if is_vision_available() else None






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				ChineseCLIPImageProcessingTester(self			,					do_center_crop=snake_case   )






       @property
       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        return self.image_processor_tester.prepare_image_processor_dict()






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				str 			=				self.image_processing_class(**self.image_processor_dict   )
        self.assertTrue(hasattr(snake_case			,					"do_resize"   )   )
        self.assertTrue(hasattr(snake_case			,					"size"   )   )
        self.assertTrue(hasattr(snake_case			,					"do_center_crop"   )   )
        self.assertTrue(hasattr(snake_case			,					"center_crop"   )   )
        self.assertTrue(hasattr(snake_case			,					"do_normalize"   )   )
        self.assertTrue(hasattr(snake_case			,					"image_mean"   )   )
        self.assertTrue(hasattr(snake_case			,					"image_std"   )   )
        self.assertTrue(hasattr(snake_case			,					"do_convert_rgb"   )   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Tuple 			=				self.image_processing_class.from_dict(self.image_processor_dict   )
        self.assertEqual(image_processor.size			,					{"height": 224, "width": 224}   )
        self.assertEqual(image_processor.crop_size			,					{"height": 18, "width": 18}   )

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






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        pass






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				List[Any] 			=				self.image_processing_class(**self.image_processor_dict   )
        # create random PIL images
        lowerCAmelCase__   :				Tuple 			=				self.image_processor_tester.prepare_inputs(equal_resolution=snake_case   )
        for image in image_inputs:
         self.assertIsInstance(snake_case			,					Image.Image   )

        # Test not batched input
        lowerCAmelCase__   :				List[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.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )			,					)

        # Test batched
        lowerCAmelCase__   :				Any 			=				image_processing(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.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )			,					)






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				List[Any] 			=				self.image_processing_class(**self.image_processor_dict   )
        # create random numpy tensors
        lowerCAmelCase__   :				List[str] 			=				self.image_processor_tester.prepare_inputs(equal_resolution=snake_case			,					numpify=snake_case   )
        for image in image_inputs:
         self.assertIsInstance(snake_case			,					np.ndarray   )

        # Test not batched input
        lowerCAmelCase__   :				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.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )			,					)

        # Test batched
        lowerCAmelCase__   :				List[str] 			=				image_processing(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.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )			,					)






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				int 			=				self.image_processing_class(**self.image_processor_dict   )
        # create random PyTorch tensors
        lowerCAmelCase__   :				Dict 			=				self.image_processor_tester.prepare_inputs(equal_resolution=snake_case			,					torchify=snake_case   )
        for image in image_inputs:
         self.assertIsInstance(snake_case			,					torch.Tensor   )

        # Test not batched input
        lowerCAmelCase__   :				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.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )			,					)

        # Test batched
        lowerCAmelCase__   :				Tuple 			=				image_processing(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.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )			,					)






@require_torch
@require_vision
class 			__a					(						__magic_name__       ,	unittest.TestCase       ):





       """simple docstring"""


       __UpperCamelCase					:      Dict    =						ChineseCLIPImageProcessor if is_vision_available() else None






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[Any] 			=				ChineseCLIPImageProcessingTester(self			,					num_channels=4			,					do_center_crop=snake_case   )
        lowerCAmelCase__   :				List[str] 			=				3






       @property
       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        return self.image_processor_tester.prepare_image_processor_dict()






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Dict 			=				self.image_processing_class(**self.image_processor_dict   )
        self.assertTrue(hasattr(snake_case			,					"do_resize"   )   )
        self.assertTrue(hasattr(snake_case			,					"size"   )   )
        self.assertTrue(hasattr(snake_case			,					"do_center_crop"   )   )
        self.assertTrue(hasattr(snake_case			,					"center_crop"   )   )
        self.assertTrue(hasattr(snake_case			,					"do_normalize"   )   )
        self.assertTrue(hasattr(snake_case			,					"image_mean"   )   )
        self.assertTrue(hasattr(snake_case			,					"image_std"   )   )
        self.assertTrue(hasattr(snake_case			,					"do_convert_rgb"   )   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        pass






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Dict 			=				self.image_processing_class(**self.image_processor_dict   )
        # create random PIL images
        lowerCAmelCase__   :				Any 			=				self.image_processor_tester.prepare_inputs(equal_resolution=snake_case   )
        for image in image_inputs:
         self.assertIsInstance(snake_case			,					Image.Image   )

        # Test not batched input
        lowerCAmelCase__   :				Tuple 			=				image_processing(image_inputs[0]			,					return_tensors="pt"   ).pixel_values
        self.assertEqual(
            encoded_images.shape			,					(
                1,
                self.expected_encoded_image_num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )			,					)

        # Test batched
        lowerCAmelCase__   :				str 			=				image_processing(snake_case			,					return_tensors="pt"   ).pixel_values
        self.assertEqual(
            encoded_images.shape			,					(
                self.image_processor_tester.batch_size,
                self.expected_encoded_image_num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
            )			,					)









"""simple docstring"""

from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
    from PIL import Image

    from ..image_utils import load_image

if is_torch_available():
    from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

_UpperCamelCase		     =    logging.get_logger(__name__)






@add_end_docstrings(__magic_name__       )
class 			__a					(						__magic_name__       ):





       """simple docstring"""


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





        """simple docstring"""

        super().__init__(*snake_case			,					**snake_case   )
        self.check_model_type(snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case=None			,					snake_case=None			,					snake_case=None			,					**snake_case   ):





        """simple docstring"""

        lowerCAmelCase__			,						lowerCAmelCase__   :				List[Any] 			=				{}, {}
        if padding is not None:
         lowerCAmelCase__   :				Dict 			=				padding
        if truncation is not None:
         lowerCAmelCase__   :				Tuple 			=				truncation
        if top_k is not None:
         lowerCAmelCase__   :				str 			=				top_k
        return preprocess_params, {}, postprocess_params






       def __call__(   self			,					snake_case			,					snake_case = None			,					**snake_case   ):





        """simple docstring"""

        if isinstance(snake_case			,					(Image.Image, str)   ) and isinstance(snake_case			,					snake_case   ):
         lowerCAmelCase__   :				int 			=				{"image": image, "question": question}
        else:
         lowerCAmelCase__   :				Optional[int] 			=				image
        lowerCAmelCase__   :				Optional[Any] 			=				super().__call__(snake_case			,					**snake_case   )
        return results






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case=False			,					snake_case=False   ):





        """simple docstring"""

        lowerCAmelCase__   :				Union[str, Any] 			=				load_image(inputs["image"]   )
        lowerCAmelCase__   :				str 			=				self.tokenizer(
            inputs["question"]			,					return_tensors=self.framework			,					padding=snake_case			,					truncation=snake_case   )
        lowerCAmelCase__   :				int 			=				self.image_processor(images=snake_case			,					return_tensors=self.framework   )
        model_inputs.update(snake_case   )
        return model_inputs






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				self.model(**snake_case   )
        return model_outputs






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case=5   ):





        """simple docstring"""

        if top_k > self.model.config.num_labels:
         lowerCAmelCase__   :				List[Any] 			=				self.model.config.num_labels

        if self.framework == "pt":
         lowerCAmelCase__   :				Optional[int] 			=				model_outputs.logits.sigmoid()[0]
         lowerCAmelCase__			,						lowerCAmelCase__   :				Optional[int] 			=				probs.topk(snake_case   )
        else:
         raise ValueError(F"""Unsupported framework: {self.framework}"""   )

        lowerCAmelCase__   :				int 			=				scores.tolist()
        lowerCAmelCase__   :				str 			=				ids.tolist()
        return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(snake_case			,					snake_case   )]






