ui.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // various functions for interaction with ui.py not large enough to warrant putting them in separate files
  2. function set_theme(theme){
  3. gradioURL = window.location.href
  4. if (!gradioURL.includes('?__theme=')) {
  5. window.location.replace(gradioURL + '?__theme=' + theme);
  6. }
  7. }
  8. function selected_gallery_index(){
  9. var buttons = gradioApp().querySelectorAll('[style="display: block;"].tabitem div[id$=_gallery] .gallery-item')
  10. var button = gradioApp().querySelector('[style="display: block;"].tabitem div[id$=_gallery] .gallery-item.\\!ring-2')
  11. var result = -1
  12. buttons.forEach(function(v, i){ if(v==button) { result = i } })
  13. return result
  14. }
  15. function extract_image_from_gallery(gallery){
  16. if(gallery.length == 1){
  17. return [gallery[0]]
  18. }
  19. index = selected_gallery_index()
  20. if (index < 0 || index >= gallery.length){
  21. return [null]
  22. }
  23. return [gallery[index]];
  24. }
  25. function args_to_array(args){
  26. res = []
  27. for(var i=0;i<args.length;i++){
  28. res.push(args[i])
  29. }
  30. return res
  31. }
  32. function switch_to_txt2img(){
  33. gradioApp().querySelector('#tabs').querySelectorAll('button')[0].click();
  34. return args_to_array(arguments);
  35. }
  36. function switch_to_img2img_tab(no){
  37. gradioApp().querySelector('#tabs').querySelectorAll('button')[1].click();
  38. gradioApp().getElementById('mode_img2img').querySelectorAll('button')[no].click();
  39. }
  40. function switch_to_img2img(){
  41. switch_to_img2img_tab(0);
  42. return args_to_array(arguments);
  43. }
  44. function switch_to_sketch(){
  45. switch_to_img2img_tab(1);
  46. return args_to_array(arguments);
  47. }
  48. function switch_to_inpaint(){
  49. switch_to_img2img_tab(2);
  50. return args_to_array(arguments);
  51. }
  52. function switch_to_inpaint_sketch(){
  53. switch_to_img2img_tab(3);
  54. return args_to_array(arguments);
  55. }
  56. function switch_to_inpaint(){
  57. gradioApp().querySelector('#tabs').querySelectorAll('button')[1].click();
  58. gradioApp().getElementById('mode_img2img').querySelectorAll('button')[2].click();
  59. return args_to_array(arguments);
  60. }
  61. function switch_to_extras(){
  62. gradioApp().querySelector('#tabs').querySelectorAll('button')[2].click();
  63. return args_to_array(arguments);
  64. }
  65. function get_tab_index(tabId){
  66. var res = 0
  67. gradioApp().getElementById(tabId).querySelector('div').querySelectorAll('button').forEach(function(button, i){
  68. if(button.className.indexOf('bg-white') != -1)
  69. res = i
  70. })
  71. return res
  72. }
  73. function create_tab_index_args(tabId, args){
  74. var res = []
  75. for(var i=0; i<args.length; i++){
  76. res.push(args[i])
  77. }
  78. res[0] = get_tab_index(tabId)
  79. return res
  80. }
  81. function get_extras_tab_index(){
  82. const [,,...args] = [...arguments]
  83. return [get_tab_index('mode_extras'), get_tab_index('extras_resize_mode'), ...args]
  84. }
  85. function get_img2img_tab_index() {
  86. let res = args_to_array(arguments)
  87. res.splice(-2)
  88. res[0] = get_tab_index('mode_img2img')
  89. return res
  90. }
  91. function create_submit_args(args){
  92. res = []
  93. for(var i=0;i<args.length;i++){
  94. res.push(args[i])
  95. }
  96. // 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.
  97. // This can lead to uploading a huge gallery of previously generated images, which leads to an unnecessary delay between submitting and beginning to generate.
  98. // 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.
  99. // If gradio at some point stops sending outputs, this may break something
  100. if(Array.isArray(res[res.length - 3])){
  101. res[res.length - 3] = null
  102. }
  103. return res
  104. }
  105. function showSubmitButtons(tabname, show){
  106. gradioApp().getElementById(tabname+'_interrupt').style.display = show ? "none" : "block"
  107. gradioApp().getElementById(tabname+'_skip').style.display = show ? "none" : "block"
  108. }
  109. function submit(){
  110. rememberGallerySelection('txt2img_gallery')
  111. showSubmitButtons('txt2img', false)
  112. var id = randomId()
  113. requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function(){
  114. showSubmitButtons('txt2img', true)
  115. })
  116. var res = create_submit_args(arguments)
  117. res[0] = id
  118. return res
  119. }
  120. function submit_img2img(){
  121. rememberGallerySelection('img2img_gallery')
  122. showSubmitButtons('img2img', false)
  123. var id = randomId()
  124. requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function(){
  125. showSubmitButtons('img2img', true)
  126. })
  127. var res = create_submit_args(arguments)
  128. res[0] = id
  129. res[1] = get_tab_index('mode_img2img')
  130. return res
  131. }
  132. function modelmerger(){
  133. var id = randomId()
  134. requestProgress(id, gradioApp().getElementById('modelmerger_results_panel'), null, function(){})
  135. var res = create_submit_args(arguments)
  136. res[0] = id
  137. return res
  138. }
  139. function ask_for_style_name(_, prompt_text, negative_prompt_text) {
  140. name_ = prompt('Style name:')
  141. return [name_, prompt_text, negative_prompt_text]
  142. }
  143. function confirm_clear_prompt(prompt, negative_prompt) {
  144. if(confirm("Delete prompt?")) {
  145. prompt = ""
  146. negative_prompt = ""
  147. }
  148. return [prompt, negative_prompt]
  149. }
  150. opts = {}
  151. onUiUpdate(function(){
  152. if(Object.keys(opts).length != 0) return;
  153. json_elem = gradioApp().getElementById('settings_json')
  154. if(json_elem == null) return;
  155. textarea = json_elem.querySelector('textarea')
  156. jsdata = textarea.value
  157. opts = JSON.parse(jsdata)
  158. executeCallbacks(optionsChangedCallbacks);
  159. Object.defineProperty(textarea, 'value', {
  160. set: function(newValue) {
  161. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  162. var oldValue = valueProp.get.call(textarea);
  163. valueProp.set.call(textarea, newValue);
  164. if (oldValue != newValue) {
  165. opts = JSON.parse(textarea.value)
  166. }
  167. executeCallbacks(optionsChangedCallbacks);
  168. },
  169. get: function() {
  170. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  171. return valueProp.get.call(textarea);
  172. }
  173. });
  174. json_elem.parentElement.style.display="none"
  175. function registerTextarea(id, id_counter, id_button){
  176. var prompt = gradioApp().getElementById(id)
  177. var counter = gradioApp().getElementById(id_counter)
  178. var textarea = gradioApp().querySelector("#" + id + " > label > textarea");
  179. if(counter.parentElement == prompt.parentElement){
  180. return
  181. }
  182. prompt.parentElement.insertBefore(counter, prompt)
  183. counter.classList.add("token-counter")
  184. prompt.parentElement.style.position = "relative"
  185. textarea.addEventListener("input", function(){
  186. update_token_counter(id_button);
  187. });
  188. }
  189. registerTextarea('txt2img_prompt', 'txt2img_token_counter', 'txt2img_token_button')
  190. registerTextarea('txt2img_neg_prompt', 'txt2img_negative_token_counter', 'txt2img_negative_token_button')
  191. registerTextarea('img2img_prompt', 'img2img_token_counter', 'img2img_token_button')
  192. registerTextarea('img2img_neg_prompt', 'img2img_negative_token_counter', 'img2img_negative_token_button')
  193. show_all_pages = gradioApp().getElementById('settings_show_all_pages')
  194. settings_tabs = gradioApp().querySelector('#settings div')
  195. if(show_all_pages && settings_tabs){
  196. settings_tabs.appendChild(show_all_pages)
  197. show_all_pages.onclick = function(){
  198. gradioApp().querySelectorAll('#settings > div').forEach(function(elem){
  199. elem.style.display = "block";
  200. })
  201. }
  202. }
  203. })
  204. onOptionsChanged(function(){
  205. elem = gradioApp().getElementById('sd_checkpoint_hash')
  206. sd_checkpoint_hash = opts.sd_checkpoint_hash || ""
  207. shorthash = sd_checkpoint_hash.substr(0,10)
  208. if(elem && elem.textContent != shorthash){
  209. elem.textContent = shorthash
  210. elem.title = sd_checkpoint_hash
  211. elem.href = "https://google.com/search?q=" + sd_checkpoint_hash
  212. }
  213. })
  214. let txt2img_textarea, img2img_textarea = undefined;
  215. let wait_time = 800
  216. let token_timeout;
  217. function update_txt2img_tokens(...args) {
  218. update_token_counter("txt2img_token_button")
  219. if (args.length == 2)
  220. return args[0]
  221. return args;
  222. }
  223. function update_img2img_tokens(...args) {
  224. update_token_counter("img2img_token_button")
  225. if (args.length == 2)
  226. return args[0]
  227. return args;
  228. }
  229. function update_token_counter(button_id) {
  230. if (token_timeout)
  231. clearTimeout(token_timeout);
  232. token_timeout = setTimeout(() => gradioApp().getElementById(button_id)?.click(), wait_time);
  233. }
  234. function restart_reload(){
  235. document.body.innerHTML='<h1 style="font-family:monospace;margin-top:20%;color:lightgray;text-align:center;">Reloading...</h1>';
  236. setTimeout(function(){location.reload()},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. }