




"""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  SCREAMING_SNAKE_CASE__   (   A__   ):






						"""simple docstring"""




						_a        =  'google/flan-t5-base'
						_a        =  (
						    '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        =  'text_qa'
						_a        =  AutoTokenizer
						_a        =  AutoModelForSeqaSeqLM

						_a        =  ['text', 'text']
						_a        =  ['text']


						def 					__lowercase	(							self			:     Any  ,  lowerCAmelCase			:     str  ,  lowerCAmelCase			:     str   ):
										lowerCAmelCase		=			QA_PROMPT.format(text=_A  ,  question=_A   )
										return self.pre_processor(_A  ,  return_tensors="""pt"""   )


						def 					__lowercase	(							self			:     Union[str, Any]  ,  lowerCAmelCase			:     Tuple   ):
										lowerCAmelCase		=			self.model.generate(**_A   )

										lowerCAmelCase		=			inputs["input_ids"].shape
										lowerCAmelCase		=			output_ids.shape[0]

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




						def 					__lowercase	(							self			:     str  ,  lowerCAmelCase			:     Union[str, Any]   ):
										return self.pre_processor.decode(_A  ,  skip_special_tokens=_A  ,  clean_up_tokenization_spaces=_A   )











"""simple docstring"""







import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import OwlViTImageProcessor






class  SCREAMING_SNAKE_CASE__   (   unittest.TestCase   ):


						def __init__(							self			:     int  ,  lowerCAmelCase			:     Optional[int]  ,  lowerCAmelCase			:     Optional[Any]=7  ,  lowerCAmelCase			:     str=3  ,  lowerCAmelCase			:     str=18  ,  lowerCAmelCase			:     Tuple=30  ,  lowerCAmelCase			:     Union[str, Any]=400  ,  lowerCAmelCase			:     List[str]=True  ,  lowerCAmelCase			:     List[str]=None  ,  lowerCAmelCase			:     int=True  ,  lowerCAmelCase			:     int=None  ,  lowerCAmelCase			:     Optional[int]=True  ,  lowerCAmelCase			:     Dict=[0.4814_5466, 0.457_8275, 0.4082_1073]  ,  lowerCAmelCase			:     str=[0.2686_2954, 0.2613_0258, 0.2757_7711]  ,  lowerCAmelCase			:     Any=True  ,  ):
										lowerCAmelCase		=			parent
										lowerCAmelCase		=			batch_size
										lowerCAmelCase		=			num_channels
										lowerCAmelCase		=			image_size
										lowerCAmelCase		=			min_resolution
										lowerCAmelCase		=			max_resolution
										lowerCAmelCase		=			do_resize
										lowerCAmelCase		=			size if size is not None else {"""height""": 18, """width""": 18}
										lowerCAmelCase		=			do_center_crop
										lowerCAmelCase		=			crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
										lowerCAmelCase		=			do_normalize
										lowerCAmelCase		=			image_mean
										lowerCAmelCase		=			image_std
										lowerCAmelCase		=			do_convert_rgb




						def 					__lowercase	(							self			:     Tuple   ):
										return {
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "do_center_crop": self.do_center_crop,
										    "crop_size": self.crop_size,
										    "do_normalize": self.do_normalize,
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_convert_rgb": self.do_convert_rgb,
										}






@require_torch
@require_vision
class  SCREAMING_SNAKE_CASE__   (   _a  ,			unittest.TestCase   ):
						_a        =  OwlViTImageProcessor if is_vision_available() else None


						def 					__lowercase	(							self			:     Union[str, Any]   ):
										lowerCAmelCase		=			OwlViTImageProcessingTester(self   )


						@property
						def 					__lowercase	(							self			:     List[Any]   ):
										return self.image_processor_tester.prepare_image_processor_dict()


						def 					__lowercase	(							self			:     List[str]   ):
										lowerCAmelCase		=			self.image_processing_class(**self.image_processor_dict   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """do_resize"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """size"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """do_center_crop"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """center_crop"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """do_normalize"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """image_mean"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """image_std"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """do_convert_rgb"""   )   )


						def 					__lowercase	(							self			:     List[Any]   ):
										lowerCAmelCase		=			self.image_processing_class.from_dict(self.image_processor_dict   )
										self.assertEqual(image_processor.size  ,  {"""height""": 18, """width""": 18}   )
										self.assertEqual(image_processor.crop_size  ,  {"""height""": 18, """width""": 18}   )

										lowerCAmelCase		=			self.image_processing_class.from_dict(self.image_processor_dict  ,  size=42  ,  crop_size=84   )
										self.assertEqual(image_processor.size  ,  {"""height""": 42, """width""": 42}   )
										self.assertEqual(image_processor.crop_size  ,  {"""height""": 84, """width""": 84}   )


						def 					__lowercase	(							self			:     str   ):
										# Initialize image_processing
										lowerCAmelCase		=			self.image_processing_class(**self.image_processor_dict   )
										# create random PIL images
										lowerCAmelCase		=			prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=lowerCAmelCase   )

										for image in image_inputs:
														self.assertIsInstance(lowerCAmelCase  ,  Image.Image   )

										# Test not batched input
										lowerCAmelCase		=			image_processing(image_inputs[0]  ,  return_tensors="""pt"""   ).pixel_values
										self.assertEqual(
										    encoded_images.shape  ,  (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )  ,  )

										# Test batched
										lowerCAmelCase		=			image_processing(lowerCAmelCase  ,  return_tensors="""pt"""   ).pixel_values
										self.assertEqual(
										    encoded_images.shape  ,  (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )  ,  )


						def 					__lowercase	(							self			:     Tuple   ):
										# Initialize image_processing
										lowerCAmelCase		=			self.image_processing_class(**self.image_processor_dict   )
										# create random numpy tensors
										lowerCAmelCase		=			prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=lowerCAmelCase  ,  numpify=lowerCAmelCase   )
										for image in image_inputs:
														self.assertIsInstance(lowerCAmelCase  ,  np.ndarray   )

										# Test not batched input
										lowerCAmelCase		=			image_processing(image_inputs[0]  ,  return_tensors="""pt"""   ).pixel_values
										self.assertEqual(
										    encoded_images.shape  ,  (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )  ,  )

										# Test batched
										lowerCAmelCase		=			image_processing(lowerCAmelCase  ,  return_tensors="""pt"""   ).pixel_values
										self.assertEqual(
										    encoded_images.shape  ,  (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )  ,  )




						def 					__lowercase	(							self			:     Optional[int]   ):
										# Initialize image_processing
										lowerCAmelCase		=			self.image_processing_class(**self.image_processor_dict   )
										# create random PyTorch tensors
										lowerCAmelCase		=			prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=lowerCAmelCase  ,  torchify=lowerCAmelCase   )
										for image in image_inputs:
														self.assertIsInstance(lowerCAmelCase  ,  torch.Tensor   )

										# Test not batched input
										lowerCAmelCase		=			image_processing(image_inputs[0]  ,  return_tensors="""pt"""   ).pixel_values
										self.assertEqual(
										    encoded_images.shape  ,  (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )  ,  )

										# Test batched
										lowerCAmelCase		=			image_processing(lowerCAmelCase  ,  return_tensors="""pt"""   ).pixel_values
										self.assertEqual(
										    encoded_images.shape  ,  (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )  ,  )





