


import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_flax_cross_test,
    require_flax,
    require_torch,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import is_flax_available, is_torch_available, is_vision_available

from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_flax_bert import FlaxBertModelTester
from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester
from ..vit.test_modeling_flax_vit import FlaxViTModelTester


if is_flax_available():
						from transformers import (
						    FlaxBertModel,
						    FlaxCLIPVisionModel,
						    FlaxVisionTextDualEncoderModel,
						    FlaxViTModel,
						    VisionTextDualEncoderConfig,
						    VisionTextDualEncoderProcessor,
						)
						from transformers.modeling_flax_pytorch_utils import (
						    convert_pytorch_state_dict_to_flax,
						    load_flax_weights_in_pytorch_model,
						)


if is_torch_available():
						import torch

						from transformers import VisionTextDualEncoderModel

if is_vision_available():
						from PIL import Image



def 			lowerCAmelCase (		lowerCAmelCase_ )-> Optional[Any]:



				if isinstance(lowerCAmelCase_ ,   collections.abc.Iterable ):
								return x
				return (x, x)







@require_flax
class 				snake_case__:







						'''simple docstring'''




						def 						lowercase_ (				self   ,						__lowercase   ,						__lowercase							)					->    Optional[Any]:
										pass




						def 						lowercase_ (				self							)					->    List[str]:
										pass




						def 						lowercase_ (				self							)					->    int:
										pass




						def 						lowercase_ (				self   ,						__lowercase   ,						__lowercase   ,						__lowercase							)					->    Tuple:
										lowerCAmelCase_ :      Optional[int]    				=				np.abs((a - b)							).max()
										self.assertLessEqual(snake_case__   ,						snake_case__   ,						f"""Difference between torch and flax is {diff} (>= {tol})."""							)




						def 						lowercase_ (				self   ,						__lowercase   ,						__lowercase   ,						__lowercase   ,						__lowercase   ,						__lowercase=None   ,						**__lowercase							)					->    Any:
										lowerCAmelCase_ :      List[Any]    				=				VisionTextDualEncoderConfig.from_vision_text_configs(snake_case__   ,						snake_case__							)

										lowerCAmelCase_ :      Any    				=				FlaxVisionTextDualEncoderModel(snake_case__							)

										lowerCAmelCase_ :      List[str]    				=				model(input_ids=snake_case__   ,						pixel_values=snake_case__   ,						attention_mask=snake_case__							)

										self.assertEqual(output['''text_embeds'''].shape   ,						(input_ids.shape[0], config.projection_dim)							)
										self.assertEqual(output['''image_embeds'''].shape   ,						(pixel_values.shape[0], config.projection_dim)							)




						def 						lowercase_ (				self   ,						__lowercase   ,						__lowercase   ,						__lowercase   ,						__lowercase   ,						__lowercase=None   ,						**__lowercase							)					->    Union[str, Any]:
										lowerCAmelCase_ :      Any    				=				self.get_vision_text_model(snake_case__   ,						snake_case__							)
										lowerCAmelCase_ :      Any    				=				{"vision_model": vision_model, "text_model": text_model}
										lowerCAmelCase_ :      Optional[Any]    				=				FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**snake_case__							)

										lowerCAmelCase_ :      int    				=				model(input_ids=snake_case__   ,						pixel_values=snake_case__   ,						attention_mask=snake_case__							)

										self.assertEqual(output['''text_embeds'''].shape   ,						(input_ids.shape[0], model.config.projection_dim)							)
										self.assertEqual(output['''image_embeds'''].shape   ,						(pixel_values.shape[0], model.config.projection_dim)							)




						def 						lowercase_ (				self   ,						__lowercase   ,						__lowercase   ,						__lowercase   ,						__lowercase   ,						__lowercase=None   ,						**__lowercase							)					->    Optional[int]:
										lowerCAmelCase_ :      Union[str, Any]    				=				self.get_vision_text_model(snake_case__   ,						snake_case__							)
										lowerCAmelCase_ :      int    				=				{"vision_model": vision_model, "text_model": text_model}
										lowerCAmelCase_ :      Tuple    				=				FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**snake_case__							)

										lowerCAmelCase_ :      Optional[int]    				=				model(input_ids=snake_case__   ,						pixel_values=snake_case__   ,						attention_mask=snake_case__							)
										lowerCAmelCase_ :      int    				=				output[0]

										with tempfile.TemporaryDirectory() as tmpdirname:
														model.save_pretrained(snake_case__							)
														lowerCAmelCase_ :      Any    				=				FlaxVisionTextDualEncoderModel.from_pretrained(snake_case__							)

														lowerCAmelCase_ :      Union[str, Any]    				=				model(input_ids=snake_case__   ,						pixel_values=snake_case__   ,						attention_mask=snake_case__							)
														lowerCAmelCase_ :      Optional[Any]    				=				after_output[0]
														lowerCAmelCase_ :      List[str]    				=				np.amax(np.abs(out_a - out_a							)							)
														self.assertLessEqual(snake_case__   ,						1e-3							)




						def 						lowercase_ (				self   ,						__lowercase   ,						__lowercase   ,						__lowercase   ,						__lowercase   ,						__lowercase=None   ,						**__lowercase							)					->    Any:
										lowerCAmelCase_ :      Union[str, Any]    				=				self.get_vision_text_model(snake_case__   ,						snake_case__							)
										lowerCAmelCase_ :      Tuple    				=				{"vision_model": vision_model, "text_model": text_model}
										lowerCAmelCase_ :      Any    				=				FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**snake_case__							)

										lowerCAmelCase_ :      Union[str, Any]    				=				model(
										    input_ids=snake_case__   ,						pixel_values=snake_case__   ,						attention_mask=snake_case__   ,						output_attentions=snake_case__							)

										lowerCAmelCase_ :      Tuple    				=				output.vision_model_output.attentions
										self.assertEqual(len(snake_case__							)   ,						vision_config.num_hidden_layers							)

										# in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
										lowerCAmelCase_ :      List[Any]    				=				to_atuple(vision_model.config.image_size							)
										lowerCAmelCase_ :      Dict    				=				to_atuple(vision_model.config.patch_size							)
										lowerCAmelCase_ :      Tuple    				=				(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
										lowerCAmelCase_ :      List[str]    				=				num_patches + 1
										self.assertEqual(vision_attentions[0].shape[-3:]   ,						(vision_config.num_attention_heads, seq_len, seq_len)							)

										lowerCAmelCase_ :      int    				=				output.text_model_output.attentions
										self.assertEqual(len(snake_case__							)   ,						text_config.num_hidden_layers							)

										self.assertEqual(
										    text_attentions[0].shape[-3:]   ,						(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])   ,						)




						def 						lowercase_ (				self   ,						__lowercase   ,						__lowercase   ,						__lowercase							)					->    int:
										pt_model.to(snake_case__							)
										pt_model.eval()

										# prepare inputs
										lowerCAmelCase_ :      Dict    				=				inputs_dict
										lowerCAmelCase_ :      Tuple    				=				{k: torch.tensor(v.tolist()							) for k, v in flax_inputs.items()}

										with torch.no_grad():
														lowerCAmelCase_ :      Union[str, Any]    				=				pt_model(**snake_case__							).to_tuple()

										lowerCAmelCase_ :      Optional[int]    				=				fx_model(**snake_case__							).to_tuple()
										self.assertEqual(len(snake_case__							)   ,						len(snake_case__							)   ,						'''Output lengths differ between Flax and PyTorch'''							)
										for fx_output, pt_output in zip(fx_outputs[:4]   ,						pt_outputs[:4]							):
														self.assert_almost_equals(snake_case__   ,						pt_output.numpy()   ,						4e-2							)

										# PT -> Flax
										with tempfile.TemporaryDirectory() as tmpdirname:
														pt_model.save_pretrained(snake_case__							)
														lowerCAmelCase_ :      Union[str, Any]    				=				FlaxVisionTextDualEncoderModel.from_pretrained(snake_case__   ,						from_pt=snake_case__							)

										lowerCAmelCase_ :      Union[str, Any]    				=				fx_model_loaded(**snake_case__							).to_tuple()
										self.assertEqual(len(snake_case__							)   ,						len(snake_case__							)   ,						'''Output lengths differ between Flax and PyTorch'''							)
										for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4]   ,						pt_outputs[:4]							):
														self.assert_almost_equals(snake_case__   ,						pt_output.numpy()   ,						4e-2							)

										# Flax -> PT
										with tempfile.TemporaryDirectory() as tmpdirname:
														fx_model.save_pretrained(snake_case__							)
														lowerCAmelCase_ :      Optional[Any]    				=				VisionTextDualEncoderModel.from_pretrained(snake_case__   ,						from_flax=snake_case__							)

										pt_model_loaded.to(snake_case__							)
										pt_model_loaded.eval()

										with torch.no_grad():
														lowerCAmelCase_ :      List[str]    				=				pt_model_loaded(**snake_case__							).to_tuple()

										self.assertEqual(len(snake_case__							)   ,						len(snake_case__							)   ,						'''Output lengths differ between Flax and PyTorch'''							)
										for fx_output, pt_output_loaded in zip(fx_outputs[:4]   ,						pt_outputs_loaded[:4]							):
														self.assert_almost_equals(snake_case__   ,						pt_output_loaded.numpy()   ,						4e-2							)




						def 						lowercase_ (				self   ,						__lowercase   ,						__lowercase   ,						__lowercase							)					->    Tuple:
										lowerCAmelCase_ :      str    				=				VisionTextDualEncoderConfig.from_vision_text_configs(snake_case__   ,						snake_case__							)

										lowerCAmelCase_ :      Tuple    				=				VisionTextDualEncoderModel(snake_case__							)
										lowerCAmelCase_ :      Optional[int]    				=				FlaxVisionTextDualEncoderModel(snake_case__							)

										lowerCAmelCase_ :      Optional[Any]    				=				convert_pytorch_state_dict_to_flax(pt_model.state_dict()   ,						snake_case__							)
										lowerCAmelCase_ :      Optional[int]    				=				fx_state

										self.check_pt_flax_equivalence(snake_case__   ,						snake_case__   ,						snake_case__							)




						def 						lowercase_ (				self   ,						__lowercase   ,						__lowercase   ,						__lowercase							)					->    int:
										lowerCAmelCase_ :      Union[str, Any]    				=				VisionTextDualEncoderConfig.from_vision_text_configs(snake_case__   ,						snake_case__							)

										lowerCAmelCase_ :      Dict    				=				VisionTextDualEncoderModel(snake_case__							)
										lowerCAmelCase_ :      Optional[Any]    				=				FlaxVisionTextDualEncoderModel(snake_case__							)

										lowerCAmelCase_ :      Tuple    				=				load_flax_weights_in_pytorch_model(snake_case__   ,						fx_model.params							)

										self.check_pt_flax_equivalence(snake_case__   ,						snake_case__   ,						snake_case__							)




						def 						lowercase_ (				self							)					->    Dict:
										lowerCAmelCase_ :      int    				=				self.prepare_config_and_inputs()
										self.check_model_from_pretrained_configs(**snake_case__							)




						def 						lowercase_ (				self							)					->    Dict:
										lowerCAmelCase_ :      Any    				=				self.prepare_config_and_inputs()
										self.check_vision_text_dual_encoder_from_pretrained(**snake_case__							)




						def 						lowercase_ (				self							)					->    Tuple:
										lowerCAmelCase_ :      int    				=				self.prepare_config_and_inputs()
										self.check_save_load(**snake_case__							)




						def 						lowercase_ (				self							)					->    Dict:
										lowerCAmelCase_ :      Optional[int]    				=				self.prepare_config_and_inputs()
										self.check_vision_text_output_attention(**snake_case__							)




						@is_pt_flax_cross_test
						def 						lowercase_ (				self							)					->    Union[str, Any]:
										lowerCAmelCase_ :      Tuple    				=				self.prepare_config_and_inputs()
										lowerCAmelCase_ :      Dict    				=				config_inputs_dict.pop('''vision_config'''							)
										lowerCAmelCase_ :      List[str]    				=				config_inputs_dict.pop('''text_config'''							)

										lowerCAmelCase_ :      Optional[int]    				=				config_inputs_dict

										self.check_equivalence_pt_to_flax(snake_case__   ,						snake_case__   ,						snake_case__							)
										self.check_equivalence_flax_to_pt(snake_case__   ,						snake_case__   ,						snake_case__							)




						@slow
						def 						lowercase_ (				self							)					->    Optional[Any]:
										lowerCAmelCase_ :      Optional[int]    				=				self.get_pretrained_model_and_inputs()

										lowerCAmelCase_ :      Optional[Any]    				=				model_a(**snake_case__							)
										lowerCAmelCase_ :      List[str]    				=				outputs[0]

										with tempfile.TemporaryDirectory() as tmp_dirname:
														model_a.save_pretrained(snake_case__							)
														lowerCAmelCase_ :      Optional[int]    				=				FlaxVisionTextDualEncoderModel.from_pretrained(snake_case__							)

														lowerCAmelCase_ :      Union[str, Any]    				=				model_a(**snake_case__							)
														lowerCAmelCase_ :      Optional[Any]    				=				after_outputs[0]
														lowerCAmelCase_ :      Optional[Any]    				=				np.amax(np.abs(out_a - out_a							)							)
														self.assertLessEqual(snake_case__   ,						1e-5							)







