ui.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // various functions for interation with ui.py not large enough to warrant putting them in separate files
  2. function selected_gallery_index(){
  3. var buttons = gradioApp().querySelectorAll('[style="display: block;"].tabitem .gallery-item')
  4. var button = gradioApp().querySelector('[style="display: block;"].tabitem .gallery-item.\\!ring-2')
  5. var result = -1
  6. buttons.forEach(function(v, i){ if(v==button) { result = i } })
  7. return result
  8. }
  9. function extract_image_from_gallery(gallery){
  10. if(gallery.length == 1){
  11. return gallery[0]
  12. }
  13. index = selected_gallery_index()
  14. if (index < 0 || index >= gallery.length){
  15. return [null]
  16. }
  17. return gallery[index];
  18. }
  19. function args_to_array(args){
  20. res = []
  21. for(var i=0;i<args.length;i++){
  22. res.push(args[i])
  23. }
  24. return res
  25. }
  26. function switch_to_txt2img(){
  27. gradioApp().querySelectorAll('button')[0].click();
  28. return args_to_array(arguments);
  29. }
  30. function switch_to_img2img_img2img(){
  31. gradioApp().querySelectorAll('button')[1].click();
  32. gradioApp().getElementById('mode_img2img').querySelectorAll('button')[0].click();
  33. return args_to_array(arguments);
  34. }
  35. function switch_to_img2img_inpaint(){
  36. gradioApp().querySelectorAll('button')[1].click();
  37. gradioApp().getElementById('mode_img2img').querySelectorAll('button')[1].click();
  38. return args_to_array(arguments);
  39. }
  40. function switch_to_extras(){
  41. gradioApp().querySelectorAll('button')[2].click();
  42. return args_to_array(arguments);
  43. }
  44. function extract_image_from_gallery_txt2img(gallery){
  45. switch_to_txt2img()
  46. return extract_image_from_gallery(gallery);
  47. }
  48. function extract_image_from_gallery_img2img(gallery){
  49. switch_to_img2img_img2img()
  50. return extract_image_from_gallery(gallery);
  51. }
  52. function extract_image_from_gallery_inpaint(gallery){
  53. switch_to_img2img_inpaint()
  54. return extract_image_from_gallery(gallery);
  55. }
  56. function extract_image_from_gallery_extras(gallery){
  57. switch_to_extras()
  58. return extract_image_from_gallery(gallery);
  59. }
  60. function get_tab_index(tabId){
  61. var res = 0
  62. gradioApp().getElementById(tabId).querySelector('div').querySelectorAll('button').forEach(function(button, i){
  63. if(button.className.indexOf('bg-white') != -1)
  64. res = i
  65. })
  66. return res
  67. }
  68. function create_tab_index_args(tabId, args){
  69. var res = []
  70. for(var i=0; i<args.length; i++){
  71. res.push(args[i])
  72. }
  73. res[0] = get_tab_index(tabId)
  74. return res
  75. }
  76. function get_extras_tab_index(){
  77. return create_tab_index_args('mode_extras', arguments)
  78. }
  79. function create_submit_args(args){
  80. res = []
  81. for(var i=0;i<args.length;i++){
  82. res.push(args[i])
  83. }
  84. // 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.
  85. // This can lead to uploading a huge gallery of previously generated images, which leads to an unnecessary delay between submitting and beginning to generate.
  86. // 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.
  87. // If gradio at some point stops sending outputs, this may break something
  88. if(Array.isArray(res[res.length - 3])){
  89. res[res.length - 3] = null
  90. }
  91. return res
  92. }
  93. function submit(){
  94. requestProgress('txt2img')
  95. return create_submit_args(arguments)
  96. }
  97. function submit_img2img(){
  98. requestProgress('img2img')
  99. res = create_submit_args(arguments)
  100. res[0] = get_tab_index('mode_img2img')
  101. return res
  102. }
  103. function ask_for_style_name(_, prompt_text, negative_prompt_text) {
  104. name_ = prompt('Style name:')
  105. return name_ === null ? [null, null, null]: [name_, prompt_text, negative_prompt_text]
  106. }
  107. opts = {}
  108. function apply_settings(jsdata){
  109. console.log(jsdata)
  110. opts = JSON.parse(jsdata)
  111. return jsdata
  112. }
  113. onUiUpdate(function(){
  114. if(Object.keys(opts).length != 0) return;
  115. json_elem = gradioApp().getElementById('settings_json')
  116. if(json_elem == null) return;
  117. textarea = json_elem.querySelector('textarea')
  118. jsdata = textarea.value
  119. opts = JSON.parse(jsdata)
  120. Object.defineProperty(textarea, 'value', {
  121. set: function(newValue) {
  122. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  123. var oldValue = valueProp.get.call(textarea);
  124. valueProp.set.call(textarea, newValue);
  125. if (oldValue != newValue) {
  126. opts = JSON.parse(textarea.value)
  127. }
  128. },
  129. get: function() {
  130. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  131. return valueProp.get.call(textarea);
  132. }
  133. });
  134. json_elem.parentElement.style.display="none"
  135. })
  136. let wait_time = 800
  137. let token_timeout;
  138. function txt2img_token_counter(text) {
  139. return update_token_counter("txt2img_token_button", text);
  140. }
  141. function img2img_token_counter(text) {
  142. return update_token_counter("img2img_token_button", text);
  143. }
  144. function update_token_counter(button_id, text) {
  145. if (token_timeout)
  146. clearTimeout(token_timeout);
  147. token_timeout = setTimeout(() => gradioApp().getElementById(button_id)?.click(), wait_time);
  148. return [];
  149. }