ui.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 gr = gradioApp()
  4. var buttons = gradioApp().querySelectorAll(".gallery-item")
  5. var button = gr.querySelector(".gallery-item.\\!ring-2")
  6. var result = -1
  7. buttons.forEach(function(v, i){ if(v==button) { result = i } })
  8. return result
  9. }
  10. function extract_image_from_gallery(gallery){
  11. if(gallery.length == 1){
  12. return gallery[0]
  13. }
  14. index = selected_gallery_index()
  15. if (index < 0 || index >= gallery.length){
  16. return [null]
  17. }
  18. return gallery[index];
  19. }
  20. function extract_image_from_gallery_img2img(gallery){
  21. gradioApp().querySelectorAll('button')[1].click();
  22. return extract_image_from_gallery(gallery);
  23. }
  24. function extract_image_from_gallery_extras(gallery){
  25. gradioApp().querySelectorAll('button')[2].click();
  26. return extract_image_from_gallery(gallery);
  27. }
  28. function get_tab_index(tabId){
  29. var res = 0
  30. gradioApp().getElementById(tabId).querySelector('div').querySelectorAll('button').forEach(function(button, i){
  31. if(button.className.indexOf('bg-white') != -1)
  32. res = i
  33. })
  34. return res
  35. }
  36. function create_tab_index_args(tabId, args){
  37. var res = []
  38. for(var i=0; i<args.length; i++){
  39. res.push(args[i])
  40. }
  41. res[0] = get_tab_index(tabId)
  42. return res
  43. }
  44. function get_extras_tab_index(){
  45. return create_tab_index_args('mode_extras', arguments)
  46. }
  47. function create_submit_args(args){
  48. res = []
  49. for(var i=0;i<args.length;i++){
  50. res.push(args[i])
  51. }
  52. // 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.
  53. // This can lead to uploading a huge gallery of previously generated images, which leads to an unnecessary delay between submitting and beginning to generate.
  54. // 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.
  55. // If gradio at some point stops sending outputs, this may break something
  56. if(Array.isArray(res[res.length - 3])){
  57. res[res.length - 3] = null
  58. }
  59. return res
  60. }
  61. function submit(){
  62. requestProgress()
  63. return create_submit_args(arguments)
  64. }
  65. function submit_img2img(){
  66. requestProgress()
  67. res = create_submit_args(arguments)
  68. res[0] = get_tab_index('mode_img2img')
  69. return res
  70. }
  71. function ask_for_style_name(_, prompt_text, negative_prompt_text) {
  72. name_ = prompt('Style name:')
  73. return name_ === null ? [null, null, null]: [name_, prompt_text, negative_prompt_text]
  74. }
  75. opts = {}
  76. function apply_settings(jsdata){
  77. console.log(jsdata)
  78. opts = JSON.parse(jsdata)
  79. return jsdata
  80. }
  81. onUiUpdate(function(){
  82. if(Object.keys(opts).length != 0) return;
  83. json_elem = gradioApp().getElementById('settings_json')
  84. if(json_elem == null) return;
  85. textarea = json_elem.querySelector('textarea')
  86. jsdata = textarea.value
  87. opts = JSON.parse(jsdata)
  88. Object.defineProperty(textarea, 'value', {
  89. set: function(newValue) {
  90. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  91. var oldValue = valueProp.get.call(textarea);
  92. valueProp.set.call(textarea, newValue);
  93. if (oldValue != newValue) {
  94. opts = JSON.parse(textarea.value)
  95. }
  96. },
  97. get: function() {
  98. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  99. return valueProp.get.call(textarea);
  100. }
  101. });
  102. json_elem.parentElement.style.display="none"
  103. })