2022-09-18 06:00:06 +00:00
// various functions for interation with ui.py not large enough to warrant putting them in separate files
2022-09-02 20:25:29 +00:00
function selected _gallery _index ( ) {
2022-09-26 21:18:57 +00:00
var buttons = gradioApp ( ) . querySelectorAll ( '[style="display: block;"].tabitem .gallery-item' )
var button = gradioApp ( ) . querySelector ( '[style="display: block;"].tabitem .gallery-item.\\!ring-2' )
2022-09-02 20:25:29 +00:00
var result = - 1
buttons . forEach ( function ( v , i ) { if ( v == button ) { result = i } } )
return result
}
function extract _image _from _gallery ( gallery ) {
if ( gallery . length == 1 ) {
return gallery [ 0 ]
}
index = selected _gallery _index ( )
if ( index < 0 || index >= gallery . length ) {
2022-09-10 08:10:00 +00:00
return [ null ]
2022-09-02 20:25:29 +00:00
}
return gallery [ index ] ;
2022-09-10 21:17:34 +00:00
}
2022-09-23 19:49:21 +00:00
function args _to _array ( args ) {
res = [ ]
for ( var i = 0 ; i < args . length ; i ++ ) {
res . push ( args [ i ] )
}
return res
}
function switch _to _txt2img ( ) {
gradioApp ( ) . querySelectorAll ( 'button' ) [ 0 ] . click ( ) ;
return args _to _array ( arguments ) ;
}
function switch _to _img2img _img2img ( ) {
gradioApp ( ) . querySelectorAll ( 'button' ) [ 1 ] . click ( ) ;
gradioApp ( ) . getElementById ( 'mode_img2img' ) . querySelectorAll ( 'button' ) [ 0 ] . click ( ) ;
return args _to _array ( arguments ) ;
}
function switch _to _img2img _inpaint ( ) {
2022-09-10 21:17:34 +00:00
gradioApp ( ) . querySelectorAll ( 'button' ) [ 1 ] . click ( ) ;
2022-09-23 19:49:21 +00:00
gradioApp ( ) . getElementById ( 'mode_img2img' ) . querySelectorAll ( 'button' ) [ 1 ] . click ( ) ;
return args _to _array ( arguments ) ;
}
function switch _to _extras ( ) {
gradioApp ( ) . querySelectorAll ( 'button' ) [ 2 ] . click ( ) ;
return args _to _array ( arguments ) ;
}
function extract _image _from _gallery _txt2img ( gallery ) {
switch _to _txt2img ( )
return extract _image _from _gallery ( gallery ) ;
}
function extract _image _from _gallery _img2img ( gallery ) {
switch _to _img2img _img2img ( )
return extract _image _from _gallery ( gallery ) ;
}
function extract _image _from _gallery _inpaint ( gallery ) {
switch _to _img2img _inpaint ( )
2022-09-10 21:17:34 +00:00
return extract _image _from _gallery ( gallery ) ;
}
function extract _image _from _gallery _extras ( gallery ) {
2022-09-23 19:49:21 +00:00
switch _to _extras ( )
2022-09-10 21:17:34 +00:00
return extract _image _from _gallery ( gallery ) ;
2022-09-02 20:25:29 +00:00
}
2022-09-05 23:09:01 +00:00
2022-09-22 09:11:48 +00:00
function get _tab _index ( tabId ) {
var res = 0
gradioApp ( ) . getElementById ( tabId ) . querySelector ( 'div' ) . querySelectorAll ( 'button' ) . forEach ( function ( button , i ) {
if ( button . className . indexOf ( 'bg-white' ) != - 1 )
res = i
} )
return res
}
function create _tab _index _args ( tabId , args ) {
var res = [ ]
for ( var i = 0 ; i < args . length ; i ++ ) {
res . push ( args [ i ] )
}
2022-09-05 23:09:01 +00:00
2022-09-22 09:11:48 +00:00
res [ 0 ] = get _tab _index ( tabId )
return res
}
function get _extras _tab _index ( ) {
return create _tab _index _args ( 'mode_extras' , arguments )
}
function create _submit _args ( args ) {
2022-09-05 23:09:01 +00:00
res = [ ]
2022-09-22 09:11:48 +00:00
for ( var i = 0 ; i < args . length ; i ++ ) {
res . push ( args [ i ] )
2022-09-05 23:09:01 +00:00
}
2022-09-17 05:03:47 +00:00
// 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.
// This can lead to uploading a huge gallery of previously generated images, which leads to an unnecessary delay between submitting and beginning to generate.
// 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.
// If gradio at some point stops sending outputs, this may break something
if ( Array . isArray ( res [ res . length - 3 ] ) ) {
res [ res . length - 3 ] = null
}
2022-09-05 23:09:01 +00:00
return res
2022-09-07 18:26:19 +00:00
}
2022-09-07 19:58:11 +00:00
2022-09-22 09:11:48 +00:00
function submit ( ) {
2022-09-23 17:46:02 +00:00
requestProgress ( 'txt2img' )
2022-09-22 09:11:48 +00:00
return create _submit _args ( arguments )
}
function submit _img2img ( ) {
2022-09-23 17:46:02 +00:00
requestProgress ( 'img2img' )
2022-09-22 09:11:48 +00:00
res = create _submit _args ( arguments )
res [ 0 ] = get _tab _index ( 'mode_img2img' )
return res
}
2022-09-11 14:35:12 +00:00
function ask _for _style _name ( _ , prompt _text , negative _prompt _text ) {
name _ = prompt ( 'Style name:' )
return name _ === null ? [ null , null , null ] : [ name _ , prompt _text , negative _prompt _text ]
2022-09-09 20:16:02 +00:00
}
2022-09-18 19:25:18 +00:00
2022-09-22 09:11:48 +00:00
2022-09-18 19:25:18 +00:00
opts = { }
function apply _settings ( jsdata ) {
console . log ( jsdata )
opts = JSON . parse ( jsdata )
return jsdata
}
onUiUpdate ( function ( ) {
if ( Object . keys ( opts ) . length != 0 ) return ;
json _elem = gradioApp ( ) . getElementById ( 'settings_json' )
if ( json _elem == null ) return ;
textarea = json _elem . querySelector ( 'textarea' )
jsdata = textarea . value
opts = JSON . parse ( jsdata )
Object . defineProperty ( textarea , 'value' , {
set : function ( newValue ) {
var valueProp = Object . getOwnPropertyDescriptor ( HTMLTextAreaElement . prototype , 'value' ) ;
var oldValue = valueProp . get . call ( textarea ) ;
valueProp . set . call ( textarea , newValue ) ;
if ( oldValue != newValue ) {
opts = JSON . parse ( textarea . value )
}
} ,
get : function ( ) {
var valueProp = Object . getOwnPropertyDescriptor ( HTMLTextAreaElement . prototype , 'value' ) ;
return valueProp . get . call ( textarea ) ;
}
} ) ;
json _elem . parentElement . style . display = "none"
} )
2022-09-27 08:09:17 +00:00
/ * *
2022-09-27 17:31:01 +00:00
* Implement script - dependent UI restraints , e . g . forcing a specific sampling method
2022-09-27 08:09:17 +00:00
* /
2022-09-27 17:31:01 +00:00
let prev _ui _states = { } ;
function updateScriptRestraints ( ) {
const currentTab = get _uiCurrentTab ( ) ? . textContent . trim ( ) ;
const restraintsField = Array . from ( gradioApp ( ) . querySelectorAll ( ` # ${ currentTab } _script_restraints_json textarea ` ) )
. filter ( el => uiElementIsVisible ( el . closest ( '.gr-form' ) ) ) ? . [ 0 ] ;
if ( ! restraintsField ) {
2022-09-27 08:09:17 +00:00
return ;
}
2022-09-27 17:31:01 +00:00
if ( typeof prev _ui _states [ currentTab ] === 'undefined' ) {
prev _ui _states [ currentTab ] = { } ;
}
window . requestAnimationFrame ( ( ) => {
const restraints = JSON . parse ( restraintsField . value ) ;
// const scriptSelect = gradioApp().querySelector(`#${currentTab}_scripts select`);
const methodRadios = gradioApp ( ) . querySelectorAll ( ` [name="radio- ${ currentTab } _sampling"] ` ) ;
if ( restraints ? . methods ? . length ) {
prev _ui _states [ currentTab ] . sampling _method = gradioApp ( ) . querySelector ( ` [name="radio- ${ currentTab } _sampling"]:checked ` ) ;
2022-09-27 08:09:17 +00:00
methodRadios . forEach ( radio => {
2022-09-27 17:31:01 +00:00
const isAllowed = restraints . methods . includes ( radio . value ) ;
2022-09-27 08:09:17 +00:00
const label = radio . closest ( 'label' ) ;
2022-09-27 17:31:01 +00:00
radio . disabled = ! isAllowed ;
radio . checked = isAllowed ;
label . classList [ isAllowed ? 'remove' : 'add' ] ( '!cursor-not-allowed' , 'disabled' ) ;
label . title = ! isAllowed ? ` The selected script does not work with this method ` : '' ;
2022-09-27 08:09:17 +00:00
} ) ;
} else {
2022-09-27 17:31:01 +00:00
// reset to previously selected method
2022-09-27 08:09:17 +00:00
methodRadios . forEach ( radio => {
const label = radio . closest ( 'label' ) ;
radio . disabled = false ;
2022-09-27 17:31:01 +00:00
radio . checked = radio === prev _ui _states [ currentTab ] . sampling _method ;
label . classList . remove ( '!cursor-not-allowed' , 'disabled' ) ;
2022-09-27 08:09:17 +00:00
label . title = '' ;
} ) ;
}
2022-09-27 17:31:01 +00:00
} )
}