2022-09-03 09:08:45 +00:00
|
|
|
import time
|
|
|
|
|
2022-10-22 11:07:00 +00:00
|
|
|
import gradio as gr
|
2022-09-03 09:08:45 +00:00
|
|
|
|
2023-01-10 09:29:45 +00:00
|
|
|
from modules.shared import opts
|
2022-10-16 19:06:21 +00:00
|
|
|
|
2022-10-22 11:07:00 +00:00
|
|
|
import modules.shared as shared
|
2022-10-15 10:11:28 +00:00
|
|
|
|
|
|
|
|
2023-01-04 15:42:51 +00:00
|
|
|
def calc_time_left(progress, threshold, label, force_display, show_eta):
|
2022-10-17 20:35:20 +00:00
|
|
|
if progress == 0:
|
2022-10-18 17:38:07 +00:00
|
|
|
return ""
|
2022-10-17 20:35:20 +00:00
|
|
|
else:
|
|
|
|
time_since_start = time.time() - shared.state.time_start
|
|
|
|
eta = (time_since_start/progress)
|
2022-10-18 17:38:07 +00:00
|
|
|
eta_relative = eta-time_since_start
|
2023-01-04 15:42:51 +00:00
|
|
|
if (eta_relative > threshold and show_eta) or force_display:
|
2022-10-19 16:28:27 +00:00
|
|
|
if eta_relative > 3600:
|
|
|
|
return label + time.strftime('%H:%M:%S', time.gmtime(eta_relative))
|
|
|
|
elif eta_relative > 60:
|
|
|
|
return label + time.strftime('%M:%S', time.gmtime(eta_relative))
|
|
|
|
else:
|
|
|
|
return label + time.strftime('%Ss', time.gmtime(eta_relative))
|
2022-10-18 17:38:07 +00:00
|
|
|
else:
|
|
|
|
return ""
|
2022-10-17 20:35:20 +00:00
|
|
|
|
|
|
|
|
2022-09-23 17:46:02 +00:00
|
|
|
def check_progress_call(id_part):
|
2022-09-05 23:09:01 +00:00
|
|
|
if shared.state.job_count == 0:
|
2023-01-10 09:29:45 +00:00
|
|
|
return "", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-06 07:11:25 +00:00
|
|
|
progress = 0
|
|
|
|
|
|
|
|
if shared.state.job_count > 0:
|
|
|
|
progress += shared.state.job_no / shared.state.job_count
|
2022-09-05 23:09:01 +00:00
|
|
|
if shared.state.sampling_steps > 0:
|
|
|
|
progress += 1 / shared.state.job_count * shared.state.sampling_step / shared.state.sampling_steps
|
|
|
|
|
2022-10-19 17:53:52 +00:00
|
|
|
# Show progress percentage and time left at the same moment, and base it also on steps done
|
2023-01-04 15:42:51 +00:00
|
|
|
show_eta = progress >= 0.01 or shared.state.sampling_step >= 10
|
2022-10-19 17:53:52 +00:00
|
|
|
|
2023-01-04 15:42:51 +00:00
|
|
|
time_left = calc_time_left(progress, 1, " ETA: ", shared.state.time_left_force_display, show_eta)
|
2022-10-18 17:38:07 +00:00
|
|
|
if time_left != "":
|
|
|
|
shared.state.time_left_force_display = True
|
2022-10-17 20:35:20 +00:00
|
|
|
|
2022-09-05 23:09:01 +00:00
|
|
|
progress = min(progress, 1)
|
|
|
|
|
2022-09-06 16:33:51 +00:00
|
|
|
progressbar = ""
|
|
|
|
if opts.show_progressbar:
|
2023-01-04 15:42:51 +00:00
|
|
|
progressbar = f"""<div class='progressDiv'><div class='progress' style="overflow:visible;width:{progress * 100}%;white-space:nowrap;">{" " * 2 + str(int(progress*100))+"%" + time_left if show_eta else ""}</div></div>"""
|
2022-09-06 16:33:51 +00:00
|
|
|
|
2023-01-10 09:29:45 +00:00
|
|
|
image = gr.update(visible=False)
|
|
|
|
preview_visibility = gr.update(visible=False)
|
2022-09-06 16:33:51 +00:00
|
|
|
|
2023-01-14 13:29:23 +00:00
|
|
|
if opts.live_previews_enable:
|
2022-11-02 09:12:32 +00:00
|
|
|
shared.state.set_current_image()
|
2022-09-06 16:33:51 +00:00
|
|
|
image = shared.state.current_image
|
|
|
|
|
2022-09-24 06:16:54 +00:00
|
|
|
if image is None:
|
2022-09-06 16:33:51 +00:00
|
|
|
image = gr.update(value=None)
|
|
|
|
else:
|
2023-01-10 09:29:45 +00:00
|
|
|
preview_visibility = gr.update(visible=True)
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-10-02 12:03:39 +00:00
|
|
|
if shared.state.textinfo is not None:
|
|
|
|
textinfo_result = gr.HTML.update(value=shared.state.textinfo, visible=True)
|
|
|
|
else:
|
2023-01-10 09:29:45 +00:00
|
|
|
textinfo_result = gr.update(visible=False)
|
2022-10-02 12:03:39 +00:00
|
|
|
|
|
|
|
return f"<span id='{id_part}_progress_span' style='display: none'>{time.time()}</span><p>{progressbar}</p>", preview_visibility, image, textinfo_result
|
2022-09-05 23:09:01 +00:00
|
|
|
|
|
|
|
|
2022-09-23 17:46:02 +00:00
|
|
|
def check_progress_call_initial(id_part):
|
2022-09-18 08:14:42 +00:00
|
|
|
shared.state.job_count = -1
|
2022-09-18 13:36:34 +00:00
|
|
|
shared.state.current_latent = None
|
|
|
|
shared.state.current_image = None
|
2022-10-02 12:03:39 +00:00
|
|
|
shared.state.textinfo = None
|
2022-10-17 20:35:20 +00:00
|
|
|
shared.state.time_start = time.time()
|
2022-10-18 17:38:07 +00:00
|
|
|
shared.state.time_left_force_display = False
|
2022-09-18 08:14:42 +00:00
|
|
|
|
2022-09-23 17:46:02 +00:00
|
|
|
return check_progress_call(id_part)
|
2022-09-18 08:14:42 +00:00
|
|
|
|
|
|
|
|
2022-10-02 12:03:39 +00:00
|
|
|
def setup_progressbar(progressbar, preview, id_part, textinfo=None):
|
|
|
|
if textinfo is None:
|
|
|
|
textinfo = gr.HTML(visible=False)
|
2022-09-18 08:14:42 +00:00
|
|
|
|
2022-09-23 17:46:02 +00:00
|
|
|
check_progress = gr.Button('Check progress', elem_id=f"{id_part}_check_progress", visible=False)
|
2022-09-18 08:14:42 +00:00
|
|
|
check_progress.click(
|
2022-09-23 17:46:02 +00:00
|
|
|
fn=lambda: check_progress_call(id_part),
|
2022-09-18 08:14:42 +00:00
|
|
|
show_progress=False,
|
|
|
|
inputs=[],
|
2022-10-02 12:03:39 +00:00
|
|
|
outputs=[progressbar, preview, preview, textinfo],
|
2022-09-18 08:14:42 +00:00
|
|
|
)
|
|
|
|
|
2022-09-23 17:46:02 +00:00
|
|
|
check_progress_initial = gr.Button('Check progress (first)', elem_id=f"{id_part}_check_progress_initial", visible=False)
|
2022-09-18 08:14:42 +00:00
|
|
|
check_progress_initial.click(
|
2022-09-23 17:46:02 +00:00
|
|
|
fn=lambda: check_progress_call_initial(id_part),
|
2022-09-18 08:14:42 +00:00
|
|
|
show_progress=False,
|
|
|
|
inputs=[],
|
2022-10-02 12:03:39 +00:00
|
|
|
outputs=[progressbar, preview, preview, textinfo],
|
2022-09-18 08:14:42 +00:00
|
|
|
)
|