2022-08-31 22:19:30 +03:00
function gradioApp ( ) {
2022-09-02 23:25:29 +03:00
return document . getElementsByTagName ( 'gradio-app' ) [ 0 ] . shadowRoot ;
2022-08-31 22:19:30 +03:00
}
2022-09-18 08:37:03 +03:00
uiUpdateCallbacks = [ ]
function onUiUpdate ( callback ) {
uiUpdateCallbacks . push ( callback )
2022-09-17 03:03:03 +03:00
}
2022-09-18 08:37:03 +03:00
function uiUpdate ( root ) {
uiUpdateCallbacks . forEach ( function ( x ) {
x ( )
} )
2022-08-31 22:19:30 +03:00
}
document . addEventListener ( "DOMContentLoaded" , function ( ) {
var mutationObserver = new MutationObserver ( function ( m ) {
2022-09-18 08:37:03 +03:00
uiUpdate ( gradioApp ( ) ) ;
2022-08-31 22:19:30 +03:00
} ) ;
2022-09-02 23:25:29 +03:00
mutationObserver . observe ( gradioApp ( ) , { childList : true , subtree : true } )
2022-08-31 22:19:30 +03:00
} ) ;
2022-09-02 23:25:29 +03: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 11:10:00 +03:00
return [ null ]
2022-09-02 23:25:29 +03:00
}
return gallery [ index ] ;
2022-09-11 00:17:34 +03: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 23:25:29 +03:00
}
2022-09-06 02:09:01 +03:00
function requestProgress ( ) {
btn = gradioApp ( ) . getElementById ( "check_progress" ) ;
if ( btn == null ) return ;
btn . click ( ) ;
}
function submit ( ) {
window . setTimeout ( requestProgress , 500 )
res = [ ]
2022-09-17 08:03:47 +03:00
for ( var i = 0 ; i < arguments . length ; i ++ ) {
2022-09-06 02:09:01 +03:00
res . push ( arguments [ i ] )
}
2022-09-17 08:03:47 +03: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-06 02:09:01 +03:00
return res
2022-09-07 21:26:19 +03:00
}
2022-09-07 22:58:11 +03:00
2022-09-11 17:35:12 +03: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 23:16:02 +03:00
}