





"""simple docstring"""






import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_torch, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available


if is_torch_available():
		import torch


if is_vision_available():
		from PIL import Image

		from transformers import MgpstrProcessor, ViTImageProcessor




@require_torch
@require_vision
class 					__lowercase      (      unittest.TestCase				):







					'''simple docstring'''




					__lowerCAmelCase	=	ViTImageProcessor if is_vision_available() else None







					@property
					def 			_lowerCamelCase		(    self  ):
									return self.image_processor_tester.prepare_image_processor_dict()







					def 			_lowerCamelCase		(    self  ):
									__a			:    int      							=	(3, 32, 128)
									__a			:    List[str]      							=	tempfile.mkdtemp()

									# fmt: off
									__a			:    List[str]      							=	['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z''']
									# fmt: on
									__a			:    Optional[int]      							=	dict(zip(_UpperCAmelCase						,      range(len(_UpperCAmelCase  )  )  )  )

									__a			:    List[str]      							=	os.path.join(self.tmpdirname						,      VOCAB_FILES_NAMES['''vocab_file''']  )
									with open(self.vocab_file						,      '''w'''						,      encoding='''utf-8'''  ) as fp:
													fp.write(json.dumps(_UpperCAmelCase  ) + '''\n'''  )

									__a			:    Any      							=	{
									    '''do_normalize''': False,
									    '''do_resize''': True,
									    '''image_processor_type''': '''ViTImageProcessor''',
									    '''resample''': 3,
									    '''size''': {'''height''': 32, '''width''': 128},
									}
									__a			:    int      							=	os.path.join(self.tmpdirname						,      _UpperCAmelCase  )
									with open(self.image_processor_file						,      '''w'''						,      encoding='''utf-8'''  ) as fp:
													json.dump(_UpperCAmelCase						,      _UpperCAmelCase  )







					def 			_lowerCamelCase		(    self						,      **_UpperCAmelCase  ):
									return MgpstrTokenizer.from_pretrained(self.tmpdirname						,      **_UpperCAmelCase  )







					def 			_lowerCamelCase		(    self						,      **_UpperCAmelCase  ):
									return ViTImageProcessor.from_pretrained(self.tmpdirname						,      **_UpperCAmelCase  )







					def 			_lowerCamelCase		(    self  ):
									shutil.rmtree(self.tmpdirname  )







					def 			_lowerCamelCase		(    self  ):
									__a			:    List[str]      							=	np.random.randint(255						,      size=(3, 30, 400)						,      dtype=np.uinta  )

									__a			:    Optional[Any]      							=	Image.fromarray(np.moveaxis(_UpperCAmelCase						,      0						,      -1  )  )

									return image_input







					def 			_lowerCamelCase		(    self  ):
									__a			:    Union[str, Any]      							=	self.get_tokenizer()
									__a			:    List[str]      							=	self.get_image_processor()

									__a			:    Dict      							=	MgpstrProcessor(tokenizer=_UpperCAmelCase						,      image_processor=_UpperCAmelCase  )
									processor.save_pretrained(self.tmpdirname  )
									__a			:    List[Any]      							=	MgpstrProcessor.from_pretrained(self.tmpdirname						,      use_fast=_UpperCAmelCase  )

									self.assertEqual(processor.char_tokenizer.get_vocab()						,      tokenizer.get_vocab()  )
									self.assertIsInstance(processor.char_tokenizer						,      _UpperCAmelCase  )

									self.assertEqual(processor.image_processor.to_json_string()						,      image_processor.to_json_string()  )
									self.assertIsInstance(processor.image_processor						,      _UpperCAmelCase  )







					def 			_lowerCamelCase		(    self  ):
									__a			:    Tuple      							=	self.get_tokenizer()
									__a			:    List[str]      							=	self.get_image_processor()

									__a			:    Optional[int]      							=	MgpstrProcessor(tokenizer=_UpperCAmelCase						,      image_processor=_UpperCAmelCase  )
									processor.save_pretrained(self.tmpdirname  )

									__a			:    List[Any]      							=	self.get_tokenizer(bos_token='''(BOS)'''						,      eos_token='''(EOS)'''  )
									__a			:    Union[str, Any]      							=	self.get_image_processor(do_normalize=_UpperCAmelCase						,      padding_value=1.0  )

									__a			:    List[str]      							=	MgpstrProcessor.from_pretrained(
									    self.tmpdirname						,      bos_token='''(BOS)'''						,      eos_token='''(EOS)'''						,      do_normalize=_UpperCAmelCase						,      padding_value=1.0  )

									self.assertEqual(processor.char_tokenizer.get_vocab()						,      tokenizer_add_kwargs.get_vocab()  )
									self.assertIsInstance(processor.char_tokenizer						,      _UpperCAmelCase  )

									self.assertEqual(processor.image_processor.to_json_string()						,      image_processor_add_kwargs.to_json_string()  )
									self.assertIsInstance(processor.image_processor						,      _UpperCAmelCase  )







					def 			_lowerCamelCase		(    self  ):
									__a			:    List[str]      							=	self.get_image_processor()
									__a			:    Optional[Any]      							=	self.get_tokenizer()

									__a			:    Any      							=	MgpstrProcessor(tokenizer=_UpperCAmelCase						,      image_processor=_UpperCAmelCase  )

									__a			:    Optional[Any]      							=	self.prepare_image_inputs()

									__a			:    int      							=	image_processor(_UpperCAmelCase						,      return_tensors='''np'''  )
									__a			:    str      							=	processor(images=_UpperCAmelCase						,      return_tensors='''np'''  )

									for key in input_image_proc.keys():
													self.assertAlmostEqual(input_image_proc[key].sum()						,      input_processor[key].sum()						,      delta=1e-2  )







					def 			_lowerCamelCase		(    self  ):
									__a			:    Any      							=	self.get_image_processor()
									__a			:    Dict      							=	self.get_tokenizer()

									__a			:    str      							=	MgpstrProcessor(tokenizer=_UpperCAmelCase						,      image_processor=_UpperCAmelCase  )

									__a			:    List[Any]      							=	'''test'''

									__a			:    Optional[Any]      							=	processor(text=_UpperCAmelCase  )

									__a			:    Any      							=	tokenizer(_UpperCAmelCase  )
									for key in encoded_tok.keys():
													self.assertListEqual(encoded_tok[key]						,      encoded_processor[key]  )







					def 			_lowerCamelCase		(    self  ):
									__a			:    List[Any]      							=	self.get_image_processor()
									__a			:    Any      							=	self.get_tokenizer()

									__a			:    int      							=	MgpstrProcessor(tokenizer=_UpperCAmelCase						,      image_processor=_UpperCAmelCase  )

									__a			:    str      							=	'''test'''
									__a			:    Union[str, Any]      							=	self.prepare_image_inputs()

									__a			:    Tuple      							=	processor(text=_UpperCAmelCase						,      images=_UpperCAmelCase  )

									self.assertListEqual(list(inputs.keys()  )						,      ['''pixel_values''', '''labels''']  )

									# test if it raises when no input is passed
									with pytest.raises(_UpperCAmelCase  ):
													processor()







					def 			_lowerCamelCase		(    self  ):
									__a			:    Optional[int]      							=	self.get_image_processor()
									__a			:    Optional[int]      							=	self.get_tokenizer()

									__a			:    Optional[int]      							=	MgpstrProcessor(tokenizer=_UpperCAmelCase						,      image_processor=_UpperCAmelCase  )

									__a			:    Tuple      							=	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]]

									__a			:    int      							=	processor.char_decode(_UpperCAmelCase  )
									__a			:    str      							=	tokenizer.batch_decode(_UpperCAmelCase  )
									__a			:    List[Any]      							=	[seq.replace(''' '''						,      ''''''  ) for seq in decoded_tok]

									self.assertListEqual(_UpperCAmelCase						,      _UpperCAmelCase  )







					def 			_lowerCamelCase		(    self  ):
									__a			:    Any      							=	self.get_image_processor()
									__a			:    str      							=	self.get_tokenizer()

									__a			:    Union[str, Any]      							=	MgpstrProcessor(tokenizer=_UpperCAmelCase						,      image_processor=_UpperCAmelCase  )

									__a			:    Tuple      							=	None
									__a			:    Dict      							=	self.prepare_image_inputs()

									__a			:    Optional[Any]      							=	processor(text=_UpperCAmelCase						,      images=_UpperCAmelCase  )

									self.assertListEqual(list(inputs.keys()  )						,      processor.model_input_names  )







					def 			_lowerCamelCase		(    self  ):
									__a			:    str      							=	self.get_image_processor()
									__a			:    str      							=	self.get_tokenizer()

									__a			:    Any      							=	MgpstrProcessor(tokenizer=_UpperCAmelCase						,      image_processor=_UpperCAmelCase  )

									__a			:    Optional[Any]      							=	torch.randn(1						,      27						,      38  )
									__a			:    Optional[Any]      							=	torch.randn(1						,      27						,      50257  )
									__a			:    List[str]      							=	torch.randn(1						,      27						,      30522  )

									__a			:    Dict      							=	processor.batch_decode([char_input, bpe_input, wp_input]  )

									self.assertListEqual(list(results.keys()  )						,      ['''generated_text''', '''scores''', '''char_preds''', '''bpe_preds''', '''wp_preds''']  )

'''simple docstring'''




def 						lowerCAmelCase_							(  snake_case__		):




    '''simple docstring'''



    if len(snake_case__		) <= 1:
        return [tuple(snake_case__		)]

    A			:  int   =		[]

    def generate(snake_case__						,							snake_case__		):
        if k == 1:
            res.append(tuple(arr[:]		)		)
            return

        generate(k - 1						,							snake_case__		)

        for i in range(k - 1		):
            if k % 2 == 0:  # k is even
                A,    A			:  Any   =		arr[k - 1], arr[i]
            else:  # k is odd
                A,    A			:  List[str]   =		arr[k - 1], arr[0]
            generate(k - 1						,							snake_case__		)

    generate(len(snake_case__		)						,							snake_case__		)
    return res


if __name__ == "__main__":
      lowercase     :							int       						=     input('Enter numbers separated by a comma:\n').strip()
      lowercase     :							Dict       						=     [int(item) for item in user_input.split(',')]
      print(heaps(arr))




