Skip to content

Commit

Permalink
Add & use modules.errors.print_error where currently printing excepti…
Browse files Browse the repository at this point in the history
…on info by hand
  • Loading branch information
akx committed May 29, 2023
1 parent b957dcf commit 00dfe27
Show file tree
Hide file tree
Showing 25 changed files with 117 additions and 153 deletions.
7 changes: 2 additions & 5 deletions extensions-builtin/LDSR/scripts/ldsr_model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import os
import sys
import traceback

from basicsr.utils.download_util import load_file_from_url

from modules.errors import print_error
from modules.upscaler import Upscaler, UpscalerData
from ldsr_model_arch import LDSR
from modules import shared, script_callbacks
Expand Down Expand Up @@ -51,10 +50,8 @@ def load_model(self, path: str):

try:
return LDSR(model, yaml)

except Exception:
print("Error importing LDSR:", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
print_error("Error importing LDSR", exc_info=True)
return None

def do_upscale(self, img, path):
Expand Down
6 changes: 3 additions & 3 deletions extensions-builtin/ScuNET/scripts/scunet_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os.path
import sys
import traceback

import PIL.Image
import numpy as np
Expand All @@ -12,6 +11,8 @@
import modules.upscaler
from modules import devices, modelloader, script_callbacks
from scunet_model_arch import SCUNet as net

from modules.errors import print_error
from modules.shared import opts


Expand All @@ -38,8 +39,7 @@ def __init__(self, dirname):
scaler_data = modules.upscaler.UpscalerData(name, file, self, 4)
scalers.append(scaler_data)
except Exception:
print(f"Error loading ScuNET model: {file}", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
print_error(f"Error loading ScuNET model: {file}", exc_info=True)
if add_model2:
scaler_data2 = modules.upscaler.UpscalerData(self.model_name2, self.model_url2, self)
scalers.append(scaler_data2)
Expand Down
7 changes: 4 additions & 3 deletions modules/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import modules.shared as shared
from modules import sd_samplers, deepbooru, sd_hijack, images, scripts, ui, postprocessing
from modules.api import models
from modules.errors import print_error
from modules.shared import opts
from modules.processing import StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img, process_images
from modules.textual_inversion.textual_inversion import create_embedding, train_embedding
Expand Down Expand Up @@ -108,7 +109,6 @@ def api_middleware(app: FastAPI):
from rich.console import Console
console = Console()
except Exception:
import traceback
rich_available = False

@app.middleware("http")
Expand Down Expand Up @@ -139,11 +139,12 @@ def handle_exception(request: Request, e: Exception):
"errors": str(e),
}
if not isinstance(e, HTTPException): # do not print backtrace on known httpexceptions
print(f"API error: {request.method}: {request.url} {err}")
message = f"API error: {request.method}: {request.url} {err}"
if rich_available:
print(message)
console.print_exception(show_locals=True, max_frames=2, extra_lines=1, suppress=[anyio, starlette], word_wrap=False, width=min([console.width, 200]))
else:
traceback.print_exc()
print_error(message, exc_info=True)
return JSONResponse(status_code=vars(e).get('status_code', 500), content=jsonable_encoder(err))

@app.middleware("http")
Expand Down
22 changes: 9 additions & 13 deletions modules/call_queue.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import html
import sys
import threading
import traceback
import time

from modules import shared, progress
from modules.errors import print_error

queue_lock = threading.Lock()

Expand Down Expand Up @@ -56,16 +55,14 @@ def f(*args, extra_outputs_array=extra_outputs, **kwargs):
try:
res = list(func(*args, **kwargs))
except Exception as e:
# When printing out our debug argument list, do not print out more than a MB of text
max_debug_str_len = 131072 # (1024*1024)/8

print("Error completing request", file=sys.stderr)
argStr = f"Arguments: {args} {kwargs}"
print(argStr[:max_debug_str_len], file=sys.stderr)
if len(argStr) > max_debug_str_len:
print(f"(Argument list truncated at {max_debug_str_len}/{len(argStr)} characters)", file=sys.stderr)

print(traceback.format_exc(), file=sys.stderr)
# When printing out our debug argument list,
# do not print out more than a 100 KB of text
max_debug_str_len = 131072
message = "Error completing request"
arg_str = f"Arguments: {args} {kwargs}"[:max_debug_str_len]
if len(arg_str) > max_debug_str_len:
arg_str += f" (Argument list truncated at {max_debug_str_len}/{len(arg_str)} characters)"
print_error(f"{message}\n{arg_str}", exc_info=True)

shared.state.job = ""
shared.state.job_count = 0
Expand Down Expand Up @@ -108,4 +105,3 @@ def f(*args, extra_outputs_array=extra_outputs, **kwargs):
return tuple(res)

return f

10 changes: 4 additions & 6 deletions modules/codeformer_model.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
import sys
import traceback

import cv2
import torch

import modules.face_restoration
import modules.shared
from modules import shared, devices, modelloader
from modules.errors import print_error
from modules.paths import models_path

# codeformer people made a choice to include modified basicsr library to their project which makes
Expand Down Expand Up @@ -105,8 +104,8 @@ def restore(self, np_image, w=None):
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
del output
torch.cuda.empty_cache()
except Exception as error:
print(f'\tFailed inference for CodeFormer: {error}', file=sys.stderr)
except Exception:
print_error('Failed inference for CodeFormer', exc_info=True)
restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))

