



import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


a_								=    logging.get_logger(__name__)

a_								=    {
    """salesforce/blip2-opt-2.7b""": """https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json""",
}



class        __lowerCAmelCase   (		_a      ):
 lowerCAmelCase__				    =     '''blip_2_vision_model'''




 def __init__(  self		, __UpperCAmelCase=1408		, __UpperCAmelCase=6144		, __UpperCAmelCase=39		, __UpperCAmelCase=16		, __UpperCAmelCase=224		, __UpperCAmelCase=14		, __UpperCAmelCase="gelu"		, __UpperCAmelCase=0.00_001		, __UpperCAmelCase=0.0		, __UpperCAmelCase=1E-1_0		, __UpperCAmelCase=True		, **__UpperCAmelCase		, ):



    '''simple docstring'''
    super().__init__(**__UpperCAmelCase )

    __lowerCamelCase  							=			hidden_size
    __lowerCamelCase  							=			intermediate_size
    __lowerCamelCase  							=			num_hidden_layers
    __lowerCamelCase  							=			num_attention_heads
    __lowerCamelCase  							=			patch_size
    __lowerCamelCase  							=			image_size
    __lowerCamelCase  							=			initializer_range
    __lowerCamelCase  							=			attention_dropout
    __lowerCamelCase  							=			layer_norm_eps
    __lowerCamelCase  							=			hidden_act
    __lowerCamelCase  							=			qkv_bias




 @classmethod
 def 				lowerCamelCase					(  cls		, __UpperCAmelCase		, **__UpperCAmelCase ):



    '''simple docstring'''
    cls._set_token_in_kwargs(__UpperCAmelCase )

    __lowerCamelCase  							=			cls.get_config_dict(__UpperCAmelCase		, **__UpperCAmelCase )

    # get the vision config dict if we are loading from Blip2Config
    if config_dict.get('''model_type''' ) == "blip-2":
       __lowerCamelCase  							=			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(__UpperCAmelCase		, **__UpperCAmelCase )



class        __lowerCAmelCase   (		_a      ):
 lowerCAmelCase__				    =     '''blip_2_qformer'''




 def __init__(  self		, __UpperCAmelCase=30522		, __UpperCAmelCase=768		, __UpperCAmelCase=12		, __UpperCAmelCase=12		, __UpperCAmelCase=3072		, __UpperCAmelCase="gelu"		, __UpperCAmelCase=0.1		, __UpperCAmelCase=0.1		, __UpperCAmelCase=512		, __UpperCAmelCase=0.02		, __UpperCAmelCase=1E-1_2		, __UpperCAmelCase=0		, __UpperCAmelCase="absolute"		, __UpperCAmelCase=2		, __UpperCAmelCase=1408		, **__UpperCAmelCase		, ):



    '''simple docstring'''
    super().__init__(pad_token_id=__UpperCAmelCase		, **__UpperCAmelCase )

    __lowerCamelCase  							=			vocab_size
    __lowerCamelCase  							=			hidden_size
    __lowerCamelCase  							=			num_hidden_layers
    __lowerCamelCase  							=			num_attention_heads
    __lowerCamelCase  							=			hidden_act
    __lowerCamelCase  							=			intermediate_size
    __lowerCamelCase  							=			hidden_dropout_prob
    __lowerCamelCase  							=			attention_probs_dropout_prob
    __lowerCamelCase  							=			max_position_embeddings
    __lowerCamelCase  							=			initializer_range
    __lowerCamelCase  							=			layer_norm_eps
    __lowerCamelCase  							=			position_embedding_type
    __lowerCamelCase  							=			cross_attention_frequency
    __lowerCamelCase  							=			encoder_hidden_size




 @classmethod
 def 				lowerCamelCase					(  cls		, __UpperCAmelCase		, **__UpperCAmelCase ):



    '''simple docstring'''
    cls._set_token_in_kwargs(__UpperCAmelCase )

    __lowerCamelCase  							=			cls.get_config_dict(__UpperCAmelCase		, **__UpperCAmelCase )

    # get the qformer config dict if we are loading from Blip2Config
    if config_dict.get('''model_type''' ) == "blip-2":
       __lowerCamelCase  							=			config_dict['''qformer_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(__UpperCAmelCase		, **__UpperCAmelCase )



class        __lowerCAmelCase   (		_a      ):
 lowerCAmelCase__				    =     '''blip-2'''
 lowerCAmelCase__				    =     True




 def __init__(  self		, __UpperCAmelCase=None		, __UpperCAmelCase=None		, __UpperCAmelCase=None		, __UpperCAmelCase=32		, **__UpperCAmelCase ):



    '''simple docstring'''
    super().__init__(**__UpperCAmelCase )

    if vision_config is None:
       __lowerCamelCase  							=			{}
       logger.info('''vision_config is None. initializing the Blip2VisionConfig with default values.''' )

    if qformer_config is None:
       __lowerCamelCase  							=			{}
       logger.info('''qformer_config is None. Initializing the Blip2QFormerConfig with default values.''' )

    if text_config is None:
       __lowerCamelCase  							=			{}
       logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).''' )

    __lowerCamelCase  							=			BlipaVisionConfig(**__UpperCAmelCase )
    __lowerCamelCase  							=			BlipaQFormerConfig(**__UpperCAmelCase )
    __lowerCamelCase  							=			text_config['''model_type'''] if '''model_type''' in text_config else '''opt'''
    __lowerCamelCase  							=			CONFIG_MAPPING[text_model_type](**__UpperCAmelCase )

    __lowerCamelCase  							=			self.text_config.tie_word_embeddings
    __lowerCamelCase  							=			self.text_config.is_encoder_decoder

    __lowerCamelCase  							=			num_query_tokens
    __lowerCamelCase  							=			self.vision_config.hidden_size
    __lowerCamelCase  							=			self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
    __lowerCamelCase  							=			1.0
    __lowerCamelCase  							=			0.02




 @classmethod
 def 				lowerCamelCase					(  cls		, __UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase		, **__UpperCAmelCase		, ):



    '''simple docstring'''
    return cls(
        vision_config=vision_config.to_dict()		, qformer_config=qformer_config.to_dict()		, text_config=text_config.to_dict()		, **__UpperCAmelCase		, )




 def 				lowerCamelCase					(  self ):



    '''simple docstring'''
    __lowerCamelCase  							=			copy.deepcopy(self.__dict__ )
    __lowerCamelCase  							=			self.vision_config.to_dict()
    __lowerCamelCase  							=			self.qformer_config.to_dict()
    __lowerCamelCase  							=			self.text_config.to_dict()
    __lowerCamelCase  							=			self.__class__.model_type
    return output













