





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       __A     (						unittest.TestCase    ):


				def __init__(		self   , UpperCAmelCase_							):
								lowerCamelCase	 =parent


				def _snake_case       (		self							):
								return {}





def      _lowercase					(						)						->  str:
				lowerCamelCase	 ="""<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>"""

				lowerCamelCase	 ="""
    <!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       __A     (						a    ,					unittest.TestCase    ):
				__A      =				MarkupLMFeatureExtractor if is_bsa_available() else None


				def _snake_case       (		self							):
								lowerCamelCase	 =MarkupLMFeatureExtractionTester(self							)


				@property
				def _snake_case       (		self							):
								return self.feature_extract_tester.prepare_feat_extract_dict()


				def _snake_case       (		self							):
								# Initialize feature_extractor
								lowerCamelCase	 =self.feature_extraction_class()

								# Test not batched input
								lowerCamelCase	 =get_html_strings()[0]
								lowerCamelCase	 =feature_extractor(UpperCAmelCase_							)

								# fmt: off
								lowerCamelCase	 =[["""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"""]]
								lowerCamelCase	 =[["""/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   , UpperCAmelCase_							)
								self.assertEqual(encoding.xpaths   , UpperCAmelCase_							)

								# Test batched
								lowerCamelCase	 =get_html_strings()
								lowerCamelCase	 =feature_extractor(UpperCAmelCase_							)

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

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

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












from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch

@require_torch
@require_tf
class       __A     (						a    ):


				def _snake_case       (		self							):
								lowerCamelCase	 =SMALL_MODEL_IDENTIFIER
								lowerCamelCase	 ="""pt"""
								lowerCamelCase	 ="""tf"""


				def _snake_case       (		self   , UpperCAmelCase_							):
								lowerCamelCase	 =AutoModel.from_pretrained(self.test_model							)
								model_pt.save_pretrained(UpperCAmelCase_							)


				def _snake_case       (		self   , UpperCAmelCase_							):
								lowerCamelCase	 =TFAutoModel.from_pretrained(self.test_model   , from_pt=UpperCAmelCase_							)
								model_tf.save_pretrained(UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 ="""mock_framework"""

								# Framework provided - return whatever the user provides
								lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model   , UpperCAmelCase_							)
								self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)

								# Local checkpoint and framework provided - return provided framework
								# PyTorch checkpoint
								with TemporaryDirectory() as local_pt_ckpt:
												self._setup_pt_ckpt(UpperCAmelCase_							)
												lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_   , UpperCAmelCase_							)
												self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)

								# TensorFlow checkpoint
								with TemporaryDirectory() as local_tf_ckpt:
												self._setup_tf_ckpt(UpperCAmelCase_							)
												lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_   , UpperCAmelCase_							)
												self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)


				def _snake_case       (		self							):
								# PyTorch checkpoint
								with TemporaryDirectory() as local_pt_ckpt:
												self._setup_pt_ckpt(UpperCAmelCase_							)
												lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_							)
												self.assertEqual(UpperCAmelCase_   , self.framework_pt							)

								# TensorFlow checkpoint
								with TemporaryDirectory() as local_tf_ckpt:
												self._setup_tf_ckpt(UpperCAmelCase_							)
												lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_							)
												self.assertEqual(UpperCAmelCase_   , self.framework_tf							)

								# Invalid local checkpoint
								with TemporaryDirectory() as local_invalid_ckpt:
												with self.assertRaises(UpperCAmelCase_							):
																lowerCamelCase	 =FeaturesManager.determine_framework(UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								with patch("""transformers.onnx.features.is_tf_available"""   , UpperCAmelCase_							):
												lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model							)
												self.assertEqual(UpperCAmelCase_   , self.framework_pt							)

								# PyTorch not in environment -> use TensorFlow
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								with patch("""transformers.onnx.features.is_torch_available"""   , UpperCAmelCase_							):
												lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model							)
												self.assertEqual(UpperCAmelCase_   , self.framework_tf							)

								# Both in environment -> use PyTorch
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								with patch("""transformers.onnx.features.is_tf_available"""   , UpperCAmelCase_							), patch(
								    """transformers.onnx.features.is_torch_available"""   , UpperCAmelCase_							):
												lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model							)
												self.assertEqual(UpperCAmelCase_   , self.framework_pt							)

								# Both not in environment -> raise error
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								lowerCamelCase	 =MagicMock(return_value=UpperCAmelCase_							)
								with patch("""transformers.onnx.features.is_tf_available"""   , UpperCAmelCase_							), patch(
								    """transformers.onnx.features.is_torch_available"""   , UpperCAmelCase_							):
												with self.assertRaises(UpperCAmelCase_							):
																lowerCamelCase	 =FeaturesManager.determine_framework(self.test_model							)





