

"""simple docstring"""




import unittest

from transformers.testing_utils import require_bsa
from transformers.utils import is_bsa_available

from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin


if is_bsa_available():
						from transformers import MarkupLMFeatureExtractor







class      __lowercase			(    unittest.TestCase					):

		def __init__(  self		:			Any					,A		:			Dict    ):






			'''simple docstring'''
			UpperCAmelCase__					:  int					   =					parent




		def    __lowercase       (  self		:			Any    ):






			'''simple docstring'''
			return {}







def   lowerCAmelCase						(					):
	'''simple docstring'''





	UpperCAmelCase__					:  str					   =					"""<HTML>

    <HEAD>
    <TITLE>sample document</TITLE>
    </HEAD>

    <BODY BGCOLOR=\"FFFFFF\">
    <HR>
    <a href=\"http://google.com\">Goog</a>
    <H1>This is one header</H1>
    <H2>This is a another Header</H2>
    <P>Travel from
        <P>
        <B>SFO to JFK</B>
        <BR>
        <B><I>on May 2, 2015 at 2:00 pm. For details go to confirm.com </I></B>
        <HR>
        <div style=\"color:#0000FF\">
            <h3>Traveler <b> name </b> is
            <p> John Doe </p>
        </div>"""

	UpperCAmelCase__					:  List[Any]					   =					"""
    <!DOCTYPE html>
    <html>
    <body>

    <h1>My First Heading</h1>
    <p>My first paragraph.</p>

    </body>
    </html>
    """

	return [html_string_a, html_string_a]







@require_bsa
class      __lowercase			(    __UpperCAmelCase	,			unittest.TestCase					):
		snake_case_				       =       MarkupLMFeatureExtractor if is_bsa_available() else None

		def    __lowercase       (  self		:			Dict    ):






			'''simple docstring'''
			UpperCAmelCase__					:  List[Any]					   =					MarkupLMFeatureExtractionTester(self    )

		@property
		def    __lowercase       (  self		:			List[Any]    ):






			'''simple docstring'''
			return self.feature_extract_tester.prepare_feat_extract_dict()




		def    __lowercase       (  self		:			Optional[int]    ):






			'''simple docstring'''
			UpperCAmelCase__					:  List[str]					   =					self.feature_extraction_class()

			# Test not batched input
			UpperCAmelCase__					:  List[str]					   =					get_html_strings()[0]
			UpperCAmelCase__					:  List[Any]					   =					feature_extractor(__SCREAMING_SNAKE_CASE    )

			# fmt: off
			UpperCAmelCase__					:  Optional[int]					   =					[["""sample document""", """Goog""", """This is one header""", """This is a another Header""", """Travel from""", """SFO to JFK""", """on May 2, 2015 at 2:00 pm. For details go to confirm.com""", """Traveler""", """name""", """is""", """John Doe"""]]
			UpperCAmelCase__					:  Any					   =					[["""/html/head/title""", """/html/body/a""", """/html/body/h1""", """/html/body/h2""", """/html/body/p""", """/html/body/p/p/b[1]""", """/html/body/p/p/b[2]/i""", """/html/body/p/p/div/h3""", """/html/body/p/p/div/h3/b""", """/html/body/p/p/div/h3""", """/html/body/p/p/div/h3/p"""]]
			# fmt: on

			self.assertEqual(encoding.nodes					,__SCREAMING_SNAKE_CASE    )
			self.assertEqual(encoding.xpaths					,__SCREAMING_SNAKE_CASE    )

			# Test batched
			UpperCAmelCase__					:  Dict					   =					get_html_strings()
			UpperCAmelCase__					:  Tuple					   =					feature_extractor(__SCREAMING_SNAKE_CASE    )

			# fmt: off
			UpperCAmelCase__					:  str					   =					expected_nodes + [["""My First Heading""", """My first paragraph."""]]
			UpperCAmelCase__					:  Dict					   =					expected_xpaths + [["""/html/body/h1""", """/html/body/p"""]]

			self.assertEqual(len(encoding.nodes    )					,2    )
			self.assertEqual(len(encoding.xpaths    )					,2    )

			self.assertEqual(encoding.nodes					,__SCREAMING_SNAKE_CASE    )
			self.assertEqual(encoding.xpaths					,__SCREAMING_SNAKE_CASE    )








"""simple docstring"""




from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
      from PIL import Image

      from ..image_utils import load_image

if is_torch_available():
      from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

__UpperCAmelCase =				logging.get_logger(__name__)







@add_end_docstrings(__lowerCamelCase					)
class      __lowercase			(    __lowerCamelCase					):

  def __init__(  self		:			List[str]					,*A		:			Optional[int]					,**A		:			List[str]    ):






   '''simple docstring'''
   super().__init__(*A					,**A    )
   self.check_model_type(A    )

  def    __lowercase       (  self		:			Optional[int]					,A		:			Dict=None					,A		:			Optional[int]=None					,A		:			Tuple=None					,**A		:			str    ):






   '''simple docstring'''
   UpperCAmelCase__   ,      UpperCAmelCase__					:  Any					   =					{}, {}
   if padding is not None:
    UpperCAmelCase__					:  Union[str, Any]					   =					padding
   if truncation is not None:
    UpperCAmelCase__					:  List[str]					   =					truncation
   if top_k is not None:
    UpperCAmelCase__					:  List[Any]					   =					top_k
   return preprocess_params, {}, postprocess_params

  def __call__(  self		:			List[Any]					,A		:			Union["Image.Image", str]					,A		:			str = None					,**A		:			Union[str, Any]    ):






   '''simple docstring'''
   if isinstance(A					,(Image.Image, str)    ) and isinstance(A					,A    ):
    UpperCAmelCase__					:  Any					   =					{"""image""": image, """question""": question}
   else:
    UpperCAmelCase__					:  Dict					   =					image
   UpperCAmelCase__					:  Dict					   =					super().__call__(A					,**A    )
   return results

  def    __lowercase       (  self		:			Optional[int]					,A		:			Optional[Any]					,A		:			Optional[Any]=False					,A		:			List[Any]=False    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					load_image(inputs["""image"""]    )
   UpperCAmelCase__					:  Optional[Any]					   =					self.tokenizer(
       inputs["""question"""]					,return_tensors=self.framework					,padding=A					,truncation=A    )
   UpperCAmelCase__					:  int					   =					self.image_processor(images=A					,return_tensors=self.framework    )
   model_inputs.update(A    )
   return model_inputs

  def    __lowercase       (  self		:			Dict					,A		:			Tuple    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					self.model(**A    )
   return model_outputs




  def    __lowercase       (  self		:			int					,A		:			List[Any]					,A		:			List[str]=5    ):






   '''simple docstring'''
   if top_k > self.model.config.num_labels:
    UpperCAmelCase__					:  Any					   =					self.model.config.num_labels

   if self.framework == "pt":
    UpperCAmelCase__					:  Optional[int]					   =					model_outputs.logits.sigmoid()[0]
    UpperCAmelCase__   ,      UpperCAmelCase__					:  Optional[int]					   =					probs.topk(A    )
   else:
    raise ValueError(f"Unsupported framework: {self.framework}"    )

   UpperCAmelCase__					:  List[str]					   =					scores.tolist()
   UpperCAmelCase__					:  Optional[int]					   =					ids.tolist()
   return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(A					,A    )]





