a
    Ad\<                     @   s  d dl Z d dlmZmZmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZmZmZ d dlmZmZ g d	ZG d
d deZG dd dZedededddedddeg ddedddedddei dg Zedededddedddedddedddeddddedddeg ddedddedddei dg
 ZG d d! d!eZ G d"d# d#eZ!G d$d% d%eZ"G d&d' d'eZ#G d(d) d)e"Z$G d*d+ d+e#Z%G d,d- d-eZ&G d.d/ d/e"Z'G d0d1 d1e#Z(G d2d3 d3eZ)G d4d5 d5eZ*G d6d7 d7eZ+G d8d9 d9eZ,G d:d; d;eZ-G d<d= d=eZ.G d>d? d?eZ/G d@dA dAeZ0G dBdC dCeZ1i Z2ej34 D ]\Z5Z6ej78e5Z9e6j:rej;8e<e6j:e<e6j:neZ=e6dure2>e5ee= ee6j:e6j?dDfi ne2>e5ee= e fi qedgi e2Z@i ZAeBedF ZCeCD ]`Z5eCe5 jDdGkr4eCe5 ZEeZFeCe5 j:durre<eCe5 j:ZFeA>eEjDeFeeEj:eEjGdDfi q4edhi eAZHG dIdJ dJeZIG dKdL dLeZJG dMdN dNeZKG dOdP dPeZLG dQdR dReZMG dSdT dTeZNG dUdV dVeZOG dWdX dXeZPG dYdZ dZeZQG d[d\ d\eZRG d]d^ d^eZSG d_d` d`eZTG dadb dbeZUG dcdd ddeZVG dedf dfeZWdS )i    N)	BaseModelFieldcreate_model)AnyOptional)Literal)
underscore) StableDiffusionProcessingTxt2Img StableDiffusionProcessingImg2Img)sd_upscalersoptsparser)DictList)selfkwargssd_modeloutpath_samplesoutpath_gridssampler_indexextra_generation_paramsoverlay_imagesdo_not_reload_embeddingsseed_enable_extrasprompt_for_display sampler_noise_scheduler_overrideddim_discretizec                   @   s>   e Zd ZU dZeed< eed< eed< eed< dZeed< dS )	ModelDefz6Assistance Class for Pydantic Dynamic Model Generationfieldfield_alias
field_typefield_valueFfield_excludeN)	__name__
__module____qualname____doc__str__annotations__r   r"   bool r*   r*   :/var/www/html/stable-diffusion-webui/modules/api/models.pyr      s   
r   c                   @   s(   e Zd ZdZdedddZdd ZdS )	PydanticModelGeneratorz
    Takes in created classes and stubs them out in a way FastAPI/Pydantic is happy about:
    source_data is a snapshot of the default values produced by the class
    params are the names of the actual keys required by __init__
    N)
model_namec              
      s   dd  dd }|| _ ||| _ fdd| j D | _|D ]B}| jtt|d |d |d |d	 d
|v rv|d
 ndd q>d S )Nc                 S   s   |j }|dkrd}t| S )NImager'   )
annotationr   )kvr    r*   r*   r+   field_type_generator4   s    z=PydanticModelGenerator.__init__.<locals>.field_type_generatorc                 S   s@   t tdd t| }i }|D ]}i |t|jj}q |S )Nc                 S   s   | t uS )N)object)xr*   r*   r+   <lambda>>       zMPydanticModelGenerator.__init__.<locals>.merge_class_params.<locals>.<lambda>)listfilterinspectgetmro	signature__init__
parameters)class_Zall_classesr=   classesr*   r*   r+   merge_class_params=   s
    z;PydanticModelGenerator.__init__.<locals>.merge_class_paramsc              	      sD   g | ]<\}}|t vrtt|| ||t|jtr6d n|jdqS )N)r   r   r    r!   )API_NOT_ALLOWEDr   r   
isinstancedefaultproperty).0r0   r1   r2   r*   r+   
<listcomp>G   s   z3PydanticModelGenerator.__init__.<locals>.<listcomp>keytyperC   excludeF)r   r   r    r!   r"   )_model_nameZ_class_dataitems
_model_defappendr   r   )r   r-   class_instanceZadditional_fieldsr@   fieldsr*   rF   r+   r<   .   s    	



zPydanticModelGenerator.__init__c                 C   s6   dd | j D }t| jfi |}d|j_d|j_|S )zm
        Creates a pydantic BaseModel
        from the json and overrides provided at initialization
        c              	   S   s*   i | ]"}|j |jt|j|j|jd fqS ))rC   aliasrJ   )r   r    r   r!   r   r"   )rE   dr*   r*   r+   
