






"""simple docstring"""


from typing import Tuple, Union

from ...modeling_outputs import BackboneOutput
from ...modeling_utils import PreTrainedModel
from ...utils import is_timm_available, is_torch_available, requires_backends
from ...utils.backbone_utils import BackboneMixin
from .configuration_timm_backbone import TimmBackboneConfig


if is_timm_available():
		import timm


if is_torch_available():
		from torch import Tensor




class 						__UpperCamelCase     (     UpperCamelCase   ,     UpperCamelCase						):
					lowercase_    :				List[str]      =  """pixel_values"""
					lowercase_    :				int      =  False
					lowercase_    :				str      =  TimmBackboneConfig
					def __init__(							self							:	Optional[Any]     , UpperCAmelCase							:	Dict     , **UpperCAmelCase							:	Tuple   )	->       Dict:
											requires_backends(self     , 'timm'   )
											super().__init__(UpperCAmelCase   )
											lowerCAmelCase   :Dict						=			config

											if config.backbone is None:
																	raise ValueError('backbone is not set in the config. Please set it to a timm model name.'   )

											if config.backbone not in timm.list_models():
																	raise ValueError(f"""backbone {config.backbone} is not supported by timm."""   )

											if hasattr(UpperCAmelCase     , 'out_features'   ) and config.out_features is not None:
																	raise ValueError('out_features is not supported by TimmBackbone. Please use out_indices instead.'   )

											lowerCAmelCase   :Optional[int]						=			getattr(UpperCAmelCase     , 'use_pretrained_backbone'     , UpperCAmelCase   )
											if pretrained is None:
																	raise ValueError('use_pretrained_backbone is not set in the config. Please set it to True or False.'   )

											# We just take the final layer by default. This matches the default for the transformers models.
											lowerCAmelCase   :List[str]						=			config.out_indices if getattr(UpperCAmelCase     , 'out_indices'     , UpperCAmelCase   ) is not None else (-1,)

											lowerCAmelCase   :Tuple						=			timm.create_model(
											    config.backbone     , pretrained=UpperCAmelCase     , features_only=config.features_only     , in_chans=config.num_channels     , out_indices=UpperCAmelCase     , **UpperCAmelCase     , )
											# These are used to control the output of the model when called. If output_hidden_states is True, then
											# return_layers is modified to include all layers.
											lowerCAmelCase   :Tuple						=			self._backbone.return_layers
											lowerCAmelCase   :str						=			{layer['module']: str(UpperCAmelCase   ) for i, layer in enumerate(self._backbone.feature_info.info   )}
											super()._init_backbone(UpperCAmelCase   )
					@classmethod
					def        UpperCAmelCase__ (							cls							:	Union[str, Any]     , UpperCAmelCase							:	List[Any]     , *UpperCAmelCase							:	List[Any]     , **UpperCAmelCase							:	Tuple   )	->       int:
											requires_backends(cls     , ['vision', 'timm']   )
											from ...models.timm_backbone import TimmBackboneConfig

											lowerCAmelCase   :Optional[int]						=			kwargs.pop('config'     , TimmBackboneConfig()   )

											lowerCAmelCase   :Any						=			kwargs.pop('use_timm_backbone'     , UpperCAmelCase   )
											if not use_timm:
																	raise ValueError('use_timm_backbone must be True for timm backbones'   )

											lowerCAmelCase   :Union[str, Any]						=			kwargs.pop('num_channels'     , config.num_channels   )
											lowerCAmelCase   :Tuple						=			kwargs.pop('features_only'     , config.features_only   )
											lowerCAmelCase   :int						=			kwargs.pop('use_pretrained_backbone'     , config.use_pretrained_backbone   )
											lowerCAmelCase   :Tuple						=			kwargs.pop('out_indices'     , config.out_indices   )
											lowerCAmelCase   :Union[str, Any]						=			TimmBackboneConfig(
											    backbone=UpperCAmelCase     , num_channels=UpperCAmelCase     , features_only=UpperCAmelCase     , use_pretrained_backbone=UpperCAmelCase     , out_indices=UpperCAmelCase     , )
											return super()._from_config(UpperCAmelCase     , **UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	int     , UpperCAmelCase							:	List[str]   )	->       Union[str, Any]:
											pass
					def        UpperCAmelCase__ (							self							:	str     , UpperCAmelCase							:	List[str]     , UpperCAmelCase							:	Any=None     , UpperCAmelCase							:	List[Any]=None     , UpperCAmelCase							:	Optional[int]=None     , **UpperCAmelCase							:	List[str]   )	->       Union[BackboneOutput, Tuple[Tensor, ...]]:
											lowerCAmelCase   :Optional[Any]						=			return_dict if return_dict is not None else self.config.use_return_dict
											lowerCAmelCase   :Dict						=			(
											    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
											)
											lowerCAmelCase   :Dict						=			output_attentions if output_attentions is not None else self.config.output_attentions

											if output_attentions:
																	raise ValueError('Cannot output attentions for timm backbones at the moment'   )

											if output_hidden_states:
																	# We modify the return layers to include all the stages of the backbone
																	lowerCAmelCase   :Any						=			self._all_layers
																	lowerCAmelCase   :Optional[Any]						=			self._backbone(UpperCAmelCase     , **UpperCAmelCase   )
																	lowerCAmelCase   :Optional[int]						=			self._return_layers
																	lowerCAmelCase   :str						=			tuple(hidden_states[i] for i in self.out_indices   )
											else:
																	lowerCAmelCase   :Dict						=			self._backbone(UpperCAmelCase     , **UpperCAmelCase   )
																	lowerCAmelCase   :Optional[Any]						=			None

											lowerCAmelCase   :str						=			tuple(UpperCAmelCase   )
											lowerCAmelCase   :Dict						=			tuple(UpperCAmelCase   ) if hidden_states is not None else None

											if not return_dict:
																	lowerCAmelCase   :Optional[int]						=			(feature_maps,)
																	if output_hidden_states:
																							lowerCAmelCase   :Union[str, Any]						=			output + (hidden_states,)
																	return output

											return BackboneOutput(feature_maps=UpperCAmelCase     , hidden_states=UpperCAmelCase     , attentions=UpperCAmelCase   )







