"""simple docstring"""




from __future__ import annotations

import unittest

from transformers import is_tf_available, is_torch_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow


if is_tf_available():
			from transformers import (
			    AutoConfig,
			    BertConfig,
			    GPTaConfig,
			    TaConfig,
			    TFAutoModel,
			    TFAutoModelForCausalLM,
			    TFAutoModelForMaskedLM,
			    TFAutoModelForPreTraining,
			    TFAutoModelForQuestionAnswering,
			    TFAutoModelForSeqaSeqLM,
			    TFAutoModelForSequenceClassification,
			    TFAutoModelWithLMHead,
			    TFBertForMaskedLM,
			    TFBertForPreTraining,
			    TFBertForQuestionAnswering,
			    TFBertForSequenceClassification,
			    TFBertModel,
			    TFGPTaLMHeadModel,
			    TFRobertaForMaskedLM,
			    TFTaForConditionalGeneration,
			)
			from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
			from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
			from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST

if is_torch_available():
			from transformers import (
			    AutoModel,
			    AutoModelForCausalLM,
			    AutoModelForMaskedLM,
			    AutoModelForPreTraining,
			    AutoModelForQuestionAnswering,
			    AutoModelForSeqaSeqLM,
			    AutoModelForSequenceClassification,
			    AutoModelWithLMHead,
			    BertForMaskedLM,
			    BertForPreTraining,
			    BertForQuestionAnswering,
			    BertForSequenceClassification,
			    BertModel,
			    GPTaLMHeadModel,
			    RobertaForMaskedLM,
			    TaForConditionalGeneration,
			)

