ui.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 restoreProgressTxt2img() {
  135. showRestoreProgressButton("txt2img", false);
  136. var id = localGet("txt2img_task_id");
  137. if (id) {
  138. requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function() {
  139. showSubmitButtons('txt2img', true);
  140. }, null, 0);
  141. }
  142. return id;
  143. }
  144. function restoreProgressImg2img() {
  145. showRestoreProgressButton("img2img", false);
  146. var id = localGet("img2img_task_id");
  147. if (id) {
  148. requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function() {
  149. showSubmitButtons('img2img', true);
  150. }, null, 0);
  151. }
  152. return id;
  153. }
  154. onUiLoaded(function() {
  155. showRestoreProgressButton('txt2img', localGet("txt2img_task_id"));
  156. showRestoreProgressButton('img2img', localGet("img2img_task_id"));
  157. });
  158. function modelmerger() {
  159. var id = randomId();
  160. requestProgress(id, gradioApp().getElementById('modelmerger_results_panel'), null, function() {});
  161. var res = create_submit_args(arguments);
  162. res[0] = id;
  163. return res;
  164. }
  165. function ask_for_style_name(_, prompt_text, negative_prompt_text) {
  166. var name_ = prompt('Style name:');
  167. return [name_, prompt_text, negative_prompt_text];
  168. }
  169. function confirm_clear_prompt(prompt, negative_prompt) {
  170. if (confirm("Delete prompt?")) {
  171. prompt = "";
  172. negative_prompt = "";
  173. }
  174. return [prompt, negative_prompt];
  175. }
  176. var opts = {};
  177. onAfterUiUpdate(function() {
  178. if (Object.keys(opts).length != 0) return;
  179. var json_elem = gradioApp().getElementById('settings_json');
  180. if (json_elem == null) return;
  181. var textarea = json_elem.querySelector('textarea');
  182. var jsdata = textarea.value;
  183. opts = JSON.parse(jsdata);
  184. executeCallbacks(optionsChangedCallbacks); /*global optionsChangedCallbacks*/
  185. Object.defineProperty(textarea, 'value', {
  186. set: function(newValue) {
  187. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  188. var oldValue = valueProp.get.call(textarea);
  189. valueProp.set.call(textarea, newValue);
  190. if (oldValue != newValue) {
  191. opts = JSON.parse(textarea.value);
  192. }
  193. executeCallbacks(optionsChangedCallbacks);
  194. },
  195. get: function() {
  196. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  197. return valueProp.get.call(textarea);
  198. }
  199. });
  200. json_elem.parentElement.style.display = "none";
  201. setupTokenCounters();
  202. var show_all_pages = gradioApp().getElementById('settings_show_all_pages');
  203. var settings_tabs = gradioApp().querySelector('#settings div');
  204. if (show_all_pages && settings_tabs) {
  205. settings_tabs.appendChild(show_all_pages);
  206. show_all_pages.onclick = function() {
  207. gradioApp().querySelectorAll('#settings > div').forEach(function(elem) {
  208. if (elem.id == "settings_tab_licenses") {
  209. return;
  210. }
  211. elem.style.display = "block";
  212. });
  213. };
  214. }
  215. });
  216. onOptionsChanged(function() {
  217. var elem = gradioApp().getElementById('sd_checkpoint_hash');
  218. var sd_checkpoint_hash = opts.sd_checkpoint_hash || "";
  219. var shorthash = sd_checkpoint_hash.substring(0, 10);
  220. if (elem && elem.textContent != shorthash) {
  221. elem.textContent = shorthash;
  222. elem.title = sd_checkpoint_hash;
  223. elem.href = "https://google.com/search?q=" + sd_checkpoint_hash;
  224. }
  225. });
  226. let txt2img_textarea, img2img_textarea = undefined;
  227. function restart_reload() {
  228. document.body.innerHTML = '<h1 style="font-family:monospace;margin-top:20%;color:lightgray;text-align:center;">Reloading...</h1>';
  229. var requestPing = function() {
  230. requestGet("./internal/ping", {}, function(data) {
  231. location.reload();
  232. }, function() {
  233. setTimeout(requestPing, 500);
  234. });
  235. };
  236. setTimeout(requestPing, 2000);
  237. return [];
  238. }
  239. // Simulate an `input` DOM event for Gradio Textbox component. Needed after you edit its contents in javascript, otherwise your edits
  240. // will only visible on web page and not sent to python.
  241. function updateInput(target) {
  242. let e = new Event("input", {bubbles: true});
  243. Object.defineProperty(e, "target", {value: target});
  244. target.dispatchEvent(e);
  245. }
  246. var desiredCheckpointName = null;
  247. function selectCheckpoint(name) {
  248. desiredCheckpointName = name;
  249. gradioApp().getElementById('change_checkpoint').click();
  250. }
  251. function currentImg2imgSourceResolution(w, h, scaleBy) {
  252. var img = gradioApp().querySelector('#mode_img2img > div[style="display: block;"] img');
  253. return img ? [img.naturalWidth, img.naturalHeight, scaleBy] : [0, 0, scaleBy];
  254. }
  255. function updateImg2imgResizeToTextAfterChangingImage() {
  256. // At the time this is called from gradio, the image has no yet been replaced.
  257. // There may be a better solution, but this is simple and straightforward so I'm going with it.
  258. setTimeout(function() {
  259. gradioApp().getElementById('img2img_update_resize_to').click();
  260. }, 500);
  261. return [];
  262. }
  263. function setRandomSeed(elem_id) {
  264. var input = gradioApp().querySelector("#" + elem_id + " input");
  265. if (!input) return [];
  266. input.value = "-1";
  267. updateInput(input);
  268. return [];
  269. }
  270. function switchWidthHeight(tabname) {
  271. var width = gradioApp().querySelector("#" + tabname + "_width input[type=number]");
  272. var height = gradioApp().querySelector("#" + tabname + "_height input[type=number]");
  273. if (!width || !height) return [];
  274. var tmp = width.value;
  275. width.value = height.value;
  276. height.value = tmp;
  277. updateInput(width);
  278. updateInput(height);
  279. return [];
  280. }