



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

__snake_case						:Union[str, Any]      		=     logging.get_logger(__name__)

@add_end_docstrings(__UpperCAmelCase    )
class     _A       (	__UpperCAmelCase    ):



       def __init__(		self							:     List[Any]     ,		*__SCREAMING_SNAKE_CASE							:     Union[str, Any]     ,		**__SCREAMING_SNAKE_CASE							:     Dict):


           '''simple docstring'''

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



       def 		_lowerCamelCase			(		self							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=None     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=None     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=None     ,		**__SCREAMING_SNAKE_CASE							:     str):


           '''simple docstring'''

           __a       ,   __a						       =				{}, {}
           if padding is not None:
               __a						       =				padding
           if truncation is not None:
               __a						       =				truncation
           if top_k is not None:
               __a						       =				top_k
           return preprocess_params, {}, postprocess_params



       def __call__(		self							:     str     ,		__SCREAMING_SNAKE_CASE							:     Union["Image.Image", str]     ,		__SCREAMING_SNAKE_CASE							:     str = None     ,		**__SCREAMING_SNAKE_CASE							:     Any):


           '''simple docstring'''

           if isinstance(__SCREAMING_SNAKE_CASE     ,		(Image.Image, str)) and isinstance(__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE):
               __a						       =				{'''image''': image, '''question''': question}
           else:
               __a						       =				image
           __a						       =				super().__call__(__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE)
           return results



       def 		_lowerCamelCase			(		self							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     List[Any]     ,		__SCREAMING_SNAKE_CASE							:     List[Any]=False     ,		__SCREAMING_SNAKE_CASE							:     Any=False):


           '''simple docstring'''

           __a						       =				load_image(inputs['''image'''])
           __a						       =				self.tokenizer(
               inputs['''question''']     ,		return_tensors=self.framework     ,		padding=__SCREAMING_SNAKE_CASE     ,		truncation=__SCREAMING_SNAKE_CASE)
           __a						       =				self.image_processor(images=__SCREAMING_SNAKE_CASE     ,		return_tensors=self.framework)
           model_inputs.update(__SCREAMING_SNAKE_CASE)
           return model_inputs



       def 		_lowerCamelCase			(		self							:     str     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]):


           '''simple docstring'''

           __a						       =				self.model(**__SCREAMING_SNAKE_CASE)
           return model_outputs





       def 		_lowerCamelCase			(		self							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     int     ,		__SCREAMING_SNAKE_CASE							:     List[str]=5):


           '''simple docstring'''

           if top_k > self.model.config.num_labels:
               __a						       =				self.model.config.num_labels

           if self.framework == "pt":
               __a						       =				model_outputs.logits.sigmoid()[0]
               __a       ,   __a						       =				probs.topk(__SCREAMING_SNAKE_CASE)
           else:
               raise ValueError(F'Unsupported framework: {self.framework}')

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










import argparse

import torch

from transformers import LxmertConfig, LxmertForPreTraining, load_tf_weights_in_lxmert
from transformers.utils import logging


logging.set_verbosity_info()





def      __snake_case	(			_UpperCAmelCase   , _UpperCAmelCase   , _UpperCAmelCase				):
    # Initialise PyTorch model
    __a						       =				LxmertConfig.from_json_file(_UpperCAmelCase				)
    print(f'Building PyTorch model from configuration: {config}'				)
    __a						       =				LxmertForPreTraining(_UpperCAmelCase				)

    # Load weights from tf checkpoint
    load_tf_weights_in_lxmert(_UpperCAmelCase   , _UpperCAmelCase   , _UpperCAmelCase				)

    # Save pytorch-model
    print(f'Save PyTorch model to {pytorch_dump_path}'				)
    torch.save(model.state_dict()   , _UpperCAmelCase				)


if __name__ == "__main__":
 __snake_case						:List[Any]      		=     argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.'''
 )
 parser.add_argument(
     '''--config_file''',
     default=None,
     type=str,
     required=True,
     help='''The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.''',
 )
 parser.add_argument(
     '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
 )
 __snake_case						:Optional[Any]      		=     parser.parse_args()
 convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)





