






import argparse
import json

from tqdm import tqdm


def 				__lowerCamelCase     (							)     ->  Optional[int]:
   __UpperCamelCase	:			Any					=					argparse.ArgumentParser()

   # Required parameters
   parser.add_argument(
       """--src_path"""					,							type=__lowerCAmelCase					,							default="""biencoder-nq-dev.json"""					,							help="""Path to raw DPR training data"""					,							)
   parser.add_argument(
       """--evaluation_set"""					,							type=__lowerCAmelCase					,							help="""where to store parsed evaluation_set file"""					,							)
   parser.add_argument(
       """--gold_data_path"""					,							type=__lowerCAmelCase					,							help="""where to store parsed gold_data_path file"""					,							)
   __UpperCamelCase	:			int					=					parser.parse_args()

   with open(args.src_path					,							"""r"""							) as src_file, open(args.evaluation_set					,							"""w"""							) as eval_file, open(
       args.gold_data_path					,							"""w"""							) as gold_file:
      __UpperCamelCase	:			str					=					json.load(__lowerCAmelCase							)
      for dpr_record in tqdm(__lowerCAmelCase							):
         __UpperCamelCase	:			Union[str, Any]					=					dpr_record["""question"""]
         __UpperCamelCase	:			List[str]					=					[context["""title"""] for context in dpr_record["""positive_ctxs"""]]
         eval_file.write(question + """\n"""							)
         gold_file.write("""\t""".join(__lowerCAmelCase							) + """\n"""							)


if __name__ == "__main__":
       main()











from typing import Any, Callable, Dict, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


UpperCamelCase    							=      'CompVis/stable-diffusion-v1-1'
UpperCamelCase    							=      'CompVis/stable-diffusion-v1-2'
UpperCamelCase    							=      'CompVis/stable-diffusion-v1-3'
UpperCamelCase    							=      'CompVis/stable-diffusion-v1-4'







