






import numpy as np






def   UpperCamelCase  (       snake_case__     :   np.array  ):




     '''simple docstring'''






     return (2 / (1 + np.exp(-2 * vector  ))) - 1


if __name__ == "__main__":
     import doctest

     doctest.testmod()










import argparse
import os
from io import BytesIO
from pathlib import Path

import requests
from clip_retrieval.clip_client import ClipClient
from PIL import Image
from tqdm import tqdm






def   UpperCamelCase  (       snake_case__     :   str	,snake_case__     :   Dict	,snake_case__     :   List[str]  ):




     '''simple docstring'''






     __snake_case			:Tuple          =						1.5
     __snake_case			:Any          =						int(factor * num_class_images  )
     __snake_case			:List[str]          =						ClipClient(
         url="""https://knn.laion.ai/knn-service"""	,indice_name="""laion_400m"""	,num_images=snake_case__	,aesthetic_weight=0.1  )

     os.makedirs(f'''{class_data_dir}/images'''	,exist_ok=snake_case__  )
     if len(list(Path(f'''{class_data_dir}/images'''  ).iterdir()  )  ) >= num_class_images:
          return

     while True:
          __snake_case			:Optional[Any]          =						client.query(text=snake_case__  )
          if len(snake_case__  ) >= factor * num_class_images or num_images > 1e4:
               break
          else:
               __snake_case			:Tuple          =						int(factor * num_images  )
               __snake_case			:Any          =						ClipClient(
                   url="""https://knn.laion.ai/knn-service"""	,indice_name="""laion_400m"""	,num_images=snake_case__	,aesthetic_weight=0.1	,)

     __snake_case			:Dict          =						0
     __snake_case			:Tuple          =						0
     __snake_case			:Dict          =						tqdm(desc="""downloading real regularization images"""	,total=snake_case__  )

     with open(f'''{class_data_dir}/caption.txt'''	,"""w"""  ) as fa, open(f'''{class_data_dir}/urls.txt'''	,"""w"""  ) as fa, open(
         f'''{class_data_dir}/images.txt'''	,"""w"""  ) as fa:
          while total < num_class_images:
               __snake_case			:List[Any]          =						class_images[count]
               count += 1
               try:
                    __snake_case			:str          =						requests.get(images["""url"""]  )
                    if img.status_code == 200:
                         __snake_case			:Any          =						Image.open(BytesIO(img.content  )  )
                         with open(f'''{class_data_dir}/images/{total}.jpg'''	,"""wb"""  ) as f:
                              f.write(img.content  )
                         fa.write(images["""caption"""] + """\n"""  )
                         fa.write(images["""url"""] + """\n"""  )
                         fa.write(f'''{class_data_dir}/images/{total}.jpg''' + """\n"""  )
                         total += 1
                         pbar.update(1  )
                    else:
                         continue
               except Exception:
                    continue
     return






def   UpperCamelCase  (       ):




     '''simple docstring'''






     __snake_case			:List[str]          =						argparse.ArgumentParser(""""""	,add_help=snake_case__  )
     parser.add_argument("""--class_prompt"""	,help="""text prompt to retrieve images"""	,required=snake_case__	,type=snake_case__  )
     parser.add_argument("""--class_data_dir"""	,help="""path to save images"""	,required=snake_case__	,type=snake_case__  )
     parser.add_argument("""--num_class_images"""	,help="""number of images to download"""	,default=200	,type=snake_case__  )
     return parser.parse_args()


if __name__ == "__main__":
     lowerCamelCase__							=     parse_args()
     retrieve(args.class_prompt, args.class_data_dir, args.num_class_images)


