





import unittest

from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available
from transformers.pipelines import pipeline
from transformers.pipelines.document_question_answering import apply_tesseract
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_detectrona,
    require_pytesseract,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
					from PIL import Image

					from transformers.image_utils import load_image
else:
					class 		lowerCamelCase  :



						@staticmethod
						def   _lowerCamelCase    (   *a_		,      **a_	):
								pass






					def      __A						(							a_						:  Optional[Any]	):
							return None


# This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace,
# so we can expect it to be available.
lowerCAmelCase       		=      (
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)
@is_pipeline_test
@require_torch
@require_vision
class 		lowerCamelCase  (      unittest.TestCase       ):
	snake_case_      =   MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING



	@require_pytesseract
	@require_vision
	def   _lowerCamelCase    (   self		,      a_		,      a_		,      a_	):
			lowerCAmelCase     :    Optional[Any]						=   pipeline(
			    "document-question-answering"		,      model=a_		,      tokenizer=a_		,      image_processor=a_	)

			lowerCAmelCase     :    List[str]						=   INVOICE_URL
			lowerCAmelCase     :    Union[str, Any]						=   list(zip(*apply_tesseract(load_image(a_	)		,      a_		,      ""	)	)	)
			lowerCAmelCase     :    Optional[int]						=   "What is the placebo?"
			lowerCAmelCase     :    Any						=   [
			    {
			        "image": load_image(a_	),
			        "question": question,
			    },
			    {
			        "image": image,
			        "question": question,
			    },
			    {
			        "image": image,
			        "question": question,
			        "word_boxes": word_boxes,
			    },
			]
			return dqa_pipeline, examples



	def   _lowerCamelCase    (   self		,      a_		,      a_	):
			lowerCAmelCase     :    Union[str, Any]						=   dqa_pipeline(a_		,      top_k=2	)
			self.assertEqual(
			    a_		,      [
			        [
			            {"score": ANY(a_	), "answer": ANY(a_	), "start": ANY(a_	), "end": ANY(a_	)},
			            {"score": ANY(a_	), "answer": ANY(a_	), "start": ANY(a_	), "end": ANY(a_	)},
			        ]
			    ]
			    * 3		,      )



	@require_torch
	@require_detectrona
	@require_pytesseract
	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[Any]						=   pipeline("document-question-answering"		,      model="hf-internal-testing/tiny-random-layoutlmv2"	)
			lowerCAmelCase     :    Dict						=   INVOICE_URL
			lowerCAmelCase     :    Optional[Any]						=   "How many cats are there?"

			lowerCAmelCase     :    Dict						=   [
			    {"score": 0.0001, "answer": "oy 2312/2019", "start": 38, "end": 39},
			    {"score": 0.0001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40},
			]
			lowerCAmelCase     :    Any						=   dqa_pipeline(image=a_		,      question=a_		,      top_k=2	)
			self.assertEqual(nested_simplify(a_		,      decimals=4	)		,      a_	)

			lowerCAmelCase     :    List[str]						=   dqa_pipeline({"image": image, "question": question}		,      top_k=2	)
			self.assertEqual(nested_simplify(a_		,      decimals=4	)		,      a_	)

			# This image does not detect ANY text in it, meaning layoutlmv2 should fail.
			# Empty answer probably
			lowerCAmelCase     :    Optional[Any]						=   "./tests/fixtures/tests_samples/COCO/000000039769.png"
			lowerCAmelCase     :    Optional[Any]						=   dqa_pipeline(image=a_		,      question=a_		,      top_k=2	)
			self.assertEqual(a_		,      []	)

			# We can optionnally pass directly the words and bounding boxes
			lowerCAmelCase     :    List[Any]						=   "./tests/fixtures/tests_samples/COCO/000000039769.png"
			lowerCAmelCase     :    Optional[int]						=   []
			lowerCAmelCase     :    Any						=   []
			lowerCAmelCase     :    Optional[int]						=   dqa_pipeline(image=a_		,      question=a_		,      words=a_		,      boxes=a_		,      top_k=2	)
			self.assertEqual(a_		,      []	)



	@slow
	@require_torch
	@require_detectrona
	@require_pytesseract
	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    int						=   pipeline(
			    "document-question-answering"		,      model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"		,      revision="9977165"		,      )
			lowerCAmelCase     :    Dict						=   INVOICE_URL
			lowerCAmelCase     :    List[str]						=   "What is the invoice number?"

			lowerCAmelCase     :    Optional[int]						=   dqa_pipeline(image=a_		,      question=a_		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
			        {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
			    ]		,      )

			lowerCAmelCase     :    int						=   dqa_pipeline({"image": image, "question": question}		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
			        {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
			    ]		,      )

			lowerCAmelCase     :    Dict						=   dqa_pipeline(
			    [{"image": image, "question": question}, {"image": image, "question": question}]		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        [
			            {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
			            {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
			        ],
			    ]
			    * 2		,      )



	@slow
	@require_torch
	@require_detectrona
	@require_pytesseract
	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Optional[int]						=   pipeline(
			    "document-question-answering"		,      model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"		,      revision="9977165"		,      max_seq_len=50		,      )
			lowerCAmelCase     :    int						=   INVOICE_URL
			lowerCAmelCase     :    Any						=   "What is the invoice number?"

			lowerCAmelCase     :    List[str]						=   dqa_pipeline(image=a_		,      question=a_		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        {"score": 0.9974, "answer": "1110212019", "start": 23, "end": 23},
			        {"score": 0.9948, "answer": "us-001", "start": 16, "end": 16},
			    ]		,      )

			lowerCAmelCase     :    Optional[int]						=   dqa_pipeline({"image": image, "question": question}		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        {"score": 0.9974, "answer": "1110212019", "start": 23, "end": 23},
			        {"score": 0.9948, "answer": "us-001", "start": 16, "end": 16},
			    ]		,      )

			lowerCAmelCase     :    int						=   dqa_pipeline(
			    [{"image": image, "question": question}, {"image": image, "question": question}]		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        [
			            {"score": 0.9974, "answer": "1110212019", "start": 23, "end": 23},
			            {"score": 0.9948, "answer": "us-001", "start": 16, "end": 16},
			        ]
			    ]
			    * 2		,      )



	@slow
	@require_torch
	@require_pytesseract
	@require_vision
	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Tuple						=   AutoTokenizer.from_pretrained(
			    "impira/layoutlm-document-qa"		,      revision="3dc6de3"		,      add_prefix_space=a_	)
			lowerCAmelCase     :    Tuple						=   pipeline(
			    "document-question-answering"		,      model="impira/layoutlm-document-qa"		,      tokenizer=a_		,      revision="3dc6de3"		,      )
			lowerCAmelCase     :    Optional[Any]						=   INVOICE_URL
			lowerCAmelCase     :    Any						=   "What is the invoice number?"

			lowerCAmelCase     :    Optional[Any]						=   dqa_pipeline(image=a_		,      question=a_		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
			        {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
			    ]		,      )

			lowerCAmelCase     :    Union[str, Any]						=   dqa_pipeline({"image": image, "question": question}		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
			        {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
			    ]		,      )

			lowerCAmelCase     :    Tuple						=   dqa_pipeline(
			    [{"image": image, "question": question}, {"image": image, "question": question}]		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        [
			            {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
			            {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
			        ]
			    ]
			    * 2		,      )

			lowerCAmelCase     :    List[str]						=   list(zip(*apply_tesseract(load_image(a_	)		,      a_		,      ""	)	)	)

			# This model should also work if `image` is set to None
			lowerCAmelCase     :    int						=   dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
			        {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
			    ]		,      )



	@slow
	@require_torch
	@require_pytesseract
	@require_vision
	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Tuple						=   AutoTokenizer.from_pretrained(
			    "impira/layoutlm-document-qa"		,      revision="3dc6de3"		,      add_prefix_space=a_	)
			lowerCAmelCase     :    Optional[Any]						=   pipeline(
			    "document-question-answering"		,      model="impira/layoutlm-document-qa"		,      tokenizer=a_		,      revision="3dc6de3"		,      max_seq_len=50		,      )
			lowerCAmelCase     :    int						=   INVOICE_URL
			lowerCAmelCase     :    str						=   "What is the invoice number?"

			lowerCAmelCase     :    int						=   dqa_pipeline(image=a_		,      question=a_		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        {"score": 0.9999, "answer": "us-001", "start": 16, "end": 16},
			        {"score": 0.9998, "answer": "us-001", "start": 16, "end": 16},
			    ]		,      )

			lowerCAmelCase     :    Any						=   dqa_pipeline(
			    [{"image": image, "question": question}, {"image": image, "question": question}]		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        [
			            {"score": 0.9999, "answer": "us-001", "start": 16, "end": 16},
			            {"score": 0.9998, "answer": "us-001", "start": 16, "end": 16},
			        ]
			    ]
			    * 2		,      )

			lowerCAmelCase     :    Any						=   list(zip(*apply_tesseract(load_image(a_	)		,      a_		,      ""	)	)	)

			# This model should also work if `image` is set to None
			lowerCAmelCase     :    List[Any]						=   dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}		,      top_k=2	)
			self.assertEqual(
			    nested_simplify(a_		,      decimals=4	)		,      [
			        {"score": 0.9999, "answer": "us-001", "start": 16, "end": 16},
			        {"score": 0.9998, "answer": "us-001", "start": 16, "end": 16},
			    ]		,      )



	@slow
	@require_torch
	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Any						=   pipeline(
			    "document-question-answering"		,      model="naver-clova-ix/donut-base-finetuned-docvqa"		,      tokenizer=AutoTokenizer.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa"	)		,      feature_extractor="naver-clova-ix/donut-base-finetuned-docvqa"		,      )

			lowerCAmelCase     :    Union[str, Any]						=   INVOICE_URL
			lowerCAmelCase     :    Tuple						=   "What is the invoice number?"
			lowerCAmelCase     :    Any						=   dqa_pipeline(image=a_		,      question=a_		,      top_k=2	)
			self.assertEqual(nested_simplify(a_		,      decimals=4	)		,      [{"answer": "us-001"}]	)





	@require_tf
	@unittest.skip("Document question answering not implemented in TF"	)
	def   _lowerCamelCase    (   self	):
			pass










'''simple docstring'''






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

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


lowerCAmelCase       		=      logging.get_logger(__name__)

lowerCAmelCase       		=      """▁"""

lowerCAmelCase       		=      {"""vocab_file""": """sentencepiece.bpe.model"""}

lowerCAmelCase       		=      {
    """vocab_file""": {
        """xlm-roberta-base""": """https://huggingface.co/xlm-roberta-base/resolve/main/sentencepiece.bpe.model""",
        """xlm-roberta-large""": """https://huggingface.co/xlm-roberta-large/resolve/main/sentencepiece.bpe.model""",
        """xlm-roberta-large-finetuned-conll02-dutch""": (
            """https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/sentencepiece.bpe.model"""
        ),
        """xlm-roberta-large-finetuned-conll02-spanish""": (
            """https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/sentencepiece.bpe.model"""
        ),
        """xlm-roberta-large-finetuned-conll03-english""": (
            """https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/sentencepiece.bpe.model"""
        ),
        """xlm-roberta-large-finetuned-conll03-german""": (
            """https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/sentencepiece.bpe.model"""
        ),
    }
}

lowerCAmelCase       		=      {
    """xlm-roberta-base""": 5_12,
    """xlm-roberta-large""": 5_12,
    """xlm-roberta-large-finetuned-conll02-dutch""": 5_12,
    """xlm-roberta-large-finetuned-conll02-spanish""": 5_12,
    """xlm-roberta-large-finetuned-conll03-english""": 5_12,
    """xlm-roberta-large-finetuned-conll03-german""": 5_12,
}
class 		lowerCamelCase  (      _A       ):
	snake_case_      =   VOCAB_FILES_NAMES
	snake_case_      =   PRETRAINED_VOCAB_FILES_MAP
	snake_case_      =   PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
	snake_case_      =   ["input_ids", "attention_mask"]



	def __init__(   self		,      a_		,      a_="<s>"		,      a_="</s>"		,      a_="</s>"		,      a_="<s>"		,      a_="<unk>"		,      a_="<pad>"		,      a_="<mask>"		,      a_ = None		,      **a_		,      ):
			# Mask token behave like a normal word, i.e. include the space before it
			lowerCAmelCase     :    str						=   AddedToken(a_		,      lstrip=a_		,      rstrip=a_	) if isinstance(a_		,      a_	) else mask_token

			lowerCAmelCase     :    Tuple						=   {} if sp_model_kwargs is None else sp_model_kwargs

			super().__init__(
			    bos_token=a_		,      eos_token=a_		,      unk_token=a_		,      sep_token=a_		,      cls_token=a_		,      pad_token=a_		,      mask_token=a_		,      sp_model_kwargs=self.sp_model_kwargs		,      **a_		,      )

			lowerCAmelCase     :    Optional[int]						=   spm.SentencePieceProcessor(**self.sp_model_kwargs	)
			self.sp_model.Load(str(a_	)	)
			lowerCAmelCase     :    int						=   vocab_file

			# Original fairseq vocab and spm vocab must be "aligned":
			# Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
			# -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
			# fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
			# spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

			# Mimic fairseq token-to-id alignment for the first 4 token
			lowerCAmelCase     :    Any						=   {"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3}

			# The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
			lowerCAmelCase     :    Dict						=   1

			lowerCAmelCase     :    Optional[Any]						=   len(self.sp_model	) + self.fairseq_offset
			lowerCAmelCase     :    List[Any]						=   {v: k for k, v in self.fairseq_tokens_to_ids.items()}



	def __getstate__(   self	):
			lowerCAmelCase     :    Any						=   self.__dict__.copy()
			lowerCAmelCase     :    Optional[int]						=   None
			lowerCAmelCase     :    List[str]						=   self.sp_model.serialized_model_proto()
			return state



	def __setstate__(   self		,      a_	):
			lowerCAmelCase     :    Any						=   d

			# for backward compatibility
			if not hasattr(self		,      "sp_model_kwargs"	):
					lowerCAmelCase     :    Any						=   {}

			lowerCAmelCase     :    Optional[Any]						=   spm.SentencePieceProcessor(**self.sp_model_kwargs	)
			self.sp_model.LoadFromSerializedProto(self.sp_model_proto	)



	def   _lowerCamelCase    (   self		,      a_		,      a_ = None	):

			if token_ids_a is None:
					return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
			lowerCAmelCase     :    Optional[Any]						=   [self.cls_token_id]
			lowerCAmelCase     :    List[Any]						=   [self.sep_token_id]
			return cls + token_ids_a + sep + sep + token_ids_a + sep



	def   _lowerCamelCase    (   self		,      a_		,      a_ = None		,      a_ = False	):

			if already_has_special_tokens:
					return super().get_special_tokens_mask(
					    token_ids_a=a_		,      token_ids_a=a_		,      already_has_special_tokens=a_	)

			if token_ids_a is None:
					return [1] + ([0] * len(a_	)) + [1]
			return [1] + ([0] * len(a_	)) + [1, 1] + ([0] * len(a_	)) + [1]



	def   _lowerCamelCase    (   self		,      a_		,      a_ = None	):
			lowerCAmelCase     :    Dict						=   [self.sep_token_id]
			lowerCAmelCase     :    List[Any]						=   [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 + sep + token_ids_a + sep	) * [0]



	@property
	def   _lowerCamelCase    (   self	):
			return len(self.sp_model	) + self.fairseq_offset + 1  # Add the <mask> token



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Dict						=   {self.convert_ids_to_tokens(a_	): i for i in range(self.vocab_size	)}
			vocab.update(self.added_tokens_encoder	)
			return vocab



	def   _lowerCamelCase    (   self		,      a_	):
			return self.sp_model.encode(a_		,      out_type=a_	)



	def   _lowerCamelCase    (   self		,      a_	):
			if token in self.fairseq_tokens_to_ids:
					return self.fairseq_tokens_to_ids[token]
			lowerCAmelCase     :    int						=   self.sp_model.PieceToId(a_	)

			# Need to return unknown token if the SP model returned 0
			return spm_id + self.fairseq_offset if spm_id else self.unk_token_id



	def   _lowerCamelCase    (   self		,      a_	):
			if index in self.fairseq_ids_to_tokens:
					return self.fairseq_ids_to_tokens[index]
			return self.sp_model.IdToPiece(index - self.fairseq_offset	)



	def   _lowerCamelCase    (   self		,      a_	):
			lowerCAmelCase     :    Dict						=   "".join(a_	).replace(a_		,      " "	).strip()
			return out_string





	def   _lowerCamelCase    (   self		,      a_		,      a_ = None	):
			if not os.path.isdir(a_	):
					logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''	)
					return
			lowerCAmelCase     :    int						=   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_	) and os.path.isfile(self.vocab_file	):
					copyfile(self.vocab_file		,      a_	)
			elif not os.path.isfile(self.vocab_file	):
					with open(a_		,      "wb"	) as fi:
							lowerCAmelCase     :    Dict						=   self.sp_model.serialized_model_proto()
							fi.write(a_	)

			return (out_vocab_file,)



