

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       __snake_case  (    unittest.TestCase       ):

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						tempfile.mkdtemp()
      SCREAMING_SNAKE_CASE_									=						SamImageProcessor()
      SCREAMING_SNAKE_CASE_									=						SamProcessor(_A)
      processor.save_pretrained(self.tmpdirname)

  def 						lowerCAmelCase__     (					self						,		**_A):
      return AutoProcessor.from_pretrained(self.tmpdirname						,		**_A).image_processor

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

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						[np.random.randint(255						,		size=(3, 30, 400)						,		dtype=np.uinta)]

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

      return image_inputs

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=self.get_image_processor())
      processor.save_pretrained(self.tmpdirname)

      SCREAMING_SNAKE_CASE_									=						self.get_image_processor(do_normalize=_A						,		padding_value=1.0)

      SCREAMING_SNAKE_CASE_									=						SamProcessor.from_pretrained(self.tmpdirname						,		do_normalize=_A						,		padding_value=1.0)

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

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.get_image_processor()

      SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=_A)

      SCREAMING_SNAKE_CASE_									=						self.prepare_image_inputs()

      SCREAMING_SNAKE_CASE_									=						image_processor(_A						,		return_tensors='np')
      SCREAMING_SNAKE_CASE_									=						processor(images=_A						,		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 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.get_image_processor()

      SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=_A)
      SCREAMING_SNAKE_CASE_									=						[torch.ones((1, 3, 5, 5))]

      SCREAMING_SNAKE_CASE_									=						[[1764, 2646]]

      SCREAMING_SNAKE_CASE_									=						[[683, 1024]]
      SCREAMING_SNAKE_CASE_									=						processor.post_process_masks(_A						,		_A						,		_A)
      self.assertEqual(masks[0].shape						,		(1, 3, 1764, 2646))

      SCREAMING_SNAKE_CASE_									=						processor.post_process_masks(
          _A						,		torch.tensor(_A)						,		torch.tensor(_A))
      self.assertEqual(masks[0].shape						,		(1, 3, 1764, 2646))

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

      self.assertEqual(masks[0].shape						,		(1, 3, 1764, 2646))

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






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

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						tempfile.mkdtemp()
      SCREAMING_SNAKE_CASE_									=						SamImageProcessor()
      SCREAMING_SNAKE_CASE_									=						SamProcessor(_A)
      processor.save_pretrained(self.tmpdirname)

  def 						lowerCAmelCase__     (					self						,		**_A):
      return AutoProcessor.from_pretrained(self.tmpdirname						,		**_A).image_processor

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

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						[np.random.randint(255						,		size=(3, 30, 400)						,		dtype=np.uinta)]

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

      return image_inputs

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=self.get_image_processor())
      processor.save_pretrained(self.tmpdirname)

      SCREAMING_SNAKE_CASE_									=						self.get_image_processor(do_normalize=_A						,		padding_value=1.0)

      SCREAMING_SNAKE_CASE_									=						SamProcessor.from_pretrained(self.tmpdirname						,		do_normalize=_A						,		padding_value=1.0)

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

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.get_image_processor()

      SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=_A)

      SCREAMING_SNAKE_CASE_									=						self.prepare_image_inputs()

      SCREAMING_SNAKE_CASE_									=						image_processor(_A						,		return_tensors='np')
      SCREAMING_SNAKE_CASE_									=						processor(images=_A						,		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 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.get_image_processor()

      SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=_A)
      SCREAMING_SNAKE_CASE_									=						[tf.ones((1, 3, 5, 5))]

      SCREAMING_SNAKE_CASE_									=						[[1764, 2646]]

      SCREAMING_SNAKE_CASE_									=						[[683, 1024]]
      SCREAMING_SNAKE_CASE_									=						processor.post_process_masks(_A						,		_A						,		_A						,		return_tensors='tf')
      self.assertEqual(masks[0].shape						,		(1, 3, 1764, 2646))

      SCREAMING_SNAKE_CASE_									=						processor.post_process_masks(
          _A						,		tf.convert_to_tensor(_A)						,		tf.convert_to_tensor(_A)						,		return_tensors='tf'						,		)
      self.assertEqual(masks[0].shape						,		(1, 3, 1764, 2646))

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

      self.assertEqual(masks[0].shape						,		(1, 3, 1764, 2646))

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






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

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						tempfile.mkdtemp()
      SCREAMING_SNAKE_CASE_									=						SamImageProcessor()
      SCREAMING_SNAKE_CASE_									=						SamProcessor(_A)
      processor.save_pretrained(self.tmpdirname)

  def 						lowerCAmelCase__     (					self						,		**_A):
      return AutoProcessor.from_pretrained(self.tmpdirname						,		**_A).image_processor

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

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						[np.random.randint(255						,		size=(3, 30, 400)						,		dtype=np.uinta)]

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

      return image_inputs

  @is_pt_tf_cross_test
  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.get_image_processor()

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

      SCREAMING_SNAKE_CASE_									=						[[1764, 2646]]

      SCREAMING_SNAKE_CASE_									=						[[683, 1024]]
      SCREAMING_SNAKE_CASE_									=						processor.post_process_masks(
          _A						,		_A						,		_A						,		return_tensors='tf')
      SCREAMING_SNAKE_CASE_									=						processor.post_process_masks(
          _A						,		_A						,		_A						,		return_tensors='pt')

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


  @is_pt_tf_cross_test
  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.get_image_processor()

      SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=_A)

      SCREAMING_SNAKE_CASE_									=						self.prepare_image_inputs()

      SCREAMING_SNAKE_CASE_									=						image_processor(_A						,		return_tensors='pt')['pixel_values'].numpy()
      SCREAMING_SNAKE_CASE_									=						processor(images=_A						,		return_tensors='pt')['pixel_values'].numpy()

      SCREAMING_SNAKE_CASE_									=						image_processor(_A						,		return_tensors='tf')['pixel_values'].numpy()
      SCREAMING_SNAKE_CASE_									=						processor(images=_A						,		return_tensors='tf')['pixel_values'].numpy()

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





import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


UpperCamelCase__  :      int			=    "\nHugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.\n\nIn March 2021, Hugging Face raised $40 million in a Series B funding round.[3]\n\nOn April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]\n"



class       __snake_case  (    unittest.TestCase     , lowerCAmelCase__       ):

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						load_tool('text-question-answering')
      self.tool.setup()
      SCREAMING_SNAKE_CASE_									=						load_tool('text-question-answering'						,		remote=_A)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.tool(_A						,		'What did Hugging Face do in April 2021?')
      self.assertEqual(_A						,		'launched the BigScience Research Workshop')

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.remote_tool(_A						,		'What did Hugging Face do in April 2021?')
      self.assertEqual(_A						,		'launched the BigScience Research Workshop')

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.tool(text=_A						,		question='What did Hugging Face do in April 2021?')
      self.assertEqual(_A						,		'launched the BigScience Research Workshop')


  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.remote_tool(text=_A						,		question='What did Hugging Face do in April 2021?')
      self.assertEqual(_A						,		'launched the BigScience Research Workshop')