@require_flax
class 				snake_case__(							SCREAMING_SNAKE_CASE__,						unittest.TestCase							):







						'''simple docstring'''




						def 						lowercase_ (				self							)					->    str:
										lowerCAmelCase_ :      List[str]    				=				FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
										    '''hf-internal-testing/tiny-random-vit'''   ,						'''hf-internal-testing/tiny-bert'''   ,						vision_from_pt=snake_case__   ,						text_from_pt=snake_case__   ,						)
										lowerCAmelCase_ :      Tuple    				=				1_3
										lowerCAmelCase_ :      List[Any]    				=				floats_tensor(
										    [
										        batch_size,
										        model.config.vision_config.num_channels,
										        model.config.vision_config.image_size,
										        model.config.vision_config.image_size,
										    ]							)
										lowerCAmelCase_ :      List[Any]    				=				ids_tensor([batch_size, 4]   ,						model.config.text_config.vocab_size							)
										lowerCAmelCase_ :      List[str]    				=				random_attention_mask([batch_size, 4]							)
										lowerCAmelCase_ :      List[str]    				=				{"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

										return model, inputs




						def 						lowercase_ (				self   ,						__lowercase   ,						__lowercase							)					->    str:
										lowerCAmelCase_ :      Union[str, Any]    				=				FlaxViTModel(snake_case__							)
										lowerCAmelCase_ :      Optional[Any]    				=				FlaxBertModel(snake_case__							)
										return vision_model, text_model




						def 						lowercase_ (				self							)					->    str:
										lowerCAmelCase_ :      Any    				=				FlaxViTModelTester(self							)
										lowerCAmelCase_ :      List[str]    				=				FlaxBertModelTester(self							)
										lowerCAmelCase_ :      Tuple    				=				vit_model_tester.prepare_config_and_inputs()
										lowerCAmelCase_ :      List[str]    				=				bert_model_tester.prepare_config_and_inputs()

										lowerCAmelCase_ :      str    				=				vision_config_and_inputs

										lowerCAmelCase_ :      str    				=				text_config_and_inputs

										# make sure that cross attention layers are added
										return {
										    "text_config": text_config,
										    "vision_config": vision_config,
										    "pixel_values": pixel_values,
										    "attention_mask": attention_mask,
										    "input_ids": input_ids,
										    "token_type_ids": token_type_ids,
										}







@require_torch
class 				snake_case__(							SCREAMING_SNAKE_CASE__,						unittest.TestCase							):







						'''simple docstring'''




						def 						lowercase_ (				self							)					->    List[Any]:
										lowerCAmelCase_ :      List[str]    				=				FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
										    '''hf-internal-testing/tiny-random-clip'''   ,						'''hf-internal-testing/tiny-bert'''   ,						vision_from_pt=snake_case__   ,						text_from_pt=snake_case__   ,						)
										lowerCAmelCase_ :      Tuple    				=				1_3
										lowerCAmelCase_ :      Any    				=				floats_tensor(
										    [
										        batch_size,
										        model.config.vision_config.num_channels,
										        model.config.vision_config.image_size,
										        model.config.vision_config.image_size,
										    ]							)
										lowerCAmelCase_ :      List[Any]    				=				ids_tensor([batch_size, 4]   ,						model.config.text_config.vocab_size							)
										lowerCAmelCase_ :      List[Any]    				=				random_attention_mask([batch_size, 4]							)
										lowerCAmelCase_ :      Tuple    				=				{"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

										return model, inputs




						def 						lowercase_ (				self   ,						__lowercase   ,						__lowercase							)					->    Union[str, Any]:
										lowerCAmelCase_ :      Any    				=				FlaxCLIPVisionModel(snake_case__							)
										lowerCAmelCase_ :      Optional[int]    				=				FlaxBertModel(snake_case__							)
										return vision_model, text_model




						def 						lowercase_ (				self							)					->    Optional[Any]:
										lowerCAmelCase_ :      List[Any]    				=				FlaxCLIPVisionModelTester(self							)
										lowerCAmelCase_ :      Union[str, Any]    				=				FlaxBertModelTester(self							)
										lowerCAmelCase_ :      Any    				=				clip_model_tester.prepare_config_and_inputs()
										lowerCAmelCase_ :      Union[str, Any]    				=				bert_model_tester.prepare_config_and_inputs()

										lowerCAmelCase_ :      Optional[int]    				=				vision_config_and_inputs

										lowerCAmelCase_ :      Dict    				=				text_config_and_inputs

										# make sure that cross attention layers are added
										return {
										    "text_config": text_config,
										    "vision_config": vision_config,
										    "pixel_values": pixel_values,
										    "attention_mask": attention_mask,
										    "input_ids": input_ids,
										    "token_type_ids": token_type_ids,
										}







@require_flax
@require_vision
class 				snake_case__(							unittest.TestCase							):







						'''simple docstring'''




						@slow
						def 						lowercase_ (				self							)					->    Optional[Any]:
										lowerCAmelCase_ :      Tuple    				=				FlaxVisionTextDualEncoderModel.from_pretrained('''clip-italian/clip-italian'''   ,						logit_scale_init_value=1.0							)
										lowerCAmelCase_ :      Tuple    				=				VisionTextDualEncoderProcessor.from_pretrained('''clip-italian/clip-italian'''							)

										lowerCAmelCase_ :      List[str]    				=				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
										lowerCAmelCase_ :      int    				=				processor(
										    text=['''una foto di un gatto''', '''una foto di un cane''']   ,						images=snake_case__   ,						padding=snake_case__   ,						return_tensors='''np'''							)

										lowerCAmelCase_ :      Any    				=				model(**snake_case__							)

										# verify the logits
										self.assertEqual(outputs.logits_per_image.shape   ,						(inputs.pixel_values.shape[0], inputs.input_ids.shape[0])							)
										self.assertEqual(
										    outputs.logits_per_text.shape   ,						(inputs.input_ids.shape[0], inputs.pixel_values.shape[0])   ,						)

										lowerCAmelCase_ :      Tuple    				=				np.array([[1.2_28_47_27, 0.3_10_41_22]]							)

										self.assertTrue(np.allclose(outputs.logits_per_image   ,						snake_case__   ,						atol=1e-3							)							)



import csv

import tweepy

# Twitter API credentials
_UpperCAmelCase			:  int     =""""""
_UpperCAmelCase			:  Optional[int]     =""""""
_UpperCAmelCase			:  Dict     =""""""
_UpperCAmelCase			:  str     =""""""



def 			lowerCAmelCase (		lowerCAmelCase_ )-> None:
				# authorize twitter, initialize tweepy
				lowerCAmelCase_ :      Optional[int]    				=				tweepy.OAuthHandler(lowerCAmelCase_ ,   lowerCAmelCase_ )
				auth.set_access_token(lowerCAmelCase_ ,   lowerCAmelCase_ )
				lowerCAmelCase_ :      Any    				=				tweepy.API(lowerCAmelCase_ )

				# initialize a list to hold all the tweepy Tweets
				lowerCAmelCase_ :      Dict    				=				[]

				# make initial request for most recent tweets (200 is the maximum allowed count)
				lowerCAmelCase_ :      Optional[int]    				=				api.user_timeline(screen_name=lowerCAmelCase_ ,   count=200 )

				# save most recent tweets
				alltweets.extend(lowerCAmelCase_ )

				# save the id of the oldest tweet less one
				lowerCAmelCase_ :      str    				=				alltweets[-1].id - 1

				# keep grabbing tweets until there are no tweets left to grab
				while len(lowerCAmelCase_ ) > 0:
								print(f"""getting tweets before {oldest}""" )

								# all subsequent requests use the max_id param to prevent duplicates
								lowerCAmelCase_ :      Optional[Any]    				=				api.user_timeline(
								    screen_name=lowerCAmelCase_ ,   count=200 ,   max_id=lowerCAmelCase_ )

								# save most recent tweets
								alltweets.extend(lowerCAmelCase_ )

								# update the id of the oldest tweet less one
								lowerCAmelCase_ :      Optional[Any]    				=				alltweets[-1].id - 1

								print(f"""...{len(lowerCAmelCase_ )} tweets downloaded so far""" )

				# transform the tweepy tweets into a 2D array that will populate the csv
				lowerCAmelCase_ :      Union[str, Any]    				=				[[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets]

				# write the csv
				with open(f"""new_{screen_name}_tweets.csv""" ,   '''w''' ) as f:
								lowerCAmelCase_ :      Optional[int]    				=				csv.writer(lowerCAmelCase_ )
								writer.writerow(['''id''', '''created_at''', '''text'''] )
								writer.writerows(lowerCAmelCase_ )


if __name__ == "__main__":
						# pass in the username of the account you want to download
						get_all_tweets("""FirePing32""")