



import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
   from PIL import Image

   from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
   import torch

if is_tf_available():
   import tensorflow as tf



@require_vision
@require_torchvision
class     _UpperCAmelCase			(       unittest.TestCase     ):




     def 		snake_case_		(				self):
         A__		 =					tempfile.mkdtemp()
         A__		 =					SamImageProcessor()
         A__		 =					SamProcessor(UpperCamelCase__)
         processor.save_pretrained(self.tmpdirname)




     def 		snake_case_		(				self ,						**a__):
         return AutoProcessor.from_pretrained(self.tmpdirname ,						**UpperCamelCase__).image_processor




     def 		snake_case_		(				self):
         shutil.rmtree(self.tmpdirname)




     def 		snake_case_		(				self):
         A__		 =					[np.random.randint(2_5_5 ,						size=(3, 3_0, 4_0_0) ,						dtype=np.uinta)]

         A__		 =					[Image.fromarray(np.moveaxis(UpperCamelCase__ ,						0 ,						-1)) for x in image_inputs]

         return image_inputs




     def 		snake_case_		(				self):
         A__		 =					SamProcessor(image_processor=self.get_image_processor())
         processor.save_pretrained(self.tmpdirname)

         A__		 =					self.get_image_processor(do_normalize=UpperCamelCase__ ,						padding_value=1.0)

         A__		 =					SamProcessor.from_pretrained(self.tmpdirname ,						do_normalize=UpperCamelCase__ ,						padding_value=1.0)

         self.assertEqual(processor.image_processor.to_json_string() ,						image_processor_add_kwargs.to_json_string())
         self.assertIsInstance(processor.image_processor ,						UpperCamelCase__)




     def 		snake_case_		(				self):
         A__		 =					self.get_image_processor()

         A__		 =					SamProcessor(image_processor=UpperCamelCase__)

         A__		 =					self.prepare_image_inputs()

         A__		 =					image_processor(UpperCamelCase__ ,						return_tensors='''np''')
         A__		 =					processor(images=UpperCamelCase__ ,						return_tensors='''np''')

         input_feat_extract.pop('''original_sizes''')  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop('''reshaped_input_sizes''')  # pop original_sizes as it is popped in the processor

         for key in input_feat_extract.keys():
             self.assertAlmostEqual(input_feat_extract[key].sum() ,						input_processor[key].sum() ,						delta=1e-2)




     @require_torch
     def 		snake_case_		(				self):
         A__		 =					self.get_image_processor()

         A__		 =					SamProcessor(image_processor=UpperCamelCase__)
         A__		 =					[torch.ones((1, 3, 5, 5))]

         A__		 =					[[1_7_6_4, 2_6_4_6]]

         A__		 =					[[6_8_3, 1_0_2_4]]
         A__		 =					processor.post_process_masks(UpperCamelCase__ ,						UpperCamelCase__ ,						UpperCamelCase__)
         self.assertEqual(masks[0].shape ,						(1, 3, 1_7_6_4, 2_6_4_6))

         A__		 =					processor.post_process_masks(
             UpperCamelCase__ ,						torch.tensor(UpperCamelCase__) ,						torch.tensor(UpperCamelCase__))
         self.assertEqual(masks[0].shape ,						(1, 3, 1_7_6_4, 2_6_4_6))

         # should also work with np
         A__		 =					[np.ones((1, 3, 5, 5))]
         A__		 =					processor.post_process_masks(UpperCamelCase__ ,						np.array(UpperCamelCase__) ,						np.array(UpperCamelCase__))

         self.assertEqual(masks[0].shape ,						(1, 3, 1_7_6_4, 2_6_4_6))

         A__		 =					[[1, 0], [0, 1]]
         with self.assertRaises(UpperCamelCase__):
             A__		 =					processor.post_process_masks(UpperCamelCase__ ,						np.array(UpperCamelCase__) ,						np.array(UpperCamelCase__))







