
"""simple docstring"""





from manim import *






class    _UpperCAmelCase(   lowerCamelCase     ):



		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  Rectangle(height=0.5    ,		width=0.5)
							_UpperCamelCase										=  Rectangle(height=0.25    ,		width=0.25)
							_UpperCamelCase										=  Rectangle(height=0.46    ,		width=0.46).set_stroke(width=0)

							_UpperCamelCase										=  [mem.copy() for i in range(6)]
							_UpperCamelCase										=  [mem.copy() for i in range(6)]
							_UpperCamelCase										=  VGroup(*__a).arrange(__a    ,		buff=0)
							_UpperCamelCase										=  VGroup(*__a).arrange(__a    ,		buff=0)
							_UpperCamelCase										=  VGroup(__a    ,		__a).arrange(__a    ,		buff=0)
							_UpperCamelCase										=  Text('''CPU'''    ,		font_size=24)
							_UpperCamelCase										=  Group(__a    ,		__a).arrange(__a    ,		buff=0.5    ,		aligned_edge=__a)
							cpu.move_to([-2.5, -0.5, 0])
							self.add(__a)

							_UpperCamelCase										=  [mem.copy() for i in range(4)]
							_UpperCamelCase										=  VGroup(*__a).arrange(__a    ,		buff=0)
							_UpperCamelCase										=  Text('''GPU'''    ,		font_size=24)
							_UpperCamelCase										=  Group(__a    ,		__a).arrange(__a    ,		buff=0.5    ,		aligned_edge=__a)
							gpu.move_to([-1, -1, 0])
							self.add(__a)

							_UpperCamelCase										=  [mem.copy() for i in range(6)]
							_UpperCamelCase										=  VGroup(*__a).arrange(__a    ,		buff=0)

							_UpperCamelCase										=  Text('''Model'''    ,		font_size=24)
							_UpperCamelCase										=  Group(__a    ,		__a).arrange(__a    ,		buff=0.5    ,		aligned_edge=__a)
							model.move_to([3, -1.0, 0])
							self.add(__a)

							_UpperCamelCase										=  []
							_UpperCamelCase										=  []
							_UpperCamelCase										=  []

							for i, rect in enumerate(__a):
												rect.set_stroke(__a)

												_UpperCamelCase										=  Rectangle(height=0.46 / 4    ,		width=0.46 / 3).set_stroke(width=0.0).set_fill(__a    ,		opacity=0.7)

												if i == 0:
																	cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT)    ,		buff=0.02    ,		direction=__a)
																	cpu_target.set_x(cpu_target.get_x() + 0.1)
												elif i == 3:
																	cpu_target.next_to(model_cpu_arr[0]    ,		direction=__a    ,		buff=0.0)
												else:
																	cpu_target.next_to(model_cpu_arr[i - 1]    ,		direction=__a    ,		buff=0.0)
												self.add(__a)
												model_cpu_arr.append(__a)

							self.add(*__a    ,		*__a    ,		*__a)

							_UpperCamelCase										=  [mem.copy() for i in range(6)]
							_UpperCamelCase										=  VGroup(*__a).arrange(__a    ,		buff=0)

							_UpperCamelCase										=  Text('''Loaded Checkpoint'''    ,		font_size=24)
							_UpperCamelCase										=  Group(__a    ,		__a).arrange(__a    ,		buff=0.5    ,		aligned_edge=__a)
							checkpoint.move_to([3, 0.5, 0])

							self.add(__a)

							_UpperCamelCase										=  []
							_UpperCamelCase										=  []

							for i, rect in enumerate(__a):
												_UpperCamelCase										=  fill.copy().set_fill(__a    ,		opacity=0.7)
												target.move_to(__a)
												ckpt_arr.append(__a)

												_UpperCamelCase										=  target.copy()
												if i < 5:
																	cpu_target.move_to(cpu_left_col_base[i + 1])
												else:
																	cpu_target.move_to(cpu_right_col_base[i - 5])
												ckpt_cpu_arr.append(__a)
							self.add(*__a    ,		*__a)

							_UpperCamelCase										=  Square(side_length=2.2)
							key.move_to([-5, 2, 0])

							_UpperCamelCase										=  MarkupText(
							    F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''    ,		font_size=18    ,		)

							key_text.move_to([-5, 2.4, 0])

							self.add(__a    ,		__a)

							_UpperCamelCase										=  MarkupText(
							    F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''    ,		font_size=18    ,		)

							blue_text.next_to(__a    ,		DOWN * 2.4    ,		aligned_edge=key_text.get_left())
							self.add(__a)

							_UpperCamelCase										=  MarkupText(
							    F'''Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.'''    ,		font_size=24    ,		)
							step_a.move_to([2, 2, 0])

							_UpperCamelCase										=  [meta_mem.copy() for i in range(6)]
							_UpperCamelCase										=  [meta_mem.copy() for i in range(6)]
							_UpperCamelCase										=  VGroup(*__a).arrange(__a    ,		buff=0)
							_UpperCamelCase										=  VGroup(*__a).arrange(__a    ,		buff=0)
							_UpperCamelCase										=  VGroup(__a    ,		__a).arrange(__a    ,		buff=0)
							_UpperCamelCase										=  Text('''Disk'''    ,		font_size=24)
							_UpperCamelCase										=  Group(__a    ,		__a).arrange(__a    ,		buff=0.5    ,		aligned_edge=__a)
							disk.move_to([-4.0, -1.25, 0])
							self.play(Write(__a    ,		run_time=3)    ,		Write(__a    ,		run_time=1)    ,		Create(__a    ,		run_time=1))

							_UpperCamelCase										=  []
							for i, rect in enumerate(__a):
												_UpperCamelCase										=  rect.copy()
												target.generate_target()
												target.target.move_to(disk_left_col_base[i]).scale(0.5)
												animations.append(MoveToTarget(__a    ,		run_time=1.5))
							self.play(*__a)

							self.play(FadeOut(__a))

							_UpperCamelCase										=  MarkupText(F'''Then, the checkpoint is removed from memory\nthrough garbage collection.'''    ,		font_size=24)
							step_a.move_to([2, 2, 0])

							self.play(Write(__a    ,		run_time=3))

							self.play(
							    FadeOut(__a    ,		__a    ,		*__a    ,		*__a)    ,		)

							self.wait()








"""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    _UpperCAmelCase(   lowerCamelCase     ):
		lowercase__      =				(
		    '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.'
		)
		lowercase__      =				'CIDAS/clipseg-rd64-refined'
		lowercase__      =				'image_segmenter'
		lowercase__      =				CLIPSegForImageSegmentation

		lowercase__      =				['image', 'text']
		lowercase__      =				['image']



		def __init__( self    ,		*__a    ,		**__a)	->	Any:






							'''simple docstring'''
							requires_backends(self    ,		['''vision'''])
							super().__init__(*__a    ,		**__a)



		def 						UpperCAmelCase							( self    ,		__a    ,		__a)	->	Dict:






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



		def 						UpperCAmelCase							( self    ,		__a)	->	Tuple:






							'''simple docstring'''
							with torch.no_grad():
												_UpperCamelCase										=  self.model(**__a).logits
							return logits




		def 						UpperCAmelCase							( self    ,		__a)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  outputs.cpu().detach().numpy()
							_UpperCamelCase										=  0
							_UpperCamelCase										=  1
							return Image.fromarray((array * 2_55).astype(np.uinta))






