


# 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				(							__snake_case):


							"""simple docstring"""




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

							A__      		=    ["image", "text"]
							A__      		=    ["image"]





							def __init__(     self							:    int      ,   *__lowerCamelCase							:    Dict      ,   **__lowerCamelCase							:    Optional[int] ):


													'''simple docstring'''



													requires_backends(self      ,   ["vision"] )
													super().__init__(*__UpperCamelCase      ,   **__UpperCamelCase )





							def 		lowerCAmelCase  (     self							:    Any      ,   __lowerCamelCase							:    Dict      ,   __lowerCamelCase							:    Optional[int] ):


													'''simple docstring'''



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





							def 		lowerCAmelCase  (     self							:    Optional[int]      ,   __lowerCamelCase							:    int ):


													'''simple docstring'''



													with torch.no_grad():
																			lowerCamelCase__	:       Any					=						self.model(**__UpperCamelCase ).logits
													return logits





							def 		lowerCAmelCase  (     self							:    List[Any]      ,   __lowerCamelCase							:    List[str] ):


													'''simple docstring'''



													lowerCamelCase__	:       str					=						outputs.cpu().detach().numpy()
													lowerCamelCase__	:       List[str]					=						0
													lowerCamelCase__	:       Optional[int]					=						1
													return Image.fromarray((array * 255).astype(np.uinta ) )






def    lowercase_	(	_A    :					int					,      _A    :					int		):

      """simple docstring"""




      if a < 0 or b < 0:
            raise ValueError("the value of both inputs must be positive"		)

      lowerCamelCase__	:       List[str]					=						str(bin(_A		)		)[2:]  # remove the leading "0b"
      lowerCamelCase__	:       List[Any]					=						str(bin(_A		)		)[2:]  # remove the leading "0b"

      lowerCamelCase__	:       List[Any]					=						max(len(_A		)					,      len(_A		)		)

      return "0b" + "".join(
          str(int(char_a != char_b		)		)
          for char_a, char_b in zip(a_binary.zfill(_A		)					,      b_binary.zfill(_A		)		)		)


if __name__ == "__main__":
    import doctest

    doctest.testmod()


