2022-08-31 19:19:30 +00:00
function gradioApp ( ) {
2022-09-02 20:25:29 +00:00
return document . getElementsByTagName ( 'gradio-app' ) [ 0 ] . shadowRoot ;
2022-08-31 19:19:30 +00:00
}
2022-09-18 05:37:03 +00:00
uiUpdateCallbacks = [ ]
function onUiUpdate ( callback ) {
uiUpdateCallbacks . push ( callback )
2022-09-17 00:03:03 +00:00
}
2022-09-18 05:37:03 +00:00
function uiUpdate ( root ) {
uiUpdateCallbacks . forEach ( function ( x ) {
x ( )
} )
2022-08-31 19:19:30 +00:00
}
document . addEventListener ( "DOMContentLoaded" , function ( ) {
var mutationObserver = new MutationObserver ( function ( m ) {
2022-09-18 05:37:03 +00:00
uiUpdate ( gradioApp ( ) ) ;
2022-08-31 19:19:30 +00:00
} ) ;
2022-09-02 20:25:29 +00:00
mutationObserver . observe ( gradioApp ( ) , { childList : true , subtree : true } )
2022-08-31 19:19:30 +00:00
} ) ;
2022-09-02 20:25:29 +00:00
function selected _gallery _index ( ) {
var gr = gradioApp ( )
var buttons = gradioApp ( ) . querySelectorAll ( ".gallery-item" )
var button = gr . querySelector ( ".gallery-item.\\!ring-2" )
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
}
function extract _image _from _gallery _img2img ( gallery ) {
gradioApp ( ) . querySelectorAll ( 'button' ) [ 1 ] . click ( ) ;
return extract _image _from _gallery ( gallery ) ;
}
function extract _image _from _gallery _extras ( gallery ) {
gradioApp ( ) . querySelectorAll ( 'button' ) [ 2 ] . click ( ) ;
return extract _image _from _gallery ( gallery ) ;
2022-09-02 20:25:29 +00:00
}
2022-09-05 23:09:01 +00:00
function requestProgress ( ) {
btn = gradioApp ( ) . getElementById ( "check_progress" ) ;
if ( btn == null ) return ;
btn . click ( ) ;
}
function submit ( ) {
window . setTimeout ( requestProgress , 500 )
res = [ ]
2022-09-17 05:03:47 +00:00
for ( var i = 0 ; i < arguments . length ; i ++ ) {
2022-09-05 23:09:01 +00:00
res . push ( arguments [ i ] )
}
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-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
}