

import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase_       	=	logging.get_logger(__name__)

lowerCAmelCase_       	=	{
    'Salesforce/blip-vqa-base': 'https://huggingface.co/Salesforce/blip-vqa-base/resolve/main/config.json',
    'Salesforce/blip-vqa-capfit-large': (
        'https://huggingface.co/Salesforce/blip-vqa-base-capfit/resolve/main/config.json'
    ),
    'Salesforce/blip-image-captioning-base': (
        'https://huggingface.co/Salesforce/blip-image-captioning-base/resolve/main/config.json'
    ),
    'Salesforce/blip-image-captioning-large': (
        'https://huggingface.co/Salesforce/blip-image-captioning-large/resolve/main/config.json'
    ),
    'Salesforce/blip-itm-base-coco': 'https://huggingface.co/Salesforce/blip-itm-base-coco/resolve/main/config.json',
    'Salesforce/blip-itm-large-coco': 'https://huggingface.co/Salesforce/blip-itm-large-coco/resolve/main/config.json',
    'Salesforce/blip-itm-base-flikr': 'https://huggingface.co/Salesforce/blip-itm-base-flikr/resolve/main/config.json',
    'Salesforce/blip-itm-large-flikr': (
        'https://huggingface.co/Salesforce/blip-itm-large-flikr/resolve/main/config.json'
    ),
}





class 	snake_case_   (		_lowerCamelCase					):







			'''simple docstring'''

			SCREAMING_SNAKE_CASE     :   Optional[int]									=				"blip_text_model"







			def __init__(     self :				Dict							,   _UpperCamelCase :				Tuple=3_0_5_2_4							,   _UpperCamelCase :				Any=7_6_8							,   _UpperCamelCase :				Optional[Any]=7_6_8							,   _UpperCamelCase :				int=3_0_7_2							,   _UpperCamelCase :				Tuple=7_6_8							,   _UpperCamelCase :				List[Any]=1_2							,   _UpperCamelCase :				Union[str, Any]=8							,   _UpperCamelCase :				Optional[Any]=5_1_2							,   _UpperCamelCase :				Optional[int]="gelu"							,   _UpperCamelCase :				int=1e-12							,   _UpperCamelCase :				Union[str, Any]=0.0							,   _UpperCamelCase :				str=0.0							,   _UpperCamelCase :				int=0.02							,   _UpperCamelCase :				int=3_0_5_2_2							,   _UpperCamelCase :				Any=2							,   _UpperCamelCase :				Any=0							,   _UpperCamelCase :				int=1_0_2							,   _UpperCamelCase :				List[Any]=True							,   _UpperCamelCase :				Optional[int]=True							,   **_UpperCamelCase :				Optional[int]							,   )      ->int:
									super().__init__(
									    pad_token_id=lowercase_							,   bos_token_id=lowercase_							,   eos_token_id=lowercase_							,   sep_token_id=lowercase_							,   **lowercase_							,   )

									snake_case_   =	vocab_size
									snake_case_   =	hidden_size
									snake_case_   =	encoder_hidden_size
									snake_case_   =	intermediate_size
									snake_case_   =	projection_dim
									snake_case_   =	hidden_dropout_prob
									snake_case_   =	num_hidden_layers
									snake_case_   =	num_attention_heads
									snake_case_   =	max_position_embeddings
									snake_case_   =	layer_norm_eps
									snake_case_   =	hidden_act
									snake_case_   =	initializer_range
									snake_case_   =	attention_probs_dropout_prob
									snake_case_   =	is_decoder
									snake_case_   =	use_cache







			@classmethod
			def 				snake_case__(     cls :				str							,   _UpperCamelCase :				Union[str, os.PathLike]							,   **_UpperCamelCase :				int							)      ->Dict:
									cls._set_token_in_kwargs(lowercase_							)

									snake_case_,    snake_case_   =	cls.get_config_dict(lowercase_							,   **lowercase_							)

									# get the text config dict if we are loading from BlipConfig
									if config_dict.get('''model_type'''							) == "blip":
															snake_case_   =	config_dict['''text_config''']

									if "model_type" in config_dict and hasattr(cls							,   '''model_type'''							) and config_dict["model_type"] != cls.model_type:
															logger.warning(
															    f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
															    f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''							)

									return cls.from_dict(lowercase_							,   **lowercase_							)






