





import unittest

from transformers import is_vision_available
from transformers.pipelines import 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 		_lowerCamelCase      (		*a__      ,       **a__       ):
															pass

@is_pipeline_test
@require_vision
class    _UpperCAmelCase							(		unittest.TestCase     ):
						@require_torch
						def 		_lowerCamelCase      (		self       ):
									A_     :				Any			      =	pipeline(
									    model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification"""      ,       )
									A_     :				Any			      =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
									A_     :				int			      =	image_classifier(UpperCAmelCase__      ,       candidate_labels=["""a""", """b""", """c"""]       )

									# The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
									# python and torch versions.
									self.assertIn(
									    nested_simplify(UpperCAmelCase__       )      ,       [
									        [{"""score""": 0.333, """label""": """a"""}, {"""score""": 0.333, """label""": """b"""}, {"""score""": 0.333, """label""": """c"""}],
									        [{"""score""": 0.333, """label""": """a"""}, {"""score""": 0.333, """label""": """c"""}, {"""score""": 0.333, """label""": """b"""}],
									    ]      ,       )

									A_     :				List[str]			      =	image_classifier([image] * 5      ,       candidate_labels=["""A""", """B""", """C"""]      ,       batch_size=2       )
									self.assertEqual(
									    nested_simplify(UpperCAmelCase__       )      ,       [
									        [
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									        ],
									        [
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									        ],
									        [
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									        ],
									        [
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									        ],
									        [
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									        ],
									    ]      ,       )
						@require_tf
						def 		_lowerCamelCase      (		self       ):
									A_     :				str			      =	pipeline(
									    model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification"""      ,       framework="""tf"""       )
									A_     :				Any			      =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
									A_     :				int			      =	image_classifier(UpperCAmelCase__      ,       candidate_labels=["""a""", """b""", """c"""]       )

									self.assertEqual(
									    nested_simplify(UpperCAmelCase__       )      ,       [{"""score""": 0.333, """label""": """a"""}, {"""score""": 0.333, """label""": """b"""}, {"""score""": 0.333, """label""": """c"""}]      ,       )

									A_     :				str			      =	image_classifier([image] * 5      ,       candidate_labels=["""A""", """B""", """C"""]      ,       batch_size=2       )
									self.assertEqual(
									    nested_simplify(UpperCAmelCase__       )      ,       [
									        [
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									        ],
									        [
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									        ],
									        [
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									        ],
									        [
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									        ],
									        [
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									            {"""score""": 0.333, """label""": ANY(UpperCAmelCase__       )},
									        ],
									    ]      ,       )
						@slow
						@require_torch
						def 		_lowerCamelCase      (		self       ):
									A_     :				Dict			      =	pipeline(
									    task="""zero-shot-image-classification"""      ,       model="""openai/clip-vit-base-patch32"""      ,       )
									# This is an image of 2 cats with remotes and no planes
									A_     :				Tuple			      =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
									A_     :				List[str]			      =	image_classifier(UpperCAmelCase__      ,       candidate_labels=["""cat""", """plane""", """remote"""]       )

									self.assertEqual(
									    nested_simplify(UpperCAmelCase__       )      ,       [
									        {"""score""": 0.511, """label""": """remote"""},
									        {"""score""": 0.485, """label""": """cat"""},
									        {"""score""": 0.004, """label""": """plane"""},
									    ]      ,       )

									A_     :				Dict			      =	image_classifier([image] * 5      ,       candidate_labels=["""cat""", """plane""", """remote"""]      ,       batch_size=2       )
									self.assertEqual(
									    nested_simplify(UpperCAmelCase__       )      ,       [
									        [
									            {"""score""": 0.511, """label""": """remote"""},
									            {"""score""": 0.485, """label""": """cat"""},
									            {"""score""": 0.004, """label""": """plane"""},
									        ],
									    ]
									    * 5      ,       )






						@slow
						@require_tf
						def 		_lowerCamelCase      (		self       ):
									A_     :				Any			      =	pipeline(
									    task="""zero-shot-image-classification"""      ,       model="""openai/clip-vit-base-patch32"""      ,       framework="""tf"""       )
									# This is an image of 2 cats with remotes and no planes
									A_     :				List[str]			      =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
									A_     :				List[str]			      =	image_classifier(UpperCAmelCase__      ,       candidate_labels=["""cat""", """plane""", """remote"""]       )
									self.assertEqual(
									    nested_simplify(UpperCAmelCase__       )      ,       [
									        {"""score""": 0.511, """label""": """remote"""},
									        {"""score""": 0.485, """label""": """cat"""},
									        {"""score""": 0.004, """label""": """plane"""},
									    ]      ,       )

									A_     :				List[Any]			      =	image_classifier([image] * 5      ,       candidate_labels=["""cat""", """plane""", """remote"""]      ,       batch_size=2       )
									self.assertEqual(
									    nested_simplify(UpperCAmelCase__       )      ,       [
									        [
									            {"""score""": 0.511, """label""": """remote"""},
									            {"""score""": 0.485, """label""": """cat"""},
									            {"""score""": 0.004, """label""": """plane"""},
									        ],
									    ]
									    * 5      ,       )













# coding=utf-8
# Copyright 2020 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# this script dumps information about the environment

import os
import sys

import transformers


_lowerCAmelCase 				=     """3"""

print("""Python version:""", sys.version)
print("""transformers version:""", transformers.__version__)

try:
      import torch

      print("""Torch version:""", torch.__version__)
      print("""Cuda available:""", torch.cuda.is_available())
      print("""Cuda version:""", torch.version.cuda)
      print("""CuDNN version:""", torch.backends.cudnn.version())
      print("""Number of GPUs available:""", torch.cuda.device_count())
      print("""NCCL version:""", torch.cuda.nccl.version())
except ImportError:
      print("""Torch version:""", None)

try:
      import deepspeed

      print("""DeepSpeed version:""", deepspeed.__version__)
except ImportError:
      print("""DeepSpeed version:""", None)

try:
      import tensorflow as tf

      print("""TensorFlow version:""", tf.__version__)
      print("""TF GPUs available:""", bool(tf.config.list_physical_devices("""GPU""")))
      print("""Number of TF GPUs available:""", len(tf.config.list_physical_devices("""GPU""")))
except ImportError:
      print("""TensorFlow version:""", None)






