


import unittest

from transformers import MPNetConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    MPNetForMaskedLM,
							    MPNetForMultipleChoice,
							    MPNetForQuestionAnswering,
							    MPNetForSequenceClassification,
							    MPNetForTokenClassification,
							    MPNetModel,
							)

class 						A__							:




						def __init__(       self					:      List[str]				,    _a					:      Optional[int]				,    _a					:      List[str]=13				,    _a					:      List[Any]=7				,    _a					:      List[Any]=True				,    _a					:      List[str]=True				,    _a					:      Union[str, Any]=False				,    _a					:      Optional[Any]=True				,    _a					:      Any=99				,    _a					:      List[str]=64				,    _a					:      Tuple=5				,    _a					:      Any=4				,    _a					:      Dict=64				,    _a					:      Optional[Any]="gelu"				,    _a					:      List[str]=0.1				,    _a					:      Optional[int]=0.1				,    _a					:      str=512				,    _a					:      str=16				,    _a					:      Optional[int]=2				,    _a					:      Dict=0.02				,    _a					:      Any=3				,    _a					:      str=4				,    _a					:      str=None				,    )							->     Union[str, Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =parent
										_SCREAMING_SNAKE_CASE			  =batch_size
										_SCREAMING_SNAKE_CASE			  =seq_length
										_SCREAMING_SNAKE_CASE			  =is_training
										_SCREAMING_SNAKE_CASE			  =use_input_mask
										_SCREAMING_SNAKE_CASE			  =use_token_type_ids
										_SCREAMING_SNAKE_CASE			  =use_labels
										_SCREAMING_SNAKE_CASE			  =vocab_size
										_SCREAMING_SNAKE_CASE			  =hidden_size
										_SCREAMING_SNAKE_CASE			  =num_hidden_layers
										_SCREAMING_SNAKE_CASE			  =num_attention_heads
										_SCREAMING_SNAKE_CASE			  =intermediate_size
										_SCREAMING_SNAKE_CASE			  =hidden_act
										_SCREAMING_SNAKE_CASE			  =hidden_dropout_prob
										_SCREAMING_SNAKE_CASE			  =attention_probs_dropout_prob
										_SCREAMING_SNAKE_CASE			  =max_position_embeddings
										_SCREAMING_SNAKE_CASE			  =type_vocab_size
										_SCREAMING_SNAKE_CASE			  =type_sequence_label_size
										_SCREAMING_SNAKE_CASE			  =initializer_range
										_SCREAMING_SNAKE_CASE			  =num_labels
										_SCREAMING_SNAKE_CASE			  =num_choices
										_SCREAMING_SNAKE_CASE			  =scope




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


										"""simple docstring"""





										return MPNetConfig.from_pretrained('''microsoft/mpnet-base'''  )




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =ids_tensor([self.batch_size, self.seq_length]				,    self.vocab_size  )

										_SCREAMING_SNAKE_CASE			  =None
										if self.use_input_mask:
														_SCREAMING_SNAKE_CASE			  =random_attention_mask([self.batch_size, self.seq_length]  )

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

										_SCREAMING_SNAKE_CASE			  =self.get_config()
										return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels




						def 							__UpperCamelCase				(       self					:      Dict  )							->     Tuple:


										"""simple docstring"""





										return MPNetConfig(
										    vocab_size=self.vocab_size				,    hidden_size=self.hidden_size				,    num_hidden_layers=self.num_hidden_layers				,    num_attention_heads=self.num_attention_heads				,    intermediate_size=self.intermediate_size				,    hidden_act=self.hidden_act				,    hidden_dropout_prob=self.hidden_dropout_prob				,    attention_probs_dropout_prob=self.attention_probs_dropout_prob				,    max_position_embeddings=self.max_position_embeddings				,    initializer_range=self.initializer_range				,    )




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =MPNetModel(config=_a  )
										model.to(_a  )
										model.eval()
										_SCREAMING_SNAKE_CASE			  =model(_a				,    _a  )
										_SCREAMING_SNAKE_CASE			  =model(_a  )
										self.parent.assertEqual(result.last_hidden_state.shape				,    (self.batch_size, self.seq_length, self.hidden_size)  )
										self.parent.assertEqual(result.pooler_output.shape				,    (self.batch_size, self.hidden_size)  )




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =MPNetForQuestionAnswering(config=_a  )
										model.to(_a  )
										model.eval()
										_SCREAMING_SNAKE_CASE			  =model(
										    _a				,    attention_mask=_a				,    start_positions=_a				,    end_positions=_a				,    )
										self.parent.assertEqual(result.start_logits.shape				,    (self.batch_size, self.seq_length)  )
										self.parent.assertEqual(result.end_logits.shape				,    (self.batch_size, self.seq_length)  )




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.num_labels
										_SCREAMING_SNAKE_CASE			  =MPNetForSequenceClassification(_a  )
										model.to(_a  )
										model.eval()
										_SCREAMING_SNAKE_CASE			  =model(_a				,    attention_mask=_a				,    labels=_a  )
										self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)  )




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.num_choices
										_SCREAMING_SNAKE_CASE			  =MPNetForMultipleChoice(config=_a  )
										model.to(_a  )
										model.eval()
										_SCREAMING_SNAKE_CASE			  =input_ids.unsqueeze(1  ).expand(-1				,    self.num_choices				,    -1  ).contiguous()
										_SCREAMING_SNAKE_CASE			  =input_mask.unsqueeze(1  ).expand(-1				,    self.num_choices				,    -1  ).contiguous()
										_SCREAMING_SNAKE_CASE			  =model(
										    _a				,    attention_mask=_a				,    labels=_a				,    )
										self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_choices)  )




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.num_labels
										_SCREAMING_SNAKE_CASE			  =MPNetForTokenClassification(config=_a  )
										model.to(_a  )
										model.eval()
										_SCREAMING_SNAKE_CASE			  =model(_a				,    attention_mask=_a				,    labels=_a  )
										self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.num_labels)  )




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


										"""simple docstring"""





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

