






'''simple docstring'''
import importlib.util
import os
import platform
from argparse import ArgumentParser

import huggingface_hub

from .. import __version__ as version
from ..utils import (
    is_accelerate_available,
    is_flax_available,
    is_safetensors_available,
    is_tf_available,
    is_torch_available,
)
from . import BaseTransformersCLICommand





def  _a(  UpperCamelCase__				:						str							):




      '''simple docstring'''






      return EnvironmentCommand()





def  _a(  UpperCamelCase__				:						str							):




      '''simple docstring'''






      return EnvironmentCommand(args.accelerate_config_file							)







class      __SCREAMING_SNAKE_CASE      (			lowerCamelCase				):




 @staticmethod
 def      __magic_name__	(      __lowercase	:							ArgumentParser      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=parser.add_parser('''env'''      )
       download_parser.set_defaults(func=__lowercase      )
       download_parser.add_argument(
           '''--accelerate-config_file'''					,      default=__lowercase					,      help='''The accelerate config file to use for the default values in the launching script.'''					,      )
       download_parser.set_defaults(func=__lowercase      )




 def __init__(      self	:							Union[str, Any]					,      __lowercase	:							Optional[Any]					,      *__lowercase	:							List[Any]      )	->						None:
       SCREAMING_SNAKE_CASE__     :				str									=accelerate_config_file




 def      __magic_name__	(      self	:							int      )	->						int:
       SCREAMING_SNAKE_CASE__     :				Dict									='''not installed'''
       if is_safetensors_available():
             import safetensors

             SCREAMING_SNAKE_CASE__     :				Tuple									=safetensors.__version__
       elif importlib.util.find_spec('''safetensors'''      ) is not None:
             import safetensors

             SCREAMING_SNAKE_CASE__     :				int									=F"{safetensors.__version__} but is ignored because of PyTorch version too old."

       SCREAMING_SNAKE_CASE__     :				Optional[int]									='''not installed'''
       SCREAMING_SNAKE_CASE__     :				Tuple									='''not found'''
       if is_accelerate_available():
             import accelerate
             from accelerate.commands.config import default_config_file, load_config_from_file

             SCREAMING_SNAKE_CASE__     :				Dict									=accelerate.__version__
             # Get the default from the config file.
             if self._accelerate_config_file is not None or os.path.isfile(__lowercase      ):
                   SCREAMING_SNAKE_CASE__     :				str									=load_config_from_file(self._accelerate_config_file      ).to_dict()

             SCREAMING_SNAKE_CASE__     :				Optional[Any]									=(
                 '''\n'''.join([F"\t- {prop}: {val}" for prop, val in accelerate_config.items()]      )
                 if isinstance(__lowercase					,      __lowercase      )
                 else F"\t{accelerate_config}"
             )

       SCREAMING_SNAKE_CASE__     :				Dict									='''not installed'''
       SCREAMING_SNAKE_CASE__     :				Dict									='''NA'''
       if is_torch_available():
             import torch

             SCREAMING_SNAKE_CASE__     :				str									=torch.__version__
             SCREAMING_SNAKE_CASE__     :				int									=torch.cuda.is_available()

       SCREAMING_SNAKE_CASE__     :				str									='''not installed'''
       SCREAMING_SNAKE_CASE__     :				Dict									='''NA'''
       if is_tf_available():
             import tensorflow as tf

             SCREAMING_SNAKE_CASE__     :				List[str]									=tf.__version__
             try:
                   # deprecated in v2.1
                   SCREAMING_SNAKE_CASE__     :				List[str]									=tf.test.is_gpu_available()
             except AttributeError:
                   # returns list of devices, convert to bool
                   SCREAMING_SNAKE_CASE__     :				int									=bool(tf.config.list_physical_devices('''GPU'''      )      )

       SCREAMING_SNAKE_CASE__     :				str									='''not installed'''
       SCREAMING_SNAKE_CASE__     :				List[str]									='''not installed'''
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									='''not installed'''
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									='''NA'''
       if is_flax_available():
             import flax
             import jax
             import jaxlib

             SCREAMING_SNAKE_CASE__     :				List[Any]									=flax.__version__
             SCREAMING_SNAKE_CASE__     :				int									=jax.__version__
             SCREAMING_SNAKE_CASE__     :				Optional[int]									=jaxlib.__version__
             SCREAMING_SNAKE_CASE__     :				int									=jax.lib.xla_bridge.get_backend().platform

       SCREAMING_SNAKE_CASE__     :				Dict									={
           '''`transformers` version''': version,
           '''Platform''': platform.platform(),
           '''Python version''': platform.python_version(),
           '''Huggingface_hub version''': huggingface_hub.__version__,
           '''Safetensors version''': F"{safetensors_version}",
           '''Accelerate version''': F"{accelerate_version}",
           '''Accelerate config''': F"{accelerate_config_str}",
           '''PyTorch version (GPU?)''': F"{pt_version} ({pt_cuda_available})",
           '''Tensorflow version (GPU?)''': F"{tf_version} ({tf_cuda_available})",
           '''Flax version (CPU?/GPU?/TPU?)''': F"{flax_version} ({jax_backend})",
           '''Jax version''': F"{jax_version}",
           '''JaxLib version''': F"{jaxlib_version}",
           '''Using GPU in script?''': '''<fill in>''',
           '''Using distributed or parallel set-up in script?''': '''<fill in>''',
       }

       print('''\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n'''      )
       print(self.format_dict(__lowercase      )      )

       return info




 @staticmethod
 def      __magic_name__	(      __lowercase	:							List[str]      )	->						str:
       return "\n".join([F"- {prop}: {val}" for prop, val in d.items()]      ) + "\n"







'''simple docstring'''
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      __SCREAMING_SNAKE_CASE      :




   @staticmethod
   def      __magic_name__	(      *__lowercase	:							int					,      **__lowercase	:							Optional[Any]      )	->						Optional[Any]:
         pass






@is_pipeline_test
@require_vision
class      __SCREAMING_SNAKE_CASE      (			unittest.TestCase				):




 @require_torch
 def      __magic_name__	(      self	:							Optional[int]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''					,      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Any									=image_classifier(__lowercase					,      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(__lowercase      )					,      [
               [{'''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'''}],
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				int									=image_classifier([image] * 5					,      candidate_labels=['''A''', '''B''', '''C''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
           ]					,      )




 @require_tf
 def      __magic_name__	(      self	:							Union[str, Any]      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''					,      framework='''tf'''      )
       SCREAMING_SNAKE_CASE__     :				Dict									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Tuple									=image_classifier(__lowercase					,      candidate_labels=['''a''', '''b''', '''c''']      )

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

       SCREAMING_SNAKE_CASE__     :				List[Any]									=image_classifier([image] * 5					,      candidate_labels=['''A''', '''B''', '''C''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
           ]					,      )




 @slow
 @require_torch
 def      __magic_name__	(      self	:							List[Any]      )	->						Dict:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=pipeline(
           task='''zero-shot-image-classification'''					,      model='''openai/clip-vit-base-patch32'''					,      )
       # This is an image of 2 cats with remotes and no planes
       SCREAMING_SNAKE_CASE__     :				Any									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=image_classifier(__lowercase					,      candidate_labels=['''cat''', '''plane''', '''remote''']      )

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

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=image_classifier([image] * 5					,      candidate_labels=['''cat''', '''plane''', '''remote''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.511, '''label''': '''remote'''},
                   {'''score''': 0.485, '''label''': '''cat'''},
                   {'''score''': 0.004, '''label''': '''plane'''},
               ],
           ]
           * 5					,      )




 @slow
 @require_tf
 def      __magic_name__	(      self	:							Union[str, Any]      )	->						int:
       SCREAMING_SNAKE_CASE__     :				str									=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
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Any									=image_classifier(__lowercase					,      candidate_labels=['''cat''', '''plane''', '''remote''']      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               {'''score''': 0.511, '''label''': '''remote'''},
               {'''score''': 0.485, '''label''': '''cat'''},
               {'''score''': 0.004, '''label''': '''plane'''},
           ]					,      )

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