class 	snake_case_   (		_lowerCamelCase					):







			'''simple docstring'''

			SCREAMING_SNAKE_CASE     :   int									=				"blip_vision_model"







			def __init__(     self :				int							,   _UpperCamelCase :				Dict=7_6_8							,   _UpperCamelCase :				Tuple=3_0_7_2							,   _UpperCamelCase :				Optional[Any]=5_1_2							,   _UpperCamelCase :				Union[str, Any]=1_2							,   _UpperCamelCase :				Optional[Any]=1_2							,   _UpperCamelCase :				Any=3_8_4							,   _UpperCamelCase :				str=1_6							,   _UpperCamelCase :				Union[str, Any]="gelu"							,   _UpperCamelCase :				Tuple=1e-5							,   _UpperCamelCase :				Optional[int]=0.0							,   _UpperCamelCase :				Optional[int]=1e-10							,   **_UpperCamelCase :				Optional[Any]							,   )      ->Any:
									super().__init__(**lowercase_							)

									snake_case_   =	hidden_size
									snake_case_   =	intermediate_size
									snake_case_   =	projection_dim
									snake_case_   =	num_hidden_layers
									snake_case_   =	num_attention_heads
									snake_case_   =	patch_size
									snake_case_   =	image_size
									snake_case_   =	initializer_range
									snake_case_   =	attention_dropout
									snake_case_   =	layer_norm_eps
									snake_case_   =	hidden_act







			@classmethod
			def 				snake_case__(     cls :				Optional[Any]							,   _UpperCamelCase :				Union[str, os.PathLike]							,   **_UpperCamelCase :				int							)      ->Tuple:
									cls._set_token_in_kwargs(lowercase_							)

									snake_case_,    snake_case_   =	cls.get_config_dict(lowercase_							,   **lowercase_							)

									# get the vision config dict if we are loading from BlipConfig
									if config_dict.get('''model_type'''							) == "blip":
															snake_case_   =	config_dict['''vision_config''']

									if "model_type" in config_dict and hasattr(cls							,   '''model_type'''							) and config_dict["model_type"] != cls.model_type:
															logger.warning(
															    f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
															    f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''							)

									return cls.from_dict(lowercase_							,   **lowercase_							)






class 	snake_case_   (		_lowerCamelCase					):







			'''simple docstring'''

			SCREAMING_SNAKE_CASE     :   Dict									=				"blip"
			SCREAMING_SNAKE_CASE     :   List[str]									=				True







			def __init__(     self :				str							,   _UpperCamelCase :				str=None							,   _UpperCamelCase :				List[str]=None							,   _UpperCamelCase :				List[str]=5_1_2							,   _UpperCamelCase :				int=2.6592							,   _UpperCamelCase :				List[Any]=2_5_6							,   **_UpperCamelCase :				List[str]							,   )      ->Tuple:
									super().__init__(**lowercase_							)

									if text_config is None:
															snake_case_   =	{}
															logger.info('''`text_config` is `None`. Initializing the `BlipTextConfig` with default values.'''							)

									if vision_config is None:
															snake_case_   =	{}
															logger.info('''`vision_config` is `None`. Initializing the `BlipVisionConfig` with default values.'''							)

									snake_case_   =	BlipTextConfig(**lowercase_							)
									snake_case_   =	BlipVisionConfig(**lowercase_							)

									snake_case_   =	self.vision_config.hidden_size

									snake_case_   =	projection_dim
									snake_case_   =	logit_scale_init_value
									snake_case_   =	1.0
									snake_case_   =	0.02
									snake_case_   =	image_text_hidden_size







			@classmethod
			def 				snake_case__(     cls :				int							,   _UpperCamelCase :				BlipTextConfig							,   _UpperCamelCase :				BlipVisionConfig							,   **_UpperCamelCase :				List[str]							)      ->Optional[Any]:
									return cls(text_config=text_config.to_dict()							,   vision_config=vision_config.to_dict()							,   **lowercase_							)







			def 				snake_case__(     self :				str							)      ->Optional[Any]:
									snake_case_   =	copy.deepcopy(self.__dict__							)
									snake_case_   =	self.text_config.to_dict()
									snake_case_   =	self.vision_config.to_dict()
									snake_case_   =	self.__class__.model_type
									return output





