



"""simple docstring"""
import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
  from PIL import Image
else:
  class 							_UpperCAmelCase							:





       '''simple docstring'''



       @staticmethod
       def  __lowerCAmelCase	(					*A       ,	**A		)							->					Union[str, Any]:
           pass
@is_pipeline_test
@require_torch
@require_vision
class 							_UpperCAmelCase							( unittest.TestCase      ):





     '''simple docstring'''



     a__									=MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING





     def  __lowerCAmelCase	(					self       ,	A       ,	A       ,	A		)							->					Optional[Any]:
         _UpperCAmelCase							:  int											=    pipeline('''visual-question-answering'''       ,	model='''hf-internal-testing/tiny-vilt-random-vqa'''		)
         _UpperCAmelCase							:  Optional[Any]											=    [
             {
                 '''image''': Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		),
                 '''question''': '''How many cats are there?''',
             },
             {
                 '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
                 '''question''': '''How many cats are there?''',
             },
         ]
         return vqa_pipeline, examples





     def  __lowerCAmelCase	(					self       ,	A       ,	A		)							->					Any:
         _UpperCAmelCase							:  str											=    vqa_pipeline(A       ,	top_k=1		)
         self.assertEqual(
             A       ,	[
                 [{'''score''': ANY(A		), '''answer''': ANY(A		)}],
                 [{'''score''': ANY(A		), '''answer''': ANY(A		)}],
             ]       ,	)





     @require_torch
     def  __lowerCAmelCase	(					self		)							->					Dict:
         _UpperCAmelCase							:  Dict											=    pipeline('''visual-question-answering'''       ,	model='''hf-internal-testing/tiny-vilt-random-vqa'''		)
         _UpperCAmelCase							:  List[Any]											=    '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
         _UpperCAmelCase							:  Tuple											=    '''How many cats are there?'''

         _UpperCAmelCase							:  List[str]											=    vqa_pipeline(image=A       ,	question='''How many cats are there?'''       ,	top_k=2		)
         self.assertEqual(
             A       ,	[{'''score''': ANY(A		), '''answer''': ANY(A		)}, {'''score''': ANY(A		), '''answer''': ANY(A		)}]		)

         _UpperCAmelCase							:  Any											=    vqa_pipeline({'''image''': image, '''question''': question}       ,	top_k=2		)
         self.assertEqual(
             A       ,	[{'''score''': ANY(A		), '''answer''': ANY(A		)}, {'''score''': ANY(A		), '''answer''': ANY(A		)}]		)





     @slow
     @require_torch
     def  __lowerCAmelCase	(					self		)							->					List[Any]:
         _UpperCAmelCase							:  Optional[int]											=    pipeline('''visual-question-answering'''       ,	model='''dandelin/vilt-b32-finetuned-vqa'''		)
         _UpperCAmelCase							:  List[str]											=    '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
         _UpperCAmelCase							:  Optional[int]											=    '''How many cats are there?'''

         _UpperCAmelCase							:  int											=    vqa_pipeline(image=A       ,	question=A       ,	top_k=2		)
         self.assertEqual(
             nested_simplify(A       ,	decimals=4		)       ,	[{'''score''': 0.8_799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]		)

         _UpperCAmelCase							:  int											=    vqa_pipeline({'''image''': image, '''question''': question}       ,	top_k=2		)
         self.assertEqual(
             nested_simplify(A       ,	decimals=4		)       ,	[{'''score''': 0.8_799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]		)

         _UpperCAmelCase							:  Union[str, Any]											=    vqa_pipeline(
             [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]       ,	top_k=2		)
         self.assertEqual(
             nested_simplify(A       ,	decimals=4		)       ,	[[{'''score''': 0.8_799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]] * 2       ,	)





     @require_tf
     @unittest.skip('''Visual question answering not implemented in TF'''		)
     def  __lowerCAmelCase	(					self		)							->					Dict:
         pass







"""simple docstring"""
import argparse
import os

import torch

from transformers.utils import WEIGHTS_NAME


_lowerCAmelCase    :int     =					['small', 'medium', 'large']

_lowerCAmelCase    :int     =					'lm_head.decoder.weight'
_lowerCAmelCase    :Dict     =					'lm_head.weight'





def  lowerCamelCase_						(UpperCamelCase__		:		str     ,       UpperCamelCase__		:		str							):
    _UpperCAmelCase							:  List[Any]											=    torch.load(UpperCamelCase__							)
    _UpperCAmelCase							:  List[str]											=    d.pop(UpperCamelCase__							)
    os.makedirs(UpperCamelCase__     ,       exist_ok=UpperCamelCase__							)
    torch.save(UpperCamelCase__     ,       os.path.join(UpperCamelCase__     ,       UpperCamelCase__							)							)


if __name__ == "__main__":
  _lowerCAmelCase    :Dict     =					argparse.ArgumentParser()
  parser.add_argument('--dialogpt_path', default='.', type=str)
  _lowerCAmelCase    :str     =					parser.parse_args()
  for MODEL in DIALOGPT_MODELS:
    _lowerCAmelCase    :Tuple     =					os.path.join(args.dialogpt_path, f"{MODEL}_ft.pkl")
    _lowerCAmelCase    :int     =					f"./DialoGPT-{MODEL}"
    convert_dialogpt_checkpoint(
        checkpoint_path,
        pytorch_dump_folder_path,
    )