"""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.
from typing import TYPE_CHECKING

from ..models.auto import AutoModelForVisionaSeq
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
		from PIL import Image




class 						__UpperCamelCase     (     UpperCamelCase						):
					lowercase_    :				Optional[Any]      =  """Salesforce/blip-image-captioning-base"""
					lowercase_    :				Optional[int]      =  (
					    """This is a tool that generates a description of an image. It takes an input named `image` which should be the """
					    """image to caption, and returns a text that contains the description in English."""
					)
					lowercase_    :				int      =  """image_captioner"""
					lowercase_    :				int      =  AutoModelForVisionaSeq

					lowercase_    :				List[str]      =  ["""image"""]
					lowercase_    :				int      =  ["""text"""]
					def __init__(							self							:	Any     , *UpperCAmelCase							:	str     , **UpperCAmelCase							:	List[str]   )	->       Union[str, Any]:
											requires_backends(self     , ['vision']   )
											super().__init__(*UpperCAmelCase     , **UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	int     , UpperCAmelCase							:	"Image"   )	->       Union[str, Any]:
											return self.pre_processor(images=UpperCAmelCase     , return_tensors='pt'   )
					def        UpperCAmelCase__ (							self							:	int     , UpperCAmelCase							:	Optional[int]   )	->       Optional[int]:
											return self.model.generate(**UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	Dict     , UpperCAmelCase							:	int   )	->       Dict:
											return self.pre_processor.batch_decode(UpperCAmelCase     , skip_special_tokens=UpperCAmelCase   )[0].strip()