


# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# 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.
import numpy as np
import torch

from ..models.clipseg import CLIPSegForImageSegmentation
from ..utils import is_vision_available, requires_backends
from .base import PipelineTool


if is_vision_available():
    from PIL import Image

class 							lowercase  (						_UpperCAmelCase			):
     lowerCamelCase			:			Union[str, Any]      = (
         '''This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.'''
         '''It takes two arguments named `image` which should be the original image, and `label` which should be a text '''
         '''describing the elements what should be identified in the segmentation mask. The tool returns the mask.'''
     )
     lowerCamelCase			:			int      = '''CIDAS/clipseg-rd64-refined'''
     lowerCamelCase			:			Optional[int]      = '''image_segmenter'''
     lowerCamelCase			:			int      = CLIPSegForImageSegmentation

     lowerCamelCase			:			Tuple      = ['''image''', '''text''']
     lowerCamelCase			:			List[str]      = ['''image''']







     def __init__(						self :						Optional[Any]   ,   *_lowercase :						List[Any]   ,   **_lowercase :						List[str]      ):
           requires_backends(self   ,   ['''vision''']      )
           super().__init__(*_lowercase   ,   **_lowercase      )







     def 							lowercase__       (						self :						str   ,   _lowercase :						"Image"   ,   _lowercase :						str      ):
           return self.pre_processor(text=[label]   ,   images=[image]   ,   padding=_lowercase   ,   return_tensors='''pt'''      )







     def 							lowercase__       (						self :						int   ,   _lowercase :						Dict      ):
           with torch.no_grad():
                 SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		self.model(**_lowercase      ).logits
           return logits







     def 							lowercase__       (						self :						Tuple   ,   _lowercase :						str      ):
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		outputs.cpu().detach().numpy()
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		0
           SCREAMING_SNAKE_CASE__       :  Tuple						     =		1
           return Image.fromarray((array * 2_55).astype(np.uinta      )      )











'''simple docstring'''
import unittest

from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow


if is_flax_available():
					import jax

					from transformers.models.auto.modeling_flax_auto import FlaxAutoModel
					from transformers.models.bert.modeling_flax_bert import FlaxBertModel
					from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel






@require_flax
class 				lowercase__      (				unittest.TestCase  ):





	@slow
	def       UpperCamelCase_    (					self   :							Union[str, Any]					):







							'''simple docstring'''
							for model_name in ["bert-base-cased", "bert-large-uncased"]:
													with self.subTest(lowerCamelCase__					):
																			_UpperCamelCase   :						Optional[int]												=  AutoConfig.from_pretrained(lowerCamelCase__					)
																			self.assertIsNotNone(lowerCamelCase__					)
																			self.assertIsInstance(lowerCamelCase__					,lowerCamelCase__					)

																			_UpperCamelCase   :						Dict												=  FlaxAutoModel.from_pretrained(lowerCamelCase__					)
																			self.assertIsNotNone(lowerCamelCase__					)
																			self.assertIsInstance(lowerCamelCase__					,lowerCamelCase__					)





	@slow
	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							for model_name in ["roberta-base", "roberta-large"]:
													with self.subTest(lowerCamelCase__					):
																			_UpperCamelCase   :						Any												=  AutoConfig.from_pretrained(lowerCamelCase__					)
																			self.assertIsNotNone(lowerCamelCase__					)
																			self.assertIsInstance(lowerCamelCase__					,lowerCamelCase__					)

																			_UpperCamelCase   :						Any												=  FlaxAutoModel.from_pretrained(lowerCamelCase__					)
																			self.assertIsNotNone(lowerCamelCase__					)
																			self.assertIsInstance(lowerCamelCase__					,lowerCamelCase__					)





	@slow
	def       UpperCamelCase_    (					self   :							Union[str, Any]					):







							'''simple docstring'''
							for model_name in ["bert-base-cased", "bert-large-uncased"]:
													_UpperCamelCase   :						Optional[int]												=  AutoTokenizer.from_pretrained(lowerCamelCase__					)
													_UpperCamelCase   :						List[Any]												=  FlaxBertModel.from_pretrained(lowerCamelCase__					)
													_UpperCamelCase   :						Optional[int]												=  tokenizer('Do you support jax jitted function?'					,return_tensors=TensorType.JAX					)

													@jax.jit
													def eval(**lowerCamelCase__   :							Union[str, Any]					):
																			return model(**lowerCamelCase__					)

													eval(**lowerCamelCase__					).block_until_ready()





	@slow
	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							for model_name in ["roberta-base", "roberta-large"]:
													_UpperCamelCase   :						Optional[int]												=  AutoTokenizer.from_pretrained(lowerCamelCase__					)
													_UpperCamelCase   :						Tuple												=  FlaxRobertaModel.from_pretrained(lowerCamelCase__					)
													_UpperCamelCase   :						Optional[Any]												=  tokenizer('Do you support jax jitted function?'					,return_tensors=TensorType.JAX					)

													@jax.jit
													def eval(**lowerCamelCase__   :							Union[str, Any]					):
																			return model(**lowerCamelCase__					)

													eval(**lowerCamelCase__					).block_until_ready()





	def       UpperCamelCase_    (					self   :							Union[str, Any]					):







							'''simple docstring'''
							with self.assertRaisesRegex(
							    lowerCamelCase__					,'bert-base is not a local folder and is not a valid model identifier'					):
													_UpperCamelCase   :						int												=  FlaxAutoModel.from_pretrained('bert-base'					)





	def       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							with self.assertRaisesRegex(
							    lowerCamelCase__					,R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'					):
													_UpperCamelCase   :						Tuple												=  FlaxAutoModel.from_pretrained(lowerCamelCase__					,revision='aaaaaa'					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							with self.assertRaisesRegex(
							    lowerCamelCase__					,'hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack'					,):
													_UpperCamelCase   :						List[Any]												=  FlaxAutoModel.from_pretrained('hf-internal-testing/config-no-model'					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							with self.assertRaisesRegex(lowerCamelCase__					,'Use `from_pt=True` to load this model'					):
													_UpperCamelCase   :						Tuple												=  FlaxAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only'					)


