ui.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_img2img_tab_index() {
  82. let res = args_to_array(arguments)
  83. res.splice(-2)
  84. res[0] = get_tab_index('mode_img2img')
  85. return res
  86. }
  87. function create_submit_args(args){
  88. res = []
  89. for(var i=0;i<args.length;i++){
  90. res.push(args[i])
  91. }
  92. // 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.
  93. // This can lead to uploading a huge gallery of previously generated images, which leads to an unnecessary delay between submitting and beginning to generate.
  94. // 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.
  95. // If gradio at some point stops sending outputs, this may break something
  96. if(Array.isArray(res[res.length - 3])){
  97. res[res.length - 3] = null
  98. }
  99. return res
  100. }
  101. function showSubmitButtons(tabname, show){
  102. gradioApp().getElementById(tabname+'_interrupt').style.display = show ? "none" : "block"
  103. gradioApp().getElementById(tabname+'_skip').style.display = show ? "none" : "block"
  104. }
  105. function submit(){
  106. rememberGallerySelection('txt2img_gallery')
  107. showSubmitButtons('txt2img', false)
  108. var id = randomId()
  109. requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function(){
  110. showSubmitButtons('txt2img', true)
  111. })
  112. var res = create_submit_args(arguments)
  113. res[0] = id
  114. return res
  115. }
  116. function submit_img2img(){
  117. rememberGallerySelection('img2img_gallery')
  118. showSubmitButtons('img2img', false)
  119. var id = randomId()
  120. requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function(){
  121. showSubmitButtons('img2img', true)
  122. })
  123. var res = create_submit_args(arguments)
  124. res[0] = id
  125. res[1] = get_tab_index('mode_img2img')
  126. return res
  127. }
  128. function modelmerger(){
  129. var id = randomId()
  130. requestProgress(id, gradioApp().getElementById('modelmerger_results_panel'), null, function(){})
  131. var res = create_submit_args(arguments)
  132. res[0] = id
  133. return res
  134. }
  135. function ask_for_style_name(_, prompt_text, negative_prompt_text) {
  136. name_ = prompt('Style name:')
  137. return [name_, prompt_text, negative_prompt_text]
  138. }
  139. function confirm_clear_prompt(prompt, negative_prompt) {
  140. if(confirm("Delete prompt?")) {
  141. prompt = ""
  142. negative_prompt = ""
  143. }
  144. return [prompt, negative_prompt]
  145. }
  146. opts = {}
  147. onUiUpdate(function(){
  148. if(Object.keys(opts).length != 0) return;
  149. json_elem = gradioApp().getElementById('settings_json')
  150. if(json_elem == null) return;
  151. var textarea = json_elem.querySelector('textarea')
  152. var jsdata = textarea.value
  153. opts = JSON.parse(jsdata)
  154. executeCallbacks(optionsChangedCallbacks);
  155. Object.defineProperty(textarea, 'value', {
  156. set: function(newValue) {
  157. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  158. var oldValue = valueProp.get.call(textarea);
  159. valueProp.set.call(textarea, newValue);
  160. if (oldValue != newValue) {
  161. opts = JSON.parse(textarea.value)
  162. }
  163. executeCallbacks(optionsChangedCallbacks);
  164. },
  165. get: function() {
  166. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  167. return valueProp.get.call(textarea);
  168. }
  169. });
  170. json_elem.parentElement.style.display="none"
  171. function registerTextarea(id, id_counter, id_button){
  172. var prompt = gradioApp().getElementById(id)
  173. var counter = gradioApp().getElementById(id_counter)
  174. var textarea = gradioApp().querySelector("#" + id + " > label > textarea");
  175. if(counter.parentElement == prompt.parentElement){
  176. return
  177. }
  178. prompt.parentElement.insertBefore(counter, prompt)
  179. counter.classList.add("token-counter")
  180. prompt.parentElement.style.position = "relative"
  181. textarea.addEventListener("input", function(){
  182. update_token_counter(id_button);
  183. });
  184. }
  185. registerTextarea('txt2img_prompt', 'txt2img_token_counter', 'txt2img_token_button')
  186. registerTextarea('txt2img_neg_prompt', 'txt2img_negative_token_counter', 'txt2img_negative_token_button')
  187. registerTextarea('img2img_prompt', 'img2img_token_counter', 'img2img_token_button')
  188. registerTextarea('img2img_neg_prompt', 'img2img_negative_token_counter', 'img2img_negative_token_button')
  189. show_all_pages = gradioApp().getElementById('settings_show_all_pages')
  190. settings_tabs = gradioApp().querySelector('#settings div')
  191. if(show_all_pages && settings_tabs){
  192. settings_tabs.appendChild(show_all_pages)
  193. show_all_pages.onclick = function(){
  194. gradioApp().querySelectorAll('#settings > div').forEach(function(elem){
  195. elem.style.display = "block";
  196. })
  197. }
  198. }
  199. })
  200. onOptionsChanged(function(){
  201. elem = gradioApp().getElementById('sd_checkpoint_hash')
  202. sd_checkpoint_hash = opts.sd_checkpoint_hash || ""
  203. shorthash = sd_checkpoint_hash.substr(0,10)
  204. if(elem && elem.textContent != shorthash){
  205. elem.textContent = shorthash
  206. elem.title = sd_checkpoint_hash
  207. elem.href = "https://google.com/search?q=" + sd_checkpoint_hash
  208. }
  209. })
  210. let txt2img_textarea, img2img_textarea = undefined;
  211. let wait_time = 800
  212. let token_timeout;
  213. function update_txt2img_tokens(...args) {
  214. update_token_counter("txt2img_token_button")
  215. if (args.length == 2)
  216. return args[0]
  217. return args;
  218. }
  219. function update_img2img_tokens(...args) {
  220. update_token_counter("img2img_token_button")
  221. if (args.length == 2)
  222. return args[0]
  223. return args;
  224. }
  225. function update_token_counter(button_id) {
  226. if (token_timeout)
  227. clearTimeout(token_timeout);
  228. token_timeout = setTimeout(() => gradioApp().getElementById(button_id)?.click(), wait_time);
  229. }
  230. function restart_reload(){
  231. document.body.innerHTML='<h1 style="font-family:monospace;margin-top:20%;color:lightgray;text-align:center;">Reloading...</h1>';
  232. setTimeout(function(){location.reload()},2000)
  233. return []
  234. }
  235. // Simulate an `input` DOM event for Gradio Textbox component. Needed after you edit its contents in javascript, otherwise your edits
  236. // will only visible on web page and not sent to python.
  237. function updateInput(target){
  238. let e = new Event("input", { bubbles: true })
  239. Object.defineProperty(e, "target", {value: target})
  240. target.dispatchEvent(e);
  241. }