



'''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 CLIPSegProcessor, ViTImageProcessor






@require_vision
class 							lowercase__     (							unittest.TestCase       ):





	def      UpperCamelCase_  (			self :    int			):







							'''simple docstring'''
							_UpperCamelCase :		str			    =       tempfile.mkdtemp()

							# fmt: off
							_UpperCamelCase :		str			    =       ['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(lowerCamelCase__			,range(len(lowerCamelCase__			)			)			)			)
							_UpperCamelCase :		int			    =       ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>', '']
							_UpperCamelCase :		Optional[int]			    =       {'unk_token': '<unk>'}

							_UpperCamelCase :		Dict			    =       os.path.join(self.tmpdirname			,VOCAB_FILES_NAMES['vocab_file']			)
							_UpperCamelCase :		Optional[int]			    =       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(lowerCamelCase__			) + '\n'			)
							with open(self.merges_file			,'w'			,encoding='utf-8'			) as fp:
													fp.write('\n'.join(lowerCamelCase__			)			)

							_UpperCamelCase :		str			    =       {
							    '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 :		Dict			    =       os.path.join(self.tmpdirname			,lowerCamelCase__			)
							with open(self.image_processor_file			,'w'			,encoding='utf-8'			) as fp:
													json.dump(lowerCamelCase__			,lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    List[str]			,**lowerCamelCase__ :    Tuple			):







							'''simple docstring'''
							return CLIPTokenizer.from_pretrained(self.tmpdirname			,**lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    int			,**lowerCamelCase__ :    str			):







							'''simple docstring'''
							return CLIPTokenizerFast.from_pretrained(self.tmpdirname			,**lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    int			,**lowerCamelCase__ :    Optional[Any]			):







							'''simple docstring'''
							return ViTImageProcessor.from_pretrained(self.tmpdirname			,**lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							shutil.rmtree(self.tmpdirname			)





	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[int]			    =       [np.random.randint(255			,size=(3, 30, 400)			,dtype=np.uinta			)]

							_UpperCamelCase :		Dict			    =       [Image.fromarray(np.moveaxis(lowerCamelCase__			,0			,-1			)			) for x in image_inputs]

							return image_inputs





	def      UpperCamelCase_  (			self :    Any			):







							'''simple docstring'''
							_UpperCamelCase :		List[Any]			    =       self.get_tokenizer()
							_UpperCamelCase :		List[str]			    =       self.get_rust_tokenizer()
							_UpperCamelCase :		Dict			    =       self.get_image_processor()

							_UpperCamelCase :		Tuple			    =       CLIPSegProcessor(tokenizer=lowerCamelCase__			,image_processor=lowerCamelCase__			)
							processor_slow.save_pretrained(self.tmpdirname			)
							_UpperCamelCase :		List[str]			    =       CLIPSegProcessor.from_pretrained(self.tmpdirname			,use_fast=lowerCamelCase__			)

							_UpperCamelCase :		Tuple			    =       CLIPSegProcessor(tokenizer=lowerCamelCase__			,image_processor=lowerCamelCase__			)
							processor_fast.save_pretrained(self.tmpdirname			)
							_UpperCamelCase :		List[str]			    =       CLIPSegProcessor.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			,lowerCamelCase__			)
							self.assertIsInstance(processor_fast.tokenizer			,lowerCamelCase__			)

							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			,lowerCamelCase__			)
							self.assertIsInstance(processor_fast.image_processor			,lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    int			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[Any]			    =       CLIPSegProcessor(tokenizer=self.get_tokenizer()			,image_processor=self.get_image_processor()			)
							processor.save_pretrained(self.tmpdirname			)

							_UpperCamelCase :		Dict			    =       self.get_tokenizer(bos_token='(BOS)'			,eos_token='(EOS)'			)
							_UpperCamelCase :		Union[str, Any]			    =       self.get_image_processor(do_normalize=lowerCamelCase__			,padding_value=1.0			)

							_UpperCamelCase :		List[str]			    =       CLIPSegProcessor.from_pretrained(
							    self.tmpdirname			,bos_token='(BOS)'			,eos_token='(EOS)'			,do_normalize=lowerCamelCase__			,padding_value=1.0			)

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

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





	def      UpperCamelCase_  (			self :    Optional[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Any			    =       self.get_image_processor()
							_UpperCamelCase :		List[str]			    =       self.get_tokenizer()

							_UpperCamelCase :		Optional[Any]			    =       CLIPSegProcessor(tokenizer=lowerCamelCase__			,image_processor=lowerCamelCase__			)

							_UpperCamelCase :		Dict			    =       self.prepare_image_inputs()

							_UpperCamelCase :		int			    =       image_processor(lowerCamelCase__			,return_tensors='np'			)
							_UpperCamelCase :		Dict			    =       processor(images=lowerCamelCase__			,return_tensors='np'			)

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





	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							_UpperCamelCase :		int			    =       self.get_image_processor()
							_UpperCamelCase :		Any			    =       self.get_tokenizer()

							_UpperCamelCase :		Union[str, Any]			    =       CLIPSegProcessor(tokenizer=lowerCamelCase__			,image_processor=lowerCamelCase__			)

							_UpperCamelCase :		Dict			    =       'lower newer'

							_UpperCamelCase :		Dict			    =       processor(text=lowerCamelCase__			)

							_UpperCamelCase :		int			    =       tokenizer(lowerCamelCase__			)

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





	def      UpperCamelCase_  (			self :    Any			):







							'''simple docstring'''
							_UpperCamelCase :		Union[str, Any]			    =       self.get_image_processor()
							_UpperCamelCase :		Union[str, Any]			    =       self.get_tokenizer()

							_UpperCamelCase :		Optional[Any]			    =       CLIPSegProcessor(tokenizer=lowerCamelCase__			,image_processor=lowerCamelCase__			)

							_UpperCamelCase :		List[str]			    =       'lower newer'
							_UpperCamelCase :		List[Any]			    =       self.prepare_image_inputs()

							_UpperCamelCase :		Tuple			    =       processor(text=lowerCamelCase__			,images=lowerCamelCase__			)

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

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





	def      UpperCamelCase_  (			self :    Dict			):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       self.get_image_processor()
							_UpperCamelCase :		List[str]			    =       self.get_tokenizer()

							_UpperCamelCase :		List[str]			    =       CLIPSegProcessor(tokenizer=lowerCamelCase__			,image_processor=lowerCamelCase__			)

							_UpperCamelCase :		Dict			    =       self.prepare_image_inputs()
							_UpperCamelCase :		int			    =       self.prepare_image_inputs()

							_UpperCamelCase :		int			    =       processor(images=lowerCamelCase__			,visual_prompt=lowerCamelCase__			)

							self.assertListEqual(list(inputs.keys()			)			,['pixel_values', 'conditional_pixel_values']			)

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





	def      UpperCamelCase_  (			self :    str			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[int]			    =       self.get_image_processor()
							_UpperCamelCase :		Union[str, Any]			    =       self.get_tokenizer()

							_UpperCamelCase :		Optional[Any]			    =       CLIPSegProcessor(tokenizer=lowerCamelCase__			,image_processor=lowerCamelCase__			)

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

							_UpperCamelCase :		Dict			    =       processor.batch_decode(lowerCamelCase__			)
							_UpperCamelCase :		Any			    =       tokenizer.batch_decode(lowerCamelCase__			)

							self.assertListEqual(lowerCamelCase__			,lowerCamelCase__			)







'''simple docstring'''
from datetime import datetime

import requests






def A__   (					UpperCAmelCase_							):
						_UpperCamelCase :		List[Any]			    =       'https://downloadgram.net/wp-json/wppress/video-downloader/video?url='
						_UpperCamelCase :		Optional[int]			    =       requests.get(base_url + url							).json()[0]['urls'][0]['src']
						return requests.get(UpperCAmelCase_							).content


if __name__ == "__main__":
					snake_case_			:     List[str]  	=  input('Enter Video/IGTV url: ').strip()
					snake_case_			:     str  	=  F"""{datetime.now():%Y-%m-%d_%H:%M:%S}.mp4"""
					with open(file_name, 'wb') as fp:
										fp.write(download_video(url))
					print(F"""Done. Video saved to disk as {file_name}.""")


