


import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
	from PIL import Image
else:






	class      UpperCamelCase:


						@staticmethod
						def 				SCREAMING_SNAKE_CASE_	(    *SCREAMING_SNAKE_CASE						:		Union[str, Any]  ,     **SCREAMING_SNAKE_CASE						:		Tuple    )      ->						str:




												'''simple docstring'''

												pass






@is_pipeline_test
@require_vision
@require_torch
class      UpperCamelCase(	unittest.TestCase ):
					snake_case_		:					Any    					=						MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Union[str, Any]  ,     SCREAMING_SNAKE_CASE						:		Tuple  ,     SCREAMING_SNAKE_CASE						:		List[Any]  ,     SCREAMING_SNAKE_CASE						:		Optional[int]    )      ->						Dict:




											'''simple docstring'''

											__snake_case				=		pipeline(
											    "zero-shot-object-detection"  ,     model="hf-internal-testing/tiny-random-owlvit-object-detection"    )

											__snake_case				=		[
											    {
											        "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
											        "candidate_labels": ["cat", "remote", "couch"],
											    }
											]
											return object_detector, examples


					def 				SCREAMING_SNAKE_CASE_	(    self						:		str  ,     SCREAMING_SNAKE_CASE						:		Any  ,     SCREAMING_SNAKE_CASE						:		Dict    )      ->						List[str]:




											'''simple docstring'''

											__snake_case				=		object_detector(examples[0]  ,     threshold=0.0    )

											__snake_case				=		len(SCREAMING_SNAKE_CASE    )
											self.assertGreater(SCREAMING_SNAKE_CASE  ,     0    )
											self.assertEqual(
											    SCREAMING_SNAKE_CASE  ,     [
											        {
											            "score": ANY(SCREAMING_SNAKE_CASE    ),
											            "label": ANY(SCREAMING_SNAKE_CASE    ),
											            "box": {"xmin": ANY(SCREAMING_SNAKE_CASE    ), "ymin": ANY(SCREAMING_SNAKE_CASE    ), "xmax": ANY(SCREAMING_SNAKE_CASE    ), "ymax": ANY(SCREAMING_SNAKE_CASE    )},
											        }
											        for i in range(SCREAMING_SNAKE_CASE    )
											    ]  ,     )


					@require_tf
					@unittest.skip("Zero Shot Object Detection not implemented in TF"    )
					def 				SCREAMING_SNAKE_CASE_	(    self						:		Dict    )      ->						Optional[Any]:




											'''simple docstring'''

											pass


					@require_torch
					def 				SCREAMING_SNAKE_CASE_	(    self						:		List[Any]    )      ->						str:




											'''simple docstring'''

											__snake_case				=		pipeline(
											    "zero-shot-object-detection"  ,     model="hf-internal-testing/tiny-random-owlvit-object-detection"    )

											__snake_case				=		object_detector(
											    "./tests/fixtures/tests_samples/COCO/000000039769.png"  ,     candidate_labels=["cat", "remote", "couch"]  ,     threshold=0.64  ,     )

											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE  ,     decimals=4    )  ,     [
											        {"score": 0.7235, "label": "cat", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
											        {"score": 0.7218, "label": "remote", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
											        {"score": 0.7184, "label": "couch", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
											        {"score": 0.6748, "label": "remote", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
											        {"score": 0.6656, "label": "cat", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
											        {"score": 0.6614, "label": "couch", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
											        {"score": 0.6456, "label": "remote", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
											        {"score": 0.642, "label": "remote", "box": {"xmin": 6_7, "ymin": 2_7_4, "xmax": 9_3, "ymax": 2_9_7}},
											        {"score": 0.6419, "label": "cat", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
											    ]  ,     )

											__snake_case				=		object_detector(
											    [
											        {
											            "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
											            "candidate_labels": ["cat", "remote", "couch"],
											        }
											    ]  ,     threshold=0.64  ,     )

											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE  ,     decimals=4    )  ,     [
											        [
											            {"score": 0.7235, "label": "cat", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
											            {"score": 0.7218, "label": "remote", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
											            {"score": 0.7184, "label": "couch", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
											            {"score": 0.6748, "label": "remote", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
											            {"score": 0.6656, "label": "cat", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
											            {"score": 0.6614, "label": "couch", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
											            {"score": 0.6456, "label": "remote", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
											            {"score": 0.642, "label": "remote", "box": {"xmin": 6_7, "ymin": 2_7_4, "xmax": 9_3, "ymax": 2_9_7}},
											            {"score": 0.6419, "label": "cat", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
											        ]
											    ]  ,     )


					@require_torch
					@slow
					def 				SCREAMING_SNAKE_CASE_	(    self						:		str    )      ->						Optional[int]:




											'''simple docstring'''

											__snake_case				=		pipeline("zero-shot-object-detection"    )

											__snake_case				=		object_detector(
											    "http://images.cocodataset.org/val2017/000000039769.jpg"  ,     candidate_labels=["cat", "remote", "couch"]  ,     )
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE  ,     decimals=4    )  ,     [
											        {"score": 0.2868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
											        {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
											        {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
											        {"score": 0.1474, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
											        {"score": 0.1208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
											    ]  ,     )

											__snake_case				=		object_detector(
											    [
											        {
											            "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
											            "candidate_labels": ["cat", "remote", "couch"],
											        },
											        {
											            "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
											            "candidate_labels": ["cat", "remote", "couch"],
											        },
											    ]  ,     )
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE  ,     decimals=4    )  ,     [
											        [
											            {"score": 0.2868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
											            {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
											            {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
											            {"score": 0.1474, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
											            {"score": 0.1208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
											        ],
											        [
											            {"score": 0.2868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
											            {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
											            {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
											            {"score": 0.1474, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
											            {"score": 0.1208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
											        ],
											    ]  ,     )


					@require_tf
					@unittest.skip("Zero Shot Object Detection not implemented in TF"    )
					def 				SCREAMING_SNAKE_CASE_	(    self						:		List[str]    )      ->						int:




											'''simple docstring'''

											pass


					@require_torch
					@slow
					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[int]    )      ->						List[str]:




											'''simple docstring'''

											__snake_case				=		0.2
											__snake_case				=		pipeline("zero-shot-object-detection"    )

											__snake_case				=		object_detector(
											    "http://images.cocodataset.org/val2017/000000039769.jpg"  ,     candidate_labels=["cat", "remote", "couch"]  ,     threshold=SCREAMING_SNAKE_CASE  ,     )
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE  ,     decimals=4    )  ,     [
											        {"score": 0.2868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
											        {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
											        {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
											    ]  ,     )






					@require_torch
					@slow
					def 				SCREAMING_SNAKE_CASE_	(    self						:		Dict    )      ->						List[str]:




											'''simple docstring'''

											__snake_case				=		2
											__snake_case				=		pipeline("zero-shot-object-detection"    )

											__snake_case				=		object_detector(
											    "http://images.cocodataset.org/val2017/000000039769.jpg"  ,     candidate_labels=["cat", "remote", "couch"]  ,     top_k=SCREAMING_SNAKE_CASE  ,     )
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE  ,     decimals=4    )  ,     [
											        {"score": 0.2868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
											        {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
											    ]  ,     )









import json
import os
import subprocess
import unittest
from ast import literal_eval

import pytest
from parameterized import parameterized_class

from . import is_sagemaker_available


if is_sagemaker_available():
	from sagemaker import Session, TrainingJobAnalytics
	from sagemaker.huggingface import HuggingFace






@pytest.mark.skipif(
    literal_eval(os.getenv("""TEST_SAGEMAKER"""  ,				"""False""" ) ) is not True  ,				reason="""Skipping test because should only be run when releasing minor transformers version"""  ,				)
@pytest.mark.usefixtures("""sm_env""" )
@parameterized_class(
    [
        {
            """framework""": """pytorch""",
            """script""": """run_glue.py""",
            """model_name_or_path""": """distilbert-base-cased""",
            """instance_type""": """ml.g4dn.xlarge""",
            """results""": {"""train_runtime""": 650, """eval_accuracy""": 0.6, """eval_loss""": 0.9},
        },
        {
            """framework""": """tensorflow""",
            """script""": """run_tf.py""",
            """model_name_or_path""": """distilbert-base-cased""",
            """instance_type""": """ml.g4dn.xlarge""",
            """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.3, """eval_loss""": 0.9},
        },
    ] )
class      UpperCamelCase(	unittest.TestCase ):


					def 				SCREAMING_SNAKE_CASE_	(    self						:		List[str]    )      ->						Dict:




											'''simple docstring'''

											if self.framework == "pytorch":
																	subprocess.run(
																	    f'''cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py'''.split()  ,     encoding="utf-8"  ,     check=SCREAMING_SNAKE_CASE  ,     )
											assert hasattr(self  ,     "env"    )


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Union[str, Any]  ,     SCREAMING_SNAKE_CASE						:		Any=1    )      ->						List[str]:




											'''simple docstring'''

											return HuggingFace(
											    entry_point=self.script  ,     source_dir=self.env.test_path  ,     role=self.env.role  ,     image_uri=self.env.image_uri  ,     base_job_name=f'''{self.env.base_job_name}-single'''  ,     instance_count=SCREAMING_SNAKE_CASE  ,     instance_type=self.instance_type  ,     debugger_hook_config=SCREAMING_SNAKE_CASE  ,     hyperparameters={**self.env.hyperparameters, "model_name_or_path": self.model_name_or_path}  ,     metric_definitions=self.env.metric_definitions  ,     py_version="py36"  ,     )


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[int]  ,     SCREAMING_SNAKE_CASE						:		Any    )      ->						List[str]:




											'''simple docstring'''

											TrainingJobAnalytics(SCREAMING_SNAKE_CASE    ).export_csv(f'''{self.env.test_path}/{job_name}_metrics.csv'''    )






					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[Any]    )      ->						Optional[int]:




											'''simple docstring'''

											__snake_case				=		self.create_estimator()

											# run training
											estimator.fit()

											# result dataframe
											__snake_case				=		TrainingJobAnalytics(estimator.latest_training_job.name    ).dataframe()

											# extract kpis
											__snake_case				=		list(result_metrics_df[result_metrics_df.metric_name == "eval_accuracy"]["value"]    )
											__snake_case				=		list(result_metrics_df[result_metrics_df.metric_name == "eval_loss"]["value"]    )
											# get train time from SageMaker job, this includes starting, preprocessing, stopping
											__snake_case				=		(
											    Session().describe_training_job(estimator.latest_training_job.name    ).get("TrainingTimeInSeconds"  ,     9_9_9_9_9_9    )
											)

											# assert kpis
											assert train_runtime <= self.results["train_runtime"]
											assert all(t >= self.results["eval_accuracy"] for t in eval_accuracy    )
											assert all(t <= self.results["eval_loss"] for t in eval_loss    )

											# dump tests result into json file to share in PR
											with open(f'''{estimator.latest_training_job.name}.json'''  ,     "w"    ) as outfile:
																	json.dump({"train_time": train_runtime, "eval_accuracy": eval_accuracy, "eval_loss": eval_loss}  ,     SCREAMING_SNAKE_CASE    )





