Commit c48ab36c authored by AUTOMATIC's avatar AUTOMATIC
Browse files

alternate restore progress button implementation

parent bd970040
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ titles = {
    "\u{1f4cb}": "Apply selected styles to current prompt",
    "\u{1f4d2}": "Paste available values into the field",
    "\u{1f3b4}": "Show/hide extra networks",
    "\u{1f300}": "Restore progress",

    "Inpaint a part of image": "Draw a mask over an image, and the script will regenerate the masked area with content according to prompt",
    "SD upscale": "Upscale image normally, split result into tiles, improve each tile using img2img, merge whole image back",
+2 −2
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ function randomId(){
// starts sending progress requests to "/internal/progress" uri, creating progressbar above progressbarContainer element and
// preview inside gallery element. Cleans up all created stuff when the task is over and calls atEnd.
// calls onProgress every time there is a progress update
function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgress){
function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgress, inactivityTimeout=40){
    var dateStart = new Date()
    var wasEverActive = false
    var parentProgressbar = progressbarContainer.parentNode
@@ -138,7 +138,7 @@ function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgre
                return
            }

            if(elapsedFromStart > 40 && !res.queued && !res.active){
            if(elapsedFromStart > inactivityTimeout && !res.queued && !res.active){
                removeProgressBar()
                return
            }
+45 −1
Original line number Diff line number Diff line
@@ -159,14 +159,24 @@ function showSubmitButtons(tabname, show){
    gradioApp().getElementById(tabname+'_skip').style.display = show ? "none" : "block"
}

function showRestoreProgressButton(tabname, show){
    button = gradioApp().getElementById(tabname + "_restore_progress")
    if(! button) return

    button.style.display = show ? "flex" : "none"
}

function submit(){
    rememberGallerySelection('txt2img_gallery')
    showSubmitButtons('txt2img', false)

    var id = randomId()
    localStorage.setItem("txt2img_task_id", id);

    requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function(){
        showSubmitButtons('txt2img', true)

        localStorage.removeItem("txt2img_task_id")
        showRestoreProgressButton('txt2img', false)
    })

    var res = create_submit_args(arguments)
@@ -181,8 +191,12 @@ function submit_img2img(){
    showSubmitButtons('img2img', false)

    var id = randomId()
    localStorage.setItem("img2img_task_id", id);

    requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function(){
        showSubmitButtons('img2img', true)
        localStorage.removeItem("img2img_task_id")
        showRestoreProgressButton('img2img', false)
    })

    var res = create_submit_args(arguments)
@@ -193,6 +207,36 @@ function submit_img2img(){
    return res
}

function restoreProgressTxt2img(x){
    id = localStorage.getItem("txt2img_task_id")

    if(id) {
        requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function(){
            showSubmitButtons('txt2img', true)
        }, null, 0)
    }

    return [id]
}
function restoreProgressImg2img(x){
    id = localStorage.getItem("img2img_task_id")

    if(id) {
        requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function(){
            showSubmitButtons('img2img', true)
        }, null, 0)
    }

    return [id]
}


onUiLoaded(function () {
    showRestoreProgressButton('txt2img', localStorage.getItem("txt2img_task_id"))
    showRestoreProgressButton('img2img', localStorage.getItem("img2img_task_id"))
});


function modelmerger(){
    var id = randomId()
    requestProgress(id, gradioApp().getElementById('modelmerger_results_panel'), null, function(){})
+1 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ def wrap_gradio_gpu_call(func, extra_outputs=None):

            try:
                res = func(*args, **kwargs)
                progress.record_results(id_task, res)
            finally:
                progress.finish_task(id_task)

+18 −0
Original line number Diff line number Diff line
@@ -13,6 +13,8 @@ import modules.shared as shared
current_task = None
pending_tasks = {}
finished_tasks = []
recorded_results = []
recorded_results_limit = 2


def start_task(id_task):
@@ -33,6 +35,12 @@ def finish_task(id_task):
        finished_tasks.pop(0)


def record_results(id_task, res):
    recorded_results.append((id_task, res))
    if len(recorded_results) > recorded_results_limit:
        recorded_results.pop(0)


def add_task_to_queue(id_job):
    pending_tasks[id_job] = time.time()

@@ -97,3 +105,13 @@ def progressapi(req: ProgressRequest):

    return ProgressResponse(active=active, queued=queued, completed=completed, progress=progress, eta=eta, live_preview=live_preview, id_live_preview=id_live_preview, textinfo=shared.state.textinfo)


def restore_progress(id_task):
    while id_task == current_task or id_task in pending_tasks:
        time.sleep(0.1)

    res = next(iter([x[1] for x in recorded_results if id_task == x[0]]), None)
    if res is not None:
        return res

    return gr.update(), gr.update(), gr.update(), f"Couldn't restore progress for {id_task}: results either have been discarded or never were obtained"
Loading