@is_pt_tf_cross_test
class       a_      (     unittest.TestCase			):




					@slow
					def 						_SCREAMING_SNAKE_CASE						(	self						:     Optional[Any]     ):
							# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							for model_name in ["bert-base-uncased"]:
									lowerCAmelCase__        =   AutoConfig.from_pretrained(snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   TFAutoModel.from_pretrained(snake_case__  ,						from_pt=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   AutoModel.from_pretrained(snake_case__  ,						from_tf=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )




					@slow
					def 						_SCREAMING_SNAKE_CASE						(	self						:     Optional[Any]     ):
							# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							for model_name in ["bert-base-uncased"]:
									lowerCAmelCase__        =   AutoConfig.from_pretrained(snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   TFAutoModelForPreTraining.from_pretrained(snake_case__  ,						from_pt=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   AutoModelForPreTraining.from_pretrained(snake_case__  ,						from_tf=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )




					@slow
					def 						_SCREAMING_SNAKE_CASE						(	self						:     Optional[int]     ):
							for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCAmelCase__        =   AutoConfig.from_pretrained(snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   TFAutoModelForCausalLM.from_pretrained(snake_case__  ,						from_pt=snake_case__     )
									lowerCAmelCase__   ,					lowerCAmelCase__        =   TFAutoModelForCausalLM.from_pretrained(
									    snake_case__  ,						output_loading_info=snake_case__  ,						from_pt=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   AutoModelForCausalLM.from_pretrained(snake_case__  ,						from_tf=snake_case__     )
									lowerCAmelCase__   ,					lowerCAmelCase__        =   AutoModelForCausalLM.from_pretrained(
									    snake_case__  ,						output_loading_info=snake_case__  ,						from_tf=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )




					@slow
					def 						_SCREAMING_SNAKE_CASE						(	self						:     Dict     ):
							for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCAmelCase__        =   AutoConfig.from_pretrained(snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   TFAutoModelWithLMHead.from_pretrained(snake_case__  ,						from_pt=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   AutoModelWithLMHead.from_pretrained(snake_case__  ,						from_tf=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )




					@slow
					def 						_SCREAMING_SNAKE_CASE						(	self						:     Tuple     ):
							for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCAmelCase__        =   AutoConfig.from_pretrained(snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   TFAutoModelForMaskedLM.from_pretrained(snake_case__  ,						from_pt=snake_case__     )
									lowerCAmelCase__   ,					lowerCAmelCase__        =   TFAutoModelForMaskedLM.from_pretrained(
									    snake_case__  ,						output_loading_info=snake_case__  ,						from_pt=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   AutoModelForMaskedLM.from_pretrained(snake_case__  ,						from_tf=snake_case__     )
									lowerCAmelCase__   ,					lowerCAmelCase__        =   AutoModelForMaskedLM.from_pretrained(
									    snake_case__  ,						output_loading_info=snake_case__  ,						from_tf=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )




					@slow
					def 						_SCREAMING_SNAKE_CASE						(	self						:     int     ):
							for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCAmelCase__        =   AutoConfig.from_pretrained(snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   TFAutoModelForSeqaSeqLM.from_pretrained(snake_case__  ,						from_pt=snake_case__     )
									lowerCAmelCase__   ,					lowerCAmelCase__        =   TFAutoModelForSeqaSeqLM.from_pretrained(
									    snake_case__  ,						output_loading_info=snake_case__  ,						from_pt=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   AutoModelForSeqaSeqLM.from_pretrained(snake_case__  ,						from_tf=snake_case__     )
									lowerCAmelCase__   ,					lowerCAmelCase__        =   AutoModelForSeqaSeqLM.from_pretrained(
									    snake_case__  ,						output_loading_info=snake_case__  ,						from_tf=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )




					@slow
					def 						_SCREAMING_SNAKE_CASE						(	self						:     Any     ):
							# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							for model_name in ["bert-base-uncased"]:
									lowerCAmelCase__        =   AutoConfig.from_pretrained(snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   TFAutoModelForSequenceClassification.from_pretrained(snake_case__  ,						from_pt=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   AutoModelForSequenceClassification.from_pretrained(snake_case__  ,						from_tf=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )




					@slow
					def 						_SCREAMING_SNAKE_CASE						(	self						:     int     ):
							# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							for model_name in ["bert-base-uncased"]:
									lowerCAmelCase__        =   AutoConfig.from_pretrained(snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   TFAutoModelForQuestionAnswering.from_pretrained(snake_case__  ,						from_pt=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )

									lowerCAmelCase__        =   AutoModelForQuestionAnswering.from_pretrained(snake_case__  ,						from_tf=snake_case__     )
									self.assertIsNotNone(snake_case__     )
									self.assertIsInstance(snake_case__  ,						snake_case__     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     int     ):
							lowerCAmelCase__        =   TFAutoModelWithLMHead.from_pretrained(snake_case__  ,						from_pt=snake_case__     )
							self.assertIsInstance(snake_case__  ,						snake_case__     )
							self.assertEqual(model.num_parameters()  ,						14410     )
							self.assertEqual(model.num_parameters(only_trainable=snake_case__     )  ,						14410     )

							lowerCAmelCase__        =   AutoModelWithLMHead.from_pretrained(snake_case__  ,						from_tf=snake_case__     )
							self.assertIsInstance(snake_case__  ,						snake_case__     )
							self.assertEqual(model.num_parameters()  ,						14410     )
							self.assertEqual(model.num_parameters(only_trainable=snake_case__     )  ,						14410     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Any     ):
							lowerCAmelCase__        =   TFAutoModelWithLMHead.from_pretrained(snake_case__  ,						from_pt=snake_case__     )
							self.assertIsInstance(snake_case__  ,						snake_case__     )
							self.assertEqual(model.num_parameters()  ,						14410     )
							self.assertEqual(model.num_parameters(only_trainable=snake_case__     )  ,						14410     )

							lowerCAmelCase__        =   AutoModelWithLMHead.from_pretrained(snake_case__  ,						from_tf=snake_case__     )
							self.assertIsInstance(snake_case__  ,						snake_case__     )
							self.assertEqual(model.num_parameters()  ,						14410     )
							self.assertEqual(model.num_parameters(only_trainable=snake_case__     )  ,						14410     )





"""simple docstring"""




# 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       a_      (     __UpperCamelCase			):
					UpperCamelCase_ :	List[str]			=    (
					    "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."
					)
					UpperCamelCase_ :	str			=    "CIDAS/clipseg-rd64-refined"
					UpperCamelCase_ :	Any			=    "image_segmenter"
					UpperCamelCase_ :	Optional[Any]			=    CLIPSegForImageSegmentation

					UpperCamelCase_ :	List[str]			=    ["image", "text"]
					UpperCamelCase_ :	int			=    ["image"]




					def __init__(	self						:     Tuple  ,						*snake_case__						:     str  ,						**snake_case__						:     Optional[Any]     ):
							requires_backends(self  ,						["""vision"""]     )
							super().__init__(*snake_case__  ,						**snake_case__     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Optional[Any]  ,						snake_case__						:     "Image"  ,						snake_case__						:     str     ):
							return self.pre_processor(text=[label]  ,						images=[image]  ,						padding=snake_case__  ,						return_tensors="""pt"""     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Dict  ,						snake_case__						:     Tuple     ):
							with torch.no_grad():
									lowerCAmelCase__        =   self.model(**snake_case__     ).logits
							return logits




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Dict  ,						snake_case__						:     List[Any]     ):
							lowerCAmelCase__        =   outputs.cpu().detach().numpy()
							lowerCAmelCase__        =   0
							lowerCAmelCase__        =   1
							return Image.fromarray((array * 255).astype(np.uinta     )     )




