ui.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // various functions for interaction with ui.py not large enough to warrant putting them in separate files
  2. function set_theme(theme) {
  3. var gradioURL = window.location.href;
  4. if (!gradioURL.includes('?__theme=')) {
  5. window.location.replace(gradioURL + '?__theme=' + theme);
  6. }
  7. }
  8. function all_gallery_buttons() {
  9. var allGalleryButtons = gradioApp().querySelectorAll('[style="display: block;"].tabitem div[id$=_gallery].gradio-gallery .thumbnails > .thumbnail-item.thumbnail-small');
  10. var visibleGalleryButtons = [];
  11. allGalleryButtons.forEach(function(elem) {
  12. if (elem.parentElement.offsetParent) {
  13. visibleGalleryButtons.push(elem);
  14. }
  15. });
  16. return visibleGalleryButtons;
  17. }
  18. function selected_gallery_button() {
  19. return all_gallery_buttons().find(elem => elem.classList.contains('selected')) ?? null;
  20. }
  21. function selected_gallery_index() {
  22. return all_gallery_buttons().findIndex(elem => elem.classList.contains('selected'));
  23. }
  24. function extract_image_from_gallery(gallery) {
  25. if (gallery.length == 0) {
  26. return [null];
  27. }
  28. if (gallery.length == 1) {
  29. return [gallery[0]];
  30. }
  31. var index = selected_gallery_index();
  32. if (index < 0 || index >= gallery.length) {
  33. // Use the first image in the gallery as the default
  34. index = 0;
  35. }
  36. return [gallery[index]];
  37. }
  38. window.args_to_array = Array.from; // Compatibility with e.g. extensions that may expect this to be around
  39. function switch_to_txt2img() {
  40. gradioApp().querySelector('#tabs').querySelectorAll('button')[0].click();
  41. return Array.from(arguments);
  42. }
  43. function switch_to_img2img_tab(no) {
  44. gradioApp().querySelector('#tabs').querySelectorAll('button')[1].click();
  45. gradioApp().getElementById('mode_img2img').querySelectorAll('button')[no].click();
  46. }
  47. function switch_to_img2img() {
  48. switch_to_img2img_tab(0);
  49. return Array.from(arguments);
  50. }
  51. function switch_to_sketch() {
  52. switch_to_img2img_tab(1);
  53. return Array.from(arguments);
  54. }
  55. function switch_to_inpaint() {
  56. switch_to_img2img_tab(2);
  57. return Array.from(arguments);
  58. }
  59. function switch_to_inpaint_sketch() {
  60. switch_to_img2img_tab(3);
  61. return Array.from(arguments);
  62. }
  63. function switch_to_extras() {
  64. gradioApp().querySelector('#tabs').querySelectorAll('button')[2].click();
  65. return Array.from(arguments);
  66. }
  67. function get_tab_index(tabId) {
  68. let buttons = gradioApp().getElementById(tabId).querySelector('div').querySelectorAll('button');
  69. for (let i = 0; i < buttons.length; i++) {
  70. if (buttons[i].classList.contains('selected')) {
  71. return i;
  72. }
  73. }
  74. return 0;
  75. }
  76. function create_tab_index_args(tabId, args) {
  77. var res = Array.from(args);
  78. res[0] = get_tab_index(tabId);
  79. return res;
  80. }
  81. function get_img2img_tab_index() {
  82. let res = Array.from(arguments);
  83. res.splice(-2);
  84. res[0] = get_tab_index('mode_img2img');
  85. return res;
  86. }
  87. function create_submit_args(args) {
  88. var res = Array.from(args);
  89. // As it is currently, txt2img and img2img send back the previous output args (txt2img_gallery, generation_info, html_info) whenever you generate a new image.
  90. // This can lead to uploading a huge gallery of previously generated images, which leads to an unnecessary delay between submitting and beginning to generate.
  91. // I don't know why gradio is sending outputs along with inputs, but we can prevent sending the image gallery here, which seems to be an issue for some.
  92. // If gradio at some point stops sending outputs, this may break something
  93. if (Array.isArray(res[res.length - 3])) {
  94. res[res.length - 3] = null;
  95. }
  96. return res;
  97. }
  98. function showSubmitButtons(tabname, show) {
  99. gradioApp().getElementById(tabname + '_interrupt').style.display = show ? "none" : "block";
  100. gradioApp().getElementById(tabname + '_skip').style.display = show ? "none" : "block";
  101. }
  102. function showRestoreProgressButton(tabname, show) {
  103. var button = gradioApp().getElementById(tabname + "_restore_progress");
  104. if (!button) return;
  105. button.style.display = show ? "flex" : "none";
  106. }
  107. function submit() {
  108. showSubmitButtons('txt2img', false);
  109. var id = randomId();
  110. localSet("txt2img_task_id", id);
  111. requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function() {
  112. showSubmitButtons('txt2img', true);
  113. localRemove("txt2img_task_id");
  114. showRestoreProgressButton('txt2img', false);
  115. });
  116. var res = create_submit_args(arguments);
  117. res[0] = id;
  118. return res;
  119. }
  120. function submit_img2img() {
  121. showSubmitButtons('img2img', false);
  122. var id = randomId();
  123. localSet("img2img_task_id", id);
  124. requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function() {
  125. showSubmitButtons('img2img', true);
  126. localRemove("img2img_task_id");
  127. showRestoreProgressButton('img2img', false);
  128. });
  129. var res = create_submit_args(arguments);
  130. res[0] = id;
  131. res[1] = get_tab_index('mode_img2img');
  132. return res;
  133. }
  134. function submit_extras() {
  135. showSubmitButtons('extras', false);
  136. var id = randomId();
  137. requestProgress(id, gradioApp().getElementById('extras_gallery_container'), gradioApp().getElementById('extras_gallery'), function() {
  138. showSubmitButtons('extras', true);
  139. });
  140. var res = create_submit_args(arguments);
  141. res[0] = id;
  142. console.log(res);
  143. return res;
  144. }
  145. function restoreProgressTxt2img() {
  146. showRestoreProgressButton("txt2img", false);
  147. var id = localGet("txt2img_task_id");
  148. if (id) {
  149. requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function() {
  150. showSubmitButtons('txt2img', true);
  151. }, null, 0);
  152. }
  153. return id;
  154. }
  155. function restoreProgressImg2img() {
  156. showRestoreProgressButton("img2img", false);
  157. var id = localGet("img2img_task_id");
  158. if (id) {
  159. requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function() {
  160. showSubmitButtons('img2img', true);
  161. }, null, 0);
  162. }
  163. return id;
  164. }
  165. onUiLoaded(function() {
  166. showRestoreProgressButton('txt2img', localGet("txt2img_task_id"));
  167. showRestoreProgressButton('img2img', localGet("img2img_task_id"));
  168. });
  169. function modelmerger() {
  170. var id = randomId();
  171. requestProgress(id, gradioApp().getElementById('modelmerger_results_panel'), null, function() {});
  172. var res = create_submit_args(arguments);
  173. res[0] = id;
  174. return res;
  175. }
  176. function ask_for_style_name(_, prompt_text, negative_prompt_text) {
  177. var name_ = prompt('Style name:');
  178. return [name_, prompt_text, negative_prompt_text];
  179. }
  180. function confirm_clear_prompt(prompt, negative_prompt) {
  181. if (confirm("Delete prompt?")) {
  182. prompt = "";
  183. negative_prompt = "";
  184. }
  185. return [prompt, negative_prompt];
  186. }
  187. var opts = {};
  188. onAfterUiUpdate(function() {
  189. if (Object.keys(opts).length != 0) return;
  190. var json_elem = gradioApp().getElementById('settings_json');
  191. if (json_elem == null) return;
  192. var textarea = json_elem.querySelector('textarea');
  193. var jsdata = textarea.value;
  194. opts = JSON.parse(jsdata);
  195. executeCallbacks(optionsChangedCallbacks); /*global optionsChangedCallbacks*/
  196. Object.defineProperty(textarea, 'value', {
  197. set: function(newValue) {
  198. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  199. var oldValue = valueProp.get.call(textarea);
  200. valueProp.set.call(textarea, newValue);
  201. if (oldValue != newValue) {
  202. opts = JSON.parse(textarea.value);
  203. }
  204. executeCallbacks(optionsChangedCallbacks);
  205. },
  206. get: function() {
  207. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  208. return valueProp.get.call(textarea);
  209. }
  210. });
  211. json_elem.parentElement.style.display = "none";
  212. setupTokenCounters();
  213. });
  214. onOptionsChanged(function() {
  215. var elem = gradioApp().getElementById('sd_checkpoint_hash');
  216. var sd_checkpoint_hash = opts.sd_checkpoint_hash || "";
  217. var shorthash = sd_checkpoint_hash.substring(0, 10);
  218. if (elem && elem.textContent != shorthash) {
  219. elem.textContent = shorthash;
  220. elem.title = sd_checkpoint_hash;
  221. elem.href = "https://google.com/search?q=" + sd_checkpoint_hash;
  222. }
  223. });
  224. let txt2img_textarea, img2img_textarea = undefined;
  225. function restart_reload() {
  226. document.body.innerHTML = '<h1 style="font-family:monospace;margin-top:20%;color:lightgray;text-align:center;">Reloading...</h1>';
  227. var requestPing = function() {
  228. requestGet("./internal/ping", {}, function(data) {
  229. location.reload();
  230. }, function() {
  231. setTimeout(requestPing, 500);
  232. });
  233. };
  234. setTimeout(requestPing, 2000);
  235. return [];
  236. }
  237. // Simulate an `input` DOM event for Gradio Textbox component. Needed after you edit its contents in javascript, otherwise your edits
  238. // will only visible on web page and not sent to python.
  239. function updateInput(target) {
  240. let e = new Event("input", {bubbles: true});
  241. Object.defineProperty(e, "target", {value: target});
  242. target.dispatchEvent(e);
  243. }
  244. var desiredCheckpointName = null;
  245. function selectCheckpoint(name) {
  246. desiredCheckpointName = name;
  247. gradioApp().getElementById('change_checkpoint').click();
  248. }
  249. function currentImg2imgSourceResolution(w, h, scaleBy) {
  250. var img = gradioApp().querySelector('#mode_img2img > div[style="display: block;"] img');
  251. return img ? [img.naturalWidth, img.naturalHeight, scaleBy] : [0, 0, scaleBy];
  252. }
  253. function updateImg2imgResizeToTextAfterChangingImage() {
  254. // At the time this is called from gradio, the image has no yet been replaced.
  255. // There may be a better solution, but this is simple and straightforward so I'm going with it.
  256. setTimeout(function() {
  257. gradioApp().getElementById('img2img_update_resize_to').click();
  258. }, 500);
  259. return [];
  260. }
  261. function setRandomSeed(elem_id) {
  262. var input = gradioApp().querySelector("#" + elem_id + " input");
  263. if (!input) return [];
  264. input.value = "-1";
  265. updateInput(input);
  266. return [];
  267. }
  268. function switchWidthHeight(tabname) {
  269. var width = gradioApp().querySelector("#" + tabname + "_width input[type=number]");
  270. var height = gradioApp().querySelector("#" + tabname + "_height input[type=number]");
  271. if (!width || !height) return [];
  272. var tmp = width.value;
  273. width.value = height.value;
  274. height.value = tmp;
  275. updateInput(width);
  276. updateInput(height);
  277. return [];
  278. }
  279. var onEditTimers = {};
  280. // calls func after afterMs milliseconds has passed since the input elem has beed enited by user
  281. function onEdit(editId, elem, afterMs, func) {
  282. var edited = function() {
  283. var existingTimer = onEditTimers[editId];
  284. if (existingTimer) clearTimeout(existingTimer);
  285. onEditTimers[editId] = setTimeout(func, afterMs);
  286. };
  287. elem.addEventListener("input", edited);
  288. return edited;
  289. }