





"""simple docstring"""






import unittest

from transformers import LiltConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    LiltForQuestionAnswering,
		    LiltForSequenceClassification,
		    LiltForTokenClassification,
		    LiltModel,
		)
		from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST




class 			__lowercase     :







					'''simple docstring'''




					def __init__(  self		,     _UpperCAmelCase		,     _UpperCAmelCase=13		,     _UpperCAmelCase=7		,     _UpperCAmelCase=True		,     _UpperCAmelCase=True		,     _UpperCAmelCase=True		,     _UpperCAmelCase=True		,     _UpperCAmelCase=99		,     _UpperCAmelCase=24		,     _UpperCAmelCase=2		,     _UpperCAmelCase=6		,     _UpperCAmelCase=37		,     _UpperCAmelCase="gelu"		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=512		,     _UpperCAmelCase=16		,     _UpperCAmelCase=2		,     _UpperCAmelCase=0.0_2		,     _UpperCAmelCase=3		,     _UpperCAmelCase=None		,     _UpperCAmelCase=1000		,     ):
									__a	:  Optional[Any]         =					parent
									__a	:  int         =					batch_size
									__a	:  Union[str, Any]         =					seq_length
									__a	:  Optional[Any]         =					is_training
									__a	:  int         =					use_input_mask
									__a	:  Tuple         =					use_token_type_ids
									__a	:  Tuple         =					use_labels
									__a	:  Optional[Any]         =					vocab_size
									__a	:  int         =					hidden_size
									__a	:  Dict         =					num_hidden_layers
									__a	:  Optional[Any]         =					num_attention_heads
									__a	:  int         =					intermediate_size
									__a	:  Tuple         =					hidden_act
									__a	:  List[Any]         =					hidden_dropout_prob
									__a	:  Union[str, Any]         =					attention_probs_dropout_prob
									__a	:  Dict         =					max_position_embeddings
									__a	:  List[str]         =					type_vocab_size
									__a	:  Optional[int]         =					type_sequence_label_size
									__a	:  List[str]         =					initializer_range
									__a	:  Optional[int]         =					num_labels
									__a	:  Dict         =					scope
									__a	:  List[str]         =					range_bbox







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size       )

									__a	:  Optional[Any]         =					ids_tensor([self.batch_size, self.seq_length, 4]		,     self.range_bbox       )
									# Ensure that bbox is legal
									for i in range(bbox.shape[0]       ):
													for j in range(bbox.shape[1]       ):
																	if bbox[i, j, 3] < bbox[i, j, 1]:
																					__a	:  Any         =					bbox[i, j, 3]
																					__a	:  str         =					bbox[i, j, 1]
																					__a	:  List[Any]         =					t
																	if bbox[i, j, 2] < bbox[i, j, 0]:
																					__a	:  str         =					bbox[i, j, 2]
																					__a	:  List[str]         =					bbox[i, j, 0]
																					__a	:  int         =					t

									__a	:  Tuple         =					None
									if self.use_input_mask:
													__a	:  List[Any]         =					ids_tensor([self.batch_size, self.seq_length]		,     vocab_size=2       )

									__a	:  Union[str, Any]         =					None
									if self.use_token_type_ids:
													__a	:  Optional[Any]         =					ids_tensor([self.batch_size, self.seq_length]		,     self.type_vocab_size       )

									__a	:  Optional[Any]         =					None
									__a	:  Dict         =					None
									if self.use_labels:
													__a	:  Union[str, Any]         =					ids_tensor([self.batch_size]		,     self.type_sequence_label_size       )
													__a	:  Tuple         =					ids_tensor([self.batch_size, self.seq_length]		,     self.num_labels       )

									__a	:  int         =					self.get_config()

									return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels







					def 	_lowerCamelCase						(  self       ):
									return LiltConfig(
									    vocab_size=self.vocab_size		,     hidden_size=self.hidden_size		,     num_hidden_layers=self.num_hidden_layers		,     num_attention_heads=self.num_attention_heads		,     intermediate_size=self.intermediate_size		,     hidden_act=self.hidden_act		,     hidden_dropout_prob=self.hidden_dropout_prob		,     attention_probs_dropout_prob=self.attention_probs_dropout_prob		,     max_position_embeddings=self.max_position_embeddings		,     type_vocab_size=self.type_vocab_size		,     initializer_range=self.initializer_range		,     )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     ):
									__a	:  Optional[int]         =					LiltModel(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  List[str]         =					model(_UpperCAmelCase		,     bbox=_UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     token_type_ids=_UpperCAmelCase       )
									__a	:  Dict         =					model(_UpperCAmelCase		,     bbox=_UpperCAmelCase		,     token_type_ids=_UpperCAmelCase       )
									__a	:  Dict         =					model(_UpperCAmelCase		,     bbox=_UpperCAmelCase       )

									self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)       )
									self.parent.assertEqual(result.pooler_output.shape		,     (self.batch_size, self.hidden_size)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     ):
									__a	:  List[str]         =					self.num_labels
									__a	:  Union[str, Any]         =					LiltForTokenClassification(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  Optional[int]         =					model(
									    _UpperCAmelCase		,     bbox=_UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     token_type_ids=_UpperCAmelCase		,     labels=_UpperCAmelCase       )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     ):
									__a	:  str         =					LiltForQuestionAnswering(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  Union[str, Any]         =					model(
									    _UpperCAmelCase		,     bbox=_UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     token_type_ids=_UpperCAmelCase		,     start_positions=_UpperCAmelCase		,     end_positions=_UpperCAmelCase		,     )
									self.parent.assertEqual(result.start_logits.shape		,     (self.batch_size, self.seq_length)       )
									self.parent.assertEqual(result.end_logits.shape		,     (self.batch_size, self.seq_length)       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Tuple         =					self.prepare_config_and_inputs()
									(
									    (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  
									)	:  str         =					config_and_inputs
									__a	:  List[str]         =					{
									    '''input_ids''': input_ids,
									    '''bbox''': bbox,
									    '''token_type_ids''': token_type_ids,
									    '''attention_mask''': input_mask,
									}
									return config, inputs_dict






@require_torch
class 			__lowercase     (     _UpperCamelCase				, _UpperCamelCase				, _UpperCamelCase				, unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					(
					    (
					        LiltModel,
					        LiltForSequenceClassification,
					        LiltForTokenClassification,
					        LiltForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					__lowerCAmelCase					=					(
					    {
					        '''feature-extraction''': LiltModel,
					        '''question-answering''': LiltForQuestionAnswering,
					        '''text-classification''': LiltForSequenceClassification,
					        '''token-classification''': LiltForTokenClassification,
					        '''zero-shot''': LiltForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					False







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									return True







					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					LiltModelTester(self       )
									__a	:  Tuple         =					ConfigTester(self		,     config_class=_UpperCAmelCase		,     hidden_size=37       )







					def 	_lowerCamelCase						(  self       ):
									self.config_tester.run_common_tests()







					def 	_lowerCamelCase						(  self       ):
									__a	:  Any         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[Any]         =					self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
													__a	:  Optional[Any]         =					type
													self.model_tester.create_and_check_model(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[Any]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[int]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase       )







					@slow
					def 	_lowerCamelCase						(  self       ):
									for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__a	:  Optional[Any]         =					LiltModel.from_pretrained(_UpperCAmelCase       )
													self.assertIsNotNone(_UpperCAmelCase       )






@require_torch
@slow
class 			__lowercase     (     unittest.TestCase							):







					'''simple docstring'''




					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base'''       ).to(_UpperCAmelCase       )

									__a	:  Union[str, Any]         =					torch.tensor([[1, 2]]		,     device=_UpperCAmelCase       )
									__a	:  List[str]         =					torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]		,     device=_UpperCAmelCase       )

									# forward pass
									with torch.no_grad():
													__a	:  Optional[int]         =					model(input_ids=_UpperCAmelCase		,     bbox=_UpperCAmelCase       )

									__a	:  Optional[int]         =					torch.Size([1, 2, 768]       )
									__a	:  Optional[int]         =					torch.tensor(
									    [[-0.0_6_5_3, 0.0_9_5_0, -0.0_0_6_1], [-0.0_5_4_5, 0.0_9_2_6, -0.0_3_2_4]]		,     device=_UpperCAmelCase		,     )

									self.assertTrue(outputs.last_hidden_state.shape		,     _UpperCAmelCase       )
									self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3]		,     _UpperCAmelCase		,     atol=1e-3       )       )






"""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.
import numpy as np
import torch

from ..models.clipseg import CLIPSegForImageSegmentation
from ..utils import is_vision_available, requires_backends
from .base import PipelineTool


if is_vision_available():
		from PIL import Image




class 			__lowercase     (     _UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					(
					    '''This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.'''
					    '''It takes two arguments named `image` which should be the original image, and `label` which should be a text '''
					    '''describing the elements what should be identified in the segmentation mask. The tool returns the mask.'''
					)
					__lowerCAmelCase					=					'''CIDAS/clipseg-rd64-refined'''
					__lowerCAmelCase					=					'''image_segmenter'''
					__lowerCAmelCase					=					CLIPSegForImageSegmentation

					__lowerCAmelCase					=					['''image''', '''text''']
					__lowerCAmelCase					=					['''image''']







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







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									return self.pre_processor(text=[label]		,     images=[image]		,     padding=_UpperCAmelCase		,     return_tensors='''pt'''       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									with torch.no_grad():
													__a	:  List[str]         =					self.model(**_UpperCAmelCase       ).logits
									return logits







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									__a	:  str         =					outputs.cpu().detach().numpy()
									__a	:  int         =					0
									__a	:  Optional[int]         =					1
									return Image.fromarray((array * 255).astype(np.uinta       )       )