



'''simple docstring'''





import unittest

from transformers import (
    MODEL_FOR_OBJECT_DETECTION_MAPPING,
    AutoFeatureExtractor,
    AutoModelForObjectDetection,
    ObjectDetectionPipeline,
    is_vision_available,
    pipeline,
)
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_pytesseract,
    require_tf,
    require_timm,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image
else:






 class        lowerCAmelCase_:
   '''simple docstring'''




   @staticmethod
   def 							UpperCAmelCase_	(    *__UpperCAmelCase  ,**__UpperCAmelCase      )    ->		Tuple:
       pass






@is_pipeline_test
@require_vision
@require_timm
@require_torch
class        lowerCAmelCase_(				unittest.TestCase     ):
  '''simple docstring'''




  __lowercase							:      Tuple				=      MODEL_FOR_OBJECT_DETECTION_MAPPING




  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase      )    ->		Dict:
      lowerCAmelCase__ :  Optional[int]							      =					ObjectDetectionPipeline(model=__UpperCAmelCase  ,image_processor=__UpperCAmelCase      )
      return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]




  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase  ,__UpperCAmelCase      )    ->		Tuple:
      lowerCAmelCase__ :  Any							      =					object_detector("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  ,threshold=0.0      )

      self.assertGreater(len(__UpperCAmelCase      )  ,0      )
      for detected_object in outputs:
          self.assertEqual(
              __UpperCAmelCase  ,{
                  """score""": ANY(__UpperCAmelCase      ),
                  """label""": ANY(__UpperCAmelCase      ),
                  """box""": {"""xmin""": ANY(__UpperCAmelCase      ), """ymin""": ANY(__UpperCAmelCase      ), """xmax""": ANY(__UpperCAmelCase      ), """ymax""": ANY(__UpperCAmelCase      )},
              }  ,)

      import datasets

      lowerCAmelCase__ :  Any							      =					datasets.load_dataset("""hf-internal-testing/fixtures_image_utils"""  ,"""image"""  ,split="""test"""      )

      lowerCAmelCase__ :  List[str]							      =					[
          Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      ),
          """http://images.cocodataset.org/val2017/000000039769.jpg""",
          # RGBA
          dataset[0]["""file"""],
          # LA
          dataset[1]["""file"""],
          # L
          dataset[2]["""file"""],
      ]
      lowerCAmelCase__ :  List[str]							      =					object_detector(__UpperCAmelCase  ,threshold=0.0      )

      self.assertEqual(len(__UpperCAmelCase      )  ,len(__UpperCAmelCase      )      )
      for outputs in batch_outputs:
          self.assertGreater(len(__UpperCAmelCase      )  ,0      )
          for detected_object in outputs:
              self.assertEqual(
                  __UpperCAmelCase  ,{
                      """score""": ANY(__UpperCAmelCase      ),
                      """label""": ANY(__UpperCAmelCase      ),
                      """box""": {"""xmin""": ANY(__UpperCAmelCase      ), """ymin""": ANY(__UpperCAmelCase      ), """xmax""": ANY(__UpperCAmelCase      ), """ymax""": ANY(__UpperCAmelCase      )},
                  }  ,)




  @require_tf
  @unittest.skip("""Object detection not implemented in TF"""      )
  def 							UpperCAmelCase_	(    self      )    ->		Dict:
      pass




  @require_torch
  def 							UpperCAmelCase_	(    self      )    ->		Dict:
      lowerCAmelCase__ :  str							      =					"""hf-internal-testing/tiny-detr-mobilenetsv3"""

      lowerCAmelCase__ :  List[Any]							      =					AutoModelForObjectDetection.from_pretrained(__UpperCAmelCase      )
      lowerCAmelCase__ :  Union[str, Any]							      =					AutoFeatureExtractor.from_pretrained(__UpperCAmelCase      )
      lowerCAmelCase__ :  str							      =					ObjectDetectionPipeline(model=__UpperCAmelCase  ,feature_extractor=__UpperCAmelCase      )

      lowerCAmelCase__ :  Tuple							      =					object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""  ,threshold=0.0      )

      self.assertEqual(
          nested_simplify(__UpperCAmelCase  ,decimals=4      )  ,[
              {"""score""": 0.3_3_7_6, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
              {"""score""": 0.3_3_7_6, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
          ]  ,)

      lowerCAmelCase__ :  List[Any]							      =					object_detector(
          [
              """http://images.cocodataset.org/val2017/000000039769.jpg""",
              """http://images.cocodataset.org/val2017/000000039769.jpg""",
          ]  ,threshold=0.0  ,)

      self.assertEqual(
          nested_simplify(__UpperCAmelCase  ,decimals=4      )  ,[
              [
                  {"""score""": 0.3_3_7_6, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
                  {"""score""": 0.3_3_7_6, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
              ],
              [
                  {"""score""": 0.3_3_7_6, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
                  {"""score""": 0.3_3_7_6, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
              ],
          ]  ,)




  @require_torch
  @slow
  def 							UpperCAmelCase_	(    self      )    ->		List[str]:
      lowerCAmelCase__ :  Dict							      =					"""facebook/detr-resnet-50"""

      lowerCAmelCase__ :  int							      =					AutoModelForObjectDetection.from_pretrained(__UpperCAmelCase      )
      lowerCAmelCase__ :  Any							      =					AutoFeatureExtractor.from_pretrained(__UpperCAmelCase      )
      lowerCAmelCase__ :  int							      =					ObjectDetectionPipeline(model=__UpperCAmelCase  ,feature_extractor=__UpperCAmelCase      )

      lowerCAmelCase__ :  int							      =					object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""      )
      self.assertEqual(
          nested_simplify(__UpperCAmelCase  ,decimals=4      )  ,[
              {"""score""": 0.9_9_8_2, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
              {"""score""": 0.9_9_6_0, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
              {"""score""": 0.9_9_5_5, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
              {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
              {"""score""": 0.9_9_8_7, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
          ]  ,)

      lowerCAmelCase__ :  Optional[int]							      =					object_detector(
          [
              """http://images.cocodataset.org/val2017/000000039769.jpg""",
              """http://images.cocodataset.org/val2017/000000039769.jpg""",
          ]      )
      self.assertEqual(
          nested_simplify(__UpperCAmelCase  ,decimals=4      )  ,[
              [
                  {"""score""": 0.9_9_8_2, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
                  {"""score""": 0.9_9_6_0, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
                  {"""score""": 0.9_9_5_5, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
                  {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
                  {"""score""": 0.9_9_8_7, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
              ],
              [
                  {"""score""": 0.9_9_8_2, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
                  {"""score""": 0.9_9_6_0, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
                  {"""score""": 0.9_9_5_5, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
                  {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
                  {"""score""": 0.9_9_8_7, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
              ],
          ]  ,)




  @require_torch
  @slow
  def 							UpperCAmelCase_	(    self      )    ->		List[Any]:
      lowerCAmelCase__ :  List[Any]							      =					"""facebook/detr-resnet-50"""

      lowerCAmelCase__ :  Optional[int]							      =					pipeline("""object-detection"""  ,model=__UpperCAmelCase      )

      lowerCAmelCase__ :  Dict							      =					object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""      )
      self.assertEqual(
          nested_simplify(__UpperCAmelCase  ,decimals=4      )  ,[
              {"""score""": 0.9_9_8_2, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
              {"""score""": 0.9_9_6_0, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
              {"""score""": 0.9_9_5_5, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
              {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
              {"""score""": 0.9_9_8_7, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
          ]  ,)

      lowerCAmelCase__ :  Optional[Any]							      =					object_detector(
          [
              """http://images.cocodataset.org/val2017/000000039769.jpg""",
              """http://images.cocodataset.org/val2017/000000039769.jpg""",
          ]      )
      self.assertEqual(
          nested_simplify(__UpperCAmelCase  ,decimals=4      )  ,[
              [
                  {"""score""": 0.9_9_8_2, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
                  {"""score""": 0.9_9_6_0, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
                  {"""score""": 0.9_9_5_5, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
                  {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
                  {"""score""": 0.9_9_8_7, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
              ],
              [
                  {"""score""": 0.9_9_8_2, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
                  {"""score""": 0.9_9_6_0, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
                  {"""score""": 0.9_9_5_5, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
                  {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
                  {"""score""": 0.9_9_8_7, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
              ],
          ]  ,)




  @require_torch
  @slow
  def 							UpperCAmelCase_	(    self      )    ->		int:
      lowerCAmelCase__ :  Optional[int]							      =					0.9_9_8_5
      lowerCAmelCase__ :  Optional[Any]							      =					"""facebook/detr-resnet-50"""

      lowerCAmelCase__ :  Optional[int]							      =					pipeline("""object-detection"""  ,model=__UpperCAmelCase      )

      lowerCAmelCase__ :  Optional[int]							      =					object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""  ,threshold=__UpperCAmelCase      )
      self.assertEqual(
          nested_simplify(__UpperCAmelCase  ,decimals=4      )  ,[
              {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
              {"""score""": 0.9_9_8_7, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
          ]  ,)






  @require_torch
  @require_pytesseract
  @slow
  def 							UpperCAmelCase_	(    self      )    ->		Union[str, Any]:
      lowerCAmelCase__ :  int							      =					"""Narsil/layoutlmv3-finetuned-funsd"""
      lowerCAmelCase__ :  Union[str, Any]							      =					0.9_9_9_3

      lowerCAmelCase__ :  int							      =					pipeline("""object-detection"""  ,model=__UpperCAmelCase  ,threshold=__UpperCAmelCase      )

      lowerCAmelCase__ :  Any							      =					object_detector(
          """https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png"""      )
      self.assertEqual(
          nested_simplify(__UpperCAmelCase  ,decimals=4      )  ,[
              {"""score""": 0.9_9_9_3, """label""": """I-ANSWER""", """box""": {"""xmin""": 294, """ymin""": 254, """xmax""": 343, """ymax""": 264}},
              {"""score""": 0.9_9_9_3, """label""": """I-ANSWER""", """box""": {"""xmin""": 294, """ymin""": 254, """xmax""": 343, """ymax""": 264}},
          ]  ,)







'''simple docstring'''





import os

from bleurt import score  # From: git+https://github.com/google-research/bleurt.git

import datasets


_lowerCAmelCase      						=	datasets.logging.get_logger(__name__)


_lowerCAmelCase      						=	'''\
@inproceedings{bleurt,
  title={BLEURT: Learning Robust Metrics for Text Generation},
  author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh},
  booktitle={ACL},
  year={2020},
  url={https://arxiv.org/abs/2004.04696}
}
'''

_lowerCAmelCase      						=	'''\
BLEURT a learnt evaluation metric for Natural Language Generation. It is built using multiple phases of transfer learning starting from a pretrained BERT model (Devlin et al. 2018)
and then employing another pre-training phrase using synthetic data. Finally it is trained on WMT human annotations. You may run BLEURT out-of-the-box or fine-tune
it for your specific application (the latter is expected to perform better).

See the project\'s README at https://github.com/google-research/bleurt#readme for more information.
'''

_lowerCAmelCase      						=	'''
BLEURT score.

Args:
    `predictions` (list of str): prediction/candidate sentences
    `references` (list of str): reference sentences
    `checkpoint` BLEURT checkpoint. Will default to BLEURT-tiny if None.

Returns:
    \'scores\': List of scores.
Examples:

    >>> predictions = ["hello there", "general kenobi"]
    >>> references = ["hello there", "general kenobi"]
    >>> bleurt = datasets.load_metric("bleurt")
    >>> results = bleurt.compute(predictions=predictions, references=references)
    >>> print([round(v, 2) for v in results["scores"]])
    [1.03, 1.04]
'''

_lowerCAmelCase      						=	{
    '''bleurt-tiny-128''': '''https://storage.googleapis.com/bleurt-oss/bleurt-tiny-128.zip''',
    '''bleurt-tiny-512''': '''https://storage.googleapis.com/bleurt-oss/bleurt-tiny-512.zip''',
    '''bleurt-base-128''': '''https://storage.googleapis.com/bleurt-oss/bleurt-base-128.zip''',
    '''bleurt-base-512''': '''https://storage.googleapis.com/bleurt-oss/bleurt-base-512.zip''',
    '''bleurt-large-128''': '''https://storage.googleapis.com/bleurt-oss/bleurt-large-128.zip''',
    '''bleurt-large-512''': '''https://storage.googleapis.com/bleurt-oss/bleurt-large-512.zip''',
    '''BLEURT-20-D3''': '''https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D3.zip''',
    '''BLEURT-20-D6''': '''https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D6.zip''',
    '''BLEURT-20-D12''': '''https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip''',
    '''BLEURT-20''': '''https://storage.googleapis.com/bleurt-oss-21/BLEURT-20.zip''',
}






@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION						, _KWARGS_DESCRIPTION     )
class        lowerCAmelCase_(				datasets.Metric     ):
  '''simple docstring'''




  def 							UpperCAmelCase_	(    self      )    ->		Union[str, Any]:
      return datasets.MetricInfo(
          description=_DESCRIPTION  ,citation=_CITATION  ,homepage="""https://github.com/google-research/bleurt"""  ,inputs_description=_KWARGS_DESCRIPTION  ,features=datasets.Features(
              {
                  """predictions""": datasets.Value("""string"""  ,id="""sequence"""      ),
                  """references""": datasets.Value("""string"""  ,id="""sequence"""      ),
              }      )  ,codebase_urls=["""https://github.com/google-research/bleurt"""]  ,reference_urls=["""https://github.com/google-research/bleurt""", """https://arxiv.org/abs/2004.04696"""]  ,)




  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase      )    ->		Tuple:
      # check that config name specifies a valid BLEURT model
      if self.config_name == "default":
          logger.warning(
              """Using default BLEURT-Base checkpoint for sequence maximum length 128. """
              """You can use a bigger model for better results with e.g.: datasets.load_metric('bleurt', 'bleurt-large-512')."""      )
          lowerCAmelCase__ :  str							      =					"""bleurt-base-128"""

      if self.config_name.lower() in CHECKPOINT_URLS:
          lowerCAmelCase__ :  Union[str, Any]							      =					self.config_name.lower()

      elif self.config_name.upper() in CHECKPOINT_URLS:
          lowerCAmelCase__ :  List[Any]							      =					self.config_name.upper()

      else:
          raise KeyError(
              F"""{self.config_name} model not found. You should supply the name of a model checkpoint for bleurt in {CHECKPOINT_URLS.keys()}"""      )

      # download the model checkpoint specified by self.config_name and set up the scorer
      lowerCAmelCase__ :  int							      =					dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name]      )
      lowerCAmelCase__ :  Dict							      =					score.BleurtScorer(os.path.join(__UpperCAmelCase  ,__UpperCAmelCase      )      )






  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase  ,__UpperCAmelCase      )    ->		Union[str, Any]:
      lowerCAmelCase__ :  Union[str, Any]							      =					self.scorer.score(references=__UpperCAmelCase  ,candidates=__UpperCAmelCase      )
      return {"scores": scores}


