





'''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


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

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




class       UpperCamelCase__			(			lowercase_	):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__						=					'''google/flan-t5-base'''
					SCREAMING_SNAKE_CASE__						=					(
					    '''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.'''
					)
					SCREAMING_SNAKE_CASE__						=					'''text_qa'''
					SCREAMING_SNAKE_CASE__						=					AutoTokenizer
					SCREAMING_SNAKE_CASE__						=					AutoModelForSeqaSeqLM

					SCREAMING_SNAKE_CASE__						=					['''text''', '''text''']
					SCREAMING_SNAKE_CASE__						=					['''text''']


					def lowerCamelCase_	(		self							:	Union[str, Any]     ,		lowerCamelCase_							:	str     ,		lowerCamelCase_							:	str		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Dict						=	QA_PROMPT.format(text=lowerCamelCase_     ,		question=lowerCamelCase_		)
							return self.pre_processor(lowerCamelCase_     ,		return_tensors="""pt"""		)


					def lowerCamelCase_	(		self							:	Any     ,		lowerCamelCase_							:	int		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Tuple						=	self.model.generate(**lowerCamelCase_		)

							SCREAMING_SNAKE_CASE,    SCREAMING_SNAKE_CASE				:							Dict						=	inputs["""input_ids"""].shape
							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	output_ids.shape[0]

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



					def lowerCamelCase_	(		self							:	Optional[Any]     ,		lowerCamelCase_							:	Optional[int]		):

							'''simple docstring'''



							return self.pre_processor.decode(lowerCamelCase_     ,		skip_special_tokens=lowerCamelCase_     ,		clean_up_tokenization_spaces=lowerCamelCase_		)










'''simple docstring'''




import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
							from PIL import Image




class       UpperCamelCase__			(			unittest.TestCase		,			lowercase_	):




					"""simple docstring"""


					def lowerCamelCase_	(		self							:	Any		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							int						=	load_tool("""image-captioning"""		)
							self.tool.setup()
							SCREAMING_SNAKE_CASE				:							str						=	load_tool("""image-captioning"""     ,		remote=lowerCamelCase_		)


					def lowerCamelCase_	(		self							:	List[Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""		)		) / """000000039769.png"""		)
							SCREAMING_SNAKE_CASE				:							Optional[int]						=	self.tool(lowerCamelCase_		)
							self.assertEqual(lowerCamelCase_     ,		"""two cats sleeping on a couch"""		)


					def lowerCamelCase_	(		self							:	str		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""		)		) / """000000039769.png"""		)
							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	self.remote_tool(lowerCamelCase_		)
							self.assertEqual(lowerCamelCase_     ,		"""two cats sleeping on a couch"""		)


					def lowerCamelCase_	(		self							:	List[Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""		)		) / """000000039769.png"""		)
							SCREAMING_SNAKE_CASE				:							str						=	self.tool(image=lowerCamelCase_		)
							self.assertEqual(lowerCamelCase_     ,		"""two cats sleeping on a couch"""		)



					def lowerCamelCase_	(		self							:	Any		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""		)		) / """000000039769.png"""		)
							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	self.remote_tool(image=lowerCamelCase_		)
							self.assertEqual(lowerCamelCase_     ,		"""two cats sleeping on a couch"""		)