<dictcomp>^   s   z9PydanticModelGenerator.generate_model.<locals>.<dictcomp>T)rM   r   rK   
__config__allow_population_by_field_nameallow_mutation)r   rP   ZDynamicModelr*   r*   r+   generate_modelY   s    z%PydanticModelGenerator.generate_model)NNN)r#   r$   r%   r&   r'   r<   rW   r*   r*   r*   r+   r,   '   s      +r,   r	   r   Euler)rH   rI   rC   script_namescript_argsZsend_imagesTZsave_imagesFalwayson_scriptsr
   init_imagesdenoising_strengthg      ?maskZinclude_init_images)rH   rI   rC   rJ   c                   @   s8   e Zd ZU eddddZee ed< eed< eed< dS )TextToImageResponseNr.   %The generated image in base64 format.rC   titledescriptionimagesr=   info	r#   r$   r%   r   rd   r   r'   r(   dictr*   r*   r*   r+   r_      s   
r_   c                   @   s8   e Zd ZU eddddZee ed< eed< eed< dS )ImageToImageResponseNr.   r`   ra   rd   r=   re   rf   r*   r*   r*   r+   rh      s   
rh   c                   @   s|  e Zd ZU eddddZed ed< eddd	dZeed
< edddddddZ	e
ed< edddddddZe
ed< edddddddZe
ed< eddddddZe
ed< eddddd Zeed!< edd"dd#d Zeed$< edd%d&dZeed'< ed(d)d*d+d,d- eD  dZeed.< ed(d/d0d+d1d- eD  dZeed2< edd3dddd4dZe
ed5< edd6d7dZeed8< d9S ):ExtrasBaseRequestr   zResize ModezzSets the resize mode: 0 to upscale by upscaling_resize amount, 1 to upscale up to upscaling_resize_h x upscaling_resize_w.ra   )r      resize_modeTzShow resultsz.Should the backend return the generated image?show_extras_resultszGFPGAN Visibilityrj   Fz@Sets the visibility of GFPGAN, values should be between 0 and 1.)rC   rb   geleallow_inf_nanrc   gfpgan_visibilityzCodeFormer VisibilityzDSets the visibility of CodeFormer, values should be between 0 and 1.codeformer_visibilityzCodeFormer Weightz@Sets the weight of CodeFormer, values should be between 0 and 1.codeformer_weight   zUpscaling Factor   z?By how much to upscale the image, only used when resize_mode=0.)rC   rb   rm   rn   rc   upscaling_resizei   zTarget WidthzCTarget width for the upscaler to hit. Only used when resize_mode=1.)rC   rb   rm   rc   upscaling_resize_wzTarget HeightzDTarget height for the upscaler to hit. Only used when resize_mode=1.upscaling_resize_hzCrop to fitz=Should the upscaler crop the image to fit in the chosen size?upscaling_cropNonezMain upscalerzEThe name of the main upscaler to use, it has to be one of this list: z , c                 C   s   g | ]
}|j qS r*   namerE   r4   r*   r*   r+   rG      r6   zExtrasBaseRequest.<listcomp>
upscaler_1zSecondary upscalerzJThe name of the secondary upscaler to use, it has to be one of this list: c                 C   s   g | ]
}|j qS r*   rz   r|   r*   r*   r+   rG      r6   
upscaler_2zSecondary upscaler visibilityzLSets the visibility of secondary upscaler, values should be between 0 and 1.extras_upscaler_2_visibilityzUpscale firstz/Should the upscaler run before restoring faces?upscale_firstN)r#   r$   r%   r   rk   r   r(   rl   r)   rp   floatrq   rr   ru   rv   intrw   rx   joinr   r}   r'   r~   r   r   r*   r*   r*   r+   ri      s   
,,ri   c                   @   s"   e Zd ZU edddZeed< dS )ExtraBaseResponsez	HTML infoz2A series of HTML tags containing the process info.rb   rc   	html_infoN)r#   r$   r%   r   r   r'   r(   r*   r*   r*   r+   r      s   
r   c                   @   s$   e Zd ZU eddddZeed< dS )ExtrasSingleImageRequest r.   FImage to work on, must be a Base64 string containing the image's data.ra   imageNr#   r$   r%   r   r   r'   r(   r*   r*   r*   r+   r      s   
r   c                   @   s$   e Zd ZU eddddZeed< dS )ExtrasSingleImageResponseNr.   r`   ra   r   r   r*   r*   r*   r+   r      s   
r   c                   @   s4   e Zd ZU edddZeed< eddZeed< dS )	FileDataz	File dataz!Base64 representation of the filer   dataz	File namerb   r{   N)r#   r$   r%   r   r   r'   r(   r{   r*   r*   r*   r+   r      s   
r   c                   @   s&   e Zd ZU edddZee ed< dS )ExtrasBatchImagesRequestImagesz1List of images to work on. Must be Base64 stringsr   	imageListN)r#   r$   r%   r   r   r   r   r(   r*   r*   r*   r+   r      s   
r   c                   @   s&   e Zd ZU edddZee ed< dS )ExtrasBatchImagesResponser   z&The generated images in base64 format.r   rd   N)r#   r$   r%   r   rd   r   r'   r(   r*   r*   r*   r+   r      s   
r   c                   @   s"   e Zd ZU edddZeed< dS )PNGInfoRequestr.   zThe base64 encoded PNG imager   r   Nr   r*   r*   r*   r+   r      s   
r   c                   @   s6   e Zd ZU edddZeed< edddZeed< dS )	PNGInfoResponsez
Image infoz7A string with the parameters used to generate the imager   re   ZItemsz/An object containing all the info the image hadrL   N)	r#   r$   r%   r   re   r'   r(   rL   rg   r*   r*   r*   r+   r      s   
r   c                   @   s$   e Zd ZU eddddZeed< dS )ProgressRequestFzSkip current imagez Skip current image serializationra   skip_current_imageN)r#   r$   r%   r   r   r)   r(   r*   r*   r*   r+   r      s   
r   c                   @   st   e Zd ZU edddZeed< eddZeed< edd	dZe	ed
< eddddZ
eed< eddddZeed< dS )ProgressResponseProgressz#The progress with a range of 0 to 1r   progresszETA in secsr   eta_relativeStatezThe current state snapshotstateNzCurrent imagezbThe current image in base64 format. opts.show_progress_every_n_steps is required for this to work.ra   current_imagez	Info textzInfo text used by WebUI.textinfo)r#   r$   r%   r   r   r   r(   r   r   rg   r   r'   r   r*   r*   r*   r+   r      s
   
