





"""simple docstring"""






import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)



class 					__UpperCAmelCase							(						unittest.TestCase  ):
				def       UpperCAmelCase      (		self    :			List[str]    )	->	str:



							'''simple docstring'''



							a__  :	List[Any]		 =			["a", "b", "c"]

							# Defaults to last layer if both are None
							a__  :	Optional[Any]		 =			get_aligned_output_features_output_indices(a_	, a_	, a_    )
							self.assertEqual(a_	, ["c"]    )
							self.assertEqual(a_	, [2]    )

							# Out indices set to match out features
							a__  :	Dict		 =			get_aligned_output_features_output_indices(["a", "c"]	, a_	, a_    )
							self.assertEqual(a_	, ["a", "c"]    )
							self.assertEqual(a_	, [0, 2]    )

							# Out features set to match out indices
							a__  :	int		 =			get_aligned_output_features_output_indices(a_	, [0, 2]	, a_    )
							self.assertEqual(a_	, ["a", "c"]    )
							self.assertEqual(a_	, [0, 2]    )

							# Out features selected from negative indices
							a__  :	List[str]		 =			get_aligned_output_features_output_indices(a_	, [-3, -1]	, a_    )
							self.assertEqual(a_	, ["a", "c"]    )
							self.assertEqual(a_	, [-3, -1]    )
				def       UpperCAmelCase      (		self    :			str    )	->	str:



							'''simple docstring'''



							with self.assertRaises(a_    ):
										verify_out_features_out_indices(["a", "b"]	, (0, 1)	, a_    )

							# Out features must be a list
							with self.assertRaises(a_    ):
										verify_out_features_out_indices(("a", "b")	, (0, 1)	, ["a", "b"]    )

							# Out features must be a subset of stage names
							with self.assertRaises(a_    ):
										verify_out_features_out_indices(["a", "b"]	, (0, 1)	, ["a"]    )

							# Out indices must be a list or tuple
							with self.assertRaises(a_    ):
										verify_out_features_out_indices(a_	, 0	, ["a", "b"]    )

							# Out indices must be a subset of stage names
							with self.assertRaises(a_    ):
										verify_out_features_out_indices(a_	, (0, 1)	, ["a"]    )

							# Out features and out indices must be the same length
							with self.assertRaises(a_    ):
										verify_out_features_out_indices(["a", "b"]	, (0,)	, ["a", "b", "c"]    )

							# Out features should match out indices
							with self.assertRaises(a_    ):
										verify_out_features_out_indices(["a", "b"]	, (0, 2)	, ["a", "b", "c"]    )

							# Out features and out indices should be in order
							with self.assertRaises(a_    ):
										verify_out_features_out_indices(["b", "a"]	, (0, 1)	, ["a", "b"]    )

							# Check passes with valid inputs
							verify_out_features_out_indices(["a", "b", "d"]	, (0, 1, -1)	, ["a", "b", "c", "d"]    )






				def       UpperCAmelCase      (		self    :			Any    )	->	Dict:



							'''simple docstring'''



							a__  :	Tuple		 =			BackboneMixin()

							a__  :	Optional[Any]		 =			["a", "b", "c"]
							a__  :	Dict		 =			["a", "c"]
							a__  :	str		 =			[0, 2]

							# Check that the output features and indices are set correctly
							self.assertEqual(backbone.out_features	, ["a", "c"]    )
							self.assertEqual(backbone.out_indices	, [0, 2]    )

							# Check out features and indices are updated correctly
							a__  :	List[str]		 =			["a", "b"]
							self.assertEqual(backbone.out_features	, ["a", "b"]    )
							self.assertEqual(backbone.out_indices	, [0, 1]    )

							a__  :	Optional[int]		 =			[-3, -1]
							self.assertEqual(backbone.out_features	, ["a", "c"]    )
							self.assertEqual(backbone.out_indices	, [-3, -1]    )


"""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   )]






