import os.path from functools import wraps import html import time import traceback from modules_forge import main_thread from modules import shared, progress, errors, devices, fifo_lock, profiling queue_lock = fifo_lock.FIFOLock() def wrap_queued_call(func): def f(*args, **kwargs): with queue_lock: res = func(*args, **kwargs) return res return f def wrap_gradio_gpu_call(func, extra_outputs=None): @wraps(func) def f(*args, **kwargs): # if the first argument is a string that says "task(...)", it is treated as a job id if args and type(args[0]) == str and args[0].startswith("task(") and args[0].endswith(")"): id_task = args[0] progress.add_task_to_queue(id_task) else: id_task = None with queue_lock: shared.state.begin(job=id_task) progress.start_task(id_task) try: res = func(*args, **kwargs) progress.record_results(id_task, res) finally: progress.finish_task(id_task) shared.state.end() return res return wrap_gradio_call(f, extra_outputs=extra_outputs, add_stats=True) def wrap_gradio_call(func, extra_outputs=None, add_stats=False): @wraps(func) def f(*args, **kwargs): try: res = func(*args, **kwargs) finally: shared.state.skipped = False shared.state.interrupted = False shared.state.stopping_generation = False shared.state.job_count = 0 shared.state.job = "" return res return wrap_gradio_call_no_job(f, extra_outputs, add_stats) def wrap_gradio_call_no_job(func, extra_outputs=None, add_stats=False): @wraps(func) def f(*args, extra_outputs_array=extra_outputs, **kwargs): run_memmon = shared.opts.memmon_poll_rate > 0 and not shared.mem_mon.disabled and add_stats if run_memmon: shared.mem_mon.monitor() t = time.perf_counter() try: res = list(func(*args, **kwargs)) except Exception as e: if main_thread.last_exception is not None: e = main_thread.last_exception else: traceback.print_exc() print(e) if extra_outputs_array is None: extra_outputs_array = [None, ''] error_message = f'{type(e).__name__}: {e}' res = extra_outputs_array + [f"
{text_a},
[ Profile ]
" else: profiling_html = '' # last item is always HTML res[-1] += f"Time taken: