ui.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // various functions for interation 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 .gallery-item')
  10. var button = gradioApp().querySelector('[style="display: block;"].tabitem .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(){
  37. gradioApp().querySelector('#tabs').querySelectorAll('button')[1].click();
  38. gradioApp().getElementById('mode_img2img').querySelectorAll('button')[0].click();
  39. return args_to_array(arguments);
  40. }
  41. function switch_to_inpaint(){
  42. gradioApp().querySelector('#tabs').querySelectorAll('button')[1].click();
  43. gradioApp().getElementById('mode_img2img').querySelectorAll('button')[1].click();
  44. return args_to_array(arguments);
  45. }
  46. function switch_to_extras(){
  47. gradioApp().querySelector('#tabs').querySelectorAll('button')[2].click();
  48. return args_to_array(arguments);
  49. }
  50. function get_tab_index(tabId){
  51. var res = 0
  52. gradioApp().getElementById(tabId).querySelector('div').querySelectorAll('button').forEach(function(button, i){
  53. if(button.className.indexOf('bg-white') != -1)
  54. res = i
  55. })
  56. return res
  57. }
  58. function create_tab_index_args(tabId, args){
  59. var res = []
  60. for(var i=0; i<args.length; i++){
  61. res.push(args[i])
  62. }
  63. res[0] = get_tab_index(tabId)
  64. return res
  65. }
  66. function get_extras_tab_index(){
  67. const [,,...args] = [...arguments]
  68. return [get_tab_index('mode_extras'), get_tab_index('extras_resize_mode'), ...args]
  69. }
  70. function create_submit_args(args){
  71. res = []
  72. for(var i=0;i<args.length;i++){
  73. res.push(args[i])
  74. }
  75. // 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.
  76. // This can lead to uploading a huge gallery of previously generated images, which leads to an unnecessary delay between submitting and beginning to generate.
  77. // I don't know why gradio is seding outputs along with inputs, but we can prevent sending the image gallery here, which seems to be an issue for some.
  78. // If gradio at some point stops sending outputs, this may break something
  79. if(Array.isArray(res[res.length - 3])){
  80. res[res.length - 3] = null
  81. }
  82. return res
  83. }
  84. function submit(){
  85. requestProgress('txt2img')
  86. return create_submit_args(arguments)
  87. }
  88. function submit_img2img(){
  89. requestProgress('img2img')
  90. res = create_submit_args(arguments)
  91. res[0] = get_tab_index('mode_img2img')
  92. return res
  93. }
  94. function ask_for_style_name(_, prompt_text, negative_prompt_text) {
  95. name_ = prompt('Style name:')
  96. return [name_, prompt_text, negative_prompt_text]
  97. }
  98. opts = {}
  99. function apply_settings(jsdata){
  100. console.log(jsdata)
  101. opts = JSON.parse(jsdata)
  102. return jsdata
  103. }
  104. onUiUpdate(function(){
  105. if(Object.keys(opts).length != 0) return;
  106. json_elem = gradioApp().getElementById('settings_json')
  107. if(json_elem == null) return;
  108. textarea = json_elem.querySelector('textarea')
  109. jsdata = textarea.value
  110. opts = JSON.parse(jsdata)
  111. Object.defineProperty(textarea, 'value', {
  112. set: function(newValue) {
  113. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  114. var oldValue = valueProp.get.call(textarea);
  115. valueProp.set.call(textarea, newValue);
  116. if (oldValue != newValue) {
  117. opts = JSON.parse(textarea.value)
  118. }
  119. },
  120. get: function() {
  121. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  122. return valueProp.get.call(textarea);
  123. }
  124. });
  125. json_elem.parentElement.style.display="none"
  126. if (!txt2img_textarea) {
  127. txt2img_textarea = gradioApp().querySelector("#txt2img_prompt > label > textarea");
  128. txt2img_textarea?.addEventListener("input", () => update_token_counter("txt2img_token_button"));
  129. }
  130. if (!img2img_textarea) {
  131. img2img_textarea = gradioApp().querySelector("#img2img_prompt > label > textarea");
  132. img2img_textarea?.addEventListener("input", () => update_token_counter("img2img_token_button"));
  133. }
  134. if (!txt2img_gallery) {
  135. txt2img_gallery = gradioApp().querySelector('#txt2img_gallery')
  136. txt2img_gallery?.addEventListener('click', () => gradioApp().getElementById("txt2img_generation_info_button").click());
  137. }
  138. if (!img2img_gallery) {
  139. img2img_gallery = gradioApp().querySelector('#img2img_gallery')
  140. img2img_gallery?.addEventListener('click', () => gradioApp().getElementById("img2img_generation_info_button").click());
  141. }
  142. })
  143. let txt2img_textarea, img2img_textarea, txt2img_gallery, img2img_gallery = undefined;
  144. let wait_time = 800
  145. let token_timeout;
  146. function update_txt2img_tokens(...args) {
  147. update_token_counter("txt2img_token_button")
  148. if (args.length == 2)
  149. return args[0]
  150. return args;
  151. }
  152. function update_img2img_tokens(...args) {
  153. update_token_counter("img2img_token_button")
  154. if (args.length == 2)
  155. return args[0]
  156. return args;
  157. }
  158. function update_token_counter(button_id) {
  159. if (token_timeout)
  160. clearTimeout(token_timeout);
  161. token_timeout = setTimeout(() => gradioApp().getElementById(button_id)?.click(), wait_time);
  162. }
  163. function restart_reload(){
  164. document.body.innerHTML='<h1 style="font-family:monospace;margin-top:20%;color:lightgray;text-align:center;">Reloading...</h1>';
  165. setTimeout(function(){location.reload()},2000)
  166. return []
  167. }