2022-09-09 02:16:29 +00:00
|
|
|
import argparse
|
2022-08-28 21:27:43 +00:00
|
|
|
import json
|
|
|
|
import base64
|
|
|
|
import mimetypes
|
2022-08-28 20:37:27 +00:00
|
|
|
import os
|
2022-08-28 21:27:43 +00:00
|
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
2022-09-09 02:16:29 +00:00
|
|
|
from ldm.dream.pngwriter import PngWriter, PromptFormatter
|
2022-08-30 15:55:40 +00:00
|
|
|
from threading import Event
|
|
|
|
|
2022-09-09 02:16:29 +00:00
|
|
|
def build_opt(post_data, seed, gfpgan_model_exists):
|
|
|
|
opt = argparse.Namespace()
|
|
|
|
setattr(opt, 'prompt', post_data['prompt'])
|
|
|
|
setattr(opt, 'init_img', post_data['initimg'])
|
|
|
|
setattr(opt, 'strength', float(post_data['strength']))
|
|
|
|
setattr(opt, 'iterations', int(post_data['iterations']))
|
|
|
|
setattr(opt, 'steps', int(post_data['steps']))
|
|
|
|
setattr(opt, 'width', int(post_data['width']))
|
|
|
|
setattr(opt, 'height', int(post_data['height']))
|
|
|
|
setattr(opt, 'seamless', 'seamless' in post_data)
|
|
|
|
setattr(opt, 'fit', 'fit' in post_data)
|
|
|
|
setattr(opt, 'mask', 'mask' in post_data)
|
|
|
|
setattr(opt, 'invert_mask', 'invert_mask' in post_data)
|
|
|
|
setattr(opt, 'cfg_scale', float(post_data['cfg_scale']))
|
|
|
|
setattr(opt, 'sampler_name', post_data['sampler_name'])
|
2022-09-15 11:43:43 +00:00
|
|
|
|
|
|
|
# embiggen not practical at this point because we have no way of feeding images back into img2img
|
|
|
|
# however, this code is here against that eventuality
|
|
|
|
setattr(opt, 'embiggen', None)
|
|
|
|
setattr(opt, 'embiggen_tiles', None)
|
|
|
|
|
2022-09-09 02:16:29 +00:00
|
|
|
setattr(opt, 'gfpgan_strength', float(post_data['gfpgan_strength']) if gfpgan_model_exists else 0)
|
|
|
|
setattr(opt, 'upscale', [int(post_data['upscale_level']), float(post_data['upscale_strength'])] if post_data['upscale_level'] != '' else None)
|
|
|
|
setattr(opt, 'progress_images', 'progress_images' in post_data)
|
2022-09-09 03:57:45 +00:00
|
|
|
setattr(opt, 'seed', None if int(post_data['seed']) == -1 else int(post_data['seed']))
|
2022-09-09 02:16:29 +00:00
|
|
|
setattr(opt, 'variation_amount', float(post_data['variation_amount']) if int(post_data['seed']) != -1 else 0)
|
|
|
|
setattr(opt, 'with_variations', [])
|
|
|
|
|
|
|
|
broken = False
|
|
|
|
if int(post_data['seed']) != -1 and post_data['with_variations'] != '':
|
|
|
|
for part in post_data['with_variations'].split(','):
|
|
|
|
seed_and_weight = part.split(':')
|
|
|
|
if len(seed_and_weight) != 2:
|
|
|
|
print(f'could not parse with_variation part "{part}"')
|
|
|
|
broken = True
|
|
|
|
break
|
|
|
|
try:
|
|
|
|
seed = int(seed_and_weight[0])
|
|
|
|
weight = float(seed_and_weight[1])
|
|
|
|
except ValueError:
|
|
|
|
print(f'could not parse with_variation part "{part}"')
|
|
|
|
broken = True
|
|
|
|
break
|
|
|
|
opt.with_variations.append([seed, weight])
|
|
|
|
|
|
|
|
if broken:
|
|
|
|
raise CanceledException
|
|
|
|
|
|
|
|
if len(opt.with_variations) == 0:
|
|
|
|
opt.with_variations = None
|
|
|
|
|
|
|
|
return opt
|
|
|
|
|
2022-08-30 15:55:40 +00:00
|
|
|
class CanceledException(Exception):
|
|
|
|
pass
|
2022-08-28 21:27:43 +00:00
|
|
|
|
|
|
|
class DreamServer(BaseHTTPRequestHandler):
|
|
|
|
model = None
|
2022-09-06 00:35:04 +00:00
|
|
|
outdir = None
|
2022-08-30 15:55:40 +00:00
|
|
|
canceled = Event()
|
2022-08-28 21:27:43 +00:00
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
if self.path == "/":
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", "text/html")
|
|
|
|
self.end_headers()
|
|
|
|
with open("./static/dream_web/index.html", "rb") as content:
|
|
|
|
self.wfile.write(content.read())
|
2022-08-29 15:53:27 +00:00
|
|
|
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"))
|
2022-09-08 02:50:06 +00:00
|
|
|
elif self.path == "/run_log.json":
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", "application/json")
|
|
|
|
self.end_headers()
|
|
|
|
output = []
|
|
|
|
|
|
|
|
log_file = os.path.join(self.outdir, "dream_web_log.txt")
|
|
|
|
if os.path.exists(log_file):
|
|
|
|
with open(log_file, "r") as log:
|
|
|
|
for line in log:
|
|
|
|
url, config = line.split(": {", maxsplit=1)
|
|
|
|
config = json.loads("{" + config)
|
|
|
|
config["url"] = url.lstrip(".")
|
|
|
|
if os.path.exists(url):
|
|
|
|
output.append(config)
|
|
|
|
|
|
|
|
self.wfile.write(bytes(json.dumps({"run_log": output}), "utf-8"))
|
2022-08-30 15:55:40 +00:00
|
|
|
elif self.path == "/cancel":
|
|
|
|
self.canceled.set()
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", "application/json")
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(bytes('{}', 'utf8'))
|
2022-08-28 21:33:30 +00:00
|
|
|
else:
|
2022-09-14 11:09:01 +00:00
|
|
|
path_dir = os.path.dirname(self.path)
|
|
|
|
out_dir = os.path.realpath(self.outdir.rstrip('/'))
|
|
|
|
if self.path.startswith('/static/dream_web/'):
|
|
|
|
path = '.' + self.path
|
2022-09-15 11:40:27 +00:00
|
|
|
elif out_dir.replace('\\', '/').endswith(path_dir):
|
2022-09-14 11:09:01 +00:00
|
|
|
file = os.path.basename(self.path)
|
|
|
|
path = os.path.join(self.outdir,file)
|
|
|
|
else:
|
2022-08-28 21:33:30 +00:00
|
|
|
self.send_response(404)
|
|
|
|
return
|
|
|
|
mime_type = mimetypes.guess_type(path)[0]
|
2022-08-28 21:27:43 +00:00
|
|
|
if mime_type is not None:
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", mime_type)
|
|
|
|
self.end_headers()
|
2022-09-14 11:09:01 +00:00
|
|
|
with open(path, "rb") as content:
|
2022-08-28 21:27:43 +00:00
|
|
|
self.wfile.write(content.read())
|
2022-08-28 20:37:27 +00:00
|
|
|
else:
|
2022-08-28 21:27:43 +00:00
|
|
|
self.send_response(404)
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", "application/json")
|
|
|
|
self.end_headers()
|
|
|
|
|
2022-08-29 15:53:27 +00:00
|
|
|
# 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
|
|
|
|
|
2022-08-28 21:27:43 +00:00
|
|
|
content_length = int(self.headers['Content-Length'])
|
|
|
|
post_data = json.loads(self.rfile.read(content_length))
|
2022-09-09 02:16:29 +00:00
|
|
|
opt = build_opt(post_data, self.model.seed, gfpgan_model_exists)
|
2022-09-06 21:12:39 +00:00
|
|
|
|
2022-08-30 15:55:40 +00:00
|
|
|
self.canceled.clear()
|
2022-09-09 02:16:29 +00:00
|
|
|
print(f">> Request to generate with prompt: {opt.prompt}")
|
2022-08-29 16:08:18 +00:00
|
|
|
# In order to handle upscaled images, the PngWriter needs to maintain state
|
|
|
|
# across images generated by each call to prompt2img(), so we define it in
|
|
|
|
# the outer scope of image_done()
|
|
|
|
config = post_data.copy() # Shallow copy
|
2022-09-09 02:16:29 +00:00
|
|
|
config['initimg'] = config.pop('initimg_name', '')
|
2022-08-29 16:08:18 +00:00
|
|
|
|
|
|
|
images_generated = 0 # helps keep track of when upscaling is started
|
|
|
|
images_upscaled = 0 # helps keep track of when upscaling is completed
|
2022-09-06 00:35:04 +00:00
|
|
|
pngwriter = PngWriter(self.outdir)
|
2022-08-29 16:08:18 +00:00
|
|
|
|
2022-08-31 04:21:04 +00:00
|
|
|
prefix = pngwriter.unique_prefix()
|
2022-08-29 16:08:18 +00:00
|
|
|
# if upscaling is requested, then this will be called twice, once when
|
|
|
|
# the images are first generated, and then again when after upscaling
|
|
|
|
# is complete. The upscaling replaces the original file, so the second
|
|
|
|
# entry should not be inserted into the image list.
|
2022-08-29 15:53:27 +00:00
|
|
|
def image_done(image, seed, upscaled=False):
|
2022-08-31 04:21:04 +00:00
|
|
|
name = f'{prefix}.{seed}.png'
|
2022-09-09 02:16:29 +00:00
|
|
|
iter_opt = argparse.Namespace(**vars(opt)) # copy
|
2022-09-15 11:43:43 +00:00
|
|
|
print(f'iter_opt = {iter_opt}')
|
2022-09-09 02:16:29 +00:00
|
|
|
if opt.variation_amount > 0:
|
|
|
|
this_variation = [[seed, opt.variation_amount]]
|
|
|
|
if opt.with_variations is None:
|
|
|
|
iter_opt.with_variations = this_variation
|
|
|
|
else:
|
|
|
|
iter_opt.with_variations = opt.with_variations + this_variation
|
|
|
|
iter_opt.variation_amount = 0
|
|
|
|
elif opt.with_variations is None:
|
|
|
|
iter_opt.seed = seed
|
|
|
|
normalized_prompt = PromptFormatter(self.model, iter_opt).normalize_prompt()
|
|
|
|
path = pngwriter.save_image_and_prompt_to_png(image, f'{normalized_prompt} -S{iter_opt.seed}', name)
|
|
|
|
|
|
|
|
if int(config['seed']) == -1:
|
|
|
|
config['seed'] = seed
|
2022-08-29 16:08:18 +00:00
|
|
|
# Append post_data to log, but only once!
|
|
|
|
if not upscaled:
|
2022-09-06 00:35:04 +00:00
|
|
|
with open(os.path.join(self.outdir, "dream_web_log.txt"), "a") as log:
|
2022-08-31 04:21:04 +00:00
|
|
|
log.write(f"{path}: {json.dumps(config)}\n")
|
|
|
|
|
2022-08-29 16:08:18 +00:00
|
|
|
self.wfile.write(bytes(json.dumps(
|
2022-08-31 04:33:42 +00:00
|
|
|
{'event': 'result', 'url': path, 'seed': seed, 'config': config}
|
2022-08-29 16:08:18 +00:00
|
|
|
) + '\n',"utf-8"))
|
|
|
|
|
|
|
|
# control state of the "postprocessing..." message
|
2022-09-09 02:16:29 +00:00
|
|
|
upscaling_requested = opt.upscale or opt.gfpgan_strength > 0
|
2022-08-29 16:08:18 +00:00
|
|
|
nonlocal images_generated # NB: Is this bad python style? It is typical usage in a perl closure.
|
|
|
|
nonlocal images_upscaled # NB: Is this bad python style? It is typical usage in a perl closure.
|
|
|
|
if upscaled:
|
|
|
|
images_upscaled += 1
|
|
|
|
else:
|
2022-09-09 02:16:29 +00:00
|
|
|
images_generated += 1
|
2022-08-29 16:08:18 +00:00
|
|
|
if upscaling_requested:
|
|
|
|
action = None
|
2022-09-09 02:16:29 +00:00
|
|
|
if images_generated >= opt.iterations:
|
|
|
|
if images_upscaled < opt.iterations:
|
2022-08-29 16:08:18 +00:00
|
|
|
action = 'upscaling-started'
|
|
|
|
else:
|
|
|
|
action = 'upscaling-done'
|
|
|
|
if action:
|
2022-09-09 02:16:29 +00:00
|
|
|
x = images_upscaled + 1
|
2022-08-29 16:08:18 +00:00
|
|
|
self.wfile.write(bytes(json.dumps(
|
2022-09-09 02:16:29 +00:00
|
|
|
{'event': action, 'processed_file_cnt': f'{x}/{opt.iterations}'}
|
2022-08-29 16:08:18 +00:00
|
|
|
) + '\n',"utf-8"))
|
2022-08-27 01:10:13 +00:00
|
|
|
|
2022-09-06 00:35:04 +00:00
|
|
|
step_writer = PngWriter(os.path.join(self.outdir, "intermediates"))
|
2022-08-31 04:21:04 +00:00
|
|
|
step_index = 1
|
2022-08-29 16:36:48 +00:00
|
|
|
def image_progress(sample, step):
|
2022-08-30 15:55:40 +00:00
|
|
|
if self.canceled.is_set():
|
|
|
|
self.wfile.write(bytes(json.dumps({'event':'canceled'}) + '\n', 'utf-8'))
|
|
|
|
raise CanceledException
|
2022-08-31 04:21:04 +00:00
|
|
|
path = None
|
2022-08-29 16:36:48 +00:00
|
|
|
# since rendering images is moderately expensive, only render every 5th image
|
|
|
|
# and don't bother with the last one, since it'll render anyway
|
2022-08-31 04:21:04 +00:00
|
|
|
nonlocal step_index
|
2022-09-09 02:16:29 +00:00
|
|
|
if opt.progress_images and step % 5 == 0 and step < opt.steps - 1:
|
2022-09-06 00:40:10 +00:00
|
|
|
image = self.model.sample_to_image(sample)
|
2022-09-09 02:16:29 +00:00
|
|
|
name = f'{prefix}.{opt.seed}.{step_index}.png'
|
|
|
|
metadata = f'{opt.prompt} -S{opt.seed} [intermediate]'
|
2022-08-31 04:21:04 +00:00
|
|
|
path = step_writer.save_image_and_prompt_to_png(image, metadata, name)
|
|
|
|
step_index += 1
|
2022-08-27 01:10:13 +00:00
|
|
|
self.wfile.write(bytes(json.dumps(
|
2022-08-31 04:21:04 +00:00
|
|
|
{'event': 'step', 'step': step + 1, 'url': path}
|
2022-08-27 01:10:13 +00:00
|
|
|
) + '\n',"utf-8"))
|
2022-08-28 21:27:43 +00:00
|
|
|
|
2022-08-30 15:55:40 +00:00
|
|
|
try:
|
2022-09-09 02:16:29 +00:00
|
|
|
if opt.init_img is None:
|
2022-08-30 15:55:40 +00:00
|
|
|
# Run txt2img
|
2022-09-09 02:16:29 +00:00
|
|
|
self.model.prompt2image(**vars(opt), step_callback=image_progress, image_callback=image_done)
|
2022-08-30 15:55:40 +00:00
|
|
|
else:
|
|
|
|
# Decode initimg as base64 to temp file
|
|
|
|
with open("./img2img-tmp.png", "wb") as f:
|
2022-09-09 02:16:29 +00:00
|
|
|
initimg = opt.init_img.split(",")[1] # Ignore mime type
|
2022-08-30 15:55:40 +00:00
|
|
|
f.write(base64.b64decode(initimg))
|
2022-09-09 02:16:29 +00:00
|
|
|
opt1 = argparse.Namespace(**vars(opt))
|
|
|
|
opt1.init_img = "./img2img-tmp.png"
|
2022-08-30 15:55:40 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Run img2img
|
2022-09-09 02:16:29 +00:00
|
|
|
self.model.prompt2image(**vars(opt1), step_callback=image_progress, image_callback=image_done)
|
2022-08-30 15:55:40 +00:00
|
|
|
finally:
|
|
|
|
# Remove the temp file
|
|
|
|
os.remove("./img2img-tmp.png")
|
|
|
|
except CanceledException:
|
|
|
|
print(f"Canceled.")
|
|
|
|
return
|
2022-09-16 11:33:42 +00:00
|
|
|
except Exception as e:
|
|
|
|
print("Error happened")
|
|
|
|
print(e)
|
|
|
|
self.wfile.write(bytes(json.dumps(
|
|
|
|
{'event': 'error',
|
|
|
|
'message': str(e),
|
|
|
|
'type': e.__class__.__name__}
|
|
|
|
) + '\n',"utf-8"))
|
|
|
|
raise e
|
2022-08-28 21:27:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ThreadingDreamServer(ThreadingHTTPServer):
|
|
|
|
def __init__(self, server_address):
|
|
|
|
super(ThreadingDreamServer, self).__init__(server_address, DreamServer)
|