r   c                   @   s:   e Zd ZU eddddZeed< eddddZeed	< d
S )InterrogateRequestr   r.   r   ra   r   clipModelzThe interrogate model used.modelN)r#   r$   r%   r   r   r'   r(   r   r*   r*   r*   r+   r      s   
r   c                   @   s$   e Zd ZU eddddZeed< dS )InterrogateResponseNZCaptionz$The generated caption for the image.ra   caption)r#   r$   r%   r   r   r'   r(   r*   r*   r*   r+   r      s   
r   c                   @   s"   e Zd ZU edddZeed< dS )TrainResponsez
Train infoz:Response string from train embedding or hypernetwork task.r   re   Nr#   r$   r%   r   re   r'   r(   r*   r*   r*   r+   r      s   
r   c                   @   s"   e Zd ZU edddZeed< dS )CreateResponsezCreate infoz;Response string from create embedding or hypernetwork task.r   re   Nr   r*   r*   r*   r+   r      s   
r   c                   @   s"   e Zd ZU edddZeed< dS )PreprocessResponsezPreprocess infoz(Response string from preprocessing task.r   re   Nr   r*   r*   r*   r+   r      s   
r   )rC   rc   Options_option_string_actionshelpFlagsc                   @   sP   e Zd ZU eddZeed< eddZee ed< eddZ	e
eef ed< dS )	SamplerItemNamer   r{   ZAliasesaliasesr   optionsN)r#   r$   r%   r   r{   r'   r(   r   r   r   r   r*   r*   r*   r+   r      s   
r   c                   @   sx   e Zd ZU eddZeed< eddZee ed< eddZ	ee ed< eddZ
ee ed	< ed
dZee ed< dS )UpscalerItemr   r   r{   
Model Namer-   Path
model_pathURL	model_urlScalescaleN)r#   r$   r%   r   r{   r'   r(   r-   r   r   r   r   r   r*   r*   r*   r+   r      s
   
r   c                   @   s    e Zd ZU eddZeed< dS )LatentUpscalerModeItemr   r   r{   N)r#   r$   r%   r   r{   r'   r(   r*   r*   r*   r+   r      s   
r   c                   @   s   e Zd ZU eddZeed< eddZeed< eddZe	e ed< eddZ
e	e ed	< ed
dZeed< eddZe	e ed< dS )SDModelItemTitler   rb   r   r-   z
Short hashhashzsha256 hashsha256FilenamefilenamezConfig fileconfigN)r#   r$   r%   r   rb   r'   r(   r-   r   r   r   r   r   r*   r*   r*   r+   r      s   
r   c                   @   s2   e Zd ZU eddZeed< eddZeed< dS )	SDVaeItemr   r   r-   r   r   N)r#   r$   r%   r   r-   r'   r(   r   r*   r*   r*   r+   r      s   
r   c                   @   s6   e Zd ZU eddZeed< eddZee ed< dS )HypernetworkItemr   r   r{   r   pathN)	r#   r$   r%   r   r{   r'   r(   r   r   r*   r*   r*   r+   r     s   
r   c                   @   s6   e Zd ZU eddZeed< eddZee ed< dS )FaceRestorerItemr   r   r{   r   cmd_dirN)	r#   r$   r%   r   r{   r'   r(   r   r   r*   r*   r*   r+   r     s   
r   c                   @   sL   e Zd ZU eddZeed< eddZee ed< eddZ	ee
 ed< dS )	RealesrganItemr   r   r{   r   r   r   r   N)r#   r$   r%   r   r{   r'   r(   r   r   r   r   r*   r*   r*   r+   r     s   