@require_vision
@require_tf
class     _UpperCAmelCase			(       unittest.TestCase     ):




     def 		snake_case_		(				self):
         A__		 =					tempfile.mkdtemp()
         A__		 =					SamImageProcessor()
         A__		 =					SamProcessor(UpperCamelCase__)
         processor.save_pretrained(self.tmpdirname)




     def 		snake_case_		(				self ,						**a__):
         return AutoProcessor.from_pretrained(self.tmpdirname ,						**UpperCamelCase__).image_processor




     def 		snake_case_		(				self):
         shutil.rmtree(self.tmpdirname)




     def 		snake_case_		(				self):
         A__		 =					[np.random.randint(2_5_5 ,						size=(3, 3_0, 4_0_0) ,						dtype=np.uinta)]

         A__		 =					[Image.fromarray(np.moveaxis(UpperCamelCase__ ,						0 ,						-1)) for x in image_inputs]

         return image_inputs




     def 		snake_case_		(				self):
         A__		 =					SamProcessor(image_processor=self.get_image_processor())
         processor.save_pretrained(self.tmpdirname)

         A__		 =					self.get_image_processor(do_normalize=UpperCamelCase__ ,						padding_value=1.0)

         A__		 =					SamProcessor.from_pretrained(self.tmpdirname ,						do_normalize=UpperCamelCase__ ,						padding_value=1.0)

         self.assertEqual(processor.image_processor.to_json_string() ,						image_processor_add_kwargs.to_json_string())
         self.assertIsInstance(processor.image_processor ,						UpperCamelCase__)




     def 		snake_case_		(				self):
         A__		 =					self.get_image_processor()

         A__		 =					SamProcessor(image_processor=UpperCamelCase__)

         A__		 =					self.prepare_image_inputs()

         A__		 =					image_processor(UpperCamelCase__ ,						return_tensors='''np''')
         A__		 =					processor(images=UpperCamelCase__ ,						return_tensors='''np''')

         input_feat_extract.pop('''original_sizes''')  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop('''reshaped_input_sizes''')  # pop reshaped_input_sizes as it is popped in the processor

         for key in input_feat_extract.keys():
             self.assertAlmostEqual(input_feat_extract[key].sum() ,						input_processor[key].sum() ,						delta=1e-2)




     @require_tf
     def 		snake_case_		(				self):
         A__		 =					self.get_image_processor()

         A__		 =					SamProcessor(image_processor=UpperCamelCase__)
         A__		 =					[tf.ones((1, 3, 5, 5))]

         A__		 =					[[1_7_6_4, 2_6_4_6]]

         A__		 =					[[6_8_3, 1_0_2_4]]
         A__		 =					processor.post_process_masks(UpperCamelCase__ ,						UpperCamelCase__ ,						UpperCamelCase__ ,						return_tensors='''tf''')
         self.assertEqual(masks[0].shape ,						(1, 3, 1_7_6_4, 2_6_4_6))

         A__		 =					processor.post_process_masks(
             UpperCamelCase__ ,						tf.convert_to_tensor(UpperCamelCase__) ,						tf.convert_to_tensor(UpperCamelCase__) ,						return_tensors='''tf''' ,						)
         self.assertEqual(masks[0].shape ,						(1, 3, 1_7_6_4, 2_6_4_6))

         # should also work with np
         A__		 =					[np.ones((1, 3, 5, 5))]
         A__		 =					processor.post_process_masks(
             UpperCamelCase__ ,						np.array(UpperCamelCase__) ,						np.array(UpperCamelCase__) ,						return_tensors='''tf''')

         self.assertEqual(masks[0].shape ,						(1, 3, 1_7_6_4, 2_6_4_6))

         A__		 =					[[1, 0], [0, 1]]
         with self.assertRaises(tf.errors.InvalidArgumentError):
             A__		 =					processor.post_process_masks(
                 UpperCamelCase__ ,						np.array(UpperCamelCase__) ,						np.array(UpperCamelCase__) ,						return_tensors='''tf''')







