From d667fc435f6210575ba50a6f3a05d3853b233caa Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Tue, 28 Mar 2023 22:23:40 +0300 Subject: add "resize by" and "resize to" tabs to img2img --- javascript/ui.js | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'javascript/ui.js') diff --git a/javascript/ui.js b/javascript/ui.js index 4a440193..dc538231 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -361,3 +361,8 @@ function selectCheckpoint(name){ desiredCheckpointName = name; gradioApp().getElementById('change_checkpoint').click() } + +function currentImg2imgSourceResolution(_, _, scaleBy){ + var img = gradioApp().querySelector('#mode_img2img > div[style="display: block;"] img') + return img ? [img.naturalWidth, img.naturalHeight, scaleBy] : [0, 0, scaleBy] +} -- cgit v1.2.3 From 4242e194e417ec5008d09ec6d756594ac65f77bd Mon Sep 17 00:00:00 2001 From: siutin Date: Mon, 6 Feb 2023 03:55:31 +0800 Subject: add a button to restore the current progress --- javascript/progressbar.js | 4 ++-- javascript/ui.js | 36 ++++++++++++++++++++++++++++++++++-- modules/progress.py | 14 ++++++++++++++ modules/ui.py | 34 ++++++++++++++++++++++++++++++---- 4 files changed, 80 insertions(+), 8 deletions(-) (limited to 'javascript/ui.js') diff --git a/javascript/progressbar.js b/javascript/progressbar.js index 4ac9b8db..7ba14192 100644 --- a/javascript/progressbar.js +++ b/javascript/progressbar.js @@ -59,8 +59,8 @@ function setTitle(progress){ } -function randomId(){ - return "task(" + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7)+")" +function randomId(prefix=null){ + return "task(" + (prefix == null ? "" : prefix + "_") + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7)+")" } // starts sending progress requests to "/internal/progress" uri, creating progressbar above progressbarContainer element and diff --git a/javascript/ui.js b/javascript/ui.js index 4a440193..9fe884c0 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -163,7 +163,7 @@ function submit(){ rememberGallerySelection('txt2img_gallery') showSubmitButtons('txt2img', false) - var id = randomId() + var id = randomId("txt2img") requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function(){ showSubmitButtons('txt2img', true) @@ -180,7 +180,7 @@ function submit_img2img(){ rememberGallerySelection('img2img_gallery') showSubmitButtons('img2img', false) - var id = randomId() + var id = randomId("img2img") requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function(){ showSubmitButtons('img2img', true) }) @@ -361,3 +361,35 @@ function selectCheckpoint(name){ desiredCheckpointName = name; gradioApp().getElementById('change_checkpoint').click() } + +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 + +} \ No newline at end of file diff --git a/modules/progress.py b/modules/progress.py index 27a336ad..36963c92 100644 --- a/modules/progress.py +++ b/modules/progress.py @@ -48,6 +48,20 @@ def set_last_task_result(id_job, result): last_task_result = result +def restore_progress_call(task_tag): + if current_task is None or not current_task[5:-1].startswith(task_tag): + + # image, generation_info, html_info, html_log + return tuple(list([None, None, None, None])) + + else: + + t_task = current_task + while t_task != last_task_id: + time.sleep(2.5) + return last_task_result + + class CurrentTaskResponse(BaseModel): current_task: str = Field(default=None, title="Task ID", description="id of the current progress task") diff --git a/modules/ui.py b/modules/ui.py index 627fbe0b..0133ee12 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -41,6 +41,7 @@ from modules.textual_inversion import textual_inversion import modules.hypernetworks.ui from modules.generation_parameters_copypaste import image_from_url_text import modules.extras +from modules.progress import restore_progress_call warnings.filterwarnings("default" if opts.show_warnings else "ignore", category=UserWarning) @@ -293,6 +294,7 @@ def create_toprow(is_img2img): interrupt = gr.Button('Interrupt', elem_id=f"{id_part}_interrupt", elem_classes="generate-box-interrupt") skip = gr.Button('Skip', elem_id=f"{id_part}_skip", elem_classes="generate-box-skip") submit = gr.Button('Generate', elem_id=f"{id_part}_generate", variant='primary') + restore_progress = gr.Button('Restore Progress', elem_id=f"{id_part}_restore_progress") skip.click( fn=lambda: shared.state.skip(), @@ -329,7 +331,7 @@ def create_toprow(is_img2img): prompt_styles = gr.Dropdown(label="Styles", elem_id=f"{id_part}_styles", choices=[k for k, v in shared.prompt_styles.styles.items()], value=[], multiselect=True) create_refresh_button(prompt_styles, shared.prompt_styles.reload, lambda: {"choices": [k for k, v in shared.prompt_styles.styles.items()]}, f"refresh_{id_part}_styles") - return prompt, prompt_styles, negative_prompt, submit, button_interrogate, button_deepbooru, prompt_style_apply, save_style, paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button + return prompt, prompt_styles, negative_prompt, submit, restore_progress, button_interrogate, button_deepbooru, prompt_style_apply, save_style, paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button def setup_progressbar(*args, **kwargs): @@ -446,7 +448,7 @@ def create_ui(): modules.scripts.scripts_txt2img.initialize_scripts(is_img2img=False) with gr.Blocks(analytics_enabled=False) as txt2img_interface: - txt2img_prompt, txt2img_prompt_styles, txt2img_negative_prompt, submit, _, _, txt2img_prompt_style_apply, txt2img_save_style, txt2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button = create_toprow(is_img2img=False) + txt2img_prompt, txt2img_prompt_styles, txt2img_negative_prompt, submit, restore_progress, _, _, txt2img_prompt_style_apply, txt2img_save_style, txt2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button = create_toprow(is_img2img=False) dummy_component = gr.Label(visible=False) txt_prompt_img = gr.File(label="", elem_id="txt2img_prompt_image", file_count="single", type="binary", visible=False) @@ -578,6 +580,18 @@ def create_ui(): res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False) + restore_progress.click( + fn=lambda: restore_progress_call('txt2img'), + _js="() => restoreProgress('txt2img')", + inputs=[], + outputs=[ + txt2img_gallery, + generation_info, + html_info, + html_log, + ] + ) + txt_prompt_img.change( fn=modules.images.image_data, inputs=[ @@ -646,7 +660,7 @@ def create_ui(): modules.scripts.scripts_img2img.initialize_scripts(is_img2img=True) with gr.Blocks(analytics_enabled=False) as img2img_interface: - img2img_prompt, img2img_prompt_styles, img2img_negative_prompt, submit, img2img_interrogate, img2img_deepbooru, img2img_prompt_style_apply, img2img_save_style, img2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button = create_toprow(is_img2img=True) + img2img_prompt, img2img_prompt_styles, img2img_negative_prompt, submit, restore_progress, img2img_interrogate, img2img_deepbooru, img2img_prompt_style_apply, img2img_save_style, img2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button = create_toprow(is_img2img=True) img2img_prompt_img = gr.File(label="", elem_id="img2img_prompt_image", file_count="single", type="binary", visible=False) @@ -898,6 +912,18 @@ def create_ui(): submit.click(**img2img_args) res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False) + restore_progress.click( + fn=lambda: restore_progress_call('img2img'), + _js="() => restoreProgress('img2img')", + inputs=[], + outputs=[ + img2img_gallery, + generation_info, + html_info, + html_log, + ] + ) + img2img_interrogate.click( fn=lambda *args: process_interrogate(interrogate, *args), **interrogate_args, @@ -1491,7 +1517,7 @@ def create_ui(): gr.HTML(shared.html("licenses.html"), elem_id="licenses") gr.Button(value="Show all pages", elem_id="settings_show_all_pages") - + def unload_sd_weights(): modules.sd_models.unload_model_weights() -- cgit v1.2.3 From 70ab21e67d128b953fbf4a360e02ac783f40dd55 Mon Sep 17 00:00:00 2001 From: siutin Date: Wed, 29 Mar 2023 00:17:19 +0800 Subject: keep randomId simpler --- javascript/progressbar.js | 4 ++-- javascript/ui.js | 10 ++-------- modules/progress.py | 4 ++-- 3 files changed, 6 insertions(+), 12 deletions(-) (limited to 'javascript/ui.js') diff --git a/javascript/progressbar.js b/javascript/progressbar.js index 7ba14192..4ac9b8db 100644 --- a/javascript/progressbar.js +++ b/javascript/progressbar.js @@ -59,8 +59,8 @@ function setTitle(progress){ } -function randomId(prefix=null){ - return "task(" + (prefix == null ? "" : prefix + "_") + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7)+")" +function randomId(){ + return "task(" + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7)+")" } // starts sending progress requests to "/internal/progress" uri, creating progressbar above progressbarContainer element and diff --git a/javascript/ui.js b/javascript/ui.js index 9fe884c0..c9df066d 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -163,7 +163,7 @@ function submit(){ rememberGallerySelection('txt2img_gallery') showSubmitButtons('txt2img', false) - var id = randomId("txt2img") + var id = randomId() requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function(){ showSubmitButtons('txt2img', true) @@ -180,7 +180,7 @@ function submit_img2img(){ rememberGallerySelection('img2img_gallery') showSubmitButtons('img2img', false) - var id = randomId("img2img") + var id = randomId() requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function(){ showSubmitButtons('img2img', true) }) @@ -367,12 +367,6 @@ 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) diff --git a/modules/progress.py b/modules/progress.py index 1947c0fd..e99267f5 100644 --- a/modules/progress.py +++ b/modules/progress.py @@ -49,8 +49,8 @@ def set_last_task_result(id_job, result): last_task_result = result -def restore_progress_call(task_tag): - if current_task is None or not current_task[5:-1].startswith(task_tag): +def restore_progress_call(): + if current_task is None: # image, generation_info, html_info, html_log return tuple(list([None, None, None, None])) -- cgit v1.2.3 From e40b2d947d874ed5504701bfd8e32243e0c456eb Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Sat, 29 Apr 2023 19:39:22 +0300 Subject: change gradio callback from change to release in a bunch of places now that it's fixed in gradio --- javascript/ui.js | 11 +++++++++++ modules/ui.py | 12 ++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) (limited to 'javascript/ui.js') diff --git a/javascript/ui.js b/javascript/ui.js index dc538231..0ba92ef8 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -366,3 +366,14 @@ function currentImg2imgSourceResolution(_, _, scaleBy){ var img = gradioApp().querySelector('#mode_img2img > div[style="display: block;"] img') return img ? [img.naturalWidth, img.naturalHeight, scaleBy] : [0, 0, scaleBy] } + +function updateImg2imgResizeToTextAfterChangingImage(){ + // At the time this is called from gradio, the image has no yet been replaced. + // There may be a better solution, but this is simple and straightforward so I'm going with it. + + setTimeout(function() { + gradioApp().getElementById('img2img_update_resize_to').click() + }, 500); + + return [] +} diff --git a/modules/ui.py b/modules/ui.py index 1130345c..a32500d1 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -782,8 +782,9 @@ def create_ui(): with FormRow(): scale_by_html = FormHTML(resize_from_to_html(0, 0, 0.0), elem_id="img2img_scale_resolution_preview") gr.Slider(label="Unused", elem_id="img2img_unused_scale_by_slider") + button_update_resize_to = gr.Button(visible=False, elem_id="img2img_update_resize_to") - scale_by.change( + on_change_args = dict( fn=resize_from_to_html, _js="currentImg2imgSourceResolution", inputs=[dummy_component, dummy_component, scale_by], @@ -791,6 +792,12 @@ def create_ui(): show_progress=False, ) + scale_by.release(**on_change_args) + button_update_resize_to.click(**on_change_args) + + for component in img2img_image_inputs: + component.change(fn=lambda: None, _js="updateImg2imgResizeToTextAfterChangingImage", inputs=[], outputs=[], show_progress=False) + tab_scale_to.select(fn=lambda: 0, inputs=[], outputs=[selected_scale_tab]) tab_scale_by.select(fn=lambda: 1, inputs=[], outputs=[selected_scale_tab]) @@ -1647,7 +1654,8 @@ def create_ui(): component = component_dict[k] info = opts.data_labels[k] - component.change( + change_handler = component.release if hasattr(component, 'release') else component.change + change_handler( fn=lambda value, k=k: run_settings_single(value, key=k), inputs=[component], outputs=[component, text_settings], -- cgit v1.2.3 From bd9700405a0686769b58437fd87d9106d3cd1346 Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Sat, 29 Apr 2023 22:15:20 +0300 Subject: Revert "Merge pull request #7595 from siutin/feature/restore-progress" This reverts commit 80987c36f9bfa33092ac7c75624b25d839cb2a06, reversing changes made to 2e78e65a22bfa6b116ae18d12fdcb85ec8acd727. --- javascript/hints.js | 2 +- javascript/ui.js | 27 ------------------ modules/call_queue.py | 18 ++++-------- modules/progress.py | 76 ++++++--------------------------------------------- modules/ui.py | 35 +++--------------------- webui.py | 1 - 6 files changed, 18 insertions(+), 141 deletions(-) (limited to 'javascript/ui.js') diff --git a/javascript/hints.js b/javascript/hints.js index 1a3130f8..23d85710 100644 --- a/javascript/hints.js +++ b/javascript/hints.js @@ -22,7 +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", diff --git a/javascript/ui.js b/javascript/ui.js index e50b44ee..0ba92ef8 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -362,32 +362,6 @@ function selectCheckpoint(name){ gradioApp().getElementById('change_checkpoint').click() } -function restoreProgress (task_tag) { - - if (task_tag) { - let successHandler = ({ current_task }) => { - if (current_task) { - 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 - -} - function currentImg2imgSourceResolution(_, _, scaleBy){ var img = gradioApp().querySelector('#mode_img2img > div[style="display: block;"] img') return img ? [img.naturalWidth, img.naturalHeight, scaleBy] : [0, 0, scaleBy] @@ -403,4 +377,3 @@ function updateImg2imgResizeToTextAfterChangingImage(){ return [] } - diff --git a/modules/call_queue.py b/modules/call_queue.py index 43f6ebe0..92097c15 100644 --- a/modules/call_queue.py +++ b/modules/call_queue.py @@ -4,16 +4,10 @@ import threading import traceback import time -import gradio as gr from modules import shared, progress queue_lock = threading.Lock() -queue_lock_condition = threading.Condition(lock=queue_lock) -def wrap_session_call(func): - def f(request: gr.Request, *args, **kwargs): - return func(request, *args, **kwargs) - return f def wrap_queued_call(func): def f(*args, **kwargs): @@ -26,31 +20,29 @@ def wrap_queued_call(func): def wrap_gradio_gpu_call(func, extra_outputs=None): - def f(request: gr.Request, *args, **kwargs): - user = request.username + def f(*args, **kwargs): # if the first argument is a string that says "task(...)", it is treated as a job id if len(args) > 0 and type(args[0]) == str and args[0][0:5] == "task(" and args[0][-1] == ")": id_task = args[0] - progress.add_task_to_queue(user, id_task) + progress.add_task_to_queue(id_task) else: id_task = None with queue_lock: shared.state.begin() - progress.start_task(user, id_task) + progress.start_task(id_task) try: res = func(*args, **kwargs) finally: - progress.finish_task(user, id_task) - progress.set_last_task_result(user, id_task, res) + progress.finish_task(id_task) shared.state.end() return res - return wrap_session_call(wrap_gradio_call(f, extra_outputs=extra_outputs, add_stats=True)) + return wrap_gradio_call(f, extra_outputs=extra_outputs, add_stats=True) def wrap_gradio_call(func, extra_outputs=None, add_stats=False): diff --git a/modules/progress.py b/modules/progress.py index 13568701..c69ecf3d 100644 --- a/modules/progress.py +++ b/modules/progress.py @@ -4,84 +4,38 @@ import time import gradio as gr from pydantic import BaseModel, Field -from typing import Optional -from fastapi import Depends, Security -from fastapi.security import APIKeyCookie -from modules import call_queue from modules.shared import opts import modules.shared as shared -current_task_user = None current_task = None pending_tasks = {} finished_tasks = [] -def start_task(user, id_task): +def start_task(id_task): global current_task - global current_task_user - current_task_user = user current_task = id_task - pending_tasks.pop((user, id_task), None) + pending_tasks.pop(id_task, None) -def finish_task(user, id_task): +def finish_task(id_task): global current_task - global current_task_user if current_task == id_task: current_task = None - if current_task_user == user: - current_task_user = None - - finished_tasks.append((user, id_task)) + finished_tasks.append(id_task) if len(finished_tasks) > 16: finished_tasks.pop(0) -def add_task_to_queue(user, id_job): - pending_tasks[(user, id_job)] = time.time() - -last_task_id = None -last_task_result = None -last_task_user = None - -def set_last_task_result(user, id_job, result): - - global last_task_id - global last_task_result - global last_task_user - - last_task_id = id_job - last_task_result = result - last_task_user = user - - -def restore_progress_call(request: gr.Request): - if current_task is None: - - # image, generation_info, html_info, html_log - return tuple(list([None, None, None, None])) - - else: - user = request.username +def add_task_to_queue(id_job): + pending_tasks[id_job] = time.time() - if current_task_user == user: - t_task = current_task - with call_queue.queue_lock_condition: - call_queue.queue_lock_condition.wait_for(lambda: t_task == last_task_id) - - return last_task_result - - return tuple(list([None, None, None, None])) - -class CurrentTaskResponse(BaseModel): - current_task: str = Field(default=None, title="Task ID", description="id of the current progress task") class ProgressRequest(BaseModel): id_task: str = Field(default=None, title="Task ID", description="id of the task to get progress for") @@ -102,21 +56,6 @@ class ProgressResponse(BaseModel): def setup_progress_api(app): return app.add_api_route("/internal/progress", progressapi, methods=["POST"], response_model=ProgressResponse) -def setup_current_task_api(app): - - def get_current_user(token: Optional[str] = Security(APIKeyCookie(name="access-token", auto_error=False))): - return None if token is None else app.tokens.get(token) - - def current_task_api(current_user: str = Depends(get_current_user)): - - if app.auth is None or current_task_user == current_user: - current_user_task = current_task - else: - current_user_task = None - - return CurrentTaskResponse(current_task=current_user_task) - - return app.add_api_route("/internal/current_task", current_task_api, methods=["GET"], response_model=CurrentTaskResponse) def progressapi(req: ProgressRequest): active = req.id_task == current_task @@ -156,4 +95,5 @@ def progressapi(req: ProgressRequest): else: live_preview = None - 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) \ No newline at end of file + 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) + diff --git a/modules/ui.py b/modules/ui.py index cc3c8d35..a32500d1 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -41,7 +41,6 @@ from modules.textual_inversion import textual_inversion import modules.hypernetworks.ui from modules.generation_parameters_copypaste import image_from_url_text import modules.extras -from modules.progress import restore_progress_call warnings.filterwarnings("default" if opts.show_warnings else "ignore", category=UserWarning) @@ -82,7 +81,6 @@ apply_style_symbol = '\U0001f4cb' # 📋 clear_prompt_symbol = '\U0001f5d1\ufe0f' # 🗑️ extra_networks_symbol = '\U0001F3B4' # 🎴 switch_values_symbol = '\U000021C5' # ⇅ -restore_progress_symbol = '\U0001F300' # 🌀 def plaintext_to_html(text): @@ -327,7 +325,6 @@ def create_toprow(is_img2img): extra_networks_button = ToolButton(value=extra_networks_symbol, elem_id=f"{id_part}_extra_networks") prompt_style_apply = ToolButton(value=apply_style_symbol, elem_id=f"{id_part}_style_apply") save_style = ToolButton(value=save_style_symbol, elem_id=f"{id_part}_style_create") - restore_progress_button = ToolButton(value=restore_progress_symbol, elem_id=f"{id_part}_restore_progress") token_counter = gr.HTML(value="0/75", elem_id=f"{id_part}_token_counter", elem_classes=["token-counter"]) token_button = gr.Button(visible=False, elem_id=f"{id_part}_token_button") @@ -345,7 +342,7 @@ def create_toprow(is_img2img): prompt_styles = gr.Dropdown(label="Styles", elem_id=f"{id_part}_styles", choices=[k for k, v in shared.prompt_styles.styles.items()], value=[], multiselect=True) create_refresh_button(prompt_styles, shared.prompt_styles.reload, lambda: {"choices": [k for k, v in shared.prompt_styles.styles.items()]}, f"refresh_{id_part}_styles") - return prompt, prompt_styles, negative_prompt, submit, button_interrogate, button_deepbooru, prompt_style_apply, save_style, paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button, restore_progress_button + return prompt, prompt_styles, negative_prompt, submit, button_interrogate, button_deepbooru, prompt_style_apply, save_style, paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button def setup_progressbar(*args, **kwargs): @@ -462,7 +459,7 @@ def create_ui(): modules.scripts.scripts_txt2img.initialize_scripts(is_img2img=False) with gr.Blocks(analytics_enabled=False) as txt2img_interface: - txt2img_prompt, txt2img_prompt_styles, txt2img_negative_prompt, submit, _, _, txt2img_prompt_style_apply, txt2img_save_style, txt2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button, restore_progress_button = create_toprow(is_img2img=False) + txt2img_prompt, txt2img_prompt_styles, txt2img_negative_prompt, submit, _, _, txt2img_prompt_style_apply, txt2img_save_style, txt2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button = create_toprow(is_img2img=False) dummy_component = gr.Label(visible=False) txt_prompt_img = gr.File(label="", elem_id="txt2img_prompt_image", file_count="single", type="binary", visible=False) @@ -594,18 +591,6 @@ def create_ui(): res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False) - restore_progress_button.click( - fn=restore_progress_call, - _js="() => restoreProgress('txt2img')", - inputs=[], - outputs=[ - txt2img_gallery, - generation_info, - html_info, - html_log, - ] - ) - txt_prompt_img.change( fn=modules.images.image_data, inputs=[ @@ -674,7 +659,7 @@ def create_ui(): modules.scripts.scripts_img2img.initialize_scripts(is_img2img=True) with gr.Blocks(analytics_enabled=False) as img2img_interface: - img2img_prompt, img2img_prompt_styles, img2img_negative_prompt, submit, img2img_interrogate, img2img_deepbooru, img2img_prompt_style_apply, img2img_save_style, img2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button, restore_progress_button = create_toprow(is_img2img=True) + img2img_prompt, img2img_prompt_styles, img2img_negative_prompt, submit, img2img_interrogate, img2img_deepbooru, img2img_prompt_style_apply, img2img_save_style, img2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button = create_toprow(is_img2img=True) img2img_prompt_img = gr.File(label="", elem_id="img2img_prompt_image", file_count="single", type="binary", visible=False) @@ -966,18 +951,6 @@ def create_ui(): submit.click(**img2img_args) res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False) - restore_progress_button.click( - fn=restore_progress_call, - _js="() => restoreProgress('img2img')", - inputs=[], - outputs=[ - img2img_gallery, - generation_info, - html_info, - html_log, - ] - ) - img2img_interrogate.click( fn=lambda *args: process_interrogate(interrogate, *args), **interrogate_args, @@ -1574,7 +1547,7 @@ def create_ui(): gr.HTML(shared.html("licenses.html"), elem_id="licenses") gr.Button(value="Show all pages", elem_id="settings_show_all_pages") - + def unload_sd_weights(): modules.sd_models.unload_model_weights() diff --git a/webui.py b/webui.py index 7f8d4f84..357bf4c1 100644 --- a/webui.py +++ b/webui.py @@ -339,7 +339,6 @@ def webui(): setup_middleware(app) modules.progress.setup_progress_api(app) - modules.progress.setup_current_task_api(app) if launch_api: create_api(app) -- cgit v1.2.3 From c48ab36cb9e0120c6f1779bee9e875bee8f903f5 Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Sat, 29 Apr 2023 22:16:54 +0300 Subject: alternate restore progress button implementation --- javascript/hints.js | 1 + javascript/progressbar.js | 4 ++-- javascript/ui.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- modules/call_queue.py | 1 + modules/progress.py | 18 ++++++++++++++++++ modules/ui.py | 36 ++++++++++++++++++++++++++++++++---- 6 files changed, 99 insertions(+), 7 deletions(-) (limited to 'javascript/ui.js') diff --git a/javascript/hints.js b/javascript/hints.js index 23d85710..e7d17d36 100644 --- a/javascript/hints.js +++ b/javascript/hints.js @@ -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", diff --git a/javascript/progressbar.js b/javascript/progressbar.js index 8df3f569..23bbf298 100644 --- a/javascript/progressbar.js +++ b/javascript/progressbar.js @@ -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 } diff --git a/javascript/ui.js b/javascript/ui.js index 0ba92ef8..e14b33f5 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -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(){}) diff --git a/modules/call_queue.py b/modules/call_queue.py index 92097c15..1829f3a6 100644 --- a/modules/call_queue.py +++ b/modules/call_queue.py @@ -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) diff --git a/modules/progress.py b/modules/progress.py index c69ecf3d..5655346b 100644 --- a/modules/progress.py +++ b/modules/progress.py @@ -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" diff --git a/modules/ui.py b/modules/ui.py index a32500d1..9ff4bcd9 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -19,7 +19,7 @@ import numpy as np from PIL import Image, PngImagePlugin from modules.call_queue import wrap_gradio_gpu_call, wrap_queued_call, wrap_gradio_call -from modules import sd_hijack, sd_models, localization, script_callbacks, ui_extensions, deepbooru, sd_vae, extra_networks, postprocessing, ui_components, ui_common, ui_postprocessing +from modules import sd_hijack, sd_models, localization, script_callbacks, ui_extensions, deepbooru, sd_vae, extra_networks, postprocessing, ui_components, ui_common, ui_postprocessing, progress from modules.ui_components import FormRow, FormColumn, FormGroup, ToolButton, FormHTML from modules.paths import script_path, data_path @@ -81,6 +81,7 @@ apply_style_symbol = '\U0001f4cb' # 📋 clear_prompt_symbol = '\U0001f5d1\ufe0f' # 🗑️ extra_networks_symbol = '\U0001F3B4' # 🎴 switch_values_symbol = '\U000021C5' # ⇅ +restore_progress_symbol = '\U0001F300' # 🌀 def plaintext_to_html(text): @@ -325,6 +326,7 @@ def create_toprow(is_img2img): extra_networks_button = ToolButton(value=extra_networks_symbol, elem_id=f"{id_part}_extra_networks") prompt_style_apply = ToolButton(value=apply_style_symbol, elem_id=f"{id_part}_style_apply") save_style = ToolButton(value=save_style_symbol, elem_id=f"{id_part}_style_create") + restore_progress_button = ToolButton(value=restore_progress_symbol, elem_id=f"{id_part}_restore_progress", visible=False) token_counter = gr.HTML(value="0/75", elem_id=f"{id_part}_token_counter", elem_classes=["token-counter"]) token_button = gr.Button(visible=False, elem_id=f"{id_part}_token_button") @@ -342,7 +344,7 @@ def create_toprow(is_img2img): prompt_styles = gr.Dropdown(label="Styles", elem_id=f"{id_part}_styles", choices=[k for k, v in shared.prompt_styles.styles.items()], value=[], multiselect=True) create_refresh_button(prompt_styles, shared.prompt_styles.reload, lambda: {"choices": [k for k, v in shared.prompt_styles.styles.items()]}, f"refresh_{id_part}_styles") - return prompt, prompt_styles, negative_prompt, submit, button_interrogate, button_deepbooru, prompt_style_apply, save_style, paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button + return prompt, prompt_styles, negative_prompt, submit, button_interrogate, button_deepbooru, prompt_style_apply, save_style, paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button, restore_progress_button def setup_progressbar(*args, **kwargs): @@ -459,7 +461,7 @@ def create_ui(): modules.scripts.scripts_txt2img.initialize_scripts(is_img2img=False) with gr.Blocks(analytics_enabled=False) as txt2img_interface: - txt2img_prompt, txt2img_prompt_styles, txt2img_negative_prompt, submit, _, _, txt2img_prompt_style_apply, txt2img_save_style, txt2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button = create_toprow(is_img2img=False) + txt2img_prompt, txt2img_prompt_styles, txt2img_negative_prompt, submit, _, _, txt2img_prompt_style_apply, txt2img_save_style, txt2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button, restore_progress_button = create_toprow(is_img2img=False) dummy_component = gr.Label(visible=False) txt_prompt_img = gr.File(label="", elem_id="txt2img_prompt_image", file_count="single", type="binary", visible=False) @@ -591,6 +593,19 @@ def create_ui(): res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False) + restore_progress_button.click( + fn=progress.restore_progress, + _js="restoreProgressTxt2img", + inputs=[dummy_component], + outputs=[ + txt2img_gallery, + generation_info, + html_info, + html_log, + ], + show_progress=False, + ) + txt_prompt_img.change( fn=modules.images.image_data, inputs=[ @@ -659,7 +674,7 @@ def create_ui(): modules.scripts.scripts_img2img.initialize_scripts(is_img2img=True) with gr.Blocks(analytics_enabled=False) as img2img_interface: - img2img_prompt, img2img_prompt_styles, img2img_negative_prompt, submit, img2img_interrogate, img2img_deepbooru, img2img_prompt_style_apply, img2img_save_style, img2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button = create_toprow(is_img2img=True) + img2img_prompt, img2img_prompt_styles, img2img_negative_prompt, submit, img2img_interrogate, img2img_deepbooru, img2img_prompt_style_apply, img2img_save_style, img2img_paste, extra_networks_button, token_counter, token_button, negative_token_counter, negative_token_button, restore_progress_button = create_toprow(is_img2img=True) img2img_prompt_img = gr.File(label="", elem_id="img2img_prompt_image", file_count="single", type="binary", visible=False) @@ -951,6 +966,19 @@ def create_ui(): submit.click(**img2img_args) res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False) + restore_progress_button.click( + fn=progress.restore_progress, + _js="restoreProgressImg2img", + inputs=[dummy_component], + outputs=[ + img2img_gallery, + generation_info, + html_info, + html_log, + ], + show_progress=False, + ) + img2img_interrogate.click( fn=lambda *args: process_interrogate(interrogate, *args), **interrogate_args, -- cgit v1.2.3 From 67f5c2abb0a9293245de5f00f0b8cd82e2a7cdd0 Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Mon, 1 May 2023 13:58:10 +0300 Subject: make it impossible to press the restore progress button after pressing it once --- javascript/ui.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'javascript/ui.js') diff --git a/javascript/ui.js b/javascript/ui.js index e14b33f5..42de9114 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -208,6 +208,8 @@ function submit_img2img(){ } function restoreProgressTxt2img(x){ + showRestoreProgressButton("txt2img", false) + id = localStorage.getItem("txt2img_task_id") if(id) { @@ -219,6 +221,8 @@ function restoreProgressTxt2img(x){ return [id] } function restoreProgressImg2img(x){ + showRestoreProgressButton("img2img", false) + id = localStorage.getItem("img2img_task_id") if(id) { -- cgit v1.2.3 From 33e6bc34ff4f826060489f0f76e6653672c8f1aa Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Mon, 1 May 2023 19:59:52 +0900 Subject: restore_progress fix id wrong type --- javascript/ui.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'javascript/ui.js') diff --git a/javascript/ui.js b/javascript/ui.js index e14b33f5..33a48b9a 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -216,7 +216,7 @@ function restoreProgressTxt2img(x){ }, null, 0) } - return [id] + return id } function restoreProgressImg2img(x){ id = localStorage.getItem("img2img_task_id") @@ -227,7 +227,7 @@ function restoreProgressImg2img(x){ }, null, 0) } - return [id] + return id } -- cgit v1.2.3