






# 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     _SCREAMING_SNAKE_CASE       (			_a      ):
	snake_case__							:     Union[str, Any]     	=  """naver-clova-ix/donut-base-finetuned-docvqa"""
	snake_case__							:     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."""
	)
	snake_case__							:     Any     	=  """document_qa"""
	snake_case__							:     List[str]     	=  AutoProcessor
	snake_case__							:     Optional[Any]     	=  VisionEncoderDecoderModel

	snake_case__							:     Optional[Any]     	=  ["""image""", """text"""]
	snake_case__							:     Tuple     	=  ["""text"""]





	def __init__(		self				:					Optional[int] ,  *__lowerCamelCase				:					Tuple ,  **__lowerCamelCase				:					Optional[int]	):
		if not is_vision_available():
			raise ValueError("""Pillow must be installed to use the DocumentQuestionAnsweringTool."""	)

		super().__init__(*__lowerCamelCase ,  **__lowerCamelCase	)





	def 						_A (		self				:					List[str] ,  __lowerCamelCase				:					"Image" ,  __lowerCamelCase				:					str	):
		UpperCamelCase     :int          =	"""<s_docvqa><s_question>{user_input}</s_question><s_answer>"""
		UpperCamelCase     :List[Any]          =	task_prompt.replace("""{user_input}""" ,  __lowerCamelCase	)
		UpperCamelCase     :Tuple          =	self.pre_processor.tokenizer(
		    __lowerCamelCase ,  add_special_tokens=__lowerCamelCase ,  return_tensors="""pt"""	).input_ids
		UpperCamelCase     :List[str]          =	self.pre_processor(__lowerCamelCase ,  return_tensors="""pt"""	).pixel_values

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





	def 						_A (		self				:					Union[str, Any] ,  __lowerCamelCase				:					List[Any]	):
		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=__lowerCamelCase ,  pad_token_id=self.pre_processor.tokenizer.pad_token_id ,  eos_token_id=self.pre_processor.tokenizer.eos_token_id ,  use_cache=__lowerCamelCase ,  num_beams=1 ,  bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]] ,  return_dict_in_generate=__lowerCamelCase ,  ).sequences





	def 						_A (		self				:					Union[str, Any] ,  __lowerCamelCase				:					str	):
		UpperCamelCase     :Optional[int]          =	self.pre_processor.batch_decode(__lowerCamelCase	)[0]
		UpperCamelCase     :Any          =	sequence.replace(self.pre_processor.tokenizer.eos_token ,  """"""	)
		UpperCamelCase     :List[Any]          =	sequence.replace(self.pre_processor.tokenizer.pad_token ,  """"""	)
		UpperCamelCase     :Optional[int]          =	re.sub(R"""<.*?>""" ,  """""" ,  __lowerCamelCase ,  count=1	).strip()  # remove first task start token
		UpperCamelCase     :str          =	self.pre_processor.tokenajson(__lowerCamelCase	)

		return sequence["answer"]








import html

from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from ...utils import is_bsa_available, logging, requires_backends


if is_bsa_available():
	import bsa
	from bsa import BeautifulSoup


UpperCAmelCase_ :						Any      =      logging.get_logger(__name__)

class     _SCREAMING_SNAKE_CASE       (			_a      ):





	def __init__(		self				:					Optional[int] ,  **__lowerCamelCase				:					Optional[int]	):
		requires_backends(self ,  ["""bs4"""]	)
		super().__init__(**__lowerCamelCase	)





	def 						_A (		self				:					List[str] ,  __lowerCamelCase				:					Any	):
		UpperCamelCase     :Optional[int]          =	[]
		UpperCamelCase     :List[str]          =	[]
		UpperCamelCase     :Union[str, Any]          =	element if element.name else element.parent
		for parent in child.parents:  # type: bs4.element.Tag
			UpperCamelCase     :Optional[Any]          =	parent.find_all(child.name ,  recursive=__lowerCamelCase	)
			xpath_tags.append(child.name	)
			xpath_subscripts.append(
			    0 if 1 == len(__lowerCamelCase	) else next(i for i, s in enumerate(__lowerCamelCase ,  1	) if s is child	)	)
			UpperCamelCase     :Any          =	parent
		xpath_tags.reverse()
		xpath_subscripts.reverse()
		return xpath_tags, xpath_subscripts





	def 						_A (		self				:					Any ,  __lowerCamelCase				:					Tuple	):
		UpperCamelCase     :Any          =	BeautifulSoup(__lowerCamelCase ,  """html.parser"""	)

		UpperCamelCase     :Union[str, Any]          =	[]
		UpperCamelCase     :Tuple          =	[]
		UpperCamelCase     :Tuple          =	[]

		for element in html_code.descendants:
			if type(__lowerCamelCase	) == bsa.element.NavigableString:
				if type(element.parent	) != bsa.element.Tag:
					continue

				UpperCamelCase     :Any          =	html.unescape(__lowerCamelCase	).strip()
				if not text_in_this_tag:
					continue

				all_doc_strings.append(__lowerCamelCase	)

				UpperCamelCase     ,      UpperCamelCase     :Optional[Any]          =	self.xpath_soup(__lowerCamelCase	)
				stringaxtag_seq.append(__lowerCamelCase	)
				stringaxsubs_seq.append(__lowerCamelCase	)

		if len(__lowerCamelCase	) != len(__lowerCamelCase	):
			raise ValueError("""Number of doc strings and xtags does not correspond"""	)
		if len(__lowerCamelCase	) != len(__lowerCamelCase	):
			raise ValueError("""Number of doc strings and xsubs does not correspond"""	)

		return all_doc_strings, stringaxtag_seq, stringaxsubs_seq





	def 						_A (		self				:					int ,  __lowerCamelCase				:					List[Any] ,  __lowerCamelCase				:					List[str]	):
		UpperCamelCase     :Tuple          =	""""""
		for tagname, subs in zip(__lowerCamelCase ,  __lowerCamelCase	):
			xpath += F"""/{tagname}"""
			if subs != 0:
				xpath += F"""[{subs}]"""
		return xpath





	def __call__(		self				:					Any ,  __lowerCamelCase				:					Dict	):
		UpperCamelCase     :Any          =	False

		# Check that strings has a valid type
		if isinstance(__lowerCamelCase ,  __lowerCamelCase	):
			UpperCamelCase     :List[Any]          =	True
		elif isinstance(__lowerCamelCase ,  (list, tuple)	):
			if len(__lowerCamelCase	) == 0 or isinstance(html_strings[0] ,  __lowerCamelCase	):
				UpperCamelCase     :Any          =	True

		if not valid_strings:
			raise ValueError(
			    """HTML strings must of type `str`, `List[str]` (batch of examples), """
			    F"""but is of type {type(__lowerCamelCase	)}."""	)

		UpperCamelCase     :str          =	bool(isinstance(__lowerCamelCase ,  (list, tuple)	) and (isinstance(html_strings[0] ,  __lowerCamelCase	))	)

		if not is_batched:
			UpperCamelCase     :Any          =	[html_strings]

		# Get nodes + xpaths
		UpperCamelCase     :Union[str, Any]          =	[]
		UpperCamelCase     :str          =	[]
		for html_string in html_strings:
			UpperCamelCase     ,      UpperCamelCase     ,      UpperCamelCase     :int          =	self.get_three_from_single(__lowerCamelCase	)
			nodes.append(__lowerCamelCase	)
			UpperCamelCase     :int          =	[]
			for node, tag_list, sub_list in zip(__lowerCamelCase ,  __lowerCamelCase ,  __lowerCamelCase	):
				UpperCamelCase     :str          =	self.construct_xpath(__lowerCamelCase ,  __lowerCamelCase	)
				xpath_strings.append(__lowerCamelCase	)
			xpaths.append(__lowerCamelCase	)

		# return as Dict
		UpperCamelCase     :Optional[int]          =	{"""nodes""": nodes, """xpaths""": xpaths}
		UpperCamelCase     :Any          =	BatchFeature(data=__lowerCamelCase ,  tensor_type=__lowerCamelCase	)

		return encoded_inputs
