
"""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 __lowerCAmelCase				(					_lowercase      ):




			"""simple docstring"""







			snake_case	      =				(
			    "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."
			)
			snake_case	      =				"CIDAS/clipseg-rd64-refined"
			snake_case	      =				"image_segmenter"
			snake_case	      =				CLIPSegForImageSegmentation

			snake_case	      =				["image", "text"]
			snake_case	      =				["image"]




			def __init__(				self						:					Dict       ,   *_snake_case						:					Any       ,   **_snake_case						:					Tuple	)      -> Tuple:







								"""simple docstring"""
								requires_backends(self       ,   ["vision"]	)
								super().__init__(*_snake_case       ,   **_snake_case	)




			def      lowerCamelCase__     (				self						:					Union[str, Any]       ,   _snake_case						:					"Image"       ,   _snake_case						:					str	)      -> Optional[Any]:







								"""simple docstring"""
								return self.pre_processor(text=[label]       ,   images=[image]       ,   padding=_snake_case       ,   return_tensors="pt"	)




			def      lowerCamelCase__     (				self						:					Any       ,   _snake_case						:					Optional[int]	)      -> Any:







								"""simple docstring"""
								with torch.no_grad():
													A_				 =						self.model(**_snake_case	).logits
								return logits






			def      lowerCamelCase__     (				self						:					int       ,   _snake_case						:					List[str]	)      -> List[str]:







								"""simple docstring"""
								A_				 =						outputs.cpu().detach().numpy()
								A_				 =						0
								A_				 =						1
								return Image.fromarray((array * 255).astype(np.uinta	)	)






"""simple docstring"""





import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


UpperCamelCase_					:						int				= '''
Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.

In March 2021, Hugging Face raised $40 million in a Series B funding round.[3]

On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]
'''



class __lowerCAmelCase				(					unittest.TestCase     ,				_lowercase      ):




			"""simple docstring"""







			def      lowerCamelCase__     (				self						:					str	)      -> Dict:







								"""simple docstring"""
								A_				 =						load_tool("text-question-answering"	)
								self.tool.setup()
								A_				 =						load_tool("text-question-answering"       ,   remote=_snake_case	)




			def      lowerCamelCase__     (				self						:					Union[str, Any]	)      -> Any:







								"""simple docstring"""
								A_				 =						self.tool(_snake_case       ,   "What did Hugging Face do in April 2021?"	)
								self.assertEqual(_snake_case       ,   "launched the BigScience Research Workshop"	)




			def      lowerCamelCase__     (				self						:					Union[str, Any]	)      -> int:







								"""simple docstring"""
								A_				 =						self.remote_tool(_snake_case       ,   "What did Hugging Face do in April 2021?"	)
								self.assertEqual(_snake_case       ,   "launched the BigScience Research Workshop"	)




			def      lowerCamelCase__     (				self						:					Optional[Any]	)      -> Optional[Any]:







								"""simple docstring"""
								A_				 =						self.tool(text=_snake_case       ,   question="What did Hugging Face do in April 2021?"	)
								self.assertEqual(_snake_case       ,   "launched the BigScience Research Workshop"	)






			def      lowerCamelCase__     (				self						:					str	)      -> Optional[Any]:







								"""simple docstring"""
								A_				 =						self.remote_tool(text=_snake_case       ,   question="What did Hugging Face do in April 2021?"	)
								self.assertEqual(_snake_case       ,   "launched the BigScience Research Workshop"	)