@require_vision
@require_torchvision
class     _UpperCAmelCase			(       unittest.TestCase     ):




     def 		snake_case_		(				self):
         A__		 =					tempfile.mkdtemp()
         A__		 =					SamImageProcessor()
         A__		 =					SamProcessor(UpperCamelCase__)
         processor.save_pretrained(self.tmpdirname)




     def 		snake_case_		(				self ,						**a__):
         return AutoProcessor.from_pretrained(self.tmpdirname ,						**UpperCamelCase__).image_processor




     def 		snake_case_		(				self):
         shutil.rmtree(self.tmpdirname)




     def 		snake_case_		(				self):
         A__		 =					[np.random.randint(2_5_5 ,						size=(3, 3_0, 4_0_0) ,						dtype=np.uinta)]

         A__		 =					[Image.fromarray(np.moveaxis(UpperCamelCase__ ,						0 ,						-1)) for x in image_inputs]

         return image_inputs




     @is_pt_tf_cross_test
     def 		snake_case_		(				self):
         A__		 =					self.get_image_processor()

         A__		 =					SamProcessor(image_processor=UpperCamelCase__)
         A__		 =					np.random.randint(0 ,						2 ,						size=(1, 3, 5, 5)).astype(np.floataa)
         A__		 =					[tf.convert_to_tensor(UpperCamelCase__)]
         A__		 =					[torch.tensor(UpperCamelCase__)]

         A__		 =					[[1_7_6_4, 2_6_4_6]]

         A__		 =					[[6_8_3, 1_0_2_4]]
         A__		 =					processor.post_process_masks(
             UpperCamelCase__ ,						UpperCamelCase__ ,						UpperCamelCase__ ,						return_tensors='''tf''')
         A__		 =					processor.post_process_masks(
             UpperCamelCase__ ,						UpperCamelCase__ ,						UpperCamelCase__ ,						return_tensors='''pt''')

         self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()))




     @is_pt_tf_cross_test
     def 		snake_case_		(				self):
         A__		 =					self.get_image_processor()

         A__		 =					SamProcessor(image_processor=UpperCamelCase__)

         A__		 =					self.prepare_image_inputs()

         A__		 =					image_processor(UpperCamelCase__ ,						return_tensors='''pt''')['''pixel_values'''].numpy()
         A__		 =					processor(images=UpperCamelCase__ ,						return_tensors='''pt''')['''pixel_values'''].numpy()

         A__		 =					image_processor(UpperCamelCase__ ,						return_tensors='''tf''')['''pixel_values'''].numpy()
         A__		 =					processor(images=UpperCamelCase__ ,						return_tensors='''tf''')['''pixel_values'''].numpy()

         self.assertTrue(np.allclose(UpperCamelCase__ ,						UpperCamelCase__))
         self.assertTrue(np.allclose(UpperCamelCase__ ,						UpperCamelCase__))
         self.assertTrue(np.allclose(UpperCamelCase__ ,						UpperCamelCase__))





# 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			(       A__     ):
     UpperCamelCase__		    =				'''Salesforce/blip-image-captioning-base'''
     UpperCamelCase__		    =				(
         '''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.'''
     )
     UpperCamelCase__		    =				'''image_captioner'''
     UpperCamelCase__		    =				AutoModelForVisionaSeq

     UpperCamelCase__		    =				['''image''']
     UpperCamelCase__		    =				['''text''']




     def __init__(				self ,						*a__ ,						**a__):
         requires_backends(self ,						['''vision'''])
         super().__init__(*a__ ,						**a__)




     def 		snake_case_		(				self ,						a__):
         return self.pre_processor(images=a__ ,						return_tensors='''pt''')




     def 		snake_case_		(				self ,						a__):
         return self.model.generate(**a__)




     def 		snake_case_		(				self ,						a__):
         return self.pre_processor.batch_decode(a__ ,						skip_special_tokens=a__)[0].strip()
