
"""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 ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer
from .base import PipelineTool


A_						       =				"""Here is a text containing a lot of information: '''{text}'''.

Can you answer this question about the text: '{question}'"""




class    __lowerCamelCase     (   lowerCAmelCase ):
				a__:			List[str]									=						'google/flan-t5-base'
				a__:			Any									=						(
				    'This is a tool that answers questions related to a text. It takes two arguments named `text`, which is the '
				    'text where to find the answer, and `question`, which is the question, and returns the answer to the question.'
				)
				a__:			List[str]									=						'text_qa'
				a__:			Union[str, Any]									=						AutoTokenizer
				a__:			List[str]									=						AutoModelForSeqaSeqLM

				a__:			Union[str, Any]									=						['text', 'text']
				a__:			List[Any]									=						['text']




				def 					UpperCAmelCase__	(							self       ,	UpperCAmelCase       ,	UpperCAmelCase      ):
							lowerCamelCase_ 		=     QA_PROMPT.format(text=UpperCAmelCase       ,	question=UpperCAmelCase      )
							return self.pre_processor(UpperCAmelCase       ,	return_tensors='''pt'''      )




				def 					UpperCAmelCase__	(							self       ,	UpperCAmelCase      ):
							lowerCamelCase_ 		=     self.model.generate(**UpperCAmelCase      )

							lowerCamelCase_		,   lowerCamelCase_ 		=     inputs['''input_ids'''].shape
							lowerCamelCase_ 		=     output_ids.shape[0]

							return output_ids.reshape(UpperCAmelCase       ,	out_b // in_b       ,	*output_ids.shape[1:]      )[0][0]





				def 					UpperCAmelCase__	(							self       ,	UpperCAmelCase      ):
							return self.pre_processor.decode(UpperCAmelCase       ,	skip_special_tokens=UpperCAmelCase       ,	clean_up_tokenization_spaces=UpperCAmelCase      )





"""simple docstring"""


import copy

from transformers.configuration_utils import PretrainedConfig
from transformers.utils import logging


A_						       =				logging.get_logger(__name__)




class    __lowerCamelCase     (   lowerCAmelCase ):
				a__:			List[str]									=						'hybrid-clip'
				a__:			List[Any]									=						True




				def __init__(							self       ,	UpperCAmelCase=512       ,	**UpperCAmelCase      ):
							super().__init__(**UpperCAmelCase      )

							if "text_config" not in kwargs:
										raise ValueError('''`text_config` can not be `None`.'''      )

							if "vision_config" not in kwargs:
										raise ValueError('''`vision_config` can not be `None`.'''      )

							lowerCamelCase_ 		=     kwargs.pop('''text_config'''      )
							lowerCamelCase_ 		=     kwargs.pop('''vision_config'''      )

							lowerCamelCase_ 		=     text_config.pop('''model_type'''      )
							lowerCamelCase_ 		=     vision_config.pop('''model_type'''      )

							from transformers import AutoConfig

							lowerCamelCase_ 		=     AutoConfig.for_model(UpperCAmelCase       ,	**UpperCAmelCase      )

							if vision_model_type == "clip":
										lowerCamelCase_ 		=     AutoConfig.for_model(UpperCAmelCase       ,	**UpperCAmelCase      ).vision_config
							elif vision_model_type == "clip_vision_model":
										from transformers import CLIPVisionConfig

										lowerCamelCase_ 		=     CLIPVisionConfig(**UpperCAmelCase      )
							else:
										lowerCamelCase_ 		=     AutoConfig.for_model(UpperCAmelCase       ,	**UpperCAmelCase      )

							lowerCamelCase_ 		=     projection_dim
							lowerCamelCase_ 		=     1.0




				@classmethod
				def 					UpperCAmelCase__	(							cls       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	**UpperCAmelCase      ):
							return cls(text_config=text_config.to_dict()       ,	vision_config=vision_config.to_dict()       ,	**UpperCAmelCase      )





				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     copy.deepcopy(self.__dict__      )
							lowerCamelCase_ 		=     self.text_config.to_dict()
							lowerCamelCase_ 		=     self.vision_config.to_dict()
							lowerCamelCase_ 		=     self.__class__.model_type
							return output



