

"""simple docstring"""






import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
	from PIL import Image

	from transformers import CLIPImageProcessor, CLIPProcessor







@require_vision
class       snake_case__  ( unittest.TestCase     ):







			def 			__lowerCAmelCase				(			self			: Tuple  ):





								'''simple docstring'''



								UpperCAmelCase			:   str      =				tempfile.mkdtemp()

								# fmt: off
								UpperCAmelCase			:   Dict      =				["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"]
								# fmt: on
								UpperCAmelCase			:   Tuple      =				dict(zip(lowercase			,  range(len(lowercase  )  )  )  )
								UpperCAmelCase			:   Tuple      =				["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""]
								UpperCAmelCase			:   List[Any]      =				{"unk_token": "<unk>"}

								UpperCAmelCase			:   Any      =				os.path.join(self.tmpdirname			,  VOCAB_FILES_NAMES["vocab_file"]  )
								UpperCAmelCase			:   List[str]      =				os.path.join(self.tmpdirname			,  VOCAB_FILES_NAMES["merges_file"]  )
								with open(self.vocab_file			,  "w"			,  encoding="utf-8"  ) as fp:
													fp.write(json.dumps(lowercase  ) + "\n"  )
								with open(self.merges_file			,  "w"			,  encoding="utf-8"  ) as fp:
													fp.write("\n".join(lowercase  )  )

								UpperCAmelCase			:   Union[str, Any]      =				{
								    "do_resize": True,
								    "size": 20,
								    "do_center_crop": True,
								    "crop_size": 18,
								    "do_normalize": True,
								    "image_mean": [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
								    "image_std": [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
								}
								UpperCAmelCase			:   int      =				os.path.join(self.tmpdirname			,  lowercase  )
								with open(self.image_processor_file			,  "w"			,  encoding="utf-8"  ) as fp:
													json.dump(lowercase			,  lowercase  )







			def 			__lowerCAmelCase				(			self			: Union[str, Any]			,  **lowercase			: List[Any]  ):





								'''simple docstring'''



								return CLIPTokenizer.from_pretrained(self.tmpdirname			,  **lowercase  )







			def 			__lowerCAmelCase				(			self			: Optional[Any]			,  **lowercase			: Any  ):





								'''simple docstring'''



								return CLIPTokenizerFast.from_pretrained(self.tmpdirname			,  **lowercase  )







			def 			__lowerCAmelCase				(			self			: List[Any]			,  **lowercase			: str  ):





								'''simple docstring'''



								return CLIPImageProcessor.from_pretrained(self.tmpdirname			,  **lowercase  )







			def 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



								shutil.rmtree(self.tmpdirname  )







			def 			__lowerCAmelCase				(			self			: int  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[Any]      =				[np.random.randint(2_55			,  size=(3, 30, 4_00)			,  dtype=np.uinta  )]

								UpperCAmelCase			:   Tuple      =				[Image.fromarray(np.moveaxis(lowercase			,  0			,  -1  )  ) for x in image_inputs]

								return image_inputs







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Union[str, Any]      =				self.get_tokenizer()
								UpperCAmelCase			:   int      =				self.get_rust_tokenizer()
								UpperCAmelCase			:   Dict      =				self.get_image_processor()

								UpperCAmelCase			:   str      =				CLIPProcessor(tokenizer=lowercase			,  image_processor=lowercase  )
								processor_slow.save_pretrained(self.tmpdirname  )
								UpperCAmelCase			:   Tuple      =				CLIPProcessor.from_pretrained(self.tmpdirname			,  use_fast=lowercase  )

								UpperCAmelCase			:   str      =				CLIPProcessor(tokenizer=lowercase			,  image_processor=lowercase  )
								processor_fast.save_pretrained(self.tmpdirname  )
								UpperCAmelCase			:   Optional[int]      =				CLIPProcessor.from_pretrained(self.tmpdirname  )

								self.assertEqual(processor_slow.tokenizer.get_vocab()			,  tokenizer_slow.get_vocab()  )
								self.assertEqual(processor_fast.tokenizer.get_vocab()			,  tokenizer_fast.get_vocab()  )
								self.assertEqual(tokenizer_slow.get_vocab()			,  tokenizer_fast.get_vocab()  )
								self.assertIsInstance(processor_slow.tokenizer			,  lowercase  )
								self.assertIsInstance(processor_fast.tokenizer			,  lowercase  )

								self.assertEqual(processor_slow.image_processor.to_json_string()			,  image_processor.to_json_string()  )
								self.assertEqual(processor_fast.image_processor.to_json_string()			,  image_processor.to_json_string()  )
								self.assertIsInstance(processor_slow.image_processor			,  lowercase  )
								self.assertIsInstance(processor_fast.image_processor			,  lowercase  )







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Dict      =				CLIPProcessor(tokenizer=self.get_tokenizer()			,  image_processor=self.get_image_processor()  )
								processor.save_pretrained(self.tmpdirname  )

								UpperCAmelCase			:   Optional[int]      =				self.get_tokenizer(bos_token="(BOS)"			,  eos_token="(EOS)"  )
								UpperCAmelCase			:   Tuple      =				self.get_image_processor(do_normalize=lowercase			,  padding_value=1.0  )

								UpperCAmelCase			:   Optional[int]      =				CLIPProcessor.from_pretrained(
								    self.tmpdirname			,  bos_token="(BOS)"			,  eos_token="(EOS)"			,  do_normalize=lowercase			,  padding_value=1.0  )

								self.assertEqual(processor.tokenizer.get_vocab()			,  tokenizer_add_kwargs.get_vocab()  )
								self.assertIsInstance(processor.tokenizer			,  lowercase  )

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







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Tuple      =				self.get_image_processor()
								UpperCAmelCase			:   List[str]      =				self.get_tokenizer()

								UpperCAmelCase			:   Tuple      =				CLIPProcessor(tokenizer=lowercase			,  image_processor=lowercase  )

								UpperCAmelCase			:   Optional[Any]      =				self.prepare_image_inputs()

								UpperCAmelCase			:   List[Any]      =				image_processor(lowercase			,  return_tensors="np"  )
								UpperCAmelCase			:   Dict      =				processor(images=lowercase			,  return_tensors="np"  )

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







			def 			__lowerCAmelCase				(			self			: str  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[Any]      =				self.get_image_processor()
								UpperCAmelCase			:   Optional[Any]      =				self.get_tokenizer()

								UpperCAmelCase			:   Optional[int]      =				CLIPProcessor(tokenizer=lowercase			,  image_processor=lowercase  )

								UpperCAmelCase			:   Union[str, Any]      =				"lower newer"

								UpperCAmelCase			:   List[Any]      =				processor(text=lowercase  )

								UpperCAmelCase			:   List[Any]      =				tokenizer(lowercase  )

								for key in encoded_tok.keys():
													self.assertListEqual(encoded_tok[key]			,  encoded_processor[key]  )







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   List[Any]      =				self.get_image_processor()
								UpperCAmelCase			:   Optional[int]      =				self.get_tokenizer()

								UpperCAmelCase			:   Optional[Any]      =				CLIPProcessor(tokenizer=lowercase			,  image_processor=lowercase  )

								UpperCAmelCase			:   Optional[int]      =				"lower newer"
								UpperCAmelCase			:   Optional[int]      =				self.prepare_image_inputs()

								UpperCAmelCase			:   str      =				processor(text=lowercase			,  images=lowercase  )

								self.assertListEqual(list(inputs.keys()  )			,  ["input_ids", "attention_mask", "pixel_values"]  )

								# test if it raises when no input is passed
								with pytest.raises(lowercase  ):
													processor()







			def 			__lowerCAmelCase				(			self			: List[str]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Any      =				self.get_image_processor()
								UpperCAmelCase			:   Optional[Any]      =				self.get_tokenizer()

								UpperCAmelCase			:   Optional[Any]      =				CLIPProcessor(tokenizer=lowercase			,  image_processor=lowercase  )

								UpperCAmelCase			:   List[str]      =				[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

								UpperCAmelCase			:   Dict      =				processor.batch_decode(lowercase  )
								UpperCAmelCase			:   List[Any]      =				tokenizer.batch_decode(lowercase  )

								self.assertListEqual(lowercase			,  lowercase  )







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   int      =				self.get_image_processor()
								UpperCAmelCase			:   List[Any]      =				self.get_tokenizer()

								UpperCAmelCase			:   int      =				CLIPProcessor(tokenizer=lowercase			,  image_processor=lowercase  )

								UpperCAmelCase			:   str      =				"lower newer"
								UpperCAmelCase			:   Dict      =				self.prepare_image_inputs()

								UpperCAmelCase			:   Any      =				processor(text=lowercase			,  images=lowercase  )

								self.assertListEqual(list(inputs.keys()  )			,  processor.model_input_names  )





"""simple docstring"""






from __future__ import annotations

import unittest

from transformers import BlenderbotSmallConfig, BlenderbotSmallTokenizer, is_tf_available
from transformers.testing_utils import require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
	import tensorflow as tf

	from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel







@require_tf
class       snake_case__  :
			SCREAMING_SNAKE_CASE__     				=						BlenderbotSmallConfig
			SCREAMING_SNAKE_CASE__     				=						{}
			SCREAMING_SNAKE_CASE__     				=						'''gelu'''







			def __init__(			self			: Tuple			,  lowercase			: Any			,  lowercase			: Optional[Any]=13			,  lowercase			: Any=7			,  lowercase			: List[Any]=True			,  lowercase			: List[str]=False			,  lowercase			: Optional[Any]=99			,  lowercase			: Union[str, Any]=32			,  lowercase			: List[Any]=2			,  lowercase			: Tuple=4			,  lowercase			: Union[str, Any]=37			,  lowercase			: str=0.1			,  lowercase			: Optional[Any]=0.1			,  lowercase			: Optional[Any]=20			,  lowercase			: str=2			,  lowercase			: int=1			,  lowercase			: Optional[int]=0			,  ):





								'''simple docstring'''



								UpperCAmelCase			:   Dict      =				parent
								UpperCAmelCase			:   Union[str, Any]      =				batch_size
								UpperCAmelCase			:   Tuple      =				seq_length
								UpperCAmelCase			:   Dict      =				is_training
								UpperCAmelCase			:   str      =				use_labels
								UpperCAmelCase			:   Union[str, Any]      =				vocab_size
								UpperCAmelCase			:   Tuple      =				hidden_size
								UpperCAmelCase			:   List[Any]      =				num_hidden_layers
								UpperCAmelCase			:   Union[str, Any]      =				num_attention_heads
								UpperCAmelCase			:   Dict      =				intermediate_size

								UpperCAmelCase			:   Optional[int]      =				hidden_dropout_prob
								UpperCAmelCase			:   Optional[Any]      =				attention_probs_dropout_prob
								UpperCAmelCase			:   Dict      =				max_position_embeddings
								UpperCAmelCase			:   List[Any]      =				eos_token_id
								UpperCAmelCase			:   List[Any]      =				pad_token_id
								UpperCAmelCase			:   Union[str, Any]      =				bos_token_id







			def 			__lowerCAmelCase				(			self			: Union[str, Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   List[str]      =				ids_tensor([self.batch_size, self.seq_length - 1]			,  self.vocab_size  )
								UpperCAmelCase			:   Optional[int]      =				tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size  )			,  1  )
								UpperCAmelCase			:   Optional[int]      =				tf.concat([input_ids, eos_tensor]			,  axis=1  )

								UpperCAmelCase			:   str      =				ids_tensor([self.batch_size, self.seq_length]			,  self.vocab_size  )

								UpperCAmelCase			:   int      =				self.config_cls(
								    vocab_size=self.vocab_size			,  d_model=self.hidden_size			,  encoder_layers=self.num_hidden_layers			,  decoder_layers=self.num_hidden_layers			,  encoder_attention_heads=self.num_attention_heads			,  decoder_attention_heads=self.num_attention_heads			,  encoder_ffn_dim=self.intermediate_size			,  decoder_ffn_dim=self.intermediate_size			,  dropout=self.hidden_dropout_prob			,  attention_dropout=self.attention_probs_dropout_prob			,  max_position_embeddings=self.max_position_embeddings			,  eos_token_ids=[2]			,  bos_token_id=self.bos_token_id			,  pad_token_id=self.pad_token_id			,  decoder_start_token_id=self.pad_token_id			,  **self.config_updates			,  )
								UpperCAmelCase			:   Optional[Any]      =				prepare_blenderbot_small_inputs_dict(lowercase			,  lowercase			,  lowercase  )
								return config, inputs_dict







			def 			__lowerCAmelCase				(			self			: int			,  lowercase			: List[Any]			,  lowercase			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   List[Any]      =				TFBlenderbotSmallModel(config=lowercase  ).get_decoder()
								UpperCAmelCase			:   List[Any]      =				inputs_dict["input_ids"]

								UpperCAmelCase			:   int      =				input_ids[:1, :]
								UpperCAmelCase			:   Union[str, Any]      =				inputs_dict["attention_mask"][:1, :]
								UpperCAmelCase			:   Union[str, Any]      =				inputs_dict["head_mask"]
								UpperCAmelCase			:   List[Any]      =				1

								# first forward pass
								UpperCAmelCase			:   Optional[Any]      =				model(lowercase			,  attention_mask=lowercase			,  head_mask=lowercase			,  use_cache=lowercase  )

								UpperCAmelCase      ,							UpperCAmelCase			:   List[str]      =				outputs.to_tuple()

								# create hypothetical next token and extent to next_input_ids
								UpperCAmelCase			:   Any      =				ids_tensor((self.batch_size, 3)			,  config.vocab_size  )
								UpperCAmelCase			:   Optional[int]      =				tf.cast(ids_tensor((self.batch_size, 3)			,  2  )			,  tf.inta  )

								# append to next input_ids and
								UpperCAmelCase			:   Union[str, Any]      =				tf.concat([input_ids, next_tokens]			,  axis=-1  )
								UpperCAmelCase			:   str      =				tf.concat([attention_mask, next_attn_mask]			,  axis=-1  )

								UpperCAmelCase			:   Optional[Any]      =				model(lowercase			,  attention_mask=lowercase  )[0]
								UpperCAmelCase			:   List[str]      =				model(lowercase			,  attention_mask=lowercase			,  past_key_values=lowercase  )[0]

								self.parent.assertEqual(next_tokens.shape[1]			,  output_from_past.shape[1]  )

								# select random slice
								UpperCAmelCase			:   List[str]      =				int(ids_tensor((1,)			,  output_from_past.shape[-1]  )  )
								UpperCAmelCase			:   Optional[int]      =				output_from_no_past[:, -3:, random_slice_idx]
								UpperCAmelCase			:   Optional[Any]      =				output_from_past[:, :, random_slice_idx]

								# test that outputs are equal for slice
								tf.debugging.assert_near(lowercase			,  lowercase			,  rtol=1E-3  )



def   lowercase_	(    _lowercase       :		Optional[Any]							,      _lowercase       :		Optional[int]							,      _lowercase       :		Tuple							,      _lowercase       :		Tuple=None							,      _lowercase       :		Any=None							,      _lowercase       :		str=None							,      _lowercase       :		Optional[Any]=None							,      _lowercase       :		Dict=None							,      ):




					'''simple docstring'''






					if attention_mask is None:
										UpperCAmelCase			:   List[str]      =				tf.cast(tf.math.not_equal(_lowercase							,      config.pad_token_id	)							,      tf.inta	)
					if decoder_attention_mask is None:
										UpperCAmelCase			:   str      =				tf.concat(
										    [
										        tf.ones(decoder_input_ids[:, :1].shape							,      dtype=tf.inta	),
										        tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]							,      config.pad_token_id	)							,      tf.inta	),
										    ]							,      axis=-1							,      )
					if head_mask is None:
										UpperCAmelCase			:   Dict      =				tf.ones((config.encoder_layers, config.encoder_attention_heads)	)
					if decoder_head_mask is None:
										UpperCAmelCase			:   Optional[Any]      =				tf.ones((config.decoder_layers, config.decoder_attention_heads)	)
					if cross_attn_head_mask is None:
										UpperCAmelCase			:   str      =				tf.ones((config.decoder_layers, config.decoder_attention_heads)	)
					return {
					    "input_ids": input_ids,
					    "decoder_input_ids": decoder_input_ids,
					    "attention_mask": attention_mask,
					    "decoder_attention_mask": decoder_attention_mask,
					    "head_mask": head_mask,
					    "decoder_head_mask": decoder_head_mask,
					    "cross_attn_head_mask": cross_attn_head_mask,
					}







@require_tf
class       snake_case__  ( lowerCAmelCase_					,  lowerCAmelCase_					,  unittest.TestCase     ):
			SCREAMING_SNAKE_CASE__     				=						(
			    (TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel) if is_tf_available() else ()
			)
			SCREAMING_SNAKE_CASE__     				=						(TFBlenderbotSmallForConditionalGeneration,) if is_tf_available() else ()
			SCREAMING_SNAKE_CASE__     				=						(
			    {
			        '''conversational''': TFBlenderbotSmallForConditionalGeneration,
			        '''feature-extraction''': TFBlenderbotSmallModel,
			        '''summarization''': TFBlenderbotSmallForConditionalGeneration,
			        '''text2text-generation''': TFBlenderbotSmallForConditionalGeneration,
			        '''translation''': TFBlenderbotSmallForConditionalGeneration,
			    }
			    if is_tf_available()
			    else {}
			)
			SCREAMING_SNAKE_CASE__     				=						True
			SCREAMING_SNAKE_CASE__     				=						False
			SCREAMING_SNAKE_CASE__     				=						False







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Dict      =				TFBlenderbotSmallModelTester(self  )
								UpperCAmelCase			:   Dict      =				ConfigTester(self			,  config_class=lowercase  )







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								self.config_tester.run_common_tests()







			def 			__lowerCAmelCase				(			self			: Union[str, Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Dict      =				self.model_tester.prepare_config_and_inputs_for_common()
								self.model_tester.check_decoder_model_past_large_inputs(*lowercase  )







@require_tokenizers
@require_tf
class       snake_case__  ( unittest.TestCase     ):
			SCREAMING_SNAKE_CASE__     				=						[
			    '''Social anxiety\nWow, I am never shy. Do you have anxiety?\nYes. I end up sweating and blushing and feel like '''
			    '''  i\'m going to throw up.\nand why is that?'''
			]
			SCREAMING_SNAKE_CASE__     				=						'''facebook/blenderbot_small-90M'''







			@cached_property
			def 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



								return BlenderbotSmallTokenizer.from_pretrained("facebook/blenderbot-90M"  )







			@cached_property
			def 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



								UpperCAmelCase			:   Union[str, Any]      =				TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name  )
								return model







			@slow
			def 			__lowerCAmelCase				(			self			: Optional[int]  ):





								'''simple docstring'''



								UpperCAmelCase			:   List[Any]      =				self.tokenizer(self.src_text			,  return_tensors="tf"  )
								UpperCAmelCase			:   List[Any]      =				self.model.generate(
								    model_inputs.input_ids			,  attention_mask=model_inputs.attention_mask			,  num_beams=2			,  use_cache=lowercase			,  )
								UpperCAmelCase			:   Dict      =				self.tokenizer.batch_decode(generated_ids.numpy()			,  skip_special_tokens=lowercase  )[0]
								assert generated_words in (
								    "i don't know. i just feel like i'm going to throw up. it's not fun.",
								    "i'm not sure. i just feel like i've been feeling like i have to be in a certain place",
								    "i'm not sure. i just feel like i've been in a bad situation.",
								)