restored_face = restored_face.astype('uint8')
Expand Down Expand Up @@ -135,7 +134,6 @@ def restore(self, np_image, w=None):
shared.face_restorers.append(codeformer)

except Exception:
print("Error setting up CodeFormer:", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
print_error("Error setting up CodeFormer", exc_info=True)

# sys.path = stored_sys_path
12 changes: 4 additions & 8 deletions modules/config_states.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"""

import os
import sys
import traceback
import json
import time
import tqdm
Expand All @@ -14,6 +12,7 @@
import git

from modules import shared, extensions
from modules.errors import print_error
from modules.paths_internal import script_path, config_states_dir


Expand Down Expand Up @@ -53,8 +52,7 @@ def get_webui_config():
if os.path.exists(os.path.join(script_path, ".git")):
webui_repo = git.Repo(script_path)
except Exception:
print(f"Error reading webui git info from {script_path}:", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
print_error(f"Error reading webui git info from {script_path}", exc_info=True)

webui_remote = None
webui_commit_hash = None
Expand Down Expand Up @@ -134,17 +132,15 @@ def restore_webui_config(config):
if os.path.exists(os.path.join(script_path, ".git")):
webui_repo = git.Repo(script_path)
except Exception:
print(f"Error reading webui git info from {script_path}:", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
print_error(f"Error reading webui git info from {script_path}", exc_info=True)
return

try:
webui_repo.git.fetch(all=True)
webui_repo.git.reset(webui_commit_hash, hard=True)
print(f"* Restored webui to commit {webui_commit_hash}.")
except Exception:
print(f"Error restoring webui to commit {webui_commit_hash}:", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
print_error(f"Error restoring webui to commit{webui_commit_hash}")


def restore_extension_config(config):
Expand Down
16 changes: 16 additions & 0 deletions modules/errors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import sys
import textwrap
import traceback


def print_error(
message: str,
*,
exc_info: bool = False,
) -> None:
"""
Print an error message to stderr, with optional traceback.
"""
for line in message.splitlines():
print("***", line, file=sys.stderr)
if exc_info:
print(textwrap.indent(traceback.format_exc(), " "), file=sys.stderr)
print("---")


def print_error_explanation(message):
lines = message.strip().split("\n")
max_len = max([len(x) for x in lines])
Expand Down
10 changes: 4 additions & 6 deletions modules/extensions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import os
import sys
import threading
import traceback

import git

from modules import shared
from modules.errors import print_error
from modules.paths_internal import extensions_dir, extensions_builtin_dir, script_path # noqa: F401

extensions = []
Expand Down Expand Up @@ -56,8 +55,7 @@ def do_read_info_from_repo(self):
if os.path.exists(os.path.join(self.path, ".git")):
repo = git.Repo(self.path)
except Exception:
print(f"Error reading github repository info from {self.path}:", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
print_error(f"Error reading github repository info from {self.path}", exc_info=True)

if repo is None or repo.bare:
self.remote = None
Expand All @@ -72,8 +70,8 @@ def do_read_info_from_repo(self):
self.commit_hash = commit.hexsha
self.version = self.commit_hash[:8]

except Exception as ex:
print(f"Failed reading extension data from Git repository ({self.name}): {ex}", file=sys.stderr)
except Exception:
print_error(f"Failed reading extension data from Git repository ({self.name})", exc_info=True)
self.remote = None

self.have_info_from_repo = True
Expand Down
6 changes: 2 additions & 4 deletions modules/gfpgan_model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import os
import sys
import traceback

import facexlib
import gfpgan

import modules.face_restoration
from modules import paths, shared, devices, modelloader
from modules.errors import print_error

model_dir = "GFPGAN"
user_path = None
Expand Down Expand Up @@ -112,5 +111,4 @@ def restore(self, np_image):

shared.face_restorers.append(FaceRestorerGFPGAN())
except Exception:
print("Error setting up GFPGAN:", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
print_error("Error setting up GFPGAN", exc_info=True)
14 changes: 5 additions & 9 deletions modules/hypernetworks/hypernetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import glob
import html
import os
import sys
import traceback
import inspect

import modules.textual_inversion.dataset
Expand All @@ -12,6 +10,7 @@
from einops import rearrange, repeat
from ldm.util import default
from modules import devices, processing, sd_models, shared, sd_samplers, hashes, sd_hijack_checkpoint
from modules.errors import print_error
from modules.textual_inversion import textual_inversion, logging
from modules.textual_inversion.learn_schedule import LearnRateScheduler
from torch import einsum
Expand Down Expand Up @@ -325,17 +324,14 @@ def load_hypernetwork(name):
if path is None:
return None

hypernetwork = Hypernetwork()

try:
hypernetwork = Hypernetwork()
hypernetwork.load(path)
return hypernetwork
except Exception:
print(f"Error loading hypernetwork {path}", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
print_error(f"Error loading hypernetwork {path}", exc_info=True)
return None

return hypernetwork


def load_hypernetworks(names, multipliers=None):
already_loaded = {}
Expand Down Expand Up @@ -770,7 +766,7 @@ def train_hypernetwork(id_task, hypernetwork_name, learn_rate, batch_size, gradi
</p>
"""
except Exception:
print(traceback.format_exc(), file=sys.stderr)
print_error("Exception in training hypernetwork", exc_info=True)
finally:
pbar.leave = False
pbar.close()
Expand Down
9 changes: 3 additions & 6 deletions modules/images.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import datetime
import sys
import traceback

import pytz
import io
Expand All @@ -18,6 +16,7 @@
import hashlib

from modules import sd_samplers, shared, script_callbacks, errors
from modules.errors import print_error
from modules.paths_internal import roboto_ttf_file
from modules.shared import opts

Expand Down Expand Up @@ -464,8 +463,7 @@ def apply(self, x):
replacement = fun(self, *pattern_args)
except Exception:
replacement = None
print(f"Error adding [{pattern}] to filename", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
print_error(f"Error adding [{pattern}] to filename", exc_info=True)

if replacement == NOTHING_AND_SKIP_PREVIOUS_TEXT:
continue
Expand Down Expand Up @@ -697,8 +695,7 @@ def read_info_from_image(image):
Negative prompt: {json_info["uc"]}
Steps: {json_info["steps"]}, Sampler: {sampler}, CFG scale: {json_info["scale"]}, Seed: {json_info["seed"]}, Size: {image.width}x{image.height}, Clip skip: 2, ENSD: 31337"""
except Exception:
print("Error parsing NovelAI image generation parameters:", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
print_error("Error parsing NovelAI image generation parameters", exc_info=True)

return geninfo, items

Expand Down
5 changes: 2 additions & 3 deletions modules/interrogate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import sys
import traceback
from collections import namedtuple
from pathlib import Path
import re
Expand All @@ -12,6 +11,7 @@
from torchvision.transforms.functional import InterpolationMode

from modules import devices, paths, shared, lowvram, modelloader, errors
from modules.errors import print_error

blip_image_eval_size = 384
clip_model_name = 'ViT-L/14'
Expand Down Expand Up @@ -216,8 +216,7 @@ def interrogate(self, pil_image):
res += f", {match}"

except Exception:
print("Error interrogating", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
print_error("Error interrogating", exc_info=True)
res += "<error>"

self.unload()
Expand Down
Loading

0 comments on commit 00dfe27

Please sign in to comment.