'''simple docstring'''

import unittest

from transformers import (
    MODEL_FOR_OBJECT_DETECTION_MAPPING,
    AutoFeatureExtractor,
    AutoModelForObjectDetection,
    ObjectDetectionPipeline,
    is_vision_available,
    pipeline,
)
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_pytesseract,
    require_tf,
    require_timm,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
							from PIL import Image
else:






							class 						a				:


											@staticmethod
											def   A_ (		*lowercase_    :					int       ,				**lowercase_    :					str					):
																pass






@is_pipeline_test
@require_vision
@require_timm
@require_torch
class 						a				(   unittest.TestCase     ):
				snake_case_      =		MODEL_FOR_OBJECT_DETECTION_MAPPING


				def   A_ (		self    :					Any       ,				lowercase_    :					List[Any]       ,				lowercase_    :					Optional[int]       ,				lowercase_    :					List[str]					):
									snake_case_									=				ObjectDetectionPipeline(model=lowercase_       ,				image_processor=lowercase_					)
									return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]


				def   A_ (		self    :					Dict       ,				lowercase_    :					List[Any]       ,				lowercase_    :					int					):
									snake_case_									=				object_detector('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       ,				threshold=0.0					)

									self.assertGreater(len(lowercase_					)       ,				0					)
									for detected_object in outputs:
														self.assertEqual(
														    lowercase_       ,				{
														        '''score''': ANY(lowercase_					),
														        '''label''': ANY(lowercase_					),
														        '''box''': {'''xmin''': ANY(lowercase_					), '''ymin''': ANY(lowercase_					), '''xmax''': ANY(lowercase_					), '''ymax''': ANY(lowercase_					)},
														    }       ,				)

									import datasets

									snake_case_									=				datasets.load_dataset('''hf-internal-testing/fixtures_image_utils'''       ,				'''image'''       ,				split='''test'''					)

									snake_case_									=				[
									    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					),
									    '''http://images.cocodataset.org/val2017/000000039769.jpg''',
									    # RGBA
									    dataset[0]['''file'''],
									    # LA
									    dataset[1]['''file'''],
									    # L
									    dataset[2]['''file'''],
									]
									snake_case_									=				object_detector(lowercase_       ,				threshold=0.0					)

									self.assertEqual(len(lowercase_					)       ,				len(lowercase_					)					)
									for outputs in batch_outputs:
														self.assertGreater(len(lowercase_					)       ,				0					)
														for detected_object in outputs:
																			self.assertEqual(
																			    lowercase_       ,				{
																			        '''score''': ANY(lowercase_					),
																			        '''label''': ANY(lowercase_					),
																			        '''box''': {'''xmin''': ANY(lowercase_					), '''ymin''': ANY(lowercase_					), '''xmax''': ANY(lowercase_					), '''ymax''': ANY(lowercase_					)},
																			    }       ,				)


				@require_tf
				@unittest.skip('''Object detection not implemented in TF'''					)
				def   A_ (		self    :					int					):
									pass


				@require_torch
				def   A_ (		self    :					Tuple					):
									snake_case_									=				'''hf-internal-testing/tiny-detr-mobilenetsv3'''

									snake_case_									=				AutoModelForObjectDetection.from_pretrained(lowercase_					)
									snake_case_									=				AutoFeatureExtractor.from_pretrained(lowercase_					)
									snake_case_									=				ObjectDetectionPipeline(model=lowercase_       ,				feature_extractor=lowercase_					)

									snake_case_									=				object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''       ,				threshold=0.0					)

									self.assertEqual(
									    nested_simplify(lowercase_       ,				decimals=4					)       ,				[
									        {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
									        {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
									    ]       ,				)

									snake_case_									=				object_detector(
									    [
									        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
									        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
									    ]       ,				threshold=0.0       ,				)

									self.assertEqual(
									    nested_simplify(lowercase_       ,				decimals=4					)       ,				[
									        [
									            {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
									            {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
									        ],
									        [
									            {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
									            {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
									        ],
									    ]       ,				)


				@require_torch
				@slow
				def   A_ (		self    :					Optional[int]					):
									snake_case_									=				'''facebook/detr-resnet-50'''

									snake_case_									=				AutoModelForObjectDetection.from_pretrained(lowercase_					)
									snake_case_									=				AutoFeatureExtractor.from_pretrained(lowercase_					)
									snake_case_									=				ObjectDetectionPipeline(model=lowercase_       ,				feature_extractor=lowercase_					)

									snake_case_									=				object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''					)
									self.assertEqual(
									    nested_simplify(lowercase_       ,				decimals=4					)       ,				[
									        {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
									        {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
									        {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
									        {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
									        {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
									    ]       ,				)

									snake_case_									=				object_detector(
									    [
									        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
									        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
									    ]					)
									self.assertEqual(
									    nested_simplify(lowercase_       ,				decimals=4					)       ,				[
									        [
									            {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
									            {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
									            {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
									            {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
									            {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
									        ],
									        [
									            {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
									            {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
									            {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
									            {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
									            {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
									        ],
									    ]       ,				)


				@require_torch
				@slow
				def   A_ (		self    :					Tuple					):
									snake_case_									=				'''facebook/detr-resnet-50'''

									snake_case_									=				pipeline('''object-detection'''       ,				model=lowercase_					)

									snake_case_									=				object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''					)
									self.assertEqual(
									    nested_simplify(lowercase_       ,				decimals=4					)       ,				[
									        {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
									        {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
									        {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
									        {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
									        {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
									    ]       ,				)

									snake_case_									=				object_detector(
									    [
									        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
									        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
									    ]					)
									self.assertEqual(
									    nested_simplify(lowercase_       ,				decimals=4					)       ,				[
									        [
									            {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
									            {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
									            {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
									            {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
									            {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
									        ],
									        [
									            {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
									            {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
									            {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
									            {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
									            {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
									        ],
									    ]       ,				)


				@require_torch
				@slow
				def   A_ (		self    :					str					):
									snake_case_									=				0.9985
									snake_case_									=				'''facebook/detr-resnet-50'''

									snake_case_									=				pipeline('''object-detection'''       ,				model=lowercase_					)

									snake_case_									=				object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''       ,				threshold=lowercase_					)
									self.assertEqual(
									    nested_simplify(lowercase_       ,				decimals=4					)       ,				[
									        {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
									        {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
									    ]       ,				)


				@require_torch
				@require_pytesseract
				@slow
				def   A_ (		self    :					Dict					):
									snake_case_									=				'''Narsil/layoutlmv3-finetuned-funsd'''
									snake_case_									=				0.9993

									snake_case_									=				pipeline('''object-detection'''       ,				model=lowercase_       ,				threshold=lowercase_					)

									snake_case_									=				object_detector(
									    '''https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png'''					)
									self.assertEqual(
									    nested_simplify(lowercase_       ,				decimals=4					)       ,				[
									        {'''score''': 0.9993, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 294, '''ymin''': 254, '''xmax''': 343, '''ymax''': 264}},
									        {'''score''': 0.9993, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 294, '''ymin''': 254, '''xmax''': 343, '''ymax''': 264}},
									    ]       ,				)