class 		_A						( UpperCAmelCase_ ):

  def __init__(    self						:	List[str]   ,				lowerCamelCase__						:	AutoencoderKL   ,				lowerCamelCase__						:	CLIPTextModel   ,				lowerCamelCase__						:	CLIPTokenizer   ,				lowerCamelCase__						:	UNetaDConditionModel   ,				lowerCamelCase__						:	Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]   ,				lowerCamelCase__						:	StableDiffusionSafetyChecker   ,				lowerCamelCase__						:	CLIPImageProcessor   ,				lowerCamelCase__						:	bool = True   ,				):
     """simple docstring"""

     super()._init_()

     __UpperCamelCase	:			Optional[Any]					=					StableDiffusionPipeline.from_pretrained(lowerCamelCase__			)
     __UpperCamelCase	:			int					=					StableDiffusionPipeline.from_pretrained(lowerCamelCase__			)
     __UpperCamelCase	:			List[str]					=					StableDiffusionPipeline.from_pretrained(lowerCamelCase__			)
     __UpperCamelCase	:			str					=					StableDiffusionPipeline(
         vae=lowerCamelCase__   ,				text_encoder=lowerCamelCase__   ,				tokenizer=lowerCamelCase__   ,				unet=lowerCamelCase__   ,				scheduler=lowerCamelCase__   ,				safety_checker=lowerCamelCase__   ,				feature_extractor=lowerCamelCase__   ,				requires_safety_checker=lowerCamelCase__   ,				)

     self.register_modules(pipelinea=self.pipea   ,				pipelinea=self.pipea   ,				pipelinea=self.pipea   ,				pipelinea=self.pipea			)

  @property
  def    a					(    self						:	Optional[Any]			):
     """simple docstring"""

     return {k: getattr(self   ,				lowerCamelCase__			) for k in self.config.keys() if not k.startswith("""_"""			)}

  def    a					(    self						:	List[str]   ,				lowerCamelCase__						:	Optional[Union[str, int]] = "auto"			):
     """simple docstring"""

     if slice_size == "auto":
        # half the attention head size is usually a good trade-off between
        # speed and memory
        __UpperCamelCase	:			Dict					=					self.unet.config.attention_head_dim // 2
     self.unet.set_attention_slice(lowerCamelCase__			)

  def    a					(    self						:	str			):
     """simple docstring"""

     self.enable_attention_slicing(lowerCamelCase__			)

  @torch.no_grad()
  def    a					(    self						:	Any   ,				lowerCamelCase__						:	Union[str, List[str]]   ,				lowerCamelCase__						:	int = 5_12   ,				lowerCamelCase__						:	int = 5_12   ,				lowerCamelCase__						:	int = 50   ,				lowerCamelCase__						:	float = 7.5   ,				lowerCamelCase__						:	Optional[Union[str, List[str]]] = None   ,				lowerCamelCase__						:	Optional[int] = 1   ,				lowerCamelCase__						:	float = 0.0   ,				lowerCamelCase__						:	Optional[torch.Generator] = None   ,				lowerCamelCase__						:	Optional[torch.FloatTensor] = None   ,				lowerCamelCase__						:	Optional[str] = "pil"   ,				lowerCamelCase__						:	bool = True   ,				lowerCamelCase__						:	Optional[Callable[[int, int, torch.FloatTensor], None]] = None   ,				lowerCamelCase__						:	int = 1   ,				**lowerCamelCase__						:	str   ,				):
     """simple docstring"""

     return self.pipea(
         prompt=lowerCamelCase__   ,				height=lowerCamelCase__   ,				width=lowerCamelCase__   ,				num_inference_steps=lowerCamelCase__   ,				guidance_scale=lowerCamelCase__   ,				negative_prompt=lowerCamelCase__   ,				num_images_per_prompt=lowerCamelCase__   ,				eta=lowerCamelCase__   ,				generator=lowerCamelCase__   ,				latents=lowerCamelCase__   ,				output_type=lowerCamelCase__   ,				return_dict=lowerCamelCase__   ,				callback=lowerCamelCase__   ,				callback_steps=lowerCamelCase__   ,				**lowerCamelCase__   ,				)

  @torch.no_grad()
  def    a					(    self						:	Union[str, Any]   ,				lowerCamelCase__						:	Union[str, List[str]]   ,				lowerCamelCase__						:	int = 5_12   ,				lowerCamelCase__						:	int = 5_12   ,				lowerCamelCase__						:	int = 50   ,				lowerCamelCase__						:	float = 7.5   ,				lowerCamelCase__						:	Optional[Union[str, List[str]]] = None   ,				lowerCamelCase__						:	Optional[int] = 1   ,				lowerCamelCase__						:	float = 0.0   ,				lowerCamelCase__						:	Optional[torch.Generator] = None   ,				lowerCamelCase__						:	Optional[torch.FloatTensor] = None   ,				lowerCamelCase__						:	Optional[str] = "pil"   ,				lowerCamelCase__						:	bool = True   ,				lowerCamelCase__						:	Optional[Callable[[int, int, torch.FloatTensor], None]] = None   ,				lowerCamelCase__						:	int = 1   ,				**lowerCamelCase__						:	Dict   ,				):
     """simple docstring"""

     return self.pipea(
         prompt=lowerCamelCase__   ,				height=lowerCamelCase__   ,				width=lowerCamelCase__   ,				num_inference_steps=lowerCamelCase__   ,				guidance_scale=lowerCamelCase__   ,				negative_prompt=lowerCamelCase__   ,				num_images_per_prompt=lowerCamelCase__   ,				eta=lowerCamelCase__   ,				generator=lowerCamelCase__   ,				latents=lowerCamelCase__   ,				output_type=lowerCamelCase__   ,				return_dict=lowerCamelCase__   ,				callback=lowerCamelCase__   ,				callback_steps=lowerCamelCase__   ,				**lowerCamelCase__   ,				)

  @torch.no_grad()
  def    a					(    self						:	Union[str, Any]   ,				lowerCamelCase__						:	Union[str, List[str]]   ,				lowerCamelCase__						:	int = 5_12   ,				lowerCamelCase__						:	int = 5_12   ,				lowerCamelCase__						:	int = 50   ,				lowerCamelCase__						:	float = 7.5   ,				lowerCamelCase__						:	Optional[Union[str, List[str]]] = None   ,				lowerCamelCase__						:	Optional[int] = 1   ,				lowerCamelCase__						:	float = 0.0   ,				lowerCamelCase__						:	Optional[torch.Generator] = None   ,				lowerCamelCase__						:	Optional[torch.FloatTensor] = None   ,				lowerCamelCase__						:	Optional[str] = "pil"   ,				lowerCamelCase__						:	bool = True   ,				lowerCamelCase__						:	Optional[Callable[[int, int, torch.FloatTensor], None]] = None   ,				lowerCamelCase__						:	int = 1   ,				**lowerCamelCase__						:	Any   ,				):
     """simple docstring"""

     return self.pipea(
         prompt=lowerCamelCase__   ,				height=lowerCamelCase__   ,				width=lowerCamelCase__   ,				num_inference_steps=lowerCamelCase__   ,				guidance_scale=lowerCamelCase__   ,				negative_prompt=lowerCamelCase__   ,				num_images_per_prompt=lowerCamelCase__   ,				eta=lowerCamelCase__   ,				generator=lowerCamelCase__   ,				latents=lowerCamelCase__   ,				output_type=lowerCamelCase__   ,				return_dict=lowerCamelCase__   ,				callback=lowerCamelCase__   ,				callback_steps=lowerCamelCase__   ,				**lowerCamelCase__   ,				)

  @torch.no_grad()
  def    a					(    self						:	Dict   ,				lowerCamelCase__						:	Union[str, List[str]]   ,				lowerCamelCase__						:	int = 5_12   ,				lowerCamelCase__						:	int = 5_12   ,				lowerCamelCase__						:	int = 50   ,				lowerCamelCase__						:	float = 7.5   ,				lowerCamelCase__						:	Optional[Union[str, List[str]]] = None   ,				lowerCamelCase__						:	Optional[int] = 1   ,				lowerCamelCase__						:	float = 0.0   ,				lowerCamelCase__						:	Optional[torch.Generator] = None   ,				lowerCamelCase__						:	Optional[torch.FloatTensor] = None   ,				lowerCamelCase__						:	Optional[str] = "pil"   ,				lowerCamelCase__						:	bool = True   ,				lowerCamelCase__						:	Optional[Callable[[int, int, torch.FloatTensor], None]] = None   ,				lowerCamelCase__						:	int = 1   ,				**lowerCamelCase__						:	List[Any]   ,				):
     """simple docstring"""

     return self.pipea(
         prompt=lowerCamelCase__   ,				height=lowerCamelCase__   ,				width=lowerCamelCase__   ,				num_inference_steps=lowerCamelCase__   ,				guidance_scale=lowerCamelCase__   ,				negative_prompt=lowerCamelCase__   ,				num_images_per_prompt=lowerCamelCase__   ,				eta=lowerCamelCase__   ,				generator=lowerCamelCase__   ,				latents=lowerCamelCase__   ,				output_type=lowerCamelCase__   ,				return_dict=lowerCamelCase__   ,				callback=lowerCamelCase__   ,				callback_steps=lowerCamelCase__   ,				**lowerCamelCase__   ,				)

  @torch.no_grad()
  def    a					(    self						:	Optional[Any]   ,				lowerCamelCase__						:	Union[str, List[str]]   ,				lowerCamelCase__						:	int = 5_12   ,				lowerCamelCase__						:	int = 5_12   ,				lowerCamelCase__						:	int = 50   ,				lowerCamelCase__						:	float = 7.5   ,				lowerCamelCase__						:	Optional[Union[str, List[str]]] = None   ,				lowerCamelCase__						:	Optional[int] = 1   ,				lowerCamelCase__						:	float = 0.0   ,				lowerCamelCase__						:	Optional[torch.Generator] = None   ,				lowerCamelCase__						:	Optional[torch.FloatTensor] = None   ,				lowerCamelCase__						:	Optional[str] = "pil"   ,				lowerCamelCase__						:	bool = True   ,				lowerCamelCase__						:	Optional[Callable[[int, int, torch.FloatTensor], None]] = None   ,				lowerCamelCase__						:	int = 1   ,				**lowerCamelCase__						:	Dict   ,				):
     """simple docstring"""

     __UpperCamelCase	:			Optional[Any]					=					"""cuda""" if torch.cuda.is_available() else """cpu"""
     self.to(lowerCamelCase__			)

     # Checks if the height and width are divisible by 8 or not
     if height % 8 != 0 or width % 8 != 0:
        raise ValueError(f'`height` and `width` must be divisible by 8 but are {height} and {width}.'			)

     # Get first result from Stable Diffusion Checkpoint v1.1
     __UpperCamelCase	:			Optional[int]					=					self.textaimg_sda_a(
         prompt=lowerCamelCase__   ,				height=lowerCamelCase__   ,				width=lowerCamelCase__   ,				num_inference_steps=lowerCamelCase__   ,				guidance_scale=lowerCamelCase__   ,				negative_prompt=lowerCamelCase__   ,				num_images_per_prompt=lowerCamelCase__   ,				eta=lowerCamelCase__   ,				generator=lowerCamelCase__   ,				latents=lowerCamelCase__   ,				output_type=lowerCamelCase__   ,				return_dict=lowerCamelCase__   ,				callback=lowerCamelCase__   ,				callback_steps=lowerCamelCase__   ,				**lowerCamelCase__   ,				)

     # Get first result from Stable Diffusion Checkpoint v1.2
     __UpperCamelCase	:			int					=					self.textaimg_sda_a(
         prompt=lowerCamelCase__   ,				height=lowerCamelCase__   ,				width=lowerCamelCase__   ,				num_inference_steps=lowerCamelCase__   ,				guidance_scale=lowerCamelCase__   ,				negative_prompt=lowerCamelCase__   ,				num_images_per_prompt=lowerCamelCase__   ,				eta=lowerCamelCase__   ,				generator=lowerCamelCase__   ,				latents=lowerCamelCase__   ,				output_type=lowerCamelCase__   ,				return_dict=lowerCamelCase__   ,				callback=lowerCamelCase__   ,				callback_steps=lowerCamelCase__   ,				**lowerCamelCase__   ,				)

     # Get first result from Stable Diffusion Checkpoint v1.3
     __UpperCamelCase	:			Optional[Any]					=					self.textaimg_sda_a(
         prompt=lowerCamelCase__   ,				height=lowerCamelCase__   ,				width=lowerCamelCase__   ,				num_inference_steps=lowerCamelCase__   ,				guidance_scale=lowerCamelCase__   ,				negative_prompt=lowerCamelCase__   ,				num_images_per_prompt=lowerCamelCase__   ,				eta=lowerCamelCase__   ,				generator=lowerCamelCase__   ,				latents=lowerCamelCase__   ,				output_type=lowerCamelCase__   ,				return_dict=lowerCamelCase__   ,				callback=lowerCamelCase__   ,				callback_steps=lowerCamelCase__   ,				**lowerCamelCase__   ,				)

     # Get first result from Stable Diffusion Checkpoint v1.4
     __UpperCamelCase	:			List[Any]					=					self.textaimg_sda_a(
         prompt=lowerCamelCase__   ,				height=lowerCamelCase__   ,				width=lowerCamelCase__   ,				num_inference_steps=lowerCamelCase__   ,				guidance_scale=lowerCamelCase__   ,				negative_prompt=lowerCamelCase__   ,				num_images_per_prompt=lowerCamelCase__   ,				eta=lowerCamelCase__   ,				generator=lowerCamelCase__   ,				latents=lowerCamelCase__   ,				output_type=lowerCamelCase__   ,				return_dict=lowerCamelCase__   ,				callback=lowerCamelCase__   ,				callback_steps=lowerCamelCase__   ,				**lowerCamelCase__   ,				)

     # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
     return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]]			)



