ui.py 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. import html
  2. import json
  3. import math
  4. import mimetypes
  5. import os
  6. import platform
  7. import random
  8. import sys
  9. import tempfile
  10. import time
  11. import traceback
  12. from functools import partial, reduce
  13. import warnings
  14. import gradio as gr
  15. import gradio.routes
  16. import gradio.utils
  17. import numpy as np
  18. from PIL import Image, PngImagePlugin
  19. from modules.call_queue import wrap_gradio_gpu_call, wrap_queued_call, wrap_gradio_call
  20. from modules import sd_hijack, sd_models, localization, script_callbacks, ui_extensions, deepbooru, sd_vae, extra_networks, postprocessing, ui_components, ui_common, ui_postprocessing
  21. from modules.ui_components import FormRow, FormColumn, FormGroup, ToolButton, FormHTML
  22. from modules.paths import script_path, data_path
  23. from modules.shared import opts, cmd_opts, restricted_opts
  24. import modules.codeformer_model
  25. import modules.generation_parameters_copypaste as parameters_copypaste
  26. import modules.gfpgan_model
  27. import modules.hypernetworks.ui
  28. import modules.scripts
  29. import modules.shared as shared
  30. import modules.styles
  31. import modules.textual_inversion.ui
  32. from modules import prompt_parser
  33. from modules.images import save_image
  34. from modules.sd_hijack import model_hijack
  35. from modules.sd_samplers import samplers, samplers_for_img2img
  36. from modules.textual_inversion import textual_inversion
  37. import modules.hypernetworks.ui
  38. from modules.generation_parameters_copypaste import image_from_url_text
  39. import modules.extras
  40. warnings.filterwarnings("default" if opts.show_warnings else "ignore", category=UserWarning)
  41. # this is a fix for Windows users. Without it, javascript files will be served with text/html content-type and the browser will not show any UI
  42. mimetypes.init()
  43. mimetypes.add_type('application/javascript', '.js')
  44. if not cmd_opts.share and not cmd_opts.listen:
  45. # fix gradio phoning home
  46. gradio.utils.version_check = lambda: None
  47. gradio.utils.get_local_ip_address = lambda: '127.0.0.1'
  48. if cmd_opts.ngrok is not None:
  49. import modules.ngrok as ngrok
  50. print('ngrok authtoken detected, trying to connect...')
  51. ngrok.connect(
  52. cmd_opts.ngrok,
  53. cmd_opts.port if cmd_opts.port is not None else 7860,
  54. cmd_opts.ngrok_region
  55. )
  56. def gr_show(visible=True):
  57. return {"visible": visible, "__type__": "update"}
  58. sample_img2img = "assets/stable-samples/img2img/sketch-mountains-input.jpg"
  59. sample_img2img = sample_img2img if os.path.exists(sample_img2img) else None
  60. # Using constants for these since the variation selector isn't visible.
  61. # Important that they exactly match script.js for tooltip to work.
  62. random_symbol = '\U0001f3b2\ufe0f' # 🎲️
  63. reuse_symbol = '\u267b\ufe0f' # ♻️
  64. paste_symbol = '\u2199\ufe0f' # ↙
  65. refresh_symbol = '\U0001f504' # 🔄
  66. save_style_symbol = '\U0001f4be' # 💾
  67. apply_style_symbol = '\U0001f4cb' # 📋
  68. clear_prompt_symbol = '\U0001f5d1\ufe0f' # 🗑️
  69. extra_networks_symbol = '\U0001F3B4' # 🎴
  70. switch_values_symbol = '\U000021C5' # ⇅
  71. def plaintext_to_html(text):
  72. return ui_common.plaintext_to_html(text)
  73. def send_gradio_gallery_to_image(x):
  74. if len(x) == 0:
  75. return None
  76. return image_from_url_text(x[0])
  77. def visit(x, func, path=""):
  78. if hasattr(x, 'children'):
  79. for c in x.children:
  80. visit(c, func, path)
  81. elif x.label is not None:
  82. func(path + "/" + str(x.label), x)
  83. def add_style(name: str, prompt: str, negative_prompt: str):
  84. if name is None:
  85. return [gr_show() for x in range(4)]
  86. style = modules.styles.PromptStyle(name, prompt, negative_prompt)
  87. shared.prompt_styles.styles[style.name] = style
  88. # Save all loaded prompt styles: this allows us to update the storage format in the future more easily, because we
  89. # reserialize all styles every time we save them
  90. shared.prompt_styles.save_styles(shared.styles_filename)
  91. return [gr.Dropdown.update(visible=True, choices=list(shared.prompt_styles.styles)) for _ in range(2)]
  92. def calc_resolution_hires(enable, width, height, hr_scale, hr_resize_x, hr_resize_y):
  93. from modules import processing, devices
  94. if not enable:
  95. return ""
  96. p = processing.StableDiffusionProcessingTxt2Img(width=width, height=height, enable_hr=True, hr_scale=hr_scale, hr_resize_x=hr_resize_x, hr_resize_y=hr_resize_y)
  97. with devices.autocast():
  98. p.init([""], [0], [0])
  99. return f"resize: from <span class='resolution'>{p.width}x{p.height}</span> to <span class='resolution'>{p.hr_resize_x or p.hr_upscale_to_x}x{p.hr_resize_y or p.hr_upscale_to_y}</span>"
  100. def apply_styles(prompt, prompt_neg, styles):
  101. prompt = shared.prompt_styles.apply_styles_to_prompt(prompt, styles)
  102. prompt_neg = shared.prompt_styles.apply_negative_styles_to_prompt(prompt_neg, styles)
  103. return [gr.Textbox.update(value=prompt), gr.Textbox.update(value=prompt_neg), gr.Dropdown.update(value=[])]
  104. def process_interrogate(interrogation_function, mode, ii_input_dir, ii_output_dir, *ii_singles):
  105. if mode in {0, 1, 3, 4}:
  106. return [interrogation_function(ii_singles[mode]), None]
  107. elif mode == 2:
  108. return [interrogation_function(ii_singles[mode]["image"]), None]
  109. elif mode == 5:
  110. assert not shared.cmd_opts.hide_ui_dir_config, "Launched with --hide-ui-dir-config, batch img2img disabled"
  111. images = shared.listfiles(ii_input_dir)
  112. print(f"Will process {len(images)} images.")
  113. if ii_output_dir != "":
  114. os.makedirs(ii_output_dir, exist_ok=True)
  115. else:
  116. ii_output_dir = ii_input_dir
  117. for image in images:
  118. img = Image.open(image)
  119. filename = os.path.basename(image)
  120. left, _ = os.path.splitext(filename)
  121. print(interrogation_function(img), file=open(os.path.join(ii_output_dir, left + ".txt"), 'a'))
  122. return [gr.update(), None]
  123. def interrogate(image):
  124. prompt = shared.interrogator.interrogate(image.convert("RGB"))
  125. return gr.update() if prompt is None else prompt
  126. def interrogate_deepbooru(image):
  127. prompt = deepbooru.model.tag(image)
  128. return gr.update() if prompt is None else prompt
  129. def create_seed_inputs(target_interface):
  130. with FormRow(elem_id=target_interface + '_seed_row', variant="compact"):
  131. seed = (gr.Textbox if cmd_opts.use_textbox_seed else gr.Number)(label='Seed', value=-1, elem_id=target_interface + '_seed')
  132. seed.style(container=False)
  133. random_seed = ToolButton(random_symbol, elem_id=target_interface + '_random_seed')
  134. reuse_seed = ToolButton(reuse_symbol, elem_id=target_interface + '_reuse_seed')
  135. seed_checkbox = gr.Checkbox(label='Extra', elem_id=target_interface + '_subseed_show', value=False)
  136. # Components to show/hide based on the 'Extra' checkbox
  137. seed_extras = []
  138. with FormRow(visible=False, elem_id=target_interface + '_subseed_row') as seed_extra_row_1:
  139. seed_extras.append(seed_extra_row_1)
  140. subseed = gr.Number(label='Variation seed', value=-1, elem_id=target_interface + '_subseed')
  141. subseed.style(container=False)
  142. random_subseed = ToolButton(random_symbol, elem_id=target_interface + '_random_subseed')
  143. reuse_subseed = ToolButton(reuse_symbol, elem_id=target_interface + '_reuse_subseed')
  144. subseed_strength = gr.Slider(label='Variation strength', value=0.0, minimum=0, maximum=1, step=0.01, elem_id=target_interface + '_subseed_strength')
  145. with FormRow(visible=False) as seed_extra_row_2:
  146. seed_extras.append(seed_extra_row_2)
  147. seed_resize_from_w = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize seed from width", value=0, elem_id=target_interface + '_seed_resize_from_w')
  148. seed_resize_from_h = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize seed from height", value=0, elem_id=target_interface + '_seed_resize_from_h')
  149. random_seed.click(fn=lambda: -1, show_progress=False, inputs=[], outputs=[seed])
  150. random_subseed.click(fn=lambda: -1, show_progress=False, inputs=[], outputs=[subseed])
  151. def change_visibility(show):
  152. return {comp: gr_show(show) for comp in seed_extras}
  153. seed_checkbox.change(change_visibility, show_progress=False, inputs=[seed_checkbox], outputs=seed_extras)
  154. return seed, reuse_seed, subseed, reuse_subseed, subseed_strength, seed_resize_from_h, seed_resize_from_w, seed_checkbox
  155. def connect_clear_prompt(button):
  156. """Given clear button, prompt, and token_counter objects, setup clear prompt button click event"""
  157. button.click(
  158. _js="clear_prompt",
  159. fn=None,
  160. inputs=[],
  161. outputs=[],
  162. )
  163. def connect_reuse_seed(seed: gr.Number, reuse_seed: gr.Button, generation_info: gr.Textbox, dummy_component, is_subseed):
  164. """ Connects a 'reuse (sub)seed' button's click event so that it copies last used
  165. (sub)seed value from generation info the to the seed field. If copying subseed and subseed strength
  166. was 0, i.e. no variation seed was used, it copies the normal seed value instead."""
  167. def copy_seed(gen_info_string: str, index):
  168. res = -1
  169. try:
  170. gen_info = json.loads(gen_info_string)
  171. index -= gen_info.get('index_of_first_image', 0)
  172. if is_subseed and gen_info.get('subseed_strength', 0) > 0:
  173. all_subseeds = gen_info.get('all_subseeds', [-1])
  174. res = all_subseeds[index if 0 <= index < len(all_subseeds) else 0]
  175. else:
  176. all_seeds = gen_info.get('all_seeds', [-1])
  177. res = all_seeds[index if 0 <= index < len(all_seeds) else 0]
  178. except json.decoder.JSONDecodeError as e:
  179. if gen_info_string != '':
  180. print("Error parsing JSON generation info:", file=sys.stderr)
  181. print(gen_info_string, file=sys.stderr)
  182. return [res, gr_show(False)]
  183. reuse_seed.click(
  184. fn=copy_seed,
  185. _js="(x, y) => [x, selected_gallery_index()]",
  186. show_progress=False,
  187. inputs=[generation_info, dummy_component],
  188. outputs=[seed, dummy_component]
  189. )
  190. def update_token_counter(text, steps):
  191. try:
  192. text, _ = extra_networks.parse_prompt(text)
  193. _, prompt_flat_list, _ = prompt_parser.get_multicond_prompt_list([text])
  194. prompt_schedules = prompt_parser.get_learned_conditioning_prompt_schedules(prompt_flat_list, steps)
  195. except Exception:
  196. # a parsing error can happen here during typing, and we don't want to bother the user with
  197. # messages related to it in console
  198. prompt_schedules = [[[steps, text]]]
  199. flat_prompts = reduce(lambda list1, list2: list1+list2, prompt_schedules)
  200. prompts = [prompt_text for step, prompt_text in flat_prompts]
  201. token_count, max_length = max([model_hijack.get_prompt_lengths(prompt) for prompt in prompts], key=lambda args: args[0])
  202. return f"<span class='gr-box gr-text-input'>{token_count}/{max_length}</span>"
  203. def create_toprow(is_img2img):
  204. id_part = "img2img" if is_img2img else "txt2img"
  205. with gr.Row(elem_id=f"{id_part}_toprow", variant="compact"):
  206. with gr.Column(elem_id=f"{id_part}_prompt_container", scale=6):
  207. with gr.Row():
  208. with gr.Column(scale=80):
  209. with gr.Row():
  210. prompt = gr.Textbox(label="Prompt", elem_id=f"{id_part}_prompt", show_label=False, lines=3, placeholder="Prompt (press Ctrl+Enter or Alt+Enter to generate)")
  211. with gr.Row():
  212. with gr.Column(scale=80):
  213. with gr.Row():
  214. negative_prompt = gr.Textbox(label="Negative prompt", elem_id=f"{id_part}_neg_prompt", show_label=False, lines=3, placeholder="Negative prompt (press Ctrl+Enter or Alt+Enter to generate)")
  215. button_interrogate = None
  216. button_deepbooru = None
  217. if is_img2img:
  218. with gr.Column(scale=1, elem_classes="interrogate-col"):
  219. button_interrogate = gr.Button('Interrogate\nCLIP', elem_id="interrogate")
  220. button_deepbooru = gr.Button('Interrogate\nDeepBooru', elem_id="deepbooru")
  221. with gr.Column(scale=1, elem_id=f"{id_part}_actions_column"):
  222. with gr.Row(elem_id=f"{id_part}_generate_box", elem_classes="generate-box"):
  223. interrupt = gr.Button('Interrupt', elem_id=f"{id_part}_interrupt", elem_classes="generate-box-interrupt")
  224. skip = gr.Button('Skip', elem_id=f"{id_part}_skip", elem_classes="generate-box-skip")
  225. submit = gr.Button('Generate', elem_id=f"{id_part}_generate", variant='primary')
  226. skip.click(
  227. fn=lambda: shared.state.skip(),
  228. inputs=[],
  229. outputs=[],
  230. )
  231. interrupt.click(
  232. fn=lambda: shared.state.interrupt(),
  233. inputs=[],
  234. outputs=[],
  235. )
  236. with gr.Row(elem_id=f"{id_part}_tools"):
  237. paste = ToolButton(value=paste_symbol, elem_id="paste")
  238. clear_prompt_button = ToolButton(value=clear_prompt_symbol, elem_id=f"{id_part}_clear_prompt")
  239. extra_networks_button = ToolButton(value=extra_networks_symbol, elem_id=f"{id_part}_extra_networks")
  240. prompt_style_apply = ToolButton(value=apply_style_symbol, elem_id=f"{id_part}_style_apply")
  241. save_style = ToolButton(value=save_style_symbol, elem_id=f"{id_part}_style_create")
  242. token_counter = gr.HTML(value="<span>0/75</span>", elem_id=f"{id_part}_token_counter", elem_classes=["token-counter"])
  243. token_button = gr.Button(visible=False, elem_id=f"{id_part}_token_button")
  244. negative_token_counter = gr.HTML(value="<span>0/75</span>", elem_id=f"{id_part}_negative_token_counter", elem_classes=["token-counter"])
  245. negative_token_button = gr.Button(visible=False, elem_id=f"{id_part}_negative_token_button")
  246. clear_prompt_button.click(
  247. fn=lambda *x: x,
  248. _js="confirm_clear_prompt",
  249. inputs=[prompt, negative_prompt],
  250. outputs=[prompt, negative_prompt],
  251. )
  252. with gr.Row(elem_id=f"{id_part}_styles_row"):
  253. prompt_styles = gr.Dropdown(label="Styles", elem_id=f"{id_part}_styles", choices=[k for k, v in shared.prompt_styles.styles.items()], value=[], multiselect=True)
  254. create_refresh_button(prompt_styles, shared.prompt_styles.reload, lambda: {"choices": [k for k, v in shared.prompt_styles.styles.items()]}, f"refresh_{id_part}_styles")
  255. return prompt, prompt_styles, negative_prompt, submit, button_interrogate, button_deepbooru, prompt_style_apply, save_style, paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button
  256. def setup_progressbar(*args, **kwargs):
  257. pass
  258. def apply_setting(key, value):
  259. if value is None:
  260. return gr.update()
  261. if shared.cmd_opts.freeze_settings:
  262. return gr.update()
  263. # dont allow model to be swapped when model hash exists in prompt
  264. if key == "sd_model_checkpoint" and opts.disable_weights_auto_swap:
  265. return gr.update()
  266. if key == "sd_model_checkpoint":
  267. ckpt_info = sd_models.get_closet_checkpoint_match(value)
  268. if ckpt_info is not None:
  269. value = ckpt_info.title
  270. else:
  271. return gr.update()
  272. comp_args = opts.data_labels[key].component_args
  273. if comp_args and isinstance(comp_args, dict) and comp_args.get('visible') is False:
  274. return
  275. valtype = type(opts.data_labels[key].default)
  276. oldval = opts.data.get(key, None)
  277. opts.data[key] = valtype(value) if valtype != type(None) else value
  278. if oldval != value and opts.data_labels[key].onchange is not None:
  279. opts.data_labels[key].onchange()
  280. opts.save(shared.config_filename)
  281. return getattr(opts, key)
  282. def create_refresh_button(refresh_component, refresh_method, refreshed_args, elem_id):
  283. def refresh():
  284. refresh_method()
  285. args = refreshed_args() if callable(refreshed_args) else refreshed_args
  286. for k, v in args.items():
  287. setattr(refresh_component, k, v)
  288. return gr.update(**(args or {}))
  289. refresh_button = ToolButton(value=refresh_symbol, elem_id=elem_id)
  290. refresh_button.click(
  291. fn=refresh,
  292. inputs=[],
  293. outputs=[refresh_component]
  294. )
  295. return refresh_button
  296. def create_output_panel(tabname, outdir):
  297. return ui_common.create_output_panel(tabname, outdir)
  298. def create_sampler_and_steps_selection(choices, tabname):
  299. if opts.samplers_in_dropdown:
  300. with FormRow(elem_id=f"sampler_selection_{tabname}"):
  301. sampler_index = gr.Dropdown(label='Sampling method', elem_id=f"{tabname}_sampling", choices=[x.name for x in choices], value=choices[0].name, type="index")
  302. steps = gr.Slider(minimum=1, maximum=150, step=1, elem_id=f"{tabname}_steps", label="Sampling steps", value=20)
  303. else:
  304. with FormGroup(elem_id=f"sampler_selection_{tabname}"):
  305. steps = gr.Slider(minimum=1, maximum=150, step=1, elem_id=f"{tabname}_steps", label="Sampling steps", value=20)
  306. sampler_index = gr.Radio(label='Sampling method', elem_id=f"{tabname}_sampling", choices=[x.name for x in choices], value=choices[0].name, type="index")
  307. return steps, sampler_index
  308. def ordered_ui_categories():
  309. user_order = {x.strip(): i * 2 + 1 for i, x in enumerate(shared.opts.ui_reorder.split(","))}
  310. for i, category in sorted(enumerate(shared.ui_reorder_categories), key=lambda x: user_order.get(x[1], x[0] * 2 + 0)):
  311. yield category
  312. def get_value_for_setting(key):
  313. value = getattr(opts, key)
  314. info = opts.data_labels[key]
  315. args = info.component_args() if callable(info.component_args) else info.component_args or {}
  316. args = {k: v for k, v in args.items() if k not in {'precision'}}
  317. return gr.update(value=value, **args)
  318. def create_override_settings_dropdown(tabname, row):
  319. dropdown = gr.Dropdown([], label="Override settings", visible=False, elem_id=f"{tabname}_override_settings", multiselect=True)
  320. dropdown.change(
  321. fn=lambda x: gr.Dropdown.update(visible=len(x) > 0),
  322. inputs=[dropdown],
  323. outputs=[dropdown],
  324. )
  325. return dropdown
  326. def create_ui():
  327. import modules.img2img
  328. import modules.txt2img
  329. reload_javascript()
  330. parameters_copypaste.reset()
  331. modules.scripts.scripts_current = modules.scripts.scripts_txt2img
  332. modules.scripts.scripts_txt2img.initialize_scripts(is_img2img=False)
  333. with gr.Blocks(analytics_enabled=False) as txt2img_interface:
  334. txt2img_prompt, txt2img_prompt_styles, txt2img_negative_prompt, submit, _, _, txt2img_prompt_style_apply, txt2img_save_style, txt2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button = create_toprow(is_img2img=False)
  335. dummy_component = gr.Label(visible=False)
  336. txt_prompt_img = gr.File(label="", elem_id="txt2img_prompt_image", file_count="single", type="binary", visible=False)
  337. with FormRow(variant='compact', elem_id="txt2img_extra_networks", visible=False) as extra_networks:
  338. from modules import ui_extra_networks
  339. extra_networks_ui = ui_extra_networks.create_ui(extra_networks, extra_networks_button, 'txt2img')
  340. with gr.Row().style(equal_height=False):
  341. with gr.Column(variant='compact', elem_id="txt2img_settings"):
  342. for category in ordered_ui_categories():
  343. if category == "sampler":
  344. steps, sampler_index = create_sampler_and_steps_selection(samplers, "txt2img")
  345. elif category == "dimensions":
  346. with FormRow():
  347. with gr.Column(elem_id="txt2img_column_size", scale=4):
  348. width = gr.Slider(minimum=64, maximum=2048, step=8, label="Width", value=512, elem_id="txt2img_width")
  349. height = gr.Slider(minimum=64, maximum=2048, step=8, label="Height", value=512, elem_id="txt2img_height")
  350. with gr.Column(elem_id="txt2img_dimensions_row", scale=1, elem_classes="dimensions-tools"):
  351. res_switch_btn = ToolButton(value=switch_values_symbol, elem_id="txt2img_res_switch_btn")
  352. if opts.dimensions_and_batch_together:
  353. with gr.Column(elem_id="txt2img_column_batch"):
  354. batch_count = gr.Slider(minimum=1, step=1, label='Batch count', value=1, elem_id="txt2img_batch_count")
  355. batch_size = gr.Slider(minimum=1, maximum=8, step=1, label='Batch size', value=1, elem_id="txt2img_batch_size")
  356. elif category == "cfg":
  357. cfg_scale = gr.Slider(minimum=1.0, maximum=30.0, step=0.5, label='CFG Scale', value=7.0, elem_id="txt2img_cfg_scale")
  358. elif category == "seed":
  359. seed, reuse_seed, subseed, reuse_subseed, subseed_strength, seed_resize_from_h, seed_resize_from_w, seed_checkbox = create_seed_inputs('txt2img')
  360. elif category == "checkboxes":
  361. with FormRow(elem_classes="checkboxes-row", variant="compact"):
  362. restore_faces = gr.Checkbox(label='Restore faces', value=False, visible=len(shared.face_restorers) > 1, elem_id="txt2img_restore_faces")
  363. tiling = gr.Checkbox(label='Tiling', value=False, elem_id="txt2img_tiling")
  364. enable_hr = gr.Checkbox(label='Hires. fix', value=False, elem_id="txt2img_enable_hr")
  365. hr_final_resolution = FormHTML(value="", elem_id="txtimg_hr_finalres", label="Upscaled resolution", interactive=False)
  366. elif category == "hires_fix":
  367. with FormGroup(visible=False, elem_id="txt2img_hires_fix") as hr_options:
  368. with FormRow(elem_id="txt2img_hires_fix_row1", variant="compact"):
  369. hr_upscaler = gr.Dropdown(label="Upscaler", elem_id="txt2img_hr_upscaler", choices=[*shared.latent_upscale_modes, *[x.name for x in shared.sd_upscalers]], value=shared.latent_upscale_default_mode)
  370. hr_second_pass_steps = gr.Slider(minimum=0, maximum=150, step=1, label='Hires steps', value=0, elem_id="txt2img_hires_steps")
  371. denoising_strength = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Denoising strength', value=0.7, elem_id="txt2img_denoising_strength")
  372. with FormRow(elem_id="txt2img_hires_fix_row2", variant="compact"):
  373. hr_scale = gr.Slider(minimum=1.0, maximum=4.0, step=0.05, label="Upscale by", value=2.0, elem_id="txt2img_hr_scale")
  374. hr_resize_x = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize width to", value=0, elem_id="txt2img_hr_resize_x")
  375. hr_resize_y = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize height to", value=0, elem_id="txt2img_hr_resize_y")
  376. elif category == "batch":
  377. if not opts.dimensions_and_batch_together:
  378. with FormRow(elem_id="txt2img_column_batch"):
  379. batch_count = gr.Slider(minimum=1, step=1, label='Batch count', value=1, elem_id="txt2img_batch_count")
  380. batch_size = gr.Slider(minimum=1, maximum=8, step=1, label='Batch size', value=1, elem_id="txt2img_batch_size")
  381. elif category == "override_settings":
  382. with FormRow(elem_id="txt2img_override_settings_row") as row:
  383. override_settings = create_override_settings_dropdown('txt2img', row)
  384. elif category == "scripts":
  385. with FormGroup(elem_id="txt2img_script_container"):
  386. custom_inputs = modules.scripts.scripts_txt2img.setup_ui()
  387. hr_resolution_preview_inputs = [enable_hr, width, height, hr_scale, hr_resize_x, hr_resize_y]
  388. for input in hr_resolution_preview_inputs:
  389. input.change(
  390. fn=calc_resolution_hires,
  391. inputs=hr_resolution_preview_inputs,
  392. outputs=[hr_final_resolution],
  393. show_progress=False,
  394. )
  395. input.change(
  396. None,
  397. _js="onCalcResolutionHires",
  398. inputs=hr_resolution_preview_inputs,
  399. outputs=[],
  400. show_progress=False,
  401. )
  402. txt2img_gallery, generation_info, html_info, html_log = create_output_panel("txt2img", opts.outdir_txt2img_samples)
  403. connect_reuse_seed(seed, reuse_seed, generation_info, dummy_component, is_subseed=False)
  404. connect_reuse_seed(subseed, reuse_subseed, generation_info, dummy_component, is_subseed=True)
  405. txt2img_args = dict(
  406. fn=wrap_gradio_gpu_call(modules.txt2img.txt2img, extra_outputs=[None, '', '']),
  407. _js="submit",
  408. inputs=[
  409. dummy_component,
  410. txt2img_prompt,
  411. txt2img_negative_prompt,
  412. txt2img_prompt_styles,
  413. steps,
  414. sampler_index,
  415. restore_faces,
  416. tiling,
  417. batch_count,
  418. batch_size,
  419. cfg_scale,
  420. seed,
  421. subseed, subseed_strength, seed_resize_from_h, seed_resize_from_w, seed_checkbox,
  422. height,
  423. width,
  424. enable_hr,
  425. denoising_strength,
  426. hr_scale,
  427. hr_upscaler,
  428. hr_second_pass_steps,
  429. hr_resize_x,
  430. hr_resize_y,
  431. override_settings,
  432. ] + custom_inputs,
  433. outputs=[
  434. txt2img_gallery,
  435. generation_info,
  436. html_info,
  437. html_log,
  438. ],
  439. show_progress=False,
  440. )
  441. txt2img_prompt.submit(**txt2img_args)
  442. submit.click(**txt2img_args)
  443. res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False)
  444. txt_prompt_img.change(
  445. fn=modules.images.image_data,
  446. inputs=[
  447. txt_prompt_img
  448. ],
  449. outputs=[
  450. txt2img_prompt,
  451. txt_prompt_img
  452. ]
  453. )
  454. enable_hr.change(
  455. fn=lambda x: gr_show(x),
  456. inputs=[enable_hr],
  457. outputs=[hr_options],
  458. show_progress = False,
  459. )
  460. txt2img_paste_fields = [
  461. (txt2img_prompt, "Prompt"),
  462. (txt2img_negative_prompt, "Negative prompt"),
  463. (steps, "Steps"),
  464. (sampler_index, "Sampler"),
  465. (restore_faces, "Face restoration"),
  466. (cfg_scale, "CFG scale"),
  467. (seed, "Seed"),
  468. (width, "Size-1"),
  469. (height, "Size-2"),
  470. (batch_size, "Batch size"),
  471. (subseed, "Variation seed"),
  472. (subseed_strength, "Variation seed strength"),
  473. (seed_resize_from_w, "Seed resize from-1"),
  474. (seed_resize_from_h, "Seed resize from-2"),
  475. (denoising_strength, "Denoising strength"),
  476. (enable_hr, lambda d: "Denoising strength" in d),
  477. (hr_options, lambda d: gr.Row.update(visible="Denoising strength" in d)),
  478. (hr_scale, "Hires upscale"),
  479. (hr_upscaler, "Hires upscaler"),
  480. (hr_second_pass_steps, "Hires steps"),
  481. (hr_resize_x, "Hires resize-1"),
  482. (hr_resize_y, "Hires resize-2"),
  483. *modules.scripts.scripts_txt2img.infotext_fields
  484. ]
  485. parameters_copypaste.add_paste_fields("txt2img", None, txt2img_paste_fields, override_settings)
  486. parameters_copypaste.register_paste_params_button(parameters_copypaste.ParamBinding(
  487. paste_button=txt2img_paste, tabname="txt2img", source_text_component=txt2img_prompt, source_image_component=None,
  488. ))
  489. txt2img_preview_params = [
  490. txt2img_prompt,
  491. txt2img_negative_prompt,
  492. steps,
  493. sampler_index,
  494. cfg_scale,
  495. seed,
  496. width,
  497. height,
  498. ]
  499. token_button.click(fn=wrap_queued_call(update_token_counter), inputs=[txt2img_prompt, steps], outputs=[token_counter])
  500. negative_token_button.click(fn=wrap_queued_call(update_token_counter), inputs=[txt2img_negative_prompt, steps], outputs=[negative_token_counter])
  501. ui_extra_networks.setup_ui(extra_networks_ui, txt2img_gallery)
  502. modules.scripts.scripts_current = modules.scripts.scripts_img2img
  503. modules.scripts.scripts_img2img.initialize_scripts(is_img2img=True)
  504. with gr.Blocks(analytics_enabled=False) as img2img_interface:
  505. img2img_prompt, img2img_prompt_styles, img2img_negative_prompt, submit, img2img_interrogate, img2img_deepbooru, img2img_prompt_style_apply, img2img_save_style, img2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button = create_toprow(is_img2img=True)
  506. img2img_prompt_img = gr.File(label="", elem_id="img2img_prompt_image", file_count="single", type="binary", visible=False)
  507. with FormRow(variant='compact', elem_id="img2img_extra_networks", visible=False) as extra_networks:
  508. from modules import ui_extra_networks
  509. extra_networks_ui_img2img = ui_extra_networks.create_ui(extra_networks, extra_networks_button, 'img2img')
  510. with FormRow().style(equal_height=False):
  511. with gr.Column(variant='compact', elem_id="img2img_settings"):
  512. copy_image_buttons = []
  513. copy_image_destinations = {}
  514. def add_copy_image_controls(tab_name, elem):
  515. with gr.Row(variant="compact", elem_id=f"img2img_copy_to_{tab_name}"):
  516. gr.HTML("Copy image to: ", elem_id=f"img2img_label_copy_to_{tab_name}")
  517. for title, name in zip(['img2img', 'sketch', 'inpaint', 'inpaint sketch'], ['img2img', 'sketch', 'inpaint', 'inpaint_sketch']):
  518. if name == tab_name:
  519. gr.Button(title, interactive=False)
  520. copy_image_destinations[name] = elem
  521. continue
  522. button = gr.Button(title)
  523. copy_image_buttons.append((button, name, elem))
  524. with gr.Tabs(elem_id="mode_img2img"):
  525. with gr.TabItem('img2img', id='img2img', elem_id="img2img_img2img_tab") as tab_img2img:
  526. init_img = gr.Image(label="Image for img2img", elem_id="img2img_image", show_label=False, source="upload", interactive=True, type="pil", tool="editor", image_mode="RGBA").style(height=480)
  527. add_copy_image_controls('img2img', init_img)
  528. with gr.TabItem('Sketch', id='img2img_sketch', elem_id="img2img_img2img_sketch_tab") as tab_sketch:
  529. sketch = gr.Image(label="Image for img2img", elem_id="img2img_sketch", show_label=False, source="upload", interactive=True, type="pil", tool="color-sketch", image_mode="RGBA").style(height=480)
  530. add_copy_image_controls('sketch', sketch)
  531. with gr.TabItem('Inpaint', id='inpaint', elem_id="img2img_inpaint_tab") as tab_inpaint:
  532. init_img_with_mask = gr.Image(label="Image for inpainting with mask", show_label=False, elem_id="img2maskimg", source="upload", interactive=True, type="pil", tool="sketch", image_mode="RGBA").style(height=480)
  533. add_copy_image_controls('inpaint', init_img_with_mask)
  534. with gr.TabItem('Inpaint sketch', id='inpaint_sketch', elem_id="img2img_inpaint_sketch_tab") as tab_inpaint_color:
  535. inpaint_color_sketch = gr.Image(label="Color sketch inpainting", show_label=False, elem_id="inpaint_sketch", source="upload", interactive=True, type="pil", tool="color-sketch", image_mode="RGBA").style(height=480)
  536. inpaint_color_sketch_orig = gr.State(None)
  537. add_copy_image_controls('inpaint_sketch', inpaint_color_sketch)
  538. def update_orig(image, state):
  539. if image is not None:
  540. same_size = state is not None and state.size == image.size
  541. has_exact_match = np.any(np.all(np.array(image) == np.array(state), axis=-1))
  542. edited = same_size and has_exact_match
  543. return image if not edited or state is None else state
  544. inpaint_color_sketch.change(update_orig, [inpaint_color_sketch, inpaint_color_sketch_orig], inpaint_color_sketch_orig)
  545. with gr.TabItem('Inpaint upload', id='inpaint_upload', elem_id="img2img_inpaint_upload_tab") as tab_inpaint_upload:
  546. init_img_inpaint = gr.Image(label="Image for img2img", show_label=False, source="upload", interactive=True, type="pil", elem_id="img_inpaint_base")
  547. init_mask_inpaint = gr.Image(label="Mask", source="upload", interactive=True, type="pil", elem_id="img_inpaint_mask")
  548. with gr.TabItem('Batch', id='batch', elem_id="img2img_batch_tab") as tab_batch:
  549. hidden = '<br>Disabled when launched with --hide-ui-dir-config.' if shared.cmd_opts.hide_ui_dir_config else ''
  550. gr.HTML(
  551. f"<p style='padding-bottom: 1em;' class=\"text-gray-500\">Process images in a directory on the same machine where the server is running." +
  552. f"<br>Use an empty output directory to save pictures normally instead of writing to the output directory." +
  553. f"<br>Add inpaint batch mask directory to enable inpaint batch processing."
  554. f"{hidden}</p>"
  555. )
  556. img2img_batch_input_dir = gr.Textbox(label="Input directory", **shared.hide_dirs, elem_id="img2img_batch_input_dir")
  557. img2img_batch_output_dir = gr.Textbox(label="Output directory", **shared.hide_dirs, elem_id="img2img_batch_output_dir")
  558. img2img_batch_inpaint_mask_dir = gr.Textbox(label="Inpaint batch mask directory (required for inpaint batch processing only)", **shared.hide_dirs, elem_id="img2img_batch_inpaint_mask_dir")
  559. def copy_image(img):
  560. if isinstance(img, dict) and 'image' in img:
  561. return img['image']
  562. return img
  563. for button, name, elem in copy_image_buttons:
  564. button.click(
  565. fn=copy_image,
  566. inputs=[elem],
  567. outputs=[copy_image_destinations[name]],
  568. )
  569. button.click(
  570. fn=lambda: None,
  571. _js="switch_to_"+name.replace(" ", "_"),
  572. inputs=[],
  573. outputs=[],
  574. )
  575. with FormRow():
  576. resize_mode = gr.Radio(label="Resize mode", elem_id="resize_mode", choices=["Just resize", "Crop and resize", "Resize and fill", "Just resize (latent upscale)"], type="index", value="Just resize")
  577. for category in ordered_ui_categories():
  578. if category == "sampler":
  579. steps, sampler_index = create_sampler_and_steps_selection(samplers_for_img2img, "img2img")
  580. elif category == "dimensions":
  581. with FormRow():
  582. with gr.Column(elem_id="img2img_column_size", scale=4):
  583. width = gr.Slider(minimum=64, maximum=2048, step=8, label="Width", value=512, elem_id="img2img_width")
  584. height = gr.Slider(minimum=64, maximum=2048, step=8, label="Height", value=512, elem_id="img2img_height")
  585. with gr.Column(elem_id="img2img_dimensions_row", scale=1, elem_classes="dimensions-tools"):
  586. res_switch_btn = ToolButton(value=switch_values_symbol, elem_id="img2img_res_switch_btn")
  587. if opts.dimensions_and_batch_together:
  588. with gr.Column(elem_id="img2img_column_batch"):
  589. batch_count = gr.Slider(minimum=1, step=1, label='Batch count', value=1, elem_id="img2img_batch_count")
  590. batch_size = gr.Slider(minimum=1, maximum=8, step=1, label='Batch size', value=1, elem_id="img2img_batch_size")
  591. elif category == "cfg":
  592. with FormGroup():
  593. with FormRow():
  594. cfg_scale = gr.Slider(minimum=1.0, maximum=30.0, step=0.5, label='CFG Scale', value=7.0, elem_id="img2img_cfg_scale")
  595. image_cfg_scale = gr.Slider(minimum=0, maximum=3.0, step=0.05, label='Image CFG Scale', value=1.5, elem_id="img2img_image_cfg_scale", visible=shared.sd_model and shared.sd_model.cond_stage_key == "edit")
  596. denoising_strength = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Denoising strength', value=0.75, elem_id="img2img_denoising_strength")
  597. elif category == "seed":
  598. seed, reuse_seed, subseed, reuse_subseed, subseed_strength, seed_resize_from_h, seed_resize_from_w, seed_checkbox = create_seed_inputs('img2img')
  599. elif category == "checkboxes":
  600. with FormRow(elem_classes="checkboxes-row", variant="compact"):
  601. restore_faces = gr.Checkbox(label='Restore faces', value=False, visible=len(shared.face_restorers) > 1, elem_id="img2img_restore_faces")
  602. tiling = gr.Checkbox(label='Tiling', value=False, elem_id="img2img_tiling")
  603. elif category == "batch":
  604. if not opts.dimensions_and_batch_together:
  605. with FormRow(elem_id="img2img_column_batch"):
  606. batch_count = gr.Slider(minimum=1, step=1, label='Batch count', value=1, elem_id="img2img_batch_count")
  607. batch_size = gr.Slider(minimum=1, maximum=8, step=1, label='Batch size', value=1, elem_id="img2img_batch_size")
  608. elif category == "override_settings":
  609. with FormRow(elem_id="img2img_override_settings_row") as row:
  610. override_settings = create_override_settings_dropdown('img2img', row)
  611. elif category == "scripts":
  612. with FormGroup(elem_id="img2img_script_container"):
  613. custom_inputs = modules.scripts.scripts_img2img.setup_ui()
  614. elif category == "inpaint":
  615. with FormGroup(elem_id="inpaint_controls", visible=False) as inpaint_controls:
  616. with FormRow():
  617. mask_blur = gr.Slider(label='Mask blur', minimum=0, maximum=64, step=1, value=4, elem_id="img2img_mask_blur")
  618. mask_alpha = gr.Slider(label="Mask transparency", visible=False, elem_id="img2img_mask_alpha")
  619. with FormRow():
  620. inpainting_mask_invert = gr.Radio(label='Mask mode', choices=['Inpaint masked', 'Inpaint not masked'], value='Inpaint masked', type="index", elem_id="img2img_mask_mode")
  621. with FormRow():
  622. inpainting_fill = gr.Radio(label='Masked content', choices=['fill', 'original', 'latent noise', 'latent nothing'], value='original', type="index", elem_id="img2img_inpainting_fill")
  623. with FormRow():
  624. with gr.Column():
  625. inpaint_full_res = gr.Radio(label="Inpaint area", choices=["Whole picture", "Only masked"], type="index", value="Whole picture", elem_id="img2img_inpaint_full_res")
  626. with gr.Column(scale=4):
  627. inpaint_full_res_padding = gr.Slider(label='Only masked padding, pixels', minimum=0, maximum=256, step=4, value=32, elem_id="img2img_inpaint_full_res_padding")
  628. def select_img2img_tab(tab):
  629. return gr.update(visible=tab in [2, 3, 4]), gr.update(visible=tab == 3),
  630. for i, elem in enumerate([tab_img2img, tab_sketch, tab_inpaint, tab_inpaint_color, tab_inpaint_upload, tab_batch]):
  631. elem.select(
  632. fn=lambda tab=i: select_img2img_tab(tab),
  633. inputs=[],
  634. outputs=[inpaint_controls, mask_alpha],
  635. )
  636. img2img_gallery, generation_info, html_info, html_log = create_output_panel("img2img", opts.outdir_img2img_samples)
  637. connect_reuse_seed(seed, reuse_seed, generation_info, dummy_component, is_subseed=False)
  638. connect_reuse_seed(subseed, reuse_subseed, generation_info, dummy_component, is_subseed=True)
  639. img2img_prompt_img.change(
  640. fn=modules.images.image_data,
  641. inputs=[
  642. img2img_prompt_img
  643. ],
  644. outputs=[
  645. img2img_prompt,
  646. img2img_prompt_img
  647. ]
  648. )
  649. img2img_args = dict(
  650. fn=wrap_gradio_gpu_call(modules.img2img.img2img, extra_outputs=[None, '', '']),
  651. _js="submit_img2img",
  652. inputs=[
  653. dummy_component,
  654. dummy_component,
  655. img2img_prompt,
  656. img2img_negative_prompt,
  657. img2img_prompt_styles,
  658. init_img,
  659. sketch,
  660. init_img_with_mask,
  661. inpaint_color_sketch,
  662. inpaint_color_sketch_orig,
  663. init_img_inpaint,
  664. init_mask_inpaint,
  665. steps,
  666. sampler_index,
  667. mask_blur,
  668. mask_alpha,
  669. inpainting_fill,
  670. restore_faces,
  671. tiling,
  672. batch_count,
  673. batch_size,
  674. cfg_scale,
  675. image_cfg_scale,
  676. denoising_strength,
  677. seed,
  678. subseed, subseed_strength, seed_resize_from_h, seed_resize_from_w, seed_checkbox,
  679. height,
  680. width,
  681. resize_mode,
  682. inpaint_full_res,
  683. inpaint_full_res_padding,
  684. inpainting_mask_invert,
  685. img2img_batch_input_dir,
  686. img2img_batch_output_dir,
  687. img2img_batch_inpaint_mask_dir,
  688. override_settings,
  689. ] + custom_inputs,
  690. outputs=[
  691. img2img_gallery,
  692. generation_info,
  693. html_info,
  694. html_log,
  695. ],
  696. show_progress=False,
  697. )
  698. interrogate_args = dict(
  699. _js="get_img2img_tab_index",
  700. inputs=[
  701. dummy_component,
  702. img2img_batch_input_dir,
  703. img2img_batch_output_dir,
  704. init_img,
  705. sketch,
  706. init_img_with_mask,
  707. inpaint_color_sketch,
  708. init_img_inpaint,
  709. ],
  710. outputs=[img2img_prompt, dummy_component],
  711. )
  712. img2img_prompt.submit(**img2img_args)
  713. submit.click(**img2img_args)
  714. res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False)
  715. img2img_interrogate.click(
  716. fn=lambda *args: process_interrogate(interrogate, *args),
  717. **interrogate_args,
  718. )
  719. img2img_deepbooru.click(
  720. fn=lambda *args: process_interrogate(interrogate_deepbooru, *args),
  721. **interrogate_args,
  722. )
  723. prompts = [(txt2img_prompt, txt2img_negative_prompt), (img2img_prompt, img2img_negative_prompt)]
  724. style_dropdowns = [txt2img_prompt_styles, img2img_prompt_styles]
  725. style_js_funcs = ["update_txt2img_tokens", "update_img2img_tokens"]
  726. for button, (prompt, negative_prompt) in zip([txt2img_save_style, img2img_save_style], prompts):
  727. button.click(
  728. fn=add_style,
  729. _js="ask_for_style_name",
  730. # Have to pass empty dummy component here, because the JavaScript and Python function have to accept
  731. # the same number of parameters, but we only know the style-name after the JavaScript prompt
  732. inputs=[dummy_component, prompt, negative_prompt],
  733. outputs=[txt2img_prompt_styles, img2img_prompt_styles],
  734. )
  735. for button, (prompt, negative_prompt), styles, js_func in zip([txt2img_prompt_style_apply, img2img_prompt_style_apply], prompts, style_dropdowns, style_js_funcs):
  736. button.click(
  737. fn=apply_styles,
  738. _js=js_func,
  739. inputs=[prompt, negative_prompt, styles],
  740. outputs=[prompt, negative_prompt, styles],
  741. )
  742. token_button.click(fn=update_token_counter, inputs=[img2img_prompt, steps], outputs=[token_counter])
  743. negative_token_button.click(fn=wrap_queued_call(update_token_counter), inputs=[img2img_negative_prompt, steps], outputs=[negative_token_counter])
  744. ui_extra_networks.setup_ui(extra_networks_ui_img2img, img2img_gallery)
  745. img2img_paste_fields = [
  746. (img2img_prompt, "Prompt"),
  747. (img2img_negative_prompt, "Negative prompt"),
  748. (steps, "Steps"),
  749. (sampler_index, "Sampler"),
  750. (restore_faces, "Face restoration"),
  751. (cfg_scale, "CFG scale"),
  752. (image_cfg_scale, "Image CFG scale"),
  753. (seed, "Seed"),
  754. (width, "Size-1"),
  755. (height, "Size-2"),
  756. (batch_size, "Batch size"),
  757. (subseed, "Variation seed"),
  758. (subseed_strength, "Variation seed strength"),
  759. (seed_resize_from_w, "Seed resize from-1"),
  760. (seed_resize_from_h, "Seed resize from-2"),
  761. (denoising_strength, "Denoising strength"),
  762. (mask_blur, "Mask blur"),
  763. *modules.scripts.scripts_img2img.infotext_fields
  764. ]
  765. parameters_copypaste.add_paste_fields("img2img", init_img, img2img_paste_fields, override_settings)
  766. parameters_copypaste.add_paste_fields("inpaint", init_img_with_mask, img2img_paste_fields, override_settings)
  767. parameters_copypaste.register_paste_params_button(parameters_copypaste.ParamBinding(
  768. paste_button=img2img_paste, tabname="img2img", source_text_component=img2img_prompt, source_image_component=None,
  769. ))
  770. modules.scripts.scripts_current = None
  771. with gr.Blocks(analytics_enabled=False) as extras_interface:
  772. ui_postprocessing.create_ui()
  773. with gr.Blocks(analytics_enabled=False) as pnginfo_interface:
  774. with gr.Row().style(equal_height=False):
  775. with gr.Column(variant='panel'):
  776. image = gr.Image(elem_id="pnginfo_image", label="Source", source="upload", interactive=True, type="pil")
  777. with gr.Column(variant='panel'):
  778. html = gr.HTML()
  779. generation_info = gr.Textbox(visible=False, elem_id="pnginfo_generation_info")
  780. html2 = gr.HTML()
  781. with gr.Row():
  782. buttons = parameters_copypaste.create_buttons(["txt2img", "img2img", "inpaint", "extras"])
  783. for tabname, button in buttons.items():
  784. parameters_copypaste.register_paste_params_button(parameters_copypaste.ParamBinding(
  785. paste_button=button, tabname=tabname, source_text_component=generation_info, source_image_component=image,
  786. ))
  787. image.change(
  788. fn=wrap_gradio_call(modules.extras.run_pnginfo),
  789. inputs=[image],
  790. outputs=[html, generation_info, html2],
  791. )
  792. def update_interp_description(value):
  793. interp_description_css = "<p style='margin-bottom: 2.5em'>{}</p>"
  794. interp_descriptions = {
  795. "No interpolation": interp_description_css.format("No interpolation will be used. Requires one model; A. Allows for format conversion and VAE baking."),
  796. "Weighted sum": interp_description_css.format("A weighted sum will be used for interpolation. Requires two models; A and B. The result is calculated as A * (1 - M) + B * M"),
  797. "Add difference": interp_description_css.format("The difference between the last two models will be added to the first. Requires three models; A, B and C. The result is calculated as A + (B - C) * M")
  798. }
  799. return interp_descriptions[value]
  800. with gr.Blocks(analytics_enabled=False) as modelmerger_interface:
  801. with gr.Row().style(equal_height=False):
  802. with gr.Column(variant='compact'):
  803. interp_description = gr.HTML(value=update_interp_description("Weighted sum"), elem_id="modelmerger_interp_description")
  804. with FormRow(elem_id="modelmerger_models"):
  805. primary_model_name = gr.Dropdown(modules.sd_models.checkpoint_tiles(), elem_id="modelmerger_primary_model_name", label="Primary model (A)")
  806. create_refresh_button(primary_model_name, modules.sd_models.list_models, lambda: {"choices": modules.sd_models.checkpoint_tiles()}, "refresh_checkpoint_A")
  807. secondary_model_name = gr.Dropdown(modules.sd_models.checkpoint_tiles(), elem_id="modelmerger_secondary_model_name", label="Secondary model (B)")
  808. create_refresh_button(secondary_model_name, modules.sd_models.list_models, lambda: {"choices": modules.sd_models.checkpoint_tiles()}, "refresh_checkpoint_B")
  809. tertiary_model_name = gr.Dropdown(modules.sd_models.checkpoint_tiles(), elem_id="modelmerger_tertiary_model_name", label="Tertiary model (C)")
  810. create_refresh_button(tertiary_model_name, modules.sd_models.list_models, lambda: {"choices": modules.sd_models.checkpoint_tiles()}, "refresh_checkpoint_C")
  811. custom_name = gr.Textbox(label="Custom Name (Optional)", elem_id="modelmerger_custom_name")
  812. interp_amount = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, label='Multiplier (M) - set to 0 to get model A', value=0.3, elem_id="modelmerger_interp_amount")
  813. interp_method = gr.Radio(choices=["No interpolation", "Weighted sum", "Add difference"], value="Weighted sum", label="Interpolation Method", elem_id="modelmerger_interp_method")
  814. interp_method.change(fn=update_interp_description, inputs=[interp_method], outputs=[interp_description])
  815. with FormRow():
  816. checkpoint_format = gr.Radio(choices=["ckpt", "safetensors"], value="ckpt", label="Checkpoint format", elem_id="modelmerger_checkpoint_format")
  817. save_as_half = gr.Checkbox(value=False, label="Save as float16", elem_id="modelmerger_save_as_half")
  818. with FormRow():
  819. with gr.Column():
  820. config_source = gr.Radio(choices=["A, B or C", "B", "C", "Don't"], value="A, B or C", label="Copy config from", type="index", elem_id="modelmerger_config_method")
  821. with gr.Column():
  822. with FormRow():
  823. bake_in_vae = gr.Dropdown(choices=["None"] + list(sd_vae.vae_dict), value="None", label="Bake in VAE", elem_id="modelmerger_bake_in_vae")
  824. create_refresh_button(bake_in_vae, sd_vae.refresh_vae_list, lambda: {"choices": ["None"] + list(sd_vae.vae_dict)}, "modelmerger_refresh_bake_in_vae")
  825. with FormRow():
  826. discard_weights = gr.Textbox(value="", label="Discard weights with matching name", elem_id="modelmerger_discard_weights")
  827. with gr.Row():
  828. modelmerger_merge = gr.Button(elem_id="modelmerger_merge", value="Merge", variant='primary')
  829. with gr.Column(variant='compact', elem_id="modelmerger_results_container"):
  830. with gr.Group(elem_id="modelmerger_results_panel"):
  831. modelmerger_result = gr.HTML(elem_id="modelmerger_result", show_label=False)
  832. with gr.Blocks(analytics_enabled=False) as train_interface:
  833. with gr.Row().style(equal_height=False):
  834. gr.HTML(value="<p style='margin-bottom: 0.7em'>See <b><a href=\"https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Textual-Inversion\">wiki</a></b> for detailed explanation.</p>")
  835. with gr.Row(variant="compact").style(equal_height=False):
  836. with gr.Tabs(elem_id="train_tabs"):
  837. with gr.Tab(label="Create embedding"):
  838. new_embedding_name = gr.Textbox(label="Name", elem_id="train_new_embedding_name")
  839. initialization_text = gr.Textbox(label="Initialization text", value="*", elem_id="train_initialization_text")
  840. nvpt = gr.Slider(label="Number of vectors per token", minimum=1, maximum=75, step=1, value=1, elem_id="train_nvpt")
  841. overwrite_old_embedding = gr.Checkbox(value=False, label="Overwrite Old Embedding", elem_id="train_overwrite_old_embedding")
  842. with gr.Row():
  843. with gr.Column(scale=3):
  844. gr.HTML(value="")
  845. with gr.Column():
  846. create_embedding = gr.Button(value="Create embedding", variant='primary', elem_id="train_create_embedding")
  847. with gr.Tab(label="Create hypernetwork"):
  848. new_hypernetwork_name = gr.Textbox(label="Name", elem_id="train_new_hypernetwork_name")
  849. new_hypernetwork_sizes = gr.CheckboxGroup(label="Modules", value=["768", "320", "640", "1280"], choices=["768", "1024", "320", "640", "1280"], elem_id="train_new_hypernetwork_sizes")
  850. new_hypernetwork_layer_structure = gr.Textbox("1, 2, 1", label="Enter hypernetwork layer structure", placeholder="1st and last digit must be 1. ex:'1, 2, 1'", elem_id="train_new_hypernetwork_layer_structure")
  851. new_hypernetwork_activation_func = gr.Dropdown(value="linear", label="Select activation function of hypernetwork. Recommended : Swish / Linear(none)", choices=modules.hypernetworks.ui.keys, elem_id="train_new_hypernetwork_activation_func")
  852. new_hypernetwork_initialization_option = gr.Dropdown(value = "Normal", label="Select Layer weights initialization. Recommended: Kaiming for relu-like, Xavier for sigmoid-like, Normal otherwise", choices=["Normal", "KaimingUniform", "KaimingNormal", "XavierUniform", "XavierNormal"], elem_id="train_new_hypernetwork_initialization_option")
  853. new_hypernetwork_add_layer_norm = gr.Checkbox(label="Add layer normalization", elem_id="train_new_hypernetwork_add_layer_norm")
  854. new_hypernetwork_use_dropout = gr.Checkbox(label="Use dropout", elem_id="train_new_hypernetwork_use_dropout")
  855. new_hypernetwork_dropout_structure = gr.Textbox("0, 0, 0", label="Enter hypernetwork Dropout structure (or empty). Recommended : 0~0.35 incrementing sequence: 0, 0.05, 0.15", placeholder="1st and last digit must be 0 and values should be between 0 and 1. ex:'0, 0.01, 0'")
  856. overwrite_old_hypernetwork = gr.Checkbox(value=False, label="Overwrite Old Hypernetwork", elem_id="train_overwrite_old_hypernetwork")
  857. with gr.Row():
  858. with gr.Column(scale=3):
  859. gr.HTML(value="")
  860. with gr.Column():
  861. create_hypernetwork = gr.Button(value="Create hypernetwork", variant='primary', elem_id="train_create_hypernetwork")
  862. with gr.Tab(label="Preprocess images"):
  863. process_src = gr.Textbox(label='Source directory', elem_id="train_process_src")
  864. process_dst = gr.Textbox(label='Destination directory', elem_id="train_process_dst")
  865. process_width = gr.Slider(minimum=64, maximum=2048, step=8, label="Width", value=512, elem_id="train_process_width")
  866. process_height = gr.Slider(minimum=64, maximum=2048, step=8, label="Height", value=512, elem_id="train_process_height")
  867. preprocess_txt_action = gr.Dropdown(label='Existing Caption txt Action', value="ignore", choices=["ignore", "copy", "prepend", "append"], elem_id="train_preprocess_txt_action")
  868. with gr.Row():
  869. process_flip = gr.Checkbox(label='Create flipped copies', elem_id="train_process_flip")
  870. process_split = gr.Checkbox(label='Split oversized images', elem_id="train_process_split")
  871. process_focal_crop = gr.Checkbox(label='Auto focal point crop', elem_id="train_process_focal_crop")
  872. process_multicrop = gr.Checkbox(label='Auto-sized crop', elem_id="train_process_multicrop")
  873. process_caption = gr.Checkbox(label='Use BLIP for caption', elem_id="train_process_caption")
  874. process_caption_deepbooru = gr.Checkbox(label='Use deepbooru for caption', visible=True, elem_id="train_process_caption_deepbooru")
  875. with gr.Row(visible=False) as process_split_extra_row:
  876. process_split_threshold = gr.Slider(label='Split image threshold', value=0.5, minimum=0.0, maximum=1.0, step=0.05, elem_id="train_process_split_threshold")
  877. process_overlap_ratio = gr.Slider(label='Split image overlap ratio', value=0.2, minimum=0.0, maximum=0.9, step=0.05, elem_id="train_process_overlap_ratio")
  878. with gr.Row(visible=False) as process_focal_crop_row:
  879. process_focal_crop_face_weight = gr.Slider(label='Focal point face weight', value=0.9, minimum=0.0, maximum=1.0, step=0.05, elem_id="train_process_focal_crop_face_weight")
  880. process_focal_crop_entropy_weight = gr.Slider(label='Focal point entropy weight', value=0.15, minimum=0.0, maximum=1.0, step=0.05, elem_id="train_process_focal_crop_entropy_weight")
  881. process_focal_crop_edges_weight = gr.Slider(label='Focal point edges weight', value=0.5, minimum=0.0, maximum=1.0, step=0.05, elem_id="train_process_focal_crop_edges_weight")
  882. process_focal_crop_debug = gr.Checkbox(label='Create debug image', elem_id="train_process_focal_crop_debug")
  883. with gr.Column(visible=False) as process_multicrop_col:
  884. gr.Markdown('Each image is center-cropped with an automatically chosen width and height.')
  885. with gr.Row():
  886. process_multicrop_mindim = gr.Slider(minimum=64, maximum=2048, step=8, label="Dimension lower bound", value=384, elem_id="train_process_multicrop_mindim")
  887. process_multicrop_maxdim = gr.Slider(minimum=64, maximum=2048, step=8, label="Dimension upper bound", value=768, elem_id="train_process_multicrop_maxdim")
  888. with gr.Row():
  889. process_multicrop_minarea = gr.Slider(minimum=64*64, maximum=2048*2048, step=1, label="Area lower bound", value=64*64, elem_id="train_process_multicrop_minarea")
  890. process_multicrop_maxarea = gr.Slider(minimum=64*64, maximum=2048*2048, step=1, label="Area upper bound", value=640*640, elem_id="train_process_multicrop_maxarea")
  891. with gr.Row():
  892. process_multicrop_objective = gr.Radio(["Maximize area", "Minimize error"], value="Maximize area", label="Resizing objective", elem_id="train_process_multicrop_objective")
  893. process_multicrop_threshold = gr.Slider(minimum=0, maximum=1, step=0.01, label="Error threshold", value=0.1, elem_id="train_process_multicrop_threshold")
  894. with gr.Row():
  895. with gr.Column(scale=3):
  896. gr.HTML(value="")
  897. with gr.Column():
  898. with gr.Row():
  899. interrupt_preprocessing = gr.Button("Interrupt", elem_id="train_interrupt_preprocessing")
  900. run_preprocess = gr.Button(value="Preprocess", variant='primary', elem_id="train_run_preprocess")
  901. process_split.change(
  902. fn=lambda show: gr_show(show),
  903. inputs=[process_split],
  904. outputs=[process_split_extra_row],
  905. )
  906. process_focal_crop.change(
  907. fn=lambda show: gr_show(show),
  908. inputs=[process_focal_crop],
  909. outputs=[process_focal_crop_row],
  910. )
  911. process_multicrop.change(
  912. fn=lambda show: gr_show(show),
  913. inputs=[process_multicrop],
  914. outputs=[process_multicrop_col],
  915. )
  916. def get_textual_inversion_template_names():
  917. return sorted([x for x in textual_inversion.textual_inversion_templates])
  918. with gr.Tab(label="Train"):
  919. gr.HTML(value="<p style='margin-bottom: 0.7em'>Train an embedding or Hypernetwork; you must specify a directory with a set of 1:1 ratio images <a href=\"https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Textual-Inversion\" style=\"font-weight:bold;\">[wiki]</a></p>")
  920. with FormRow():
  921. train_embedding_name = gr.Dropdown(label='Embedding', elem_id="train_embedding", choices=sorted(sd_hijack.model_hijack.embedding_db.word_embeddings.keys()))
  922. create_refresh_button(train_embedding_name, sd_hijack.model_hijack.embedding_db.load_textual_inversion_embeddings, lambda: {"choices": sorted(sd_hijack.model_hijack.embedding_db.word_embeddings.keys())}, "refresh_train_embedding_name")
  923. train_hypernetwork_name = gr.Dropdown(label='Hypernetwork', elem_id="train_hypernetwork", choices=[x for x in shared.hypernetworks.keys()])
  924. create_refresh_button(train_hypernetwork_name, shared.reload_hypernetworks, lambda: {"choices": sorted([x for x in shared.hypernetworks.keys()])}, "refresh_train_hypernetwork_name")
  925. with FormRow():
  926. embedding_learn_rate = gr.Textbox(label='Embedding Learning rate', placeholder="Embedding Learning rate", value="0.005", elem_id="train_embedding_learn_rate")
  927. hypernetwork_learn_rate = gr.Textbox(label='Hypernetwork Learning rate', placeholder="Hypernetwork Learning rate", value="0.00001", elem_id="train_hypernetwork_learn_rate")
  928. with FormRow():
  929. clip_grad_mode = gr.Dropdown(value="disabled", label="Gradient Clipping", choices=["disabled", "value", "norm"])
  930. clip_grad_value = gr.Textbox(placeholder="Gradient clip value", value="0.1", show_label=False)
  931. with FormRow():
  932. batch_size = gr.Number(label='Batch size', value=1, precision=0, elem_id="train_batch_size")
  933. gradient_step = gr.Number(label='Gradient accumulation steps', value=1, precision=0, elem_id="train_gradient_step")
  934. dataset_directory = gr.Textbox(label='Dataset directory', placeholder="Path to directory with input images", elem_id="train_dataset_directory")
  935. log_directory = gr.Textbox(label='Log directory', placeholder="Path to directory where to write outputs", value="textual_inversion", elem_id="train_log_directory")
  936. with FormRow():
  937. template_file = gr.Dropdown(label='Prompt template', value="style_filewords.txt", elem_id="train_template_file", choices=get_textual_inversion_template_names())
  938. create_refresh_button(template_file, textual_inversion.list_textual_inversion_templates, lambda: {"choices": get_textual_inversion_template_names()}, "refrsh_train_template_file")
  939. training_width = gr.Slider(minimum=64, maximum=2048, step=8, label="Width", value=512, elem_id="train_training_width")
  940. training_height = gr.Slider(minimum=64, maximum=2048, step=8, label="Height", value=512, elem_id="train_training_height")
  941. varsize = gr.Checkbox(label="Do not resize images", value=False, elem_id="train_varsize")
  942. steps = gr.Number(label='Max steps', value=100000, precision=0, elem_id="train_steps")
  943. with FormRow():
  944. create_image_every = gr.Number(label='Save an image to log directory every N steps, 0 to disable', value=500, precision=0, elem_id="train_create_image_every")
  945. save_embedding_every = gr.Number(label='Save a copy of embedding to log directory every N steps, 0 to disable', value=500, precision=0, elem_id="train_save_embedding_every")
  946. use_weight = gr.Checkbox(label="Use PNG alpha channel as loss weight", value=False, elem_id="use_weight")
  947. save_image_with_stored_embedding = gr.Checkbox(label='Save images with embedding in PNG chunks', value=True, elem_id="train_save_image_with_stored_embedding")
  948. preview_from_txt2img = gr.Checkbox(label='Read parameters (prompt, etc...) from txt2img tab when making previews', value=False, elem_id="train_preview_from_txt2img")
  949. shuffle_tags = gr.Checkbox(label="Shuffle tags by ',' when creating prompts.", value=False, elem_id="train_shuffle_tags")
  950. tag_drop_out = gr.Slider(minimum=0, maximum=1, step=0.1, label="Drop out tags when creating prompts.", value=0, elem_id="train_tag_drop_out")
  951. latent_sampling_method = gr.Radio(label='Choose latent sampling method', value="once", choices=['once', 'deterministic', 'random'], elem_id="train_latent_sampling_method")
  952. with gr.Row():
  953. train_embedding = gr.Button(value="Train Embedding", variant='primary', elem_id="train_train_embedding")
  954. interrupt_training = gr.Button(value="Interrupt", elem_id="train_interrupt_training")
  955. train_hypernetwork = gr.Button(value="Train Hypernetwork", variant='primary', elem_id="train_train_hypernetwork")
  956. params = script_callbacks.UiTrainTabParams(txt2img_preview_params)
  957. script_callbacks.ui_train_tabs_callback(params)
  958. with gr.Column(elem_id='ti_gallery_container'):
  959. ti_output = gr.Text(elem_id="ti_output", value="", show_label=False)
  960. ti_gallery = gr.Gallery(label='Output', show_label=False, elem_id='ti_gallery').style(grid=4)
  961. ti_progress = gr.HTML(elem_id="ti_progress", value="")
  962. ti_outcome = gr.HTML(elem_id="ti_error", value="")
  963. create_embedding.click(
  964. fn=modules.textual_inversion.ui.create_embedding,
  965. inputs=[
  966. new_embedding_name,
  967. initialization_text,
  968. nvpt,
  969. overwrite_old_embedding,
  970. ],
  971. outputs=[
  972. train_embedding_name,
  973. ti_output,
  974. ti_outcome,
  975. ]
  976. )
  977. create_hypernetwork.click(
  978. fn=modules.hypernetworks.ui.create_hypernetwork,
  979. inputs=[
  980. new_hypernetwork_name,
  981. new_hypernetwork_sizes,
  982. overwrite_old_hypernetwork,
  983. new_hypernetwork_layer_structure,
  984. new_hypernetwork_activation_func,
  985. new_hypernetwork_initialization_option,
  986. new_hypernetwork_add_layer_norm,
  987. new_hypernetwork_use_dropout,
  988. new_hypernetwork_dropout_structure
  989. ],
  990. outputs=[
  991. train_hypernetwork_name,
  992. ti_output,
  993. ti_outcome,
  994. ]
  995. )
  996. run_preprocess.click(
  997. fn=wrap_gradio_gpu_call(modules.textual_inversion.ui.preprocess, extra_outputs=[gr.update()]),
  998. _js="start_training_textual_inversion",
  999. inputs=[
  1000. dummy_component,
  1001. process_src,
  1002. process_dst,
  1003. process_width,
  1004. process_height,
  1005. preprocess_txt_action,
  1006. process_flip,
  1007. process_split,
  1008. process_caption,
  1009. process_caption_deepbooru,
  1010. process_split_threshold,
  1011. process_overlap_ratio,
  1012. process_focal_crop,
  1013. process_focal_crop_face_weight,
  1014. process_focal_crop_entropy_weight,
  1015. process_focal_crop_edges_weight,
  1016. process_focal_crop_debug,
  1017. process_multicrop,
  1018. process_multicrop_mindim,
  1019. process_multicrop_maxdim,
  1020. process_multicrop_minarea,
  1021. process_multicrop_maxarea,
  1022. process_multicrop_objective,
  1023. process_multicrop_threshold,
  1024. ],
  1025. outputs=[
  1026. ti_output,
  1027. ti_outcome,
  1028. ],
  1029. )
  1030. train_embedding.click(
  1031. fn=wrap_gradio_gpu_call(modules.textual_inversion.ui.train_embedding, extra_outputs=[gr.update()]),
  1032. _js="start_training_textual_inversion",
  1033. inputs=[
  1034. dummy_component,
  1035. train_embedding_name,
  1036. embedding_learn_rate,
  1037. batch_size,
  1038. gradient_step,
  1039. dataset_directory,
  1040. log_directory,
  1041. training_width,
  1042. training_height,
  1043. varsize,
  1044. steps,
  1045. clip_grad_mode,
  1046. clip_grad_value,
  1047. shuffle_tags,
  1048. tag_drop_out,
  1049. latent_sampling_method,
  1050. use_weight,
  1051. create_image_every,
  1052. save_embedding_every,
  1053. template_file,
  1054. save_image_with_stored_embedding,
  1055. preview_from_txt2img,
  1056. *txt2img_preview_params,
  1057. ],
  1058. outputs=[
  1059. ti_output,
  1060. ti_outcome,
  1061. ]
  1062. )
  1063. train_hypernetwork.click(
  1064. fn=wrap_gradio_gpu_call(modules.hypernetworks.ui.train_hypernetwork, extra_outputs=[gr.update()]),
  1065. _js="start_training_textual_inversion",
  1066. inputs=[
  1067. dummy_component,
  1068. train_hypernetwork_name,
  1069. hypernetwork_learn_rate,
  1070. batch_size,
  1071. gradient_step,
  1072. dataset_directory,
  1073. log_directory,
  1074. training_width,
  1075. training_height,
  1076. varsize,
  1077. steps,
  1078. clip_grad_mode,
  1079. clip_grad_value,
  1080. shuffle_tags,
  1081. tag_drop_out,
  1082. latent_sampling_method,
  1083. use_weight,
  1084. create_image_every,
  1085. save_embedding_every,
  1086. template_file,
  1087. preview_from_txt2img,
  1088. *txt2img_preview_params,
  1089. ],
  1090. outputs=[
  1091. ti_output,
  1092. ti_outcome,
  1093. ]
  1094. )
  1095. interrupt_training.click(
  1096. fn=lambda: shared.state.interrupt(),
  1097. inputs=[],
  1098. outputs=[],
  1099. )
  1100. interrupt_preprocessing.click(
  1101. fn=lambda: shared.state.interrupt(),
  1102. inputs=[],
  1103. outputs=[],
  1104. )
  1105. def create_setting_component(key, is_quicksettings=False):
  1106. def fun():
  1107. return opts.data[key] if key in opts.data else opts.data_labels[key].default
  1108. info = opts.data_labels[key]
  1109. t = type(info.default)
  1110. args = info.component_args() if callable(info.component_args) else info.component_args
  1111. if info.component is not None:
  1112. comp = info.component
  1113. elif t == str:
  1114. comp = gr.Textbox
  1115. elif t == int:
  1116. comp = gr.Number
  1117. elif t == bool:
  1118. comp = gr.Checkbox
  1119. else:
  1120. raise Exception(f'bad options item type: {str(t)} for key {key}')
  1121. elem_id = "setting_"+key
  1122. if info.refresh is not None:
  1123. if is_quicksettings:
  1124. res = comp(label=info.label, value=fun(), elem_id=elem_id, **(args or {}))
  1125. create_refresh_button(res, info.refresh, info.component_args, "refresh_" + key)
  1126. else:
  1127. with FormRow():
  1128. res = comp(label=info.label, value=fun(), elem_id=elem_id, **(args or {}))
  1129. create_refresh_button(res, info.refresh, info.component_args, "refresh_" + key)
  1130. else:
  1131. res = comp(label=info.label, value=fun(), elem_id=elem_id, **(args or {}))
  1132. return res
  1133. components = []
  1134. component_dict = {}
  1135. shared.settings_components = component_dict
  1136. script_callbacks.ui_settings_callback()
  1137. opts.reorder()
  1138. def run_settings(*args):
  1139. changed = []
  1140. for key, value, comp in zip(opts.data_labels.keys(), args, components):
  1141. assert comp == dummy_component or opts.same_type(value, opts.data_labels[key].default), f"Bad value for setting {key}: {value}; expecting {type(opts.data_labels[key].default).__name__}"
  1142. for key, value, comp in zip(opts.data_labels.keys(), args, components):
  1143. if comp == dummy_component:
  1144. continue
  1145. if opts.set(key, value):
  1146. changed.append(key)
  1147. try:
  1148. opts.save(shared.config_filename)
  1149. except RuntimeError:
  1150. return opts.dumpjson(), f'{len(changed)} settings changed without save: {", ".join(changed)}.'
  1151. return opts.dumpjson(), f'{len(changed)} settings changed{": " if len(changed) > 0 else ""}{", ".join(changed)}.'
  1152. def run_settings_single(value, key):
  1153. if not opts.same_type(value, opts.data_labels[key].default):
  1154. return gr.update(visible=True), opts.dumpjson()
  1155. if not opts.set(key, value):
  1156. return gr.update(value=getattr(opts, key)), opts.dumpjson()
  1157. opts.save(shared.config_filename)
  1158. return get_value_for_setting(key), opts.dumpjson()
  1159. with gr.Blocks(analytics_enabled=False) as settings_interface:
  1160. with gr.Row():
  1161. with gr.Column(scale=6):
  1162. settings_submit = gr.Button(value="Apply settings", variant='primary', elem_id="settings_submit")
  1163. with gr.Column():
  1164. restart_gradio = gr.Button(value='Reload UI', variant='primary', elem_id="settings_restart_gradio")
  1165. result = gr.HTML(elem_id="settings_result")
  1166. quicksettings_names = [x.strip() for x in opts.quicksettings.split(",")]
  1167. quicksettings_names = {x: i for i, x in enumerate(quicksettings_names) if x != 'quicksettings'}
  1168. quicksettings_list = []
  1169. previous_section = None
  1170. current_tab = None
  1171. current_row = None
  1172. with gr.Tabs(elem_id="settings"):
  1173. for i, (k, item) in enumerate(opts.data_labels.items()):
  1174. section_must_be_skipped = item.section[0] is None
  1175. if previous_section != item.section and not section_must_be_skipped:
  1176. elem_id, text = item.section
  1177. if current_tab is not None:
  1178. current_row.__exit__()
  1179. current_tab.__exit__()
  1180. gr.Group()
  1181. current_tab = gr.TabItem(elem_id="settings_{}".format(elem_id), label=text)
  1182. current_tab.__enter__()
  1183. current_row = gr.Column(variant='compact')
  1184. current_row.__enter__()
  1185. previous_section = item.section
  1186. if k in quicksettings_names and not shared.cmd_opts.freeze_settings:
  1187. quicksettings_list.append((i, k, item))
  1188. components.append(dummy_component)
  1189. elif section_must_be_skipped:
  1190. components.append(dummy_component)
  1191. else:
  1192. component = create_setting_component(k)
  1193. component_dict[k] = component
  1194. components.append(component)
  1195. if current_tab is not None:
  1196. current_row.__exit__()
  1197. current_tab.__exit__()
  1198. with gr.TabItem("Actions"):
  1199. request_notifications = gr.Button(value='Request browser notifications', elem_id="request_notifications")
  1200. download_localization = gr.Button(value='Download localization template', elem_id="download_localization")
  1201. reload_script_bodies = gr.Button(value='Reload custom script bodies (No ui updates, No restart)', variant='secondary', elem_id="settings_reload_script_bodies")
  1202. with gr.Row():
  1203. unload_sd_model = gr.Button(value='Unload SD checkpoint to free VRAM', elem_id="sett_unload_sd_model")
  1204. reload_sd_model = gr.Button(value='Reload the last SD checkpoint back into VRAM', elem_id="sett_reload_sd_model")
  1205. with gr.TabItem("Licenses"):
  1206. gr.HTML(shared.html("licenses.html"), elem_id="licenses")
  1207. gr.Button(value="Show all pages", elem_id="settings_show_all_pages")
  1208. def unload_sd_weights():
  1209. modules.sd_models.unload_model_weights()
  1210. def reload_sd_weights():
  1211. modules.sd_models.reload_model_weights()
  1212. unload_sd_model.click(
  1213. fn=unload_sd_weights,
  1214. inputs=[],
  1215. outputs=[]
  1216. )
  1217. reload_sd_model.click(
  1218. fn=reload_sd_weights,
  1219. inputs=[],
  1220. outputs=[]
  1221. )
  1222. request_notifications.click(
  1223. fn=lambda: None,
  1224. inputs=[],
  1225. outputs=[],
  1226. _js='function(){}'
  1227. )
  1228. download_localization.click(
  1229. fn=lambda: None,
  1230. inputs=[],
  1231. outputs=[],
  1232. _js='download_localization'
  1233. )
  1234. def reload_scripts():
  1235. modules.scripts.reload_script_body_only()
  1236. reload_javascript() # need to refresh the html page
  1237. reload_script_bodies.click(
  1238. fn=reload_scripts,
  1239. inputs=[],
  1240. outputs=[]
  1241. )
  1242. def request_restart():
  1243. shared.state.interrupt()
  1244. shared.state.need_restart = True
  1245. restart_gradio.click(
  1246. fn=request_restart,
  1247. _js='restart_reload',
  1248. inputs=[],
  1249. outputs=[],
  1250. )
  1251. interfaces = [
  1252. (txt2img_interface, "txt2img", "txt2img"),
  1253. (img2img_interface, "img2img", "img2img"),
  1254. (extras_interface, "Extras", "extras"),
  1255. (pnginfo_interface, "PNG Info", "pnginfo"),
  1256. (modelmerger_interface, "Checkpoint Merger", "modelmerger"),
  1257. (train_interface, "Train", "ti"),
  1258. ]
  1259. interfaces += script_callbacks.ui_tabs_callback()
  1260. interfaces += [(settings_interface, "Settings", "settings")]
  1261. extensions_interface = ui_extensions.create_ui()
  1262. interfaces += [(extensions_interface, "Extensions", "extensions")]
  1263. shared.tab_names = []
  1264. for _interface, label, _ifid in interfaces:
  1265. shared.tab_names.append(label)
  1266. with gr.Blocks(analytics_enabled=False, title="Stable Diffusion") as demo:
  1267. with gr.Row(elem_id="quicksettings", variant="compact"):
  1268. for i, k, item in sorted(quicksettings_list, key=lambda x: quicksettings_names.get(x[1], x[0])):
  1269. component = create_setting_component(k, is_quicksettings=True)
  1270. component_dict[k] = component
  1271. parameters_copypaste.connect_paste_params_buttons()
  1272. with gr.Tabs(elem_id="tabs") as tabs:
  1273. for interface, label, ifid in interfaces:
  1274. if label in shared.opts.hidden_tabs:
  1275. continue
  1276. with gr.TabItem(label, id=ifid, elem_id='tab_' + ifid):
  1277. interface.render()
  1278. if os.path.exists(os.path.join(script_path, "notification.mp3")):
  1279. audio_notification = gr.Audio(interactive=False, value=os.path.join(script_path, "notification.mp3"), elem_id="audio_notification", visible=False)
  1280. footer = shared.html("footer.html")
  1281. footer = footer.format(versions=versions_html())
  1282. gr.HTML(footer, elem_id="footer")
  1283. text_settings = gr.Textbox(elem_id="settings_json", value=lambda: opts.dumpjson(), visible=False)
  1284. settings_submit.click(
  1285. fn=wrap_gradio_call(run_settings, extra_outputs=[gr.update()]),
  1286. inputs=components,
  1287. outputs=[text_settings, result],
  1288. )
  1289. for i, k, item in quicksettings_list:
  1290. component = component_dict[k]
  1291. info = opts.data_labels[k]
  1292. component.change(
  1293. fn=lambda value, k=k: run_settings_single(value, key=k),
  1294. inputs=[component],
  1295. outputs=[component, text_settings],
  1296. show_progress=info.refresh is not None,
  1297. )
  1298. text_settings.change(
  1299. fn=lambda: gr.update(visible=shared.sd_model and shared.sd_model.cond_stage_key == "edit"),
  1300. inputs=[],
  1301. outputs=[image_cfg_scale],
  1302. )
  1303. button_set_checkpoint = gr.Button('Change checkpoint', elem_id='change_checkpoint', visible=False)
  1304. button_set_checkpoint.click(
  1305. fn=lambda value, _: run_settings_single(value, key='sd_model_checkpoint'),
  1306. _js="function(v){ var res = desiredCheckpointName; desiredCheckpointName = ''; return [res || v, null]; }",
  1307. inputs=[component_dict['sd_model_checkpoint'], dummy_component],
  1308. outputs=[component_dict['sd_model_checkpoint'], text_settings],
  1309. )
  1310. component_keys = [k for k in opts.data_labels.keys() if k in component_dict]
  1311. def get_settings_values():
  1312. return [get_value_for_setting(key) for key in component_keys]
  1313. demo.load(
  1314. fn=get_settings_values,
  1315. inputs=[],
  1316. outputs=[component_dict[k] for k in component_keys],
  1317. queue=False,
  1318. )
  1319. def modelmerger(*args):
  1320. try:
  1321. results = modules.extras.run_modelmerger(*args)
  1322. except Exception as e:
  1323. print("Error loading/saving model file:", file=sys.stderr)
  1324. print(traceback.format_exc(), file=sys.stderr)
  1325. modules.sd_models.list_models() # to remove the potentially missing models from the list
  1326. return [*[gr.Dropdown.update(choices=modules.sd_models.checkpoint_tiles()) for _ in range(4)], f"Error merging checkpoints: {e}"]
  1327. return results
  1328. modelmerger_merge.click(fn=lambda: '', inputs=[], outputs=[modelmerger_result])
  1329. modelmerger_merge.click(
  1330. fn=wrap_gradio_gpu_call(modelmerger, extra_outputs=lambda: [gr.update() for _ in range(4)]),
  1331. _js='modelmerger',
  1332. inputs=[
  1333. dummy_component,
  1334. primary_model_name,
  1335. secondary_model_name,
  1336. tertiary_model_name,
  1337. interp_method,
  1338. interp_amount,
  1339. save_as_half,
  1340. custom_name,
  1341. checkpoint_format,
  1342. config_source,
  1343. bake_in_vae,
  1344. discard_weights,
  1345. ],
  1346. outputs=[
  1347. primary_model_name,
  1348. secondary_model_name,
  1349. tertiary_model_name,
  1350. component_dict['sd_model_checkpoint'],
  1351. modelmerger_result,
  1352. ]
  1353. )
  1354. ui_config_file = cmd_opts.ui_config_file
  1355. ui_settings = {}
  1356. settings_count = len(ui_settings)
  1357. error_loading = False
  1358. try:
  1359. if os.path.exists(ui_config_file):
  1360. with open(ui_config_file, "r", encoding="utf8") as file:
  1361. ui_settings = json.load(file)
  1362. except Exception:
  1363. error_loading = True
  1364. print("Error loading settings:", file=sys.stderr)
  1365. print(traceback.format_exc(), file=sys.stderr)
  1366. def loadsave(path, x):
  1367. def apply_field(obj, field, condition=None, init_field=None):
  1368. key = path + "/" + field
  1369. if getattr(obj, 'custom_script_source', None) is not None:
  1370. key = 'customscript/' + obj.custom_script_source + '/' + key
  1371. if getattr(obj, 'do_not_save_to_config', False):
  1372. return
  1373. saved_value = ui_settings.get(key, None)
  1374. if saved_value is None:
  1375. ui_settings[key] = getattr(obj, field)
  1376. elif condition and not condition(saved_value):
  1377. pass
  1378. # this warning is generally not useful;
  1379. # print(f'Warning: Bad ui setting value: {key}: {saved_value}; Default value "{getattr(obj, field)}" will be used instead.')
  1380. else:
  1381. setattr(obj, field, saved_value)
  1382. if init_field is not None:
  1383. init_field(saved_value)
  1384. if type(x) in [gr.Slider, gr.Radio, gr.Checkbox, gr.Textbox, gr.Number, gr.Dropdown] and x.visible:
  1385. apply_field(x, 'visible')
  1386. if type(x) == gr.Slider:
  1387. apply_field(x, 'value')
  1388. apply_field(x, 'minimum')
  1389. apply_field(x, 'maximum')
  1390. apply_field(x, 'step')
  1391. if type(x) == gr.Radio:
  1392. apply_field(x, 'value', lambda val: val in x.choices)
  1393. if type(x) == gr.Checkbox:
  1394. apply_field(x, 'value')
  1395. if type(x) == gr.Textbox:
  1396. apply_field(x, 'value')
  1397. if type(x) == gr.Number:
  1398. apply_field(x, 'value')
  1399. if type(x) == gr.Dropdown:
  1400. def check_dropdown(val):
  1401. if getattr(x, 'multiselect', False):
  1402. return all([value in x.choices for value in val])
  1403. else:
  1404. return val in x.choices
  1405. apply_field(x, 'value', check_dropdown, getattr(x, 'init_field', None))
  1406. visit(txt2img_interface, loadsave, "txt2img")
  1407. visit(img2img_interface, loadsave, "img2img")
  1408. visit(extras_interface, loadsave, "extras")
  1409. visit(modelmerger_interface, loadsave, "modelmerger")
  1410. visit(train_interface, loadsave, "train")
  1411. if not error_loading and (not os.path.exists(ui_config_file) or settings_count != len(ui_settings)):
  1412. with open(ui_config_file, "w", encoding="utf8") as file:
  1413. json.dump(ui_settings, file, indent=4)
  1414. # Required as a workaround for change() event not triggering when loading values from ui-config.json
  1415. interp_description.value = update_interp_description(interp_method.value)
  1416. return demo
  1417. def webpath(fn):
  1418. if fn.startswith(script_path):
  1419. web_path = os.path.relpath(fn, script_path).replace('\\', '/')
  1420. else:
  1421. web_path = os.path.abspath(fn)
  1422. return f'file={web_path}?{os.path.getmtime(fn)}'
  1423. def javascript_html():
  1424. script_js = os.path.join(script_path, "script.js")
  1425. head = f'<script type="text/javascript" src="{webpath(script_js)}"></script>\n'
  1426. inline = f"{localization.localization_js(shared.opts.localization)};"
  1427. if cmd_opts.theme is not None:
  1428. inline += f"set_theme('{cmd_opts.theme}');"
  1429. for script in modules.scripts.list_scripts("javascript", ".js"):
  1430. head += f'<script type="text/javascript" src="{webpath(script.path)}"></script>\n'
  1431. for script in modules.scripts.list_scripts("javascript", ".mjs"):
  1432. head += f'<script type="module" src="{webpath(script.path)}"></script>\n'
  1433. head += f'<script type="text/javascript">{inline}</script>\n'
  1434. return head
  1435. def css_html():
  1436. head = ""
  1437. def stylesheet(fn):
  1438. return f'<link rel="stylesheet" property="stylesheet" href="{webpath(fn)}">'
  1439. for cssfile in modules.scripts.list_files_with_name("style.css"):
  1440. if not os.path.isfile(cssfile):
  1441. continue
  1442. head += stylesheet(cssfile)
  1443. if os.path.exists(os.path.join(data_path, "user.css")):
  1444. head += stylesheet(os.path.join(data_path, "user.css"))
  1445. return head
  1446. def reload_javascript():
  1447. js = javascript_html()
  1448. css = css_html()
  1449. def template_response(*args, **kwargs):
  1450. res = shared.GradioTemplateResponseOriginal(*args, **kwargs)
  1451. res.body = res.body.replace(b'</head>', f'{js}</head>'.encode("utf8"))
  1452. res.body = res.body.replace(b'</body>', f'{css}</body>'.encode("utf8"))
  1453. res.init_headers()
  1454. return res
  1455. gradio.routes.templates.TemplateResponse = template_response
  1456. if not hasattr(shared, 'GradioTemplateResponseOriginal'):
  1457. shared.GradioTemplateResponseOriginal = gradio.routes.templates.TemplateResponse
  1458. def versions_html():
  1459. import torch
  1460. import launch
  1461. python_version = ".".join([str(x) for x in sys.version_info[0:3]])
  1462. commit = launch.commit_hash()
  1463. short_commit = commit[0:8]
  1464. if shared.xformers_available:
  1465. import xformers
  1466. xformers_version = xformers.__version__
  1467. else:
  1468. xformers_version = "N/A"
  1469. return f"""
  1470. python: <span title="{sys.version}">{python_version}</span>
  1471.  • 
  1472. torch: {getattr(torch, '__long_version__',torch.__version__)}
  1473.  • 
  1474. xformers: {xformers_version}
  1475.  • 
  1476. gradio: {gr.__version__}
  1477.  • 
  1478. commit: <a href="https://github.com/AUTOMATIC1111/stable-diffusion-webui/commit/{commit}">{short_commit}</a>
  1479.  • 
  1480. checkpoint: <a id="sd_checkpoint_hash">N/A</a>
  1481. """