r   c                   @   sL   e Zd ZU eddZeed< eddZee ed< eddZ	ee ed< dS )	PromptStyleItemr   r   r{   PromptpromptzNegative Promptnegative_promptN)
r#   r$   r%   r   r{   r'   r(   r   r   r   r*   r*   r*   r+   r     s   
r   c                   @   s~   e Zd ZU edddZee ed< edddZee	 ed< edd	dZ
ee	 ed
< edddZeed< edddZeed< dS )EmbeddingItemStepzHThe number of steps that were used to train this embedding, if availabler   stepzSD CheckpointzFThe hash of the checkpoint this embedding was trained on, if availablesd_checkpointzSD Checkpoint NamezThe name of the checkpoint this embedding was trained on, if available. Note that this is the name that was used by the trainer; for a stable identifier, use `sd_checkpoint` insteadsd_checkpoint_nameShapez5The length of each individual vector in the embeddingshapeZVectorsz&The number of vectors in the embeddingvectorsN)r#   r$   r%   r   r   r   r   r(   r   r'   r   r   r   r*   r*   r*   r+   r     s
   
r   c                   @   sF   e Zd ZU edddZeeef ed< edddZ	eeef ed< dS )	EmbeddingsResponseZLoadedz'Embeddings loaded for the current modelr   loadedZSkippedzUEmbeddings skipped for the current model (likely due to architecture incompatibility)skippedN)
r#   r$   r%   r   r   r   r'   r   r(   r   r*   r*   r*   r+   r     s   
r   c                   @   s6   e Zd ZU edddZeed< edddZeed< dS )	MemoryResponseRAMzSystem memory statsr   ramCUDAznVidia CUDA memory statscudaN)r#   r$   r%   r   r   rg   r(   r   r*   r*   r*   r+   r   "  s   
r   c                   @   s:   e Zd ZU eddddZeed< eddddZeed< dS )	ScriptsListNZTxt2imgzTitles of scripts (txt2img)ra   txt2imgZImg2imgzTitles of scripts (img2img)img2img)r#   r$   r%   r   r   r7   r(   r   r*   r*   r*   r+   r   '  s   
r   c                   @   s   e Zd ZU eddddZeed< eddddZee	 ed< edd	d
dZ
ee	 ed< edd	ddZee	 ed< edd	ddZee	 ed< eddddZeee  ed< dS )	ScriptArgNLabelzName of the argument in UIra   labelValuezDefault value of the argumentvalueZMinimumz+Minimum allowed value for the argumentin UIminimumz+Maximum allowed value for the argumentin UImaximumz,Step for changing value of the argumentin UIr   ZChoicesz Possible values for the argumentchoices)r#   r$   r%   r   r   r'   r(   r   r   r   r   r   r   r   r   r*   r*   r*   r+   r   ,  s   
r   c                   @   sh   e Zd ZU eddddZeed< eddddZeed< edd	d
dZ	eed< edddZ
ee ed< dS )
ScriptInfoNr   zScript namera   r{   Z
IsAlwaysonz9Flag specifying whether this script is an alwayson scriptis_alwaysonZ	IsImg2imgz8Flag specifying whether this script is an img2img script
is_img2img	ArgumentszList of script's argumentsr   args)r#   r$   r%   r   r{   r'   r(   r   r)   r   r   r   r   r*   r*   r*   r+   r   5  s   
r   )r   )r   )Xr9   pydanticr   r   r   typingr   r   typing_extensionsr   Z
inflectionr   Zmodules.processingr	   r
   Zmodules.sharedr   r   r   r   r   rA   r   r,   r'   r7   r)   rg   rW   Z#StableDiffusionTxt2ImgProcessingAPIr   Z#StableDiffusionImg2ImgProcessingAPIr_   rh   ri   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rP   data_labelsrL   rH   metadatar   getr   rC   typemaprI   ZoptTypeupdater   ZOptionsModelflagsvars_optionsdestflag_typer   Z
FlagsModelr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r*   r*   r*   r+   <module>   s   
?














&
$$	