ui.js 11 KB

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