


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


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import ChineseCLIPImageProcessor



class    UpperCAmelCase_	(							unittest.TestCase):






       def __init__(     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE=7 ,					__SCREAMING_SNAKE_CASE=3 ,					__SCREAMING_SNAKE_CASE=18 ,					__SCREAMING_SNAKE_CASE=30 ,					__SCREAMING_SNAKE_CASE=400 ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=None ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=None ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=[0.48_145_466, 0.4_578_275, 0.40_821_073] ,					__SCREAMING_SNAKE_CASE=[0.26_862_954, 0.26_130_258, 0.27_577_711] ,					__SCREAMING_SNAKE_CASE=True ,					):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		size if size is not None else {'''height''': 224, '''width''': 224}
              UpperCamelCase    :	Optional[Any]   	=		crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
              UpperCamelCase    :	Optional[Any]   	=		parent
              UpperCamelCase    :	Optional[Any]   	=		batch_size
              UpperCamelCase    :	List[str]   	=		num_channels
              UpperCamelCase    :	Tuple   	=		image_size
              UpperCamelCase    :	Union[str, Any]   	=		min_resolution
              UpperCamelCase    :	List[str]   	=		max_resolution
              UpperCamelCase    :	Union[str, Any]   	=		do_resize
              UpperCamelCase    :	int   	=		size
              UpperCamelCase    :	Tuple   	=		do_center_crop
              UpperCamelCase    :	List[Any]   	=		crop_size
              UpperCamelCase    :	List[str]   	=		do_normalize
              UpperCamelCase    :	List[Any]   	=		image_mean
              UpperCamelCase    :	Any   	=		image_std
              UpperCamelCase    :	Union[str, Any]   	=		do_convert_rgb






       def  _lowercase   (     self						):




              """simple docstring"""




              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,
              }






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE=False ,					__SCREAMING_SNAKE_CASE=False ,					__SCREAMING_SNAKE_CASE=False						):




              """simple docstring"""




              assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

              if equal_resolution:
                     UpperCamelCase    :	Union[str, Any]   	=		[]
                     for i in range(self.batch_size						):
                            image_inputs.append(
                                np.random.randint(
                                    255 ,					size=(self.num_channels, self.max_resolution, self.max_resolution) ,					dtype=np.uinta						)						)
              else:
                     UpperCamelCase    :	int   	=		[]
                     for i in range(self.batch_size						):
                            UpperCamelCase    :	Optional[int]   	=		np.random.choice(np.arange(self.min_resolution ,					self.max_resolution						) ,					2						)
                            image_inputs.append(np.random.randint(255 ,					size=(self.num_channels, width, height) ,					dtype=np.uinta						)						)

              if not numpify and not torchify:
                     # PIL expects the channel dimension as last dimension
                     UpperCamelCase    :	Tuple   	=		[Image.fromarray(np.moveaxis(UpperCamelCase__ ,					0 ,					-1						)						) for x in image_inputs]

              if torchify:
                     UpperCamelCase    :	int   	=		[torch.from_numpy(UpperCamelCase__						) for x in image_inputs]

              return image_inputs




