





'''simple docstring'''







# 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      (lowerCamelCase     ):
							__a					:   Dict									=       "naver-clova-ix/donut-base-finetuned-docvqa"
							__a					:   Optional[int]									=       (
							    "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."
							)
							__a					:   int									=       "document_qa"
							__a					:   Optional[Any]									=       AutoProcessor
							__a					:   Optional[Any]									=       VisionEncoderDecoderModel

							__a					:   Any									=       ["image", "text"]
							__a					:   Union[str, Any]									=       ["text"]

							def __init__(							self							:     int       ,     *__magic_name__							:     Union[str, Any]       ,     **__magic_name__							:     List[Any]   )					->       List[Any]:







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

													super().__init__(*__magic_name__       ,     **__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Tuple       ,     __magic_name__							:     "Image"       ,     __magic_name__							:     str   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Tuple     						=  '''<s_docvqa><s_question>{user_input}</s_question><s_answer>'''
													UpperCAmelCase_			:      Any     						=  task_prompt.replace('''{user_input}'''       ,     __magic_name__   )
													UpperCAmelCase_			:      int     						=  self.pre_processor.tokenizer(
													    __magic_name__       ,     add_special_tokens=__magic_name__       ,     return_tensors='''pt'''   ).input_ids
													UpperCAmelCase_			:      int     						=  self.pre_processor(__magic_name__       ,     return_tensors='''pt'''   ).pixel_values

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

							def     UpperCAmelCase__	(							self							:     List[str]       ,     __magic_name__							:     Optional[Any]   )					->       List[str]:







													"""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=__magic_name__       ,     pad_token_id=self.pre_processor.tokenizer.pad_token_id       ,     eos_token_id=self.pre_processor.tokenizer.eos_token_id       ,     use_cache=__magic_name__       ,     num_beams=1       ,     bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]]       ,     return_dict_in_generate=__magic_name__       ,     ).sequences



							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     Tuple   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  self.pre_processor.batch_decode(__magic_name__   )[0]
													UpperCAmelCase_			:      Dict     						=  sequence.replace(self.pre_processor.tokenizer.eos_token       ,     ''''''   )
													UpperCAmelCase_			:      Optional[int]     						=  sequence.replace(self.pre_processor.tokenizer.pad_token       ,     ''''''   )
													UpperCAmelCase_			:      Dict     						=  re.sub(R'''<.*?>'''       ,     ''''''       ,     __magic_name__       ,     count=1   ).strip()  # remove first task start token
													UpperCAmelCase_			:      List[str]     						=  self.pre_processor.tokenajson(__magic_name__   )

													return sequence["answer"]












'''simple docstring'''







import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()


class 					__a      (unittest.TestCase     ):

							@property
							def     UpperCAmelCase__	(							self							:     Dict   )					->       str:







													"""simple docstring"""
													torch.manual_seed(0   )
													UpperCAmelCase_			:      Optional[Any]     						=  UNetaDModel(
													    block_out_channels=(32, 64)       ,     layers_per_block=2       ,     sample_size=32       ,     in_channels=3       ,     out_channels=3       ,     down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')       ,     up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')       ,     )
													return model



							def     UpperCAmelCase__	(							self							:     Dict   )					->       Tuple:







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  self.dummy_uncond_unet
													UpperCAmelCase_			:      Dict     						=  KarrasVeScheduler()

													UpperCAmelCase_			:      Union[str, Any]     						=  KarrasVePipeline(unet=__magic_name__       ,     scheduler=__magic_name__   )
													pipe.to(__magic_name__   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

													UpperCAmelCase_			:      Dict     						=  torch.manual_seed(0   )
													UpperCAmelCase_			:      Optional[int]     						=  pipe(num_inference_steps=2       ,     generator=__magic_name__       ,     output_type='''numpy'''   ).images

													UpperCAmelCase_			:      Tuple     						=  torch.manual_seed(0   )
													UpperCAmelCase_			:      str     						=  pipe(num_inference_steps=2       ,     generator=__magic_name__       ,     output_type='''numpy'''       ,     return_dict=__magic_name__   )[0]

													UpperCAmelCase_			:      Union[str, Any]     						=  image[0, -3:, -3:, -1]
													UpperCAmelCase_			:      Optional[Any]     						=  image_from_tuple[0, -3:, -3:, -1]

													assert image.shape == (1, 32, 32, 3)
													UpperCAmelCase_			:      Dict     						=  np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]   )

													assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1E-2
													assert np.abs(image_from_tuple_slice.flatten() - expected_slice   ).max() < 1E-2





@slow
@require_torch
class 					__a      (unittest.TestCase     ):

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







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  '''google/ncsnpp-celebahq-256'''
													UpperCAmelCase_			:      List[str]     						=  UNetaDModel.from_pretrained(__magic_name__   )
													UpperCAmelCase_			:      List[Any]     						=  KarrasVeScheduler()

													UpperCAmelCase_			:      Any     						=  KarrasVePipeline(unet=__magic_name__       ,     scheduler=__magic_name__   )
													pipe.to(__magic_name__   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

													UpperCAmelCase_			:      Dict     						=  torch.manual_seed(0   )
													UpperCAmelCase_			:      Optional[Any]     						=  pipe(num_inference_steps=20       ,     generator=__magic_name__       ,     output_type='''numpy'''   ).images

													UpperCAmelCase_			:      Any     						=  image[0, -3:, -3:, -1]
													assert image.shape == (1, 2_56, 2_56, 3)
													UpperCAmelCase_			:      Optional[Any]     						=  np.array([0.5_7_8, 0.5_8_1_1, 0.5_9_2_4, 0.5_8_0_9, 0.5_8_7, 0.5_8_8_6, 0.5_8_6_1, 0.5_8_0_2, 0.5_8_6]   )

													assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1E-2





