


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 _snake_case		(						*UpperCAmelCase				:     str   ,  **UpperCAmelCase				:     Optional[int]):
						pass



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






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

				SCREAMING_SNAKE_CASE_				:Dict				     =						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 _snake_case		(						self				:     Union[str, Any]):
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						pipeline(
				    model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification"   ,  framework="tf")
				SCREAMING_SNAKE_CASE_				:List[str]				     =						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
				SCREAMING_SNAKE_CASE_				:Any				     =						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"}]   ,  )

				SCREAMING_SNAKE_CASE_				:List[Any]				     =						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 _snake_case		(						self				:     Tuple):
				SCREAMING_SNAKE_CASE_				: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_				:Dict				     =						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
				SCREAMING_SNAKE_CASE_				:Tuple				     =						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"},
				    ]   ,  )

				SCREAMING_SNAKE_CASE_				:Tuple				     =						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 _snake_case		(						self				:     str):
				SCREAMING_SNAKE_CASE_				:Union[str, 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
				SCREAMING_SNAKE_CASE_				:int				     =						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
				SCREAMING_SNAKE_CASE_				: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"},
				    ]   ,  )

				SCREAMING_SNAKE_CASE_				: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   ,  )










from multiprocessing import Lock, Pipe, Process

# lock used to ensure that two processes do not access a pipe at the same time
SCREAMING_SNAKE_CASE__     					=			Lock()


def     lowercase	(    a     , a     , a     , a     , a     , a     , a ):



		'''simple docstring'''




		global process_lock

		# we perform n swaps since after n swaps we know we are sorted
		# we *could* stop early if we are sorted already, but it takes as long to
		# find out we are sorted as it does to sort the list with this algorithm
		for i in range(0     , 10 ):
				if (i + position) % 2 == 0 and r_send is not None:
						# send your value to your right neighbor
						process_lock.acquire()
						r_send[1].send(a )
						process_lock.release()

						# receive your right neighbor's value
						process_lock.acquire()
						SCREAMING_SNAKE_CASE_				:str				     =						rr_cv[0].recv()
						process_lock.release()

						# take the lower value since you are on the left
						SCREAMING_SNAKE_CASE_				:int				     =						min(a     , a )
				elif (i + position) % 2 != 0 and l_send is not None:
						# send your value to your left neighbor
						process_lock.acquire()
						l_send[1].send(a )
						process_lock.release()

						# receive your left neighbor's value
						process_lock.acquire()
						SCREAMING_SNAKE_CASE_				:int				     =						lr_cv[0].recv()
						process_lock.release()

						# take the higher value since you are on the right
						SCREAMING_SNAKE_CASE_				:Dict				     =						max(a     , a )
    # after all swaps are performed, send the values back to main
		result_pipe[1].send(a )


def     lowercase	(    a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:Tuple				     =						[]
		SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						[]
		# initialize the list of pipes where the values will be retrieved
		for _ in arr:
				result_pipe.append(Pipe() )
		# creates the processes
		# the first and last process only have one neighbor so they are made outside
		# of the loop
		SCREAMING_SNAKE_CASE_				:str				     =						Pipe()
		SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						Pipe()
		process_array_.append(
		    Process(
		        target=a     , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0])     , ) )
		SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						temp_rs
		SCREAMING_SNAKE_CASE_				:Any				     =						temp_rr

		for i in range(1     , len(a ) - 1 ):
				SCREAMING_SNAKE_CASE_				:int				     =						Pipe()
				SCREAMING_SNAKE_CASE_				:Dict				     =						Pipe()
				process_array_.append(
				    Process(
				        target=a     , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i])     , ) )
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						temp_rs
				SCREAMING_SNAKE_CASE_				:int				     =						temp_rr

		process_array_.append(
		    Process(
		        target=a     , args=(
		            len(a ) - 1,
		            arr[len(a ) - 1],
		            temp_ls,
		            None,
		            temp_lr,
		            None,
		            result_pipe[len(a ) - 1],
		        )     , ) )

		# start the processes
		for p in process_array_:
				p.start()

		# wait for the processes to end and write their values to the list
		for p in range(0     , len(a ) ):
				SCREAMING_SNAKE_CASE_				:Tuple				     =						result_pipe[p][0].recv()
				process_array_[p].join()
		return arr


def     lowercase	(    ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:List[str]				     =						list(range(10     , 0     , -1 ) )
		print("Initial List" )
		print(*a )
		SCREAMING_SNAKE_CASE_				:int				     =						odd_even_transposition(a )
		print("Sorted List\n" )
		print(*a )


if __name__ == "__main__":
		main()






