


import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ConvNextConfig, UperNetConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import UperNetForSemanticSegmentation
							from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor

class 						A__							:




						def __init__(       self					:      Dict				,    _a					:      str				,    _a					:      Tuple=13				,    _a					:      Optional[Any]=32				,    _a					:      Tuple=3				,    _a					:      List[Any]=4				,    _a					:      str=[10, 20, 30, 40]				,    _a					:      str=[2, 2, 3, 2]				,    _a					:      str=True				,    _a					:      List[Any]=True				,    _a					:      Union[str, Any]=37				,    _a					:      int="gelu"				,    _a					:      Tuple=10				,    _a					:      Union[str, Any]=0.02				,    _a					:      List[Any]=["stage2", "stage3", "stage4"]				,    _a					:      Optional[Any]=3				,    _a					:      int=None				,    )							->     Union[str, Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =parent
										_SCREAMING_SNAKE_CASE			  =batch_size
										_SCREAMING_SNAKE_CASE			  =image_size
										_SCREAMING_SNAKE_CASE			  =num_channels
										_SCREAMING_SNAKE_CASE			  =num_stages
										_SCREAMING_SNAKE_CASE			  =hidden_sizes
										_SCREAMING_SNAKE_CASE			  =depths
										_SCREAMING_SNAKE_CASE			  =is_training
										_SCREAMING_SNAKE_CASE			  =use_labels
										_SCREAMING_SNAKE_CASE			  =intermediate_size
										_SCREAMING_SNAKE_CASE			  =hidden_act
										_SCREAMING_SNAKE_CASE			  =type_sequence_label_size
										_SCREAMING_SNAKE_CASE			  =initializer_range
										_SCREAMING_SNAKE_CASE			  =out_features
										_SCREAMING_SNAKE_CASE			  =num_labels
										_SCREAMING_SNAKE_CASE			  =scope
										_SCREAMING_SNAKE_CASE			  =num_stages




						def 							__UpperCamelCase				(       self					:      List[str]  )							->     List[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

										_SCREAMING_SNAKE_CASE			  =None
										if self.use_labels:
														_SCREAMING_SNAKE_CASE			  =ids_tensor([self.batch_size]				,    self.type_sequence_label_size  )

										_SCREAMING_SNAKE_CASE			  =self.get_config()

										return config, pixel_values, labels




						def 							__UpperCamelCase				(       self					:      List[str]  )							->     Any:


										"""simple docstring"""





										return ConvNextConfig(
										    num_channels=self.num_channels				,    num_stages=self.num_stages				,    hidden_sizes=self.hidden_sizes				,    depths=self.depths				,    is_training=self.is_training				,    intermediate_size=self.intermediate_size				,    hidden_act=self.hidden_act				,    out_features=self.out_features				,    )




						def 							__UpperCamelCase				(       self					:      Dict  )							->     Union[str, Any]:


										"""simple docstring"""





										return UperNetConfig(
										    backbone_config=self.get_backbone_config()				,    hidden_size=512				,    pool_scales=[1, 2, 3, 6]				,    use_auxiliary_head=_a				,    auxiliary_loss_weight=0.4				,    auxiliary_in_channels=40				,    auxiliary_channels=256				,    auxiliary_num_convs=1				,    auxiliary_concat_input=_a				,    loss_ignore_index=255				,    num_labels=self.num_labels				,    )




						def 							__UpperCamelCase				(       self					:      Optional[Any]				,    _a					:      Tuple				,    _a					:      Tuple				,    _a					:      Any  )							->     Any:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =UperNetForSemanticSegmentation(config=_a  )
										model.to(_a  )
										model.eval()
										_SCREAMING_SNAKE_CASE			  =model(_a  )
										self.parent.assertEqual(
										    result.logits.shape				,    (self.batch_size, self.num_labels, self.image_size, self.image_size)  )




						def 							__UpperCamelCase				(       self					:      Dict  )							->     Optional[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.prepare_config_and_inputs()
										(
										    (
										    _SCREAMING_SNAKE_CASE
										)  ,				(
										    _SCREAMING_SNAKE_CASE
										)  ,				(
										    _SCREAMING_SNAKE_CASE
										)  ,				
										)			  =config_and_inputs
										_SCREAMING_SNAKE_CASE			  ={'''pixel_values''': pixel_values}
										return config, inputs_dict

@require_torch
class 						A__							(		UpperCamelCase__   ,     UpperCamelCase__   ,     unittest.TestCase  ):
						UpperCAmelCase             =						(UperNetForSemanticSegmentation,) if is_torch_available() else ()
						UpperCAmelCase             =						{"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {}
						UpperCAmelCase             =						False
						UpperCAmelCase             =						False
						UpperCAmelCase             =						False
						UpperCAmelCase             =						False
						UpperCAmelCase             =						False
						UpperCAmelCase             =						False




						def 							__UpperCamelCase				(       self					:      Union[str, Any]  )							->     int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =UperNetModelTester(self  )
										_SCREAMING_SNAKE_CASE			  =ConfigTester(self				,    config_class=_a				,    has_text_modality=_a				,    hidden_size=37  )




						def 							__UpperCamelCase				(       self					:      Tuple  )							->     int:


										"""simple docstring"""





										self.create_and_test_config_common_properties()
										self.config_tester.create_and_test_config_to_json_string()
										self.config_tester.create_and_test_config_to_json_file()
										self.config_tester.create_and_test_config_from_and_save_pretrained()
										self.config_tester.create_and_test_config_with_num_labels()
										self.config_tester.check_config_can_be_init_without_params()
										self.config_tester.check_config_arguments_init()




						def 							__UpperCamelCase				(       self					:      Any  )							->     List[Any]:


										"""simple docstring"""





										return




						def 							__UpperCamelCase				(       self					:      int  )							->     Any:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE			  =model_class(_a  )
														_SCREAMING_SNAKE_CASE			  =inspect.signature(model.forward  )
														# signature.parameters is an OrderedDict => so arg_names order is deterministic
														_SCREAMING_SNAKE_CASE			  =[*signature.parameters.keys()]

														_SCREAMING_SNAKE_CASE			  =['''pixel_values''']
														self.assertListEqual(arg_names[:1]				,    _a  )




						def 							__UpperCamelCase				(       self					:      Tuple  )							->     Optional[int]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_semantic_segmentation(*_a  )




						@unittest.skip(reason='''UperNet does not use inputs_embeds'''  )
						def 							__UpperCamelCase				(       self					:      int  )							->     Dict:


										"""simple docstring"""





										pass




						@unittest.skip(reason='''UperNet does not support input and output embeddings'''  )
						def 							__UpperCamelCase				(       self					:      List[Any]  )							->     List[Any]:


										"""simple docstring"""





										pass




						@unittest.skip(reason='''UperNet does not have a base model'''  )
						def 							__UpperCamelCase				(       self					:      Dict  )							->     Union[str, Any]:


										"""simple docstring"""





										pass




						@unittest.skip(reason='''UperNet does not have a base model'''  )
						def 							__UpperCamelCase				(       self					:      str  )							->     Any:


										"""simple docstring"""





										pass




						@require_torch_multi_gpu
						@unittest.skip(reason='''UperNet has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''  )
						def 							__UpperCamelCase				(       self					:      int  )							->     Any:


										"""simple docstring"""





										pass




						@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''  )
						def 							__UpperCamelCase				(       self					:      Optional[int]  )							->     List[str]:


										"""simple docstring"""





										pass




						def 							__UpperCamelCase				(       self					:      str  )							->     List[str]:


										"""simple docstring"""





										def check_hidden_states_output(_a					:      List[str]				,    _a					:      int				,    _a					:      int  ):
														_SCREAMING_SNAKE_CASE			  =model_class(_a  )
														model.to(_a  )
														model.eval()

														with torch.no_grad():
																		_SCREAMING_SNAKE_CASE			  =model(**self._prepare_for_class(_a				,    _a  )  )

														_SCREAMING_SNAKE_CASE			  =outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

														_SCREAMING_SNAKE_CASE			  =self.model_tester.num_stages
														self.assertEqual(len(_a  )				,    expected_num_stages + 1  )

														# ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
														self.assertListEqual(
														    list(hidden_states[0].shape[-2:]  )				,    [self.model_tester.image_size // 4, self.model_tester.image_size // 4]				,    )

										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE			  =True
														check_hidden_states_output(_a				,    _a				,    _a  )

														# check that output_hidden_states also work using config
														del inputs_dict["output_hidden_states"]
														_SCREAMING_SNAKE_CASE			  =True

														check_hidden_states_output(_a				,    _a				,    _a  )




						def 							__UpperCamelCase				(       self					:      Dict  )							->     Union[str, Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs_for_common()

										_SCREAMING_SNAKE_CASE			  =_config_zero_init(_a  )
										_SCREAMING_SNAKE_CASE			  =_config_zero_init(configs_no_init.backbone_config  )
										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE			  =model_class(config=_a  )
														for name, param in model.named_parameters():
																		if param.requires_grad:
																						self.assertIn(
																						    ((param.data.mean() * 1E9).round() / 1E9).item()				,    [0.0, 1.0]				,    msg=f"Parameter {name} of model {model_class} seems not properly initialized"				,    )




						@unittest.skip(reason='''UperNet does not have tied weights'''  )
						def 							__UpperCamelCase				(       self					:      Union[str, Any]  )							->     Optional[Any]:


										"""simple docstring"""





										pass




						@slow
						def 							__UpperCamelCase				(       self					:      List[str]  )							->     List[Any]:


										"""simple docstring"""





										for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_SCREAMING_SNAKE_CASE			  =UperNetForSemanticSegmentation.from_pretrained(_a  )
														self.assertIsNotNone(_a  )







def 						lowerCamelCase(   ):
				_SCREAMING_SNAKE_CASE			  =hf_hub_download(
				    repo_id='''hf-internal-testing/fixtures_ade20k'''	,repo_type='''dataset'''	,filename='''ADE_val_00000001.jpg''')
				_SCREAMING_SNAKE_CASE			  =Image.open(a__).convert('''RGB''')
				return image


@require_torch
@require_vision
@slow
class 						A__							(		unittest.TestCase  ):




						def 							__UpperCamelCase				(       self					:      Dict  )							->     int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny'''  )
										_SCREAMING_SNAKE_CASE			  =UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny'''  ).to(_a  )

										_SCREAMING_SNAKE_CASE			  =prepare_img()
										_SCREAMING_SNAKE_CASE			  =processor(images=_a				,    return_tensors='''pt'''  ).to(_a  )

										with torch.no_grad():
														_SCREAMING_SNAKE_CASE			  =model(**_a  )

										_SCREAMING_SNAKE_CASE			  =torch.Size((1, model.config.num_labels, 512, 512)  )
										self.assertEqual(outputs.logits.shape				,    _a  )

										_SCREAMING_SNAKE_CASE			  =torch.tensor(
										    [[-7.59_58, -7.59_58, -7.43_02], [-7.59_58, -7.59_58, -7.43_02], [-7.47_97, -7.47_97, -7.30_68]]  ).to(_a  )
										self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]				,    _a				,    atol=1E-4  )  )




						def 							__UpperCamelCase				(       self					:      Optional[Any]  )							->     str:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny'''  )
										_SCREAMING_SNAKE_CASE			  =UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny'''  ).to(_a  )

										_SCREAMING_SNAKE_CASE			  =prepare_img()
										_SCREAMING_SNAKE_CASE			  =processor(images=_a				,    return_tensors='''pt'''  ).to(_a  )

										with torch.no_grad():
														_SCREAMING_SNAKE_CASE			  =model(**_a  )

										_SCREAMING_SNAKE_CASE			  =torch.Size((1, model.config.num_labels, 512, 512)  )
										self.assertEqual(outputs.logits.shape				,    _a  )

										_SCREAMING_SNAKE_CASE			  =torch.tensor(
										    [[-8.81_10, -8.81_10, -8.65_21], [-8.81_10, -8.81_10, -8.65_21], [-8.77_46, -8.77_46, -8.61_30]]  ).to(_a  )
										self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]				,    _a				,    atol=1E-4  )  )



# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re

from ..models.auto import AutoProcessor
from ..models.vision_encoder_decoder import VisionEncoderDecoderModel
from ..utils import is_vision_available
from .base import PipelineTool


if is_vision_available():
							from PIL import Image

class 						A__							(		UpperCamelCase__  ):
						UpperCAmelCase             =						"naver-clova-ix/donut-base-finetuned-docvqa"
						UpperCAmelCase             =						(
						    "This is a tool that answers a question about an document (pdf). It takes an input named `document` which "
						    "should be the document containing the information, as well as a `question` that is the question about the "
						    "document. It returns a text that contains the answer to the question."
						)
						UpperCAmelCase             =						"document_qa"
						UpperCAmelCase             =						AutoProcessor
						UpperCAmelCase             =						VisionEncoderDecoderModel

						UpperCAmelCase             =						["image", "text"]
						UpperCAmelCase             =						["text"]




						def __init__(       self					:      Any				,    *_a					:      int				,    **_a					:      Dict  )							->     int:


										"""simple docstring"""





										if not is_vision_available():
														raise ValueError('''Pillow must be installed to use the DocumentQuestionAnsweringTool.'''  )

										super().__init__(*_a				,    **_a  )




						def 							__UpperCamelCase				(       self					:      Optional[Any]				,    _a					:      "Image"				,    _a					:      str  )							->     Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  ='''<s_docvqa><s_question>{user_input}</s_question><s_answer>'''
										_SCREAMING_SNAKE_CASE			  =task_prompt.replace('''{user_input}'''				,    _a  )
										_SCREAMING_SNAKE_CASE			  =self.pre_processor.tokenizer(
										    _a				,    add_special_tokens=_a				,    return_tensors='''pt'''  ).input_ids
										_SCREAMING_SNAKE_CASE			  =self.pre_processor(_a				,    return_tensors='''pt'''  ).pixel_values

										return {"decoder_input_ids": decoder_input_ids, "pixel_values": pixel_values}




						def 							__UpperCamelCase				(       self					:      List[Any]				,    _a					:      Optional[Any]  )							->     int:


										"""simple docstring"""





										return self.model.generate(
										    inputs['''pixel_values'''].to(self.device  )				,    decoder_input_ids=inputs['''decoder_input_ids'''].to(self.device  )				,    max_length=self.model.decoder.config.max_position_embeddings				,    early_stopping=_a				,    pad_token_id=self.pre_processor.tokenizer.pad_token_id				,    eos_token_id=self.pre_processor.tokenizer.eos_token_id				,    use_cache=_a				,    num_beams=1				,    bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]]				,    return_dict_in_generate=_a				,    ).sequences




						def 							__UpperCamelCase				(       self					:      Any				,    _a					:      int  )							->     Any:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.pre_processor.batch_decode(_a  )[0]
										_SCREAMING_SNAKE_CASE			  =sequence.replace(self.pre_processor.tokenizer.eos_token				,    ''''''  )
										_SCREAMING_SNAKE_CASE			  =sequence.replace(self.pre_processor.tokenizer.pad_token				,    ''''''  )
										_SCREAMING_SNAKE_CASE			  =re.sub(R'''<.*?>'''				,    ''''''				,    _a				,    count=1  ).strip()  # remove first task start token
										_SCREAMING_SNAKE_CASE			  =self.pre_processor.tokenajson(_a  )

										return sequence["answer"]