



# 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  __snake_case      (   SCREAMING_SNAKE_CASE):







							'''simple docstring'''






							UpperCamelCase__				:      Any         =					"""naver-clova-ix/donut-base-finetuned-docvqa"""
							UpperCamelCase__				:      Tuple         =					(
							    """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__				:      List[str]         =					"""document_qa"""
							UpperCamelCase__				:      Tuple         =					AutoProcessor
							UpperCamelCase__				:      Optional[Any]         =					VisionEncoderDecoderModel

							UpperCamelCase__				:      Optional[int]         =					["""image""", """text"""]
							UpperCamelCase__				:      str         =					["""text"""]


							def __init__(							self  ,  *a_  ,  **a_		):
											if not is_vision_available():
															raise ValueError("""Pillow must be installed to use the DocumentQuestionAnsweringTool."""		)

											super().__init__(*a_  ,  **a_		)


							def _a      (							self  ,  a_  ,  a_		):
											a__				      =		"""<s_docvqa><s_question>{user_input}</s_question><s_answer>"""
											a__				      =		task_prompt.replace("""{user_input}"""  ,  a_		)
											a__				      =		self.pre_processor.tokenizer(
											    a_  ,  add_special_tokens=a_  ,  return_tensors="""pt"""		).input_ids
											a__				      =		self.pre_processor(a_  ,  return_tensors="""pt"""		).pixel_values

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


							def _a      (							self  ,  a_		):
											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 _a      (							self  ,  a_		):
											a__				      =		self.pre_processor.batch_decode(a_		)[0]
											a__				      =		sequence.replace(self.pre_processor.tokenizer.eos_token  ,  """"""		)
											a__				      =		sequence.replace(self.pre_processor.tokenizer.pad_token  ,  """"""		)
											a__				      =		re.sub(r"""<.*?>"""  ,  """"""  ,  a_  ,  count=1		).strip()  # remove first task start token
											a__				      =		self.pre_processor.tokenajson(a_		)

											return sequence["answer"]






import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
	from .tokenization_albert import AlbertTokenizer
else:
	UpperCAmelCase							 =					None

UpperCAmelCase							 =					logging.get_logger(__name__)
UpperCAmelCase							 =					{"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""}

UpperCAmelCase							 =					{
    """vocab_file""": {
        """albert-base-v1""": """https://huggingface.co/albert-base-v1/resolve/main/spiece.model""",
        """albert-large-v1""": """https://huggingface.co/albert-large-v1/resolve/main/spiece.model""",
        """albert-xlarge-v1""": """https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model""",
        """albert-xxlarge-v1""": """https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model""",
        """albert-base-v2""": """https://huggingface.co/albert-base-v2/resolve/main/spiece.model""",
        """albert-large-v2""": """https://huggingface.co/albert-large-v2/resolve/main/spiece.model""",
        """albert-xlarge-v2""": """https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model""",
        """albert-xxlarge-v2""": """https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model""",
    },
    """tokenizer_file""": {
        """albert-base-v1""": """https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json""",
        """albert-large-v1""": """https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json""",
        """albert-xlarge-v1""": """https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json""",
        """albert-xxlarge-v1""": """https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json""",
        """albert-base-v2""": """https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json""",
        """albert-large-v2""": """https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json""",
        """albert-xlarge-v2""": """https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json""",
        """albert-xxlarge-v2""": """https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json""",
    },
}

UpperCAmelCase							 =					{
    """albert-base-v1""": 512,
    """albert-large-v1""": 512,
    """albert-xlarge-v1""": 512,
    """albert-xxlarge-v1""": 512,
    """albert-base-v2""": 512,
    """albert-large-v2""": 512,
    """albert-xlarge-v2""": 512,
    """albert-xxlarge-v2""": 512,
}

UpperCAmelCase							 =					"""▁"""

class  __snake_case      (   SCREAMING_SNAKE_CASE):







							'''simple docstring'''






							UpperCamelCase__				:      Tuple         =					VOCAB_FILES_NAMES
							UpperCamelCase__				:      Union[str, Any]         =					PRETRAINED_VOCAB_FILES_MAP
							UpperCamelCase__				:      List[Any]         =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							UpperCamelCase__				:      List[str]         =					AlbertTokenizer


							def __init__(							self  ,  a_=None  ,  a_=None  ,  a_=True  ,  a_=True  ,  a_=False  ,  a_="[CLS]"  ,  a_="[SEP]"  ,  a_="<unk>"  ,  a_="[SEP]"  ,  a_="<pad>"  ,  a_="[CLS]"  ,  a_="[MASK]"  ,  **a_  ,  ):
											# Mask token behave like a normal word, i.e. include the space before it and
											# is included in the raw text, there should be a match in a non-normalized sentence.
											a__				      =		(
											    AddedToken(a_  ,  lstrip=a_  ,  rstrip=a_  ,  normalized=a_		)
											    if isinstance(a_  ,  a_		)
											    else mask_token
											)

											super().__init__(
											    a_  ,  tokenizer_file=a_  ,  do_lower_case=a_  ,  remove_space=a_  ,  keep_accents=a_  ,  bos_token=a_  ,  eos_token=a_  ,  unk_token=a_  ,  sep_token=a_  ,  pad_token=a_  ,  cls_token=a_  ,  mask_token=a_  ,  **a_  ,  )

											a__				      =		do_lower_case
											a__				      =		remove_space
											a__				      =		keep_accents
											a__				      =		vocab_file
											a__				      =		False if not self.vocab_file else True


							def _a      (							self  ,  a_  ,  a_ = None		):
											a__				      =		[self.sep_token_id]
											a__				      =		[self.cls_token_id]
											if token_ids_a is None:
															return cls + token_ids_a + sep
											return cls + token_ids_a + sep + token_ids_a + sep


							def _a      (							self  ,  a_  ,  a_ = None		):
											a__				      =		[self.sep_token_id]
											a__				      =		[self.cls_token_id]

											if token_ids_a is None:
															return len(cls + token_ids_a + sep		) * [0]
											return len(cls + token_ids_a + sep		) * [0] + len(token_ids_a + sep		) * [1]







							def _a      (							self  ,  a_  ,  a_ = None		):
											if not self.can_save_slow_tokenizer:
															raise ValueError(
															    """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """
															    """tokenizer."""		)

											if not os.path.isdir(a_		):
															logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''		)
															return
											a__				      =		os.path.join(
											    a_  ,  (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]		)

											if os.path.abspath(self.vocab_file		) != os.path.abspath(a_		):
															copyfile(self.vocab_file  ,  a_		)

											return (out_vocab_file,)

