2022-11-02 10:05:00 -06:00
// various functions for interaction with ui.py not large enough to warrant putting them in separate files
2022-09-02 14:25:29 -06:00
2022-10-17 10:24:24 -06:00
function set _theme ( theme ) {
2022-10-17 03:05:05 -06:00
gradioURL = window . location . href
2022-10-17 10:24:24 -06:00
if ( ! gradioURL . includes ( '?__theme=' ) ) {
window . location . replace ( gradioURL + '?__theme=' + theme ) ;
2022-10-17 03:05:05 -06:00
}
}
2022-09-02 14:25:29 -06:00
function selected _gallery _index ( ) {
2022-11-20 11:52:18 -07:00
var buttons = gradioApp ( ) . querySelectorAll ( '[style="display: block;"].tabitem div[id$=_gallery] .gallery-item' )
var button = gradioApp ( ) . querySelector ( '[style="display: block;"].tabitem div[id$=_gallery] .gallery-item.\\!ring-2' )
2022-09-02 14:25:29 -06: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 ) {
2023-01-02 12:44:46 -07:00
return [ gallery [ 0 ] ]
2022-09-02 14:25:29 -06:00
}
index = selected _gallery _index ( )
if ( index < 0 || index >= gallery . length ) {
2022-09-10 02:10:00 -06:00
return [ null ]
2022-09-02 14:25:29 -06:00
}
2023-01-02 12:44:46 -07:00
return [ gallery [ index ] ] ;
2022-09-10 15:17:34 -06:00
}
2022-09-23 13:49:21 -06: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 ( ) {
2022-10-13 11:42:27 -06:00
gradioApp ( ) . querySelector ( '#tabs' ) . querySelectorAll ( 'button' ) [ 0 ] . click ( ) ;
2022-09-23 13:49:21 -06:00
return args _to _array ( arguments ) ;
}
2023-01-14 12:43:01 -07:00
function switch _to _img2img _tab ( no ) {
2022-10-13 11:42:27 -06:00
gradioApp ( ) . querySelector ( '#tabs' ) . querySelectorAll ( 'button' ) [ 1 ] . click ( ) ;
2023-01-14 12:43:01 -07:00
gradioApp ( ) . getElementById ( 'mode_img2img' ) . querySelectorAll ( 'button' ) [ no ] . click ( ) ;
}
function switch _to _img2img ( ) {
switch _to _img2img _tab ( 0 ) ;
return args _to _array ( arguments ) ;
}
function switch _to _sketch ( ) {
switch _to _img2img _tab ( 1 ) ;
return args _to _array ( arguments ) ;
}
function switch _to _inpaint ( ) {
switch _to _img2img _tab ( 2 ) ;
return args _to _array ( arguments ) ;
}
2022-09-23 13:49:21 -06:00
2023-01-14 12:43:01 -07:00
function switch _to _inpaint _sketch ( ) {
switch _to _img2img _tab ( 3 ) ;
2022-09-23 13:49:21 -06:00
return args _to _array ( arguments ) ;
}
2022-10-26 23:36:11 -06:00
function switch _to _inpaint ( ) {
2022-10-13 11:42:27 -06:00
gradioApp ( ) . querySelector ( '#tabs' ) . querySelectorAll ( 'button' ) [ 1 ] . click ( ) ;
2023-01-12 09:47:33 -07:00
gradioApp ( ) . getElementById ( 'mode_img2img' ) . querySelectorAll ( 'button' ) [ 2 ] . click ( ) ;
2022-09-23 13:49:21 -06:00
return args _to _array ( arguments ) ;
}
function switch _to _extras ( ) {
2022-10-13 11:42:27 -06:00
gradioApp ( ) . querySelector ( '#tabs' ) . querySelectorAll ( 'button' ) [ 2 ] . click ( ) ;
2022-09-23 13:49:21 -06:00
return args _to _array ( arguments ) ;
}
2022-09-22 03:11:48 -06: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 17:09:01 -06:00
2022-09-22 03:11:48 -06:00
res [ 0 ] = get _tab _index ( tabId )
return res
}
function get _extras _tab _index ( ) {
2022-10-09 19:26:52 -06:00
const [ , , ... args ] = [ ... arguments ]
return [ get _tab _index ( 'mode_extras' ) , get _tab _index ( 'extras_resize_mode' ) , ... args ]
2022-09-22 03:11:48 -06:00
}
function create _submit _args ( args ) {
2022-09-05 17:09:01 -06:00
res = [ ]
2022-09-22 03:11:48 -06:00
for ( var i = 0 ; i < args . length ; i ++ ) {
res . push ( args [ i ] )
2022-09-05 17:09:01 -06:00
}
2022-09-16 23:03:47 -06: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.
2022-12-14 19:01:32 -07:00
// 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.
2022-09-16 23:03:47 -06:00
// 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 17:09:01 -06:00
return res
2022-09-07 12:26:19 -06:00
}
2022-09-07 13:58:11 -06:00
2022-09-22 03:11:48 -06:00
function submit ( ) {
2022-09-23 11:46:02 -06:00
requestProgress ( 'txt2img' )
2022-09-22 03:11:48 -06:00
return create _submit _args ( arguments )
}
function submit _img2img ( ) {
2022-09-23 11:46:02 -06:00
requestProgress ( 'img2img' )
2022-09-22 03:11:48 -06:00
res = create _submit _args ( arguments )
res [ 0 ] = get _tab _index ( 'mode_img2img' )
return res
}
2022-09-11 08:35:12 -06:00
function ask _for _style _name ( _ , prompt _text , negative _prompt _text ) {
name _ = prompt ( 'Style name:' )
2022-10-15 05:22:30 -06:00
return [ name _ , prompt _text , negative _prompt _text ]
2022-09-09 14:16:02 -06:00
}
2022-09-18 13:25:18 -06:00
2022-12-10 03:46:18 -07:00
function confirm _clear _prompt ( prompt , negative _prompt ) {
if ( confirm ( "Delete prompt?" ) ) {
prompt = ""
negative _prompt = ""
2022-10-20 00:08:24 -06:00
}
2022-10-20 16:03:25 -06:00
2022-12-10 03:46:18 -07:00
return [ prompt , negative _prompt ]
2022-10-20 00:08:24 -06:00
}
2022-09-22 03:11:48 -06:00
2022-09-18 13:25:18 -06:00
opts = { }
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 )
2023-01-14 05:55:40 -07:00
executeCallbacks ( optionsChangedCallbacks ) ;
2022-09-18 13:25:18 -06:00
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 )
}
2023-01-14 05:55:40 -07:00
executeCallbacks ( optionsChangedCallbacks ) ;
2022-09-18 13:25:18 -06:00
} ,
get : function ( ) {
var valueProp = Object . getOwnPropertyDescriptor ( HTMLTextAreaElement . prototype , 'value' ) ;
return valueProp . get . call ( textarea ) ;
}
} ) ;
json _elem . parentElement . style . display = "none"
2022-09-28 07:43:54 -06:00
if ( ! txt2img _textarea ) {
txt2img _textarea = gradioApp ( ) . querySelector ( "#txt2img_prompt > label > textarea" ) ;
txt2img _textarea ? . addEventListener ( "input" , ( ) => update _token _counter ( "txt2img_token_button" ) ) ;
}
if ( ! img2img _textarea ) {
img2img _textarea = gradioApp ( ) . querySelector ( "#img2img_prompt > label > textarea" ) ;
img2img _textarea ? . addEventListener ( "input" , ( ) => update _token _counter ( "img2img_token_button" ) ) ;
}
2023-01-03 00:01:06 -07:00
show _all _pages = gradioApp ( ) . getElementById ( 'settings_show_all_pages' )
settings _tabs = gradioApp ( ) . querySelector ( '#settings div' )
if ( show _all _pages && settings _tabs ) {
settings _tabs . appendChild ( show _all _pages )
show _all _pages . onclick = function ( ) {
gradioApp ( ) . querySelectorAll ( '#settings > div' ) . forEach ( function ( elem ) {
elem . style . display = "block" ;
} )
}
}
2022-09-18 13:25:18 -06:00
} )
2022-09-27 13:56:18 -06:00
2023-01-14 05:55:40 -07:00
onOptionsChanged ( function ( ) {
elem = gradioApp ( ) . getElementById ( 'sd_checkpoint_hash' )
sd _checkpoint _hash = opts . sd _checkpoint _hash || ""
shorthash = sd _checkpoint _hash . substr ( 0 , 10 )
if ( elem && elem . textContent != shorthash ) {
elem . textContent = shorthash
elem . title = sd _checkpoint _hash
elem . href = "https://google.com/search?q=" + sd _checkpoint _hash
}
} )
2022-09-28 07:43:54 -06:00
let txt2img _textarea , img2img _textarea = undefined ;
2022-09-27 17:29:53 -06:00
let wait _time = 800
let token _timeout ;
2022-09-27 13:56:18 -06:00
2022-09-30 10:12:44 -06:00
function update _txt2img _tokens ( ... args ) {
2022-09-29 12:40:47 -06:00
update _token _counter ( "txt2img_token_button" )
2022-09-30 10:12:44 -06:00
if ( args . length == 2 )
return args [ 0 ]
return args ;
2022-09-29 12:40:47 -06:00
}
2022-09-30 10:12:44 -06:00
function update _img2img _tokens ( ... args ) {
2022-09-29 12:40:47 -06:00
update _token _counter ( "img2img_token_button" )
2022-09-30 10:12:44 -06:00
if ( args . length == 2 )
return args [ 0 ]
return args ;
2022-09-30 14:31:00 -06:00
}
2022-09-28 07:43:54 -06:00
function update _token _counter ( button _id ) {
2022-09-27 17:29:53 -06:00
if ( token _timeout )
clearTimeout ( token _timeout ) ;
token _timeout = setTimeout ( ( ) => gradioApp ( ) . getElementById ( button _id ) ? . click ( ) , wait _time ) ;
}
2022-10-01 17:12:49 -06:00
function restart _reload ( ) {
document . body . innerHTML = '<h1 style="font-family:monospace;margin-top:20%;color:lightgray;text-align:center;">Reloading...</h1>' ;
setTimeout ( function ( ) { location . reload ( ) } , 2000 )
2022-11-06 00:02:25 -06:00
return [ ]
2022-10-01 17:12:49 -06:00
}