@require_torch
class 						A__							(		UpperCamelCase__   ,     UpperCamelCase__   ,     unittest.TestCase  ):
						UpperCAmelCase             =						(
						    (
						        MPNetForMaskedLM,
						        MPNetForMultipleChoice,
						        MPNetForQuestionAnswering,
						        MPNetForSequenceClassification,
						        MPNetForTokenClassification,
						        MPNetModel,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCAmelCase             =						(
						    {
						        "feature-extraction": MPNetModel,
						        "fill-mask": MPNetForMaskedLM,
						        "question-answering": MPNetForQuestionAnswering,
						        "text-classification": MPNetForSequenceClassification,
						        "token-classification": MPNetForTokenClassification,
						        "zero-shot": MPNetForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						UpperCAmelCase             =						False
						UpperCAmelCase             =						True




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =MPNetModelTester(self  )
										_SCREAMING_SNAKE_CASE			  =ConfigTester(self				,    config_class=_a				,    hidden_size=37  )




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


										"""simple docstring"""





										self.config_tester.run_common_tests()




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


										"""simple docstring"""





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




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


										"""simple docstring"""





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




						def 							__UpperCamelCase				(       self					:      Any  )							->     Dict:


										"""simple docstring"""





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




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


										"""simple docstring"""





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




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


										"""simple docstring"""





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

@require_torch
class 						A__							(		unittest.TestCase  ):




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =MPNetModel.from_pretrained('''microsoft/mpnet-base'''  )
										_SCREAMING_SNAKE_CASE			  =torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]  )
										_SCREAMING_SNAKE_CASE			  =model(_a  )[0]
										_SCREAMING_SNAKE_CASE			  =torch.Size((1, 11, 768)  )
										self.assertEqual(output.shape				,    _a  )
										_SCREAMING_SNAKE_CASE			  =torch.tensor(
										    [[[-0.05_50, 0.19_43, -0.07_40], [-0.05_62, 0.22_11, -0.05_79], [-0.04_37, 0.33_37, -0.06_41]]]  )
										# compare the actual values for a slice.
										self.assertTrue(torch.allclose(output[:, :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"]