Merge pull request #187 from bakkot/webui-upscalers-optional

webui: hide gfpgan part if not installed
This commit is contained in:
Lincoln Stein 2022-08-29 19:29:16 -04:00 committed by GitHub
commit e7658b941e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 16 deletions

View File

@ -15,6 +15,16 @@ class DreamServer(BaseHTTPRequestHandler):
self.end_headers() self.end_headers()
with open("./static/dream_web/index.html", "rb") as content: with open("./static/dream_web/index.html", "rb") as content:
self.wfile.write(content.read()) self.wfile.write(content.read())
elif self.path == "/config.js":
# unfortunately this import can't be at the top level, since that would cause a circular import
from ldm.gfpgan.gfpgan_tools import gfpgan_model_exists
self.send_response(200)
self.send_header("Content-type", "application/javascript")
self.end_headers()
config = {
'gfpgan_model_exists': gfpgan_model_exists
}
self.wfile.write(bytes("let config = " + json.dumps(config) + ";\n", "utf-8"))
else: else:
path = "." + self.path path = "." + self.path
cwd = os.path.realpath(os.getcwd()) cwd = os.path.realpath(os.getcwd())
@ -37,6 +47,9 @@ class DreamServer(BaseHTTPRequestHandler):
self.send_header("Content-type", "application/json") self.send_header("Content-type", "application/json")
self.end_headers() self.end_headers()
# unfortunately this import can't be at the top level, since that would cause a circular import
from ldm.gfpgan.gfpgan_tools import gfpgan_model_exists
content_length = int(self.headers['Content-Length']) content_length = int(self.headers['Content-Length'])
post_data = json.loads(self.rfile.read(content_length)) post_data = json.loads(self.rfile.read(content_length))
prompt = post_data['prompt'] prompt = post_data['prompt']
@ -47,7 +60,7 @@ class DreamServer(BaseHTTPRequestHandler):
height = int(post_data['height']) height = int(post_data['height'])
cfgscale = float(post_data['cfgscale']) cfgscale = float(post_data['cfgscale'])
sampler_name = post_data['sampler'] sampler_name = post_data['sampler']
gfpgan_strength = float(post_data['gfpgan_strength']) gfpgan_strength = float(post_data['gfpgan_strength']) if gfpgan_model_exists else 0
upscale_level = post_data['upscale_level'] upscale_level = post_data['upscale_level']
upscale_strength = post_data['upscale_strength'] upscale_strength = post_data['upscale_strength']
upscale = [int(upscale_level),float(upscale_strength)] if upscale_level != '' else None upscale = [int(upscale_level),float(upscale_strength)] if upscale_level != '' else None
@ -70,7 +83,7 @@ class DreamServer(BaseHTTPRequestHandler):
# the images are first generated, and then again when after upscaling # the images are first generated, and then again when after upscaling
# is complete. The upscaling replaces the original file, so the second # is complete. The upscaling replaces the original file, so the second
# entry should not be inserted into the image list. # entry should not be inserted into the image list.
def image_done(image, seed, upscaled=False): def image_done(image, seed, upscaled=False):
pngwriter.write_image(image, seed, upscaled) pngwriter.write_image(image, seed, upscaled)
# Append post_data to log, but only once! # Append post_data to log, but only once!

View File

@ -10,16 +10,18 @@ from scripts.dream import create_argv_parser
arg_parser = create_argv_parser() arg_parser = create_argv_parser()
opt = arg_parser.parse_args() opt = arg_parser.parse_args()
model_path = os.path.join(opt.gfpgan_dir, opt.gfpgan_model_path)
gfpgan_model_exists = os.path.isfile(model_path)
def _run_gfpgan(image, strength, prompt, seed, upsampler_scale=4): def _run_gfpgan(image, strength, prompt, seed, upsampler_scale=4):
print(f'\n* GFPGAN - Restoring Faces: {prompt} : seed:{seed}') print(f'\n* GFPGAN - Restoring Faces: {prompt} : seed:{seed}')
gfpgan = None
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=DeprecationWarning) warnings.filterwarnings('ignore', category=DeprecationWarning)
warnings.filterwarnings('ignore', category=UserWarning) warnings.filterwarnings('ignore', category=UserWarning)
try: try:
model_path = os.path.join(opt.gfpgan_dir, opt.gfpgan_model_path) if not gfpgan_model_exists:
if not os.path.isfile(model_path):
raise Exception('GFPGAN model not found at path ' + model_path) raise Exception('GFPGAN model not found at path ' + model_path)
sys.path.append(os.path.abspath(opt.gfpgan_dir)) sys.path.append(os.path.abspath(opt.gfpgan_dir))

View File

@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="static/dream_web/index.css"> <link rel="stylesheet" href="static/dream_web/index.css">
<script src="config.js"></script>
<script src="static/dream_web/index.js"></script> <script src="static/dream_web/index.js"></script>
</head> </head>
<body> <body>
@ -66,18 +67,19 @@
<button type="button" id="reset-seed">&olarr;</button> <button type="button" id="reset-seed">&olarr;</button>
<span>&bull;</span> <span>&bull;</span>
<button type="button" id="reset-all">Reset to Defaults</button> <button type="button" id="reset-all">Reset to Defaults</button>
<br> <div id="gfpgan">
<p><em>The options below require the GFPGAN and ESRGAN packages to be installed</em></p> <p><em>The options below require the GFPGAN and ESRGAN packages to be installed</em></p>
<label title="Strength of the gfpgan (face fixing) algorithm." for="gfpgan_strength">GPFGAN Strength:</label> <label title="Strength of the gfpgan (face fixing) algorithm." for="gfpgan_strength">GPFGAN Strength:</label>
<input value="0.8" min="0" max="1" type="number" id="gfpgan_strength" name="gfpgan_strength" step="0.05"> <input value="0.8" min="0" max="1" type="number" id="gfpgan_strength" name="gfpgan_strength" step="0.05">
<label title="Upscaling to perform using ESRGAN." for="upscale_level">Upscaling Level</label> <label title="Upscaling to perform using ESRGAN." for="upscale_level">Upscaling Level</label>
<select id="upscale_level" name="upscale_level" value=""> <select id="upscale_level" name="upscale_level" value="">
<option value="" selected>None</option> <option value="" selected>None</option>
<option value="2">2x</option> <option value="2">2x</option>
<option value="4">4x</option> <option value="4">4x</option>
</select> </select>
<label title="Strength of the esrgan (upscaling) algorithm." for="upscale_strength">Upscale Strength:</label> <label title="Strength of the esrgan (upscaling) algorithm." for="upscale_strength">Upscale Strength:</label>
<input value="0.75" min="0" max="1" type="number" id="upscale_strength" name="upscale_strength" step="0.05"> <input value="0.75" min="0" max="1" type="number" id="upscale_strength" name="upscale_strength" step="0.05">
</div>
</fieldset> </fieldset>
</form> </form>
<div id="about">For news and support for this web service, visit our <a href="http://github.com/lstein/stable-diffusion">GitHub site</a></div> <div id="about">For news and support for this web service, visit our <a href="http://github.com/lstein/stable-diffusion">GitHub site</a></div>

View File

@ -126,4 +126,8 @@ window.onload = () => {
clearFields(e.target.form); clearFields(e.target.form);
}); });
loadFields(document.querySelector("#generate-form")); loadFields(document.querySelector("#generate-form"));
if (!config.gfpgan_model_exists) {
document.querySelector("#gfpgan").style.display = 'none';
}
}; };