@require_torch
@require_vision
class    UpperCAmelCase_	(							SCREAMING_SNAKE_CASE_,     unittest.TestCase):
       __UpperCamelCase      :     Dict							=					ChineseCLIPImageProcessor if is_vision_available() else None






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		ChineseCLIPImageProcessingTester(self ,					do_center_crop=UpperCamelCase__						)






       @property
       def  _lowercase   (     self						):




              """simple docstring"""




              return self.image_processor_tester.prepare_image_processor_dict()






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[Any]   	=		self.image_processing_class(**self.image_processor_dict						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''do_resize'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''size'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''do_center_crop'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''center_crop'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''do_normalize'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''image_mean'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''image_std'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''do_convert_rgb'''						)						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[int]   	=		self.image_processing_class.from_dict(self.image_processor_dict						)
              self.assertEqual(image_processor.size ,					{'''height''': 224, '''width''': 224}						)
              self.assertEqual(image_processor.crop_size ,					{'''height''': 18, '''width''': 18}						)

              UpperCamelCase    :	int   	=		self.image_processing_class.from_dict(self.image_processor_dict ,					size=42 ,					crop_size=84						)
              self.assertEqual(image_processor.size ,					{'''shortest_edge''': 42}						)
              self.assertEqual(image_processor.crop_size ,					{'''height''': 84, '''width''': 84}						)






       def  _lowercase   (     self						):




              """simple docstring"""




              pass






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	List[str]   	=		self.image_processing_class(**self.image_processor_dict						)
              # create random PIL images
              UpperCamelCase    :	str   	=		self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__						)
              for image in image_inputs:
                     self.assertIsInstance(UpperCamelCase__ ,					Image.Image						)

              # Test not batched input
              UpperCamelCase    :	Dict   	=		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
              UpperCamelCase    :	Optional[Any]   	=		image_processing(UpperCamelCase__ ,					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						):




              """simple docstring"""




              UpperCamelCase    :	Union[str, Any]   	=		self.image_processing_class(**self.image_processor_dict						)
              # create random numpy tensors
              UpperCamelCase    :	Dict   	=		self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__ ,					numpify=UpperCamelCase__						)
              for image in image_inputs:
                     self.assertIsInstance(UpperCamelCase__ ,					np.ndarray						)

              # Test not batched input
              UpperCamelCase    :	Optional[int]   	=		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
              UpperCamelCase    :	int   	=		image_processing(UpperCamelCase__ ,					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						):




              """simple docstring"""




              UpperCamelCase    :	Tuple   	=		self.image_processing_class(**self.image_processor_dict						)
              # create random PyTorch tensors
              UpperCamelCase    :	Dict   	=		self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__ ,					torchify=UpperCamelCase__						)
              for image in image_inputs:
                     self.assertIsInstance(UpperCamelCase__ ,					torch.Tensor						)

              # Test not batched input
              UpperCamelCase    :	Optional[Any]   	=		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
              UpperCamelCase    :	Optional[Any]   	=		image_processing(UpperCamelCase__ ,					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'''],
                  ) ,					)




@require_torch
@require_vision
class    UpperCAmelCase_	(							SCREAMING_SNAKE_CASE_,     unittest.TestCase):
       __UpperCamelCase      :     Tuple							=					ChineseCLIPImageProcessor if is_vision_available() else None






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	int   	=		ChineseCLIPImageProcessingTester(self ,					num_channels=4 ,					do_center_crop=UpperCamelCase__						)
              UpperCamelCase    :	Optional[Any]   	=		3






       @property
       def  _lowercase   (     self						):




              """simple docstring"""




              return self.image_processor_tester.prepare_image_processor_dict()






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		self.image_processing_class(**self.image_processor_dict						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''do_resize'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''size'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''do_center_crop'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''center_crop'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''do_normalize'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''image_mean'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''image_std'''						)						)
              self.assertTrue(hasattr(UpperCamelCase__ ,					'''do_convert_rgb'''						)						)






       def  _lowercase   (     self						):




              """simple docstring"""




              pass






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[Any]   	=		self.image_processing_class(**self.image_processor_dict						)
              # create random PIL images
              UpperCamelCase    :	List[Any]   	=		self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__						)
              for image in image_inputs:
                     self.assertIsInstance(UpperCamelCase__ ,					Image.Image						)

              # Test not batched input
              UpperCamelCase    :	Optional[int]   	=		image_processing(image_inputs[0] ,					return_tensors='''pt'''						).pixel_values
              self.assertEqual(
                  encoded_images.shape ,					(
                      1,
                      self.expected_encoded_image_num_channels,
                      self.image_processor_tester.crop_size['''height'''],
                      self.image_processor_tester.crop_size['''width'''],
                  ) ,					)

              # Test batched
              UpperCamelCase    :	Tuple   	=		image_processing(UpperCamelCase__ ,					return_tensors='''pt'''						).pixel_values
              self.assertEqual(
                  encoded_images.shape ,					(
                      self.image_processor_tester.batch_size,
                      self.expected_encoded_image_num_channels,
                      self.image_processor_tester.crop_size['''height'''],
                      self.image_processor_tester.crop_size['''width'''],
                  ) ,					)





import torch

from transformers import AutoModel



class    UpperCAmelCase_	(							torch.nn.Module):
       '''simple docstring'''






       def __init__(     self ,					__SCREAMING_SNAKE_CASE="sayef/fsner-bert-base-uncased"						):




              """simple docstring"""




              super(__SCREAMING_SNAKE_CASE ,					self						).__init__()

              UpperCamelCase    :	List[str]   	=		AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE ,					return_dict=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Optional[int]   	=		torch.nn.CosineSimilarity(3 ,					1e-08						)
              UpperCamelCase    :	List[Any]   	=		torch.nn.Softmax(dim=1						)






       def  _lowercase   (     self ,					**__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              return self.bert(**__SCREAMING_SNAKE_CASE						).last_hidden_state






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              return token_embeddings.sum(2 ,					keepdim=__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE=1						):




              """simple docstring"""




              return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						)						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		W_supports['''sizes'''].tolist()
              UpperCamelCase    :	Optional[int]   	=		W_supports['''start_token_id'''].item()
              UpperCamelCase    :	Any   	=		W_supports['''end_token_id'''].item()

              del W_supports["sizes"]
              del W_supports["start_token_id"]
              del W_supports["end_token_id"]

              UpperCamelCase    :	Union[str, Any]   	=		self.BERT(**__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	List[str]   	=		self.BERT(**__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	List[Any]   	=		None
              UpperCamelCase    :	Any   	=		None

              UpperCamelCase    :	Optional[Any]   	=		W_supports['''input_ids'''] == start_token_id
              UpperCamelCase    :	Any   	=		W_supports['''input_ids'''] == end_token_id

              for i, size in enumerate(__SCREAMING_SNAKE_CASE						):
                     if i == 0:
                            UpperCamelCase    :	Optional[int]   	=		0
                     else:
                            UpperCamelCase    :	Tuple   	=		support_sizes[i - 1]

                     UpperCamelCase    :	Tuple   	=		S[s : s + size][start_token_masks[s : s + size]]
                     UpperCamelCase    :	List[str]   	=		S[s : s + size][end_token_masks[s : s + size]]

                     UpperCamelCase    :	Dict   	=		torch.matmul(q[i] ,					s_start.T						).sum(1						).softmax(0						)
                     UpperCamelCase    :	Tuple   	=		torch.matmul(q[i] ,					s_end.T						).sum(1						).softmax(0						)

                     if p_starts is not None:
                            UpperCamelCase    :	List[str]   	=		torch.vstack((p_starts, p_start)						)
                            UpperCamelCase    :	Union[str, Any]   	=		torch.vstack((p_ends, p_end)						)
                     else:
                            UpperCamelCase    :	str   	=		p_start
                            UpperCamelCase    :	Optional[int]   	=		p_end

              return p_starts, p_ends