import unittest

from huggingface_hub import hf_hub_download

from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor
from transformers.pipelines import VideoClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_decord,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
)

from .test_pipelines_common import ANY



@is_pipeline_test
@require_torch_or_tf
@require_vision
@require_decord
class 					__lowerCAmelCase       (			unittest.TestCase     ):
			lowerCamelCase_     :     Any       			=      MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   Dict:


							'''simple docstring'''



							snake_case_						:    Any				=  hf_hub_download(
							    repo_id='''nateraw/video-demo'''					,							filename='''archery.mp4'''					,							repo_type='''dataset'''    )
							snake_case_						:    List[Any]				=  VideoClassificationPipeline(model=__magic_name__					,							image_processor=__magic_name__					,							top_k=2    )
							snake_case_						:    str				=  [
							    example_video_filepath,
							    '''https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4''',
							]
							return video_classifier, examples






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__    )     ->   Any:


							'''simple docstring'''



							for example in examples:
											snake_case_						:    Union[str, Any]				=  video_classifier(__magic_name__    )

											self.assertEqual(
											    __magic_name__					,							[
											        {'''score''': ANY(__magic_name__    ), '''label''': ANY(__magic_name__    )},
											        {'''score''': ANY(__magic_name__    ), '''label''': ANY(__magic_name__    )},
											    ]					,							)






			@require_torch
			def 			lowerCamelCase  (self    )     ->   str:


							'''simple docstring'''



							snake_case_						:    Any				=  '''hf-internal-testing/tiny-random-VideoMAEForVideoClassification'''
							snake_case_						:    str				=  VideoMAEFeatureExtractor(
							    size={'''shortest_edge''': 10}					,							crop_size={'''height''': 10, '''width''': 10}    )
							snake_case_						:    int				=  pipeline(
							    '''video-classification'''					,							model=__magic_name__					,							feature_extractor=__magic_name__					,							frame_sampling_rate=4    )

							snake_case_						:    List[str]				=  hf_hub_download(repo_id='''nateraw/video-demo'''					,							filename='''archery.mp4'''					,							repo_type='''dataset'''    )
							snake_case_						:    Union[str, Any]				=  video_classifier(__magic_name__					,							top_k=2    )
							self.assertEqual(
							    nested_simplify(__magic_name__					,							decimals=4    )					,							[{'''score''': 0.5_199, '''label''': '''LABEL_0'''}, {'''score''': 0.4_801, '''label''': '''LABEL_1'''}]					,							)

							snake_case_						:    int				=  video_classifier(
							    [
							        video_file_path,
							        video_file_path,
							    ]					,							top_k=2					,							)
							self.assertEqual(
							    nested_simplify(__magic_name__					,							decimals=4    )					,							[
							        [{'''score''': 0.5_199, '''label''': '''LABEL_0'''}, {'''score''': 0.4_801, '''label''': '''LABEL_1'''}],
							        [{'''score''': 0.5_199, '''label''': '''LABEL_0'''}, {'''score''': 0.4_801, '''label''': '''LABEL_1'''}],
							    ]					,							)






			@require_tf
			def 			lowerCamelCase  (self    )     ->   Optional[int]:


							'''simple docstring'''



							pass
