o yf?M@sUddlmZddlZddlZddlZddlZddlZddlZddlZ ddl m Z ddl m Z mZmZmZmZmZmZmZddlmZej eej d<dZeeZedZed Zee ZGd d d Z Gd d d e!Z"iZ#de$d<gZ%de$d<ddZ&ddZ'ddZ(ddZ)d6ddZ*ddZ+ddZ,d7d!d"Z-d#d$Z.d%d&Z/d'd(Z0d6d8d-d.Z1gZ2 d/d0Z3dd1d2d3Z4d4d5Z5dS)9) annotationsN) data_path)shared ui_tempdirscript_callbacks processinginfotext_versionsimages prompt_parsererrors)Imagez'modules.generation_parameters_copypastez4\s*(\w[\w \-/]+):\s*("(?:\\.|[^\\"])+"|[^,]*)(?:,|$)z ^(\d+)x(\d+)$z\(([0-9a-f]+)\)$c@seZdZdddZdS) ParamBindingNcCs2||_||_||_||_||_||_|pg|_dSN) paste_buttontabnamesource_text_componentsource_image_componentsource_tabnameoverride_settings_componentpaste_field_names)selfrrrrrrrrgC:\Users\Luke\Documents\Stable diffusion\Automatic1111\stable-diffusion-webui\modules\infotext_utils.py__init__szParamBinding.__init__)NNNNN)__name__ __module__ __qualname__rrrrrr sr cs4eZdZddfdd Zddfdd ZZS) PasteFieldN)apicst|||fSr)super__new__)cls componenttargetr __class__rrr #szPasteField.__new__csFt||_||_t|tr|nd|_t|r||_dSd|_dSr) rrrr" isinstancestrlabelcallablefunction)rr"r#rr$rrr&s zPasteField.__init__)rrrr r __classcell__rrr$rr"srzdict[str, dict] paste_fieldszlist[ParamBinding]registered_param_bindingscCsttdSr)r,clearr-rrrrreset3s r/cCs6dt|vrdt|vrdt|vr|Stj|ddS)N, :F) ensure_ascii)r'jsondumpstextrrrquote8s$r8cCsLt|dks|ddks|ddkr|Szt|WSty%|YSw)Nr")lenr4loads Exceptionr6rrrunquote?s$  r>cCs|durdSt|tkr"|r"t|dtkr"|dddr"|d}t|tkrL|ddrL|d}ttj|}|s?Jd|ddd}t |St|tkr^t |dkrZdS|d}| drk|t dd}t |d }t t|}|S) Nris_fileFnamez8trying to open image file outside of allowed directories?zdata:image/png;base64,zutf-8)typelistdictgetrZcheck_tmp_filerdemorsplitr readr; startswithbase64 decodebytesencodeioBytesIO)ZfiledatafilenameZis_in_right_dirimagerrrimage_from_url_textIs&0     rRcCsx|rtt|D]}t||tst||||<q|||dt|<ddl}|dkr0||j_dS|dkr:||j_dSdS)N)init_imgfieldsrrtxt2imgimg2img) ranger;r&rr,Z modules.uiuiZtxt2img_paste_fieldsZimg2img_paste_fields)rrSrTrimodulesrrradd_paste_fieldsfs  r[cCs0i}|D]}tjd||dd||<q|S)NzSend to Z_tab)elem_id)grButton)Z tabs_listbuttonstabrrrcreate_buttonsws rac CsT|D]#\}}t|tjjr|nd}t|tr|nd}tt|||||dqdS)z[old function for backwards compatibility; do not use this, use register_paste_params_buttonN)rrrrr)itemsr&r] components Componentr'register_paste_params_buttonr )r_Z send_imageZsend_generate_inforbuttonrrrrr bind_buttons~s rgbindingcCst|dSr)r-append)rhrrrresrecstD]}t|jd}t|jd}|jpt|jd}tt|r'dd|Dngd}tt|r7dd|Dngd}|jrp|rpt|jtj rQ|rLt nt }d}n |rUt ndd }d}|j j |||jg|rj|||gn|gd d |jdur|durt|j ||j||j|jdur|durgd tjjrd gng|j|j j dd fddt|jdDfdd|Dd d|j j dd|jddd d qdS)NrSrTrcSg|] \}}|dkr|qS)Size-1r.0fieldr@rrr z0connect_paste_params_buttons..cSrj)Size-2rrlrrrrorpZextract_image_from_gallerycS|Srrxrrrz.connect_paste_params_buttons..Ffn_jsinputsoutputs show_progress)PromptNegative promptStepszFace restorationSeedcWrrrrrsrrrrurvcg|] \}}|vr|qSrrrlrrrrorpcrrrrlrrrrorprxrzr{r|Z switch_to_)r-r,rrnextiterrr&r]Gallerysend_image_and_dimensionsrRrclickr connect_pasterropts send_seedr)rhZdestination_image_componentrTrZdestination_width_componentZdestination_height_componentfuncZjsfuncrrrconnect_paste_params_buttonssJ      rcCsVt|tjr |}nt|}tjjrt|tjr|j}|j}nt }t }|||fSr) r&r rRrr send_sizewidthheightr]update)rtimgwhrrrrs  rcCs|dd}|dd}tjjr.t|dd}t|dd}|r.|r.||d<||d<dS|dus6|dur8dSt|t|}}t|dd }t|dd }|dksY|dkrat||\}}||d<||d<||d<||d<dS) zifor infotexts that specify old First pass size parameter, convert it into width, height, and hr scalezFirst pass size-1NzFirst pass size-2Hires resize-1rHires resize-2rkrqi)rFrruse_old_hires_fix_width_heightintr#old_hires_fix_first_pass_dimensions)resZfirstpass_widthZfirstpass_heightZ hires_widthZ hires_heightrrrrrrestore_old_hires_fix_paramss(   rrtr' skip_fieldslist[str] | Nonec Cs|durtjj}i}d}d}d}|d^}}tt|dkr*||d}|D]0}|}| drAd}|dd}|rP||dkrJdnd|7}q,||dkrWdnd|7}q,t|D]L\} } z4| d d krw| d d krwt | } t | } | dur| d || d <| d|| d<n| || <Wqbtytd| d| d Yqbwtjjdkr.tj||\} } }d}d|vsd|vrt|d}r|tjkrn |d||d|}}tj||\}}}| |k}r|| krdn||d<||kr dn||d<|r.| |}}tjjdkr#| s*tjjdkr.| |d<||d<||d<d|vr?d|d<|dd}|dur]|dd|d|d d!d"7<d#|vrjd |d#<d |d$<d%|vrsd&|d%<d'|vr|d(|d'<d)|vrd*|d)<d|vrd|d<d|vrd|d<d+|vrd,|d+<d-|vrd.|d-<d/|vrd0|d/<d1|vrd2|d1<t|d3|vrd4|d3<d5|vrd6|d5<d7|vrd |d7<d8|vrd |d8<d9|vrd |d9<d:|vrd;|d:<d<|vrd;|d<<d=|vrd>|d=<d?|vr|d=d>krd|d?<t|}|t|7}t|td@dA|Dk}dB|vr<|rrrz Hires samplerzUse same samplerzHires schedule typezUse same schedulerzHires checkpointzUse same checkpointz Mask modezInpaint maskedzMasked contentoriginalz Inpaint areaz Whole picturezMasked area padding RNGGPUz Schedule type AutomaticzSchedule max sigmazSchedule min sigmaz Schedule rhoz VAE EncoderFullz VAE Decoderz FP8 weightDisablezCache FP16 weight for LoRAcSs(g|]}|ddks|ddkr|qS)rBg?rBREAKr)rmprrrrous(z/parse_generation_parameters..EmphasisOriginalz Refiner switch by sampling steps)rrinfotext_skip_pastingstripsplitr;re_paramfindallrirJr> re_imagesizematchgroupr=printinfotext_styles prompt_stylesextract_styles_from_promptr parse_versionrFZv180_hr_stylesrr parse_prompt_attention backcompatpop)rtrrpromptnegative_promptZdone_with_promptlineslastlinelinekvmZ found_stylesprompt_no_stylesZnegative_prompt_no_stylesZsame_hr_stylesZ infotext_ver hr_prompthr_negative_promptZhr_found_stylesZhr_prompt_no_stylesZhr_negative_prompt_no_styleshypernetZprompt_attentionZprompt_uses_emphasiskeyrrrparse_generation_parameterss     6 "   &                     rc Csi}i}|D]}|jddd\}}|||<qddtjjD}|tD]\}}||d} | dur6q'tj|| ||<q'|S)a'creates processing's override_settings parameters from gradio's multiselect Example input: ['Clip skip: 2', 'Model hash: e6e99610c4', 'ENSD: 31337'] Example output: {'CLIP_stop_at_last_layers': 2, 'sd_model_checkpoint': 'e6e99610c4', 'eta_noise_seed_delta': 31337} r2rB)maxsplitcS g|] \}}|jr|j|fqSrinfotextrmrinforrrro z1create_override_settings_dict..N) rrrr data_labelsrb infotext_to_setting_name_mappingrF cast_value) Z text_pairsrparamspairrrmapping param_name setting_namevaluerrrcreate_override_settings_dicts  rrcCsg}ddtjjD}|tD]:\}}||pivrq||d}|dur'q|dkr0tjjr0qtj||}ttj|d}||krCq| |||fq|S)aVReturns a list of settings overrides from the infotext parameters dictionary. This function checks the `params` dictionary for any keys that correspond to settings in `shared.opts` and returns a list of tuples containing the parameter name, setting name, and new value cast to correct type. It checks for conditions before adding an override: - ignores settings that match the current value - ignores parameter keys present in skip_fields argument. Example input: {"Clip skip": "2"} Example output: [("Clip skip", "CLIP_stop_at_last_layers", 2)] cSrrrrrrrrorz)get_override_settings..Nsd_model_checkpoint) rrrrbrrFdisable_weights_auto_swaprgetattrri)rrrrrrr current_valuerrrget_override_settingss   rcsxfdd}|durddDfdd}||fg|j||gddDd d |jdd |ggd d dS) Nc s|s8tjjs8tjjs8tjtd}zt|ddd }| }Wdn1s(wYWn t y7Ynwt |}t ||g}D]y\}}t|rjz||}Wntyitjd|ddd}Ynw||d}|dur||tqFt|tr||qFz(t|j}|tkr|dkrd } n |tkrt|} n||} |tj| d WqFty|tYqFw|S) Nz params.txtrutf8)encodingzError executing T)exc_infoFalseF)r)rcmd_optshide_ui_dir_configno_prompt_historyospathjoinropenrIOSErrorrrinfotext_pasted_callbackr)r=r reportrFrir]rr&type_of_gr_updaterCrboolrfloat) rrPfilerroutputrrZvaltypeval)r,rr paste_funcsL            z!connect_paste..paste_funccSsi|]\}}|dqS)rBr)rm_rrrr sz!connect_paste..cs0t|d}dd|D}tjj||t|dS)NrcSs g|] \}}}|d|qS)rr)rmZ infotext_textrrrrrrorz9connect_paste..paste_settings..)rchoicesvisible)rr]Dropdownrr)rvalsZ vals_pairs)already_handled_fieldsrrpaste_settings s z%connect_paste..paste_settingscSsg|]}|dqS)rr)rmrtrrrrosz!connect_paste..FrZrecalculate_prompts_rw)r)rfr,Z input_comprrrrr)rr,rrs$ ,   rr)rhr )rtr'rr)6 __future__rrKrNr4rresysgradior] modules.pathsrrZrrrrrr r r PILr rZ re_param_codecompilerrZre_hypernet_hashrCrrr tuplerr,__annotations__r-r/r8r>rRr[rargrerrrrrrrrrrrrsN (        .   *