2022-11-03 00:05:00 +08:00
// various functions for interaction with ui.py not large enough to warrant putting them in separate files
2022-09-03 04:25:29 +08:00
2022-10-18 00:24:24 +08:00
function set _theme ( theme ) {
2022-10-17 17:05:05 +08:00
gradioURL = window . location . href
2022-10-18 00:24:24 +08:00
if ( ! gradioURL . includes ( '?__theme=' ) ) {
window . location . replace ( gradioURL + '?__theme=' + theme ) ;
2022-10-17 17:05:05 +08:00
}
}
2023-03-26 02:52:47 +08:00
function all _gallery _buttons ( ) {
var allGalleryButtons = gradioApp ( ) . querySelectorAll ( '[style="display: block;"].tabitem div[id$=_gallery].gradio-gallery .thumbnails > .thumbnail-item.thumbnail-small' ) ;
var visibleGalleryButtons = [ ] ;
allGalleryButtons . forEach ( function ( elem ) {
if ( elem . parentElement . offsetParent ) {
visibleGalleryButtons . push ( elem ) ;
}
} )
return visibleGalleryButtons ;
}
function selected _gallery _button ( ) {
var allCurrentButtons = gradioApp ( ) . querySelectorAll ( '[style="display: block;"].tabitem div[id$=_gallery].gradio-gallery .thumbnail-item.thumbnail-small.selected' ) ;
var visibleCurrentButton = null ;
allCurrentButtons . forEach ( function ( elem ) {
if ( elem . parentElement . offsetParent ) {
visibleCurrentButton = elem ;
}
} )
return visibleCurrentButton ;
}
2022-09-03 04:25:29 +08:00
function selected _gallery _index ( ) {
2023-03-26 02:52:47 +08:00
var buttons = all _gallery _buttons ( ) ;
var button = selected _gallery _button ( ) ;
2022-09-03 04:25:29 +08:00
var result = - 1
buttons . forEach ( function ( v , i ) { if ( v == button ) { result = i } } )
return result
}
function extract _image _from _gallery ( gallery ) {
2023-03-26 02:52:47 +08:00
if ( gallery . length == 0 ) {
return [ null ] ;
}
if ( gallery . length == 1 ) {
return [ gallery [ 0 ] ] ;
2022-09-03 04:25:29 +08:00
}
index = selected _gallery _index ( )
if ( index < 0 || index >= gallery . length ) {
2023-03-26 02:52:47 +08:00
// Use the first image in the gallery as the default
index = 0 ;
2022-09-03 04:25:29 +08:00
}
2023-01-03 03:44:46 +08:00
return [ gallery [ index ] ] ;
2022-09-11 05:17:34 +08:00
}
2022-09-24 03:49:21 +08: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-14 01:42:27 +08:00
gradioApp ( ) . querySelector ( '#tabs' ) . querySelectorAll ( 'button' ) [ 0 ] . click ( ) ;
2022-09-24 03:49:21 +08:00
return args _to _array ( arguments ) ;
}
2023-01-15 03:43:01 +08:00
function switch _to _img2img _tab ( no ) {
2022-10-14 01:42:27 +08:00
gradioApp ( ) . querySelector ( '#tabs' ) . querySelectorAll ( 'button' ) [ 1 ] . click ( ) ;
2023-01-15 03:43:01 +08: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-24 03:49:21 +08:00
2023-01-15 03:43:01 +08:00
function switch _to _inpaint _sketch ( ) {
switch _to _img2img _tab ( 3 ) ;
2022-09-24 03:49:21 +08:00
return args _to _array ( arguments ) ;
}
2022-10-27 13:36:11 +08:00
function switch _to _inpaint ( ) {
2022-10-14 01:42:27 +08:00
gradioApp ( ) . querySelector ( '#tabs' ) . querySelectorAll ( 'button' ) [ 1 ] . click ( ) ;
2023-01-13 00:47:33 +08:00
gradioApp ( ) . getElementById ( 'mode_img2img' ) . querySelectorAll ( 'button' ) [ 2 ] . click ( ) ;
2022-09-24 03:49:21 +08:00
return args _to _array ( arguments ) ;
}
function switch _to _extras ( ) {
2022-10-14 01:42:27 +08:00
gradioApp ( ) . querySelector ( '#tabs' ) . querySelectorAll ( 'button' ) [ 2 ] . click ( ) ;
2022-09-24 03:49:21 +08:00
return args _to _array ( arguments ) ;
}
2022-09-22 17:11:48 +08:00
function get _tab _index ( tabId ) {
var res = 0
gradioApp ( ) . getElementById ( tabId ) . querySelector ( 'div' ) . querySelectorAll ( 'button' ) . forEach ( function ( button , i ) {
2023-03-20 21:09:36 +08:00
if ( button . className . indexOf ( 'selected' ) != - 1 )
2022-09-22 17:11:48 +08:00
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-06 07:09:01 +08:00
2022-09-22 17:11:48 +08:00
res [ 0 ] = get _tab _index ( tabId )
return res
}
2023-01-19 01:16:52 +08:00
function get _img2img _tab _index ( ) {
let res = args _to _array ( arguments )
2023-03-29 01:36:57 +08:00
res . splice ( - 2 )
2023-01-19 01:16:52 +08:00
res [ 0 ] = get _tab _index ( 'mode_img2img' )
return res
}
2022-09-22 17:11:48 +08:00
function create _submit _args ( args ) {
2022-09-06 07:09:01 +08:00
res = [ ]
2022-09-22 17:11:48 +08:00
for ( var i = 0 ; i < args . length ; i ++ ) {
res . push ( args [ i ] )
2022-09-06 07:09:01 +08:00
}
2022-09-17 13:03:47 +08: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-15 10:01:32 +08: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-17 13:03:47 +08: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-06 07:09:01 +08:00
return res
2022-09-08 02:26:19 +08:00
}
2022-09-08 03:58:11 +08:00
2023-01-15 23:50:56 +08:00
function showSubmitButtons ( tabname , show ) {
gradioApp ( ) . getElementById ( tabname + '_interrupt' ) . style . display = show ? "none" : "block"
gradioApp ( ) . getElementById ( tabname + '_skip' ) . style . display = show ? "none" : "block"
}
2022-09-22 17:11:48 +08:00
function submit ( ) {
2023-01-15 23:50:56 +08:00
rememberGallerySelection ( 'txt2img_gallery' )
showSubmitButtons ( 'txt2img' , false )
2023-02-06 03:55:31 +08:00
var id = randomId ( "txt2img" )
2023-01-15 23:50:56 +08:00
requestProgress ( id , gradioApp ( ) . getElementById ( 'txt2img_gallery_container' ) , gradioApp ( ) . getElementById ( 'txt2img_gallery' ) , function ( ) {
showSubmitButtons ( 'txt2img' , true )
} )
2022-09-22 17:11:48 +08:00
2023-01-15 23:50:56 +08:00
var res = create _submit _args ( arguments )
res [ 0 ] = id
return res
2022-09-22 17:11:48 +08:00
}
function submit _img2img ( ) {
2023-01-15 23:50:56 +08:00
rememberGallerySelection ( 'img2img_gallery' )
showSubmitButtons ( 'img2img' , false )
2023-02-06 03:55:31 +08:00
var id = randomId ( "img2img" )
2023-01-15 23:50:56 +08:00
requestProgress ( id , gradioApp ( ) . getElementById ( 'img2img_gallery_container' ) , gradioApp ( ) . getElementById ( 'img2img_gallery' ) , function ( ) {
showSubmitButtons ( 'img2img' , true )
} )
2022-09-22 17:11:48 +08:00
2023-01-15 23:50:56 +08:00
var res = create _submit _args ( arguments )
2022-09-22 17:11:48 +08:00
2023-01-15 23:50:56 +08:00
res [ 0 ] = id
res [ 1 ] = get _tab _index ( 'mode_img2img' )
2022-09-22 17:11:48 +08:00
return res
}
2023-01-19 14:25:37 +08:00
function modelmerger ( ) {
var id = randomId ( )
requestProgress ( id , gradioApp ( ) . getElementById ( 'modelmerger_results_panel' ) , null , function ( ) { } )
var res = create _submit _args ( arguments )
res [ 0 ] = id
return res
}
2022-09-22 17:11:48 +08:00
2022-09-11 22:35:12 +08:00
function ask _for _style _name ( _ , prompt _text , negative _prompt _text ) {
name _ = prompt ( 'Style name:' )
2022-10-15 19:22:30 +08:00
return [ name _ , prompt _text , negative _prompt _text ]
2022-09-10 04:16:02 +08:00
}
2022-09-19 03:25:18 +08:00
2022-12-10 18:46:18 +08:00
function confirm _clear _prompt ( prompt , negative _prompt ) {
if ( confirm ( "Delete prompt?" ) ) {
prompt = ""
negative _prompt = ""
2022-10-20 14:08:24 +08:00
}
2022-10-21 06:03:25 +08:00
2022-12-10 18:46:18 +08:00
return [ prompt , negative _prompt ]
2022-10-20 14:08:24 +08:00
}
2023-01-30 06:03:31 +08:00
promptTokecountUpdateFuncs = { }
function recalculatePromptTokens ( name ) {
if ( promptTokecountUpdateFuncs [ name ] ) {
promptTokecountUpdateFuncs [ name ] ( )
}
}
function recalculate _prompts _txt2img ( ) {
recalculatePromptTokens ( 'txt2img_prompt' )
recalculatePromptTokens ( 'txt2img_neg_prompt' )
return args _to _array ( arguments ) ;
}
function recalculate _prompts _img2img ( ) {
recalculatePromptTokens ( 'img2img_prompt' )
recalculatePromptTokens ( 'img2img_neg_prompt' )
return args _to _array ( arguments ) ;
}
2022-09-19 03:25:18 +08:00
opts = { }
onUiUpdate ( function ( ) {
if ( Object . keys ( opts ) . length != 0 ) return ;
json _elem = gradioApp ( ) . getElementById ( 'settings_json' )
if ( json _elem == null ) return ;
2023-01-21 14:48:38 +08:00
var textarea = json _elem . querySelector ( 'textarea' )
var jsdata = textarea . value
2022-09-19 03:25:18 +08:00
opts = JSON . parse ( jsdata )
2023-01-14 20:55:40 +08:00
executeCallbacks ( optionsChangedCallbacks ) ;
2022-09-19 03:25:18 +08: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 20:55:40 +08:00
executeCallbacks ( optionsChangedCallbacks ) ;
2022-09-19 03:25:18 +08:00
} ,
get : function ( ) {
var valueProp = Object . getOwnPropertyDescriptor ( HTMLTextAreaElement . prototype , 'value' ) ;
return valueProp . get . call ( textarea ) ;
}
} ) ;
json _elem . parentElement . style . display = "none"
2022-09-28 21:43:54 +08:00
2023-01-20 15:18:41 +08:00
function registerTextarea ( id , id _counter , id _button ) {
var prompt = gradioApp ( ) . getElementById ( id )
var counter = gradioApp ( ) . getElementById ( id _counter )
var textarea = gradioApp ( ) . querySelector ( "#" + id + " > label > textarea" ) ;
if ( counter . parentElement == prompt . parentElement ) {
return
}
prompt . parentElement . insertBefore ( counter , prompt )
prompt . parentElement . style . position = "relative"
2023-01-30 06:03:31 +08:00
promptTokecountUpdateFuncs [ id ] = function ( ) { update _token _counter ( id _button ) ; }
textarea . addEventListener ( "input" , promptTokecountUpdateFuncs [ id ] ) ;
2023-01-20 15:18:41 +08:00
}
registerTextarea ( 'txt2img_prompt' , 'txt2img_token_counter' , 'txt2img_token_button' )
registerTextarea ( 'txt2img_neg_prompt' , 'txt2img_negative_token_counter' , 'txt2img_negative_token_button' )
registerTextarea ( 'img2img_prompt' , 'img2img_token_counter' , 'img2img_token_button' )
registerTextarea ( 'img2img_neg_prompt' , 'img2img_negative_token_counter' , 'img2img_negative_token_button' )
2023-01-03 15:01:06 +08: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-19 03:25:18 +08:00
} )
2022-09-28 03:56:18 +08:00
2023-01-14 20:55:40 +08: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 21:43:54 +08:00
let txt2img _textarea , img2img _textarea = undefined ;
2022-09-28 07:29:53 +08:00
let wait _time = 800
2023-01-30 06:03:31 +08:00
let token _timeouts = { } ;
2022-09-28 03:56:18 +08:00
2022-10-01 00:12:44 +08:00
function update _txt2img _tokens ( ... args ) {
2022-09-30 02:40:47 +08:00
update _token _counter ( "txt2img_token_button" )
2022-10-01 00:12:44 +08:00
if ( args . length == 2 )
return args [ 0 ]
return args ;
2022-09-30 02:40:47 +08:00
}
2022-10-01 00:12:44 +08:00
function update _img2img _tokens ( ... args ) {
2022-09-30 02:40:47 +08:00
update _token _counter ( "img2img_token_button" )
2022-10-01 00:12:44 +08:00
if ( args . length == 2 )
return args [ 0 ]
return args ;
2022-10-01 04:31:00 +08:00
}
2022-09-28 21:43:54 +08:00
function update _token _counter ( button _id ) {
2023-01-30 06:03:31 +08:00
if ( token _timeouts [ button _id ] )
clearTimeout ( token _timeouts [ button _id ] ) ;
token _timeouts [ button _id ] = setTimeout ( ( ) => gradioApp ( ) . getElementById ( button _id ) ? . click ( ) , wait _time ) ;
2022-09-28 07:29:53 +08:00
}
2022-10-02 07:12:49 +08: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 14:02:25 +08:00
return [ ]
2022-10-02 07:12:49 +08:00
}
2023-01-17 19:15:47 +08:00
// Simulate an `input` DOM event for Gradio Textbox component. Needed after you edit its contents in javascript, otherwise your edits
// will only visible on web page and not sent to python.
function updateInput ( target ) {
let e = new Event ( "input" , { bubbles : true } )
Object . defineProperty ( e , "target" , { value : target } )
target . dispatchEvent ( e ) ;
}
2023-01-29 03:52:27 +08:00
var desiredCheckpointName = null ;
function selectCheckpoint ( name ) {
desiredCheckpointName = name ;
gradioApp ( ) . getElementById ( 'change_checkpoint' ) . click ( )
}
2023-02-06 03:55:31 +08:00
function restoreProgress ( task _tag ) {
if ( task _tag ) {
let successHandler = ( { current _task } ) => {
if ( current _task ) {
let _task _tag = [ "txt2img" , "img2img" ] . find ( t => current _task . startsWith ( ` task( ${ t } _ ` ) && current _task . endsWith ( ")" ) )
if ( ! _task _tag ) {
console . warn ( ` task tag ${ current _task } not implemented yet ` )
return
}
if ( task _tag != _task _tag ) return
showSubmitButtons ( task _tag , false )
requestProgress ( current _task , gradioApp ( ) . getElementById ( ` ${ task _tag } _gallery_container ` ) , gradioApp ( ) . getElementById ( ` ${ task _tag } _gallery ` ) , function ( ) {
showSubmitButtons ( task _tag , true )
} )
}
}
let errorHandler = e => window . alert ( ` invalid internal api respsonse. message: ${ e } ` )
fetch ( "./internal/current_task" )
. then ( res => res . json ( ) )
. then ( successHandler )
. catch ( errorHandler )
}
var res = create _submit _args ( arguments )
res [ 0 ] = 0
return res
}