2023-01-05 05:39:50 +00:00
|
|
|
from diffusers import (
|
2023-01-07 21:05:29 +00:00
|
|
|
# schedulers
|
2023-01-05 05:39:50 +00:00
|
|
|
DDIMScheduler,
|
2023-01-05 23:23:37 +00:00
|
|
|
DDPMScheduler,
|
|
|
|
DPMSolverMultistepScheduler,
|
2023-01-08 14:19:24 +00:00
|
|
|
DPMSolverSinglestepScheduler,
|
2023-01-05 05:39:50 +00:00
|
|
|
EulerDiscreteScheduler,
|
|
|
|
EulerAncestralDiscreteScheduler,
|
2023-01-08 14:19:24 +00:00
|
|
|
HeunDiscreteScheduler,
|
|
|
|
KDPM2AncestralDiscreteScheduler,
|
|
|
|
KDPM2DiscreteScheduler,
|
|
|
|
KarrasVeScheduler,
|
2023-01-05 23:23:37 +00:00
|
|
|
LMSDiscreteScheduler,
|
|
|
|
PNDMScheduler,
|
2023-01-07 21:05:29 +00:00
|
|
|
# onnx
|
|
|
|
OnnxStableDiffusionPipeline,
|
|
|
|
OnnxStableDiffusionImg2ImgPipeline,
|
2023-01-09 00:11:34 +00:00
|
|
|
OnnxStableDiffusionInpaintPipeline,
|
2023-01-10 05:26:47 +00:00
|
|
|
# types
|
|
|
|
DiffusionPipeline,
|
2023-01-05 05:39:50 +00:00
|
|
|
)
|
2023-01-06 03:13:45 +00:00
|
|
|
from flask import Flask, jsonify, request, send_from_directory, url_for
|
2023-01-08 16:09:58 +00:00
|
|
|
from hashlib import sha256
|
2023-01-07 21:05:29 +00:00
|
|
|
from io import BytesIO
|
|
|
|
from PIL import Image
|
2023-01-08 16:24:21 +00:00
|
|
|
from struct import pack
|
2023-01-06 04:50:30 +00:00
|
|
|
from os import environ, makedirs, path, scandir
|
2023-01-10 04:58:37 +00:00
|
|
|
from typing import Any, Tuple, Union
|
|
|
|
|
|
|
|
import json
|
2023-01-05 06:44:28 +00:00
|
|
|
import numpy as np
|
2023-01-05 00:25:00 +00:00
|
|
|
|
2023-01-05 01:42:37 +00:00
|
|
|
# defaults
|
2023-01-07 21:05:29 +00:00
|
|
|
default_model = 'stable-diffusion-onnx-v1-5'
|
|
|
|
default_platform = 'amd'
|
|
|
|
default_scheduler = 'euler-a'
|
2023-01-05 05:59:45 +00:00
|
|
|
default_prompt = "a photo of an astronaut eating a hamburger"
|
2023-01-05 06:44:28 +00:00
|
|
|
default_cfg = 8
|
|
|
|
default_steps = 20
|
2023-01-05 05:59:45 +00:00
|
|
|
default_height = 512
|
|
|
|
default_width = 512
|
2023-01-08 15:17:23 +00:00
|
|
|
default_strength = 0.5
|
2023-01-05 05:59:45 +00:00
|
|
|
|
2023-01-05 06:44:28 +00:00
|
|
|
max_cfg = 30
|
|
|
|
max_steps = 150
|
2023-01-05 00:25:00 +00:00
|
|
|
max_height = 512
|
|
|
|
max_width = 512
|
|
|
|
|
2023-01-05 01:42:37 +00:00
|
|
|
# paths
|
2023-01-07 21:05:29 +00:00
|
|
|
model_path = environ.get('ONNX_WEB_MODEL_PATH', '../models')
|
|
|
|
output_path = environ.get('ONNX_WEB_OUTPUT_PATH', '../outputs')
|
2023-01-10 04:58:37 +00:00
|
|
|
params_path = environ.get('ONNX_WEB_PARAMS_PATH', './params.json')
|
2023-01-05 01:42:37 +00:00
|
|
|
|
2023-01-06 04:50:30 +00:00
|
|
|
|
|
|
|
# pipeline caching
|
|
|
|
available_models = []
|
2023-01-10 04:58:37 +00:00
|
|
|
config_params = {}
|
2023-01-06 05:28:25 +00:00
|
|
|
last_pipeline_instance = None
|
2023-01-07 21:05:29 +00:00
|
|
|
last_pipeline_options = (None, None, None)
|
2023-01-06 05:28:25 +00:00
|
|
|
last_pipeline_scheduler = None
|
2023-01-06 04:50:30 +00:00
|
|
|
|
|
|
|
# pipeline params
|
2023-01-05 23:24:14 +00:00
|
|
|
platform_providers = {
|
|
|
|
'amd': 'DmlExecutionProvider',
|
|
|
|
'cpu': 'CPUExecutionProvider',
|
2023-01-07 14:56:21 +00:00
|
|
|
'nvidia': 'CUDAExecutionProvider',
|
2023-01-05 23:24:14 +00:00
|
|
|
}
|
2023-01-05 23:23:37 +00:00
|
|
|
pipeline_schedulers = {
|
2023-01-06 04:50:30 +00:00
|
|
|
'ddim': DDIMScheduler,
|
|
|
|
'ddpm': DDPMScheduler,
|
|
|
|
'dpm-multi': DPMSolverMultistepScheduler,
|
2023-01-08 14:19:24 +00:00
|
|
|
'dpm-single': DPMSolverSinglestepScheduler,
|
2023-01-06 04:50:30 +00:00
|
|
|
'euler': EulerDiscreteScheduler,
|
|
|
|
'euler-a': EulerAncestralDiscreteScheduler,
|
2023-01-08 14:19:24 +00:00
|
|
|
'heun': HeunDiscreteScheduler,
|
|
|
|
'k-dpm-2-a': KDPM2AncestralDiscreteScheduler,
|
|
|
|
'k-dpm-2': KDPM2DiscreteScheduler,
|
|
|
|
'karras-ve': KarrasVeScheduler,
|
2023-01-06 04:50:30 +00:00
|
|
|
'lms-discrete': LMSDiscreteScheduler,
|
|
|
|
'pndm': PNDMScheduler,
|
2023-01-05 05:39:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 17:19:42 +00:00
|
|
|
|
2023-01-10 05:26:47 +00:00
|
|
|
def get_and_clamp_float(args, key: str, default_value: float, max_value: float, min_value=0.0) -> float:
|
2023-01-08 15:17:23 +00:00
|
|
|
return min(max(float(args.get(key, default_value)), min_value), max_value)
|
|
|
|
|
|
|
|
|
2023-01-10 05:26:47 +00:00
|
|
|
def get_and_clamp_int(args, key: str, default_value: int, max_value: int, min_value=1) -> int:
|
2023-01-05 17:19:42 +00:00
|
|
|
return min(max(int(args.get(key, default_value)), min_value), max_value)
|
|
|
|
|
2023-01-05 05:59:45 +00:00
|
|
|
|
2023-01-10 05:26:47 +00:00
|
|
|
def get_from_map(args, key: str, values: dict[str, Any], default: Any):
|
2023-01-05 17:19:42 +00:00
|
|
|
selected = args.get(key, default)
|
|
|
|
if selected in values:
|
|
|
|
return values[selected]
|
|
|
|
else:
|
|
|
|
return values[default]
|
|
|
|
|
2023-01-05 00:25:00 +00:00
|
|
|
|
2023-01-08 18:46:52 +00:00
|
|
|
def get_model_path(model: str):
|
2023-01-07 21:05:29 +00:00
|
|
|
return safer_join(model_path, model)
|
|
|
|
|
|
|
|
|
2023-01-06 04:50:30 +00:00
|
|
|
# from https://www.travelneil.com/stable-diffusion-updates.html
|
2023-01-05 06:44:28 +00:00
|
|
|
def get_latents_from_seed(seed: int, width: int, height: int) -> np.ndarray:
|
|
|
|
# 1 is batch size
|
2023-01-05 17:19:42 +00:00
|
|
|
latents_shape = (1, 4, height // 8, width // 8)
|
2023-01-05 06:44:28 +00:00
|
|
|
# Gotta use numpy instead of torch, because torch's randn() doesn't support DML
|
2023-01-05 17:19:42 +00:00
|
|
|
rng = np.random.default_rng(seed)
|
|
|
|
image_latents = rng.standard_normal(latents_shape).astype(np.float32)
|
|
|
|
return image_latents
|
|
|
|
|
2023-01-05 06:44:28 +00:00
|
|
|
|
2023-01-10 05:26:47 +00:00
|
|
|
def load_pipeline(pipeline: DiffusionPipeline, model: str, provider: str, scheduler):
|
2023-01-06 05:28:25 +00:00
|
|
|
global last_pipeline_instance
|
|
|
|
global last_pipeline_scheduler
|
|
|
|
global last_pipeline_options
|
2023-01-06 04:50:30 +00:00
|
|
|
|
2023-01-07 21:05:29 +00:00
|
|
|
options = (pipeline, model, provider)
|
2023-01-06 05:28:25 +00:00
|
|
|
if last_pipeline_instance != None and last_pipeline_options == options:
|
2023-01-06 04:50:30 +00:00
|
|
|
print('reusing existing pipeline')
|
2023-01-06 05:28:25 +00:00
|
|
|
pipe = last_pipeline_instance
|
|
|
|
else:
|
|
|
|
print('loading different pipeline')
|
2023-01-07 21:05:29 +00:00
|
|
|
pipe = pipeline.from_pretrained(
|
2023-01-06 05:28:25 +00:00
|
|
|
model,
|
|
|
|
provider=provider,
|
|
|
|
safety_checker=None,
|
2023-01-07 21:05:29 +00:00
|
|
|
scheduler=scheduler.from_pretrained(model, subfolder='scheduler')
|
2023-01-06 05:28:25 +00:00
|
|
|
)
|
|
|
|
last_pipeline_instance = pipe
|
|
|
|
last_pipeline_options = options
|
|
|
|
last_pipeline_scheduler = scheduler
|
|
|
|
|
|
|
|
if last_pipeline_scheduler != scheduler:
|
|
|
|
print('changing pipeline scheduler')
|
2023-01-07 04:20:14 +00:00
|
|
|
pipe.scheduler = scheduler.from_pretrained(
|
2023-01-07 21:05:29 +00:00
|
|
|
model, subfolder='scheduler')
|
2023-01-06 05:28:25 +00:00
|
|
|
last_pipeline_scheduler = scheduler
|
|
|
|
|
2023-01-06 04:50:30 +00:00
|
|
|
return pipe
|
|
|
|
|
|
|
|
|
2023-01-06 03:54:40 +00:00
|
|
|
def json_with_cors(data, origin='*'):
|
2023-01-06 04:50:30 +00:00
|
|
|
"""Build a JSON response with CORS headers allowing `origin`"""
|
2023-01-06 03:54:40 +00:00
|
|
|
res = jsonify(data)
|
|
|
|
res.access_control_allow_origin = origin
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2023-01-10 05:26:47 +00:00
|
|
|
def make_output_path(type: str, seed: int, params: Tuple[Union[str, int, float]]):
|
2023-01-08 16:09:58 +00:00
|
|
|
sha = sha256()
|
2023-01-08 18:46:52 +00:00
|
|
|
sha.update(type.encode('utf-8'))
|
2023-01-08 16:09:58 +00:00
|
|
|
for param in params:
|
2023-01-08 16:24:21 +00:00
|
|
|
if isinstance(param, str):
|
|
|
|
sha.update(param.encode('utf-8'))
|
|
|
|
elif isinstance(param, int):
|
2023-01-08 20:26:53 +00:00
|
|
|
sha.update(bytearray(pack('!I', param)))
|
2023-01-08 16:24:21 +00:00
|
|
|
elif isinstance(param, float):
|
|
|
|
sha.update(bytearray(pack('!f', param)))
|
|
|
|
else:
|
|
|
|
print('cannot hash param: %s, %s' % (param, type(param)))
|
2023-01-08 16:09:58 +00:00
|
|
|
|
2023-01-10 05:26:47 +00:00
|
|
|
output_file = '%s_%s_%s.png' % (type, seed, sha.hexdigest())
|
2023-01-08 16:09:58 +00:00
|
|
|
output_full = safer_join(output_path, output_file)
|
|
|
|
|
|
|
|
return (output_file, output_full)
|
|
|
|
|
|
|
|
|
2023-01-06 05:28:25 +00:00
|
|
|
def safer_join(base, tail):
|
|
|
|
safer_path = path.relpath(path.normpath(path.join('/', tail)), '/')
|
|
|
|
return path.join(base, safer_path)
|
|
|
|
|
|
|
|
|
2023-01-06 03:13:45 +00:00
|
|
|
def url_from_rule(rule):
|
|
|
|
options = {}
|
|
|
|
for arg in rule.arguments:
|
|
|
|
options[arg] = ":%s" % (arg)
|
|
|
|
|
|
|
|
return url_for(rule.endpoint, **options)
|
|
|
|
|
2023-01-05 01:42:37 +00:00
|
|
|
# setup
|
2023-01-05 05:59:45 +00:00
|
|
|
|
|
|
|
|
2023-01-06 04:50:30 +00:00
|
|
|
def check_paths():
|
|
|
|
if not path.exists(model_path):
|
|
|
|
raise RuntimeError('model path must exist')
|
|
|
|
|
|
|
|
if not path.exists(output_path):
|
|
|
|
makedirs(output_path)
|
|
|
|
|
|
|
|
|
|
|
|
def load_models():
|
|
|
|
global available_models
|
|
|
|
available_models = [f.name for f in scandir(model_path) if f.is_dir()]
|
|
|
|
|
|
|
|
|
2023-01-10 04:58:37 +00:00
|
|
|
def load_params():
|
|
|
|
global config_params
|
|
|
|
with open(params_path) as f:
|
|
|
|
config_params = json.load(f)
|
|
|
|
|
|
|
|
|
2023-01-06 04:50:30 +00:00
|
|
|
check_paths()
|
|
|
|
load_models()
|
2023-01-10 04:58:37 +00:00
|
|
|
load_params()
|
2023-01-05 01:42:37 +00:00
|
|
|
app = Flask(__name__)
|
2023-01-05 00:25:00 +00:00
|
|
|
|
2023-01-05 01:42:37 +00:00
|
|
|
# routes
|
2023-01-05 17:19:42 +00:00
|
|
|
|
|
|
|
|
2023-01-05 00:25:00 +00:00
|
|
|
@app.route('/')
|
2023-01-06 03:54:40 +00:00
|
|
|
def index():
|
2023-01-06 03:13:45 +00:00
|
|
|
return {
|
|
|
|
'name': 'onnx-web',
|
|
|
|
'routes': [{
|
|
|
|
'path': url_from_rule(rule),
|
2023-01-06 17:00:20 +00:00
|
|
|
'methods': list(rule.methods).sort()
|
2023-01-06 03:13:45 +00:00
|
|
|
} for rule in app.url_map.iter_rules()]
|
|
|
|
}
|
2023-01-05 00:25:00 +00:00
|
|
|
|
2023-01-05 17:19:42 +00:00
|
|
|
|
2023-01-06 04:01:58 +00:00
|
|
|
@app.route('/settings/models')
|
|
|
|
def list_models():
|
2023-01-06 04:50:30 +00:00
|
|
|
return json_with_cors(available_models)
|
2023-01-06 04:01:58 +00:00
|
|
|
|
|
|
|
|
2023-01-10 04:58:37 +00:00
|
|
|
@app.route('/settings/params')
|
|
|
|
def list_params():
|
|
|
|
return json_with_cors(config_params)
|
|
|
|
|
|
|
|
|
2023-01-06 03:54:40 +00:00
|
|
|
@app.route('/settings/platforms')
|
|
|
|
def list_platforms():
|
|
|
|
return json_with_cors(list(platform_providers.keys()))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/settings/schedulers')
|
|
|
|
def list_schedulers():
|
|
|
|
return json_with_cors(list(pipeline_schedulers.keys()))
|
|
|
|
|
|
|
|
|
2023-01-10 05:26:47 +00:00
|
|
|
def pipeline_from_request(pipeline: DiffusionPipeline):
|
2023-01-05 17:19:42 +00:00
|
|
|
user = request.remote_addr
|
|
|
|
|
2023-01-06 04:50:30 +00:00
|
|
|
# pipeline stuff
|
2023-01-07 21:05:29 +00:00
|
|
|
model = get_model_path(request.args.get('model', default_model))
|
2023-01-06 04:50:30 +00:00
|
|
|
provider = get_from_map(request.args, 'platform',
|
2023-01-07 04:20:14 +00:00
|
|
|
platform_providers, default_platform)
|
2023-01-05 17:19:42 +00:00
|
|
|
scheduler = get_from_map(request.args, 'scheduler',
|
2023-01-07 04:20:14 +00:00
|
|
|
pipeline_schedulers, default_scheduler)
|
2023-01-06 04:50:30 +00:00
|
|
|
|
|
|
|
# image params
|
|
|
|
prompt = request.args.get('prompt', default_prompt)
|
2023-01-10 05:26:47 +00:00
|
|
|
negative_prompt = request.args.get('negative', None)
|
2023-01-08 19:05:02 +00:00
|
|
|
|
2023-01-10 05:26:47 +00:00
|
|
|
cfg = get_and_clamp_int(request.args, 'cfg', default_cfg, config_params.get('cfg').get('max'), 0)
|
|
|
|
steps = get_and_clamp_int(request.args, 'steps', default_steps, config_params.get('steps').get('max'))
|
|
|
|
height = get_and_clamp_int(
|
|
|
|
request.args, 'height', default_height, config_params.get('height').get('max'))
|
|
|
|
width = get_and_clamp_int(request.args, 'width', default_width, config_params.get('width').get('max'))
|
2023-01-05 17:19:42 +00:00
|
|
|
|
|
|
|
seed = int(request.args.get('seed', -1))
|
|
|
|
if seed == -1:
|
|
|
|
seed = np.random.randint(np.iinfo(np.int32).max)
|
|
|
|
|
2023-01-07 21:05:29 +00:00
|
|
|
print("request from %s: %s rounds of %s using %s on %s, %sx%s, %s, %s - %s" %
|
2023-01-06 05:28:25 +00:00
|
|
|
(user, steps, scheduler.__name__, model, provider, width, height, cfg, seed, prompt))
|
2023-01-05 17:19:42 +00:00
|
|
|
|
2023-01-07 21:05:29 +00:00
|
|
|
pipe = load_pipeline(pipeline, model, provider, scheduler)
|
2023-01-08 19:05:02 +00:00
|
|
|
return (model, provider, scheduler, prompt, negative_prompt, cfg, steps, height, width, seed, pipe)
|
2023-01-07 21:05:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/img2img', methods=['POST'])
|
|
|
|
def img2img():
|
|
|
|
input_file = request.files.get('source')
|
|
|
|
input_image = Image.open(BytesIO(input_file.read())).convert('RGB')
|
|
|
|
input_image.thumbnail((default_width, default_height))
|
|
|
|
|
2023-01-08 15:17:23 +00:00
|
|
|
strength = get_and_clamp_float(request.args, 'strength', 0.5, 1.0)
|
2023-01-07 21:19:24 +00:00
|
|
|
|
2023-01-08 19:05:02 +00:00
|
|
|
(model, provider, scheduler, prompt, negative_prompt, cfg, steps, height,
|
2023-01-07 21:05:29 +00:00
|
|
|
width, seed, pipe) = pipeline_from_request(OnnxStableDiffusionImg2ImgPipeline)
|
|
|
|
|
2023-01-08 15:31:22 +00:00
|
|
|
rng = np.random.RandomState(seed)
|
2023-01-07 21:05:29 +00:00
|
|
|
image = pipe(
|
2023-01-08 19:05:02 +00:00
|
|
|
prompt,
|
|
|
|
generator=rng,
|
|
|
|
guidance_scale=cfg,
|
2023-01-07 21:05:29 +00:00
|
|
|
image=input_image,
|
2023-01-08 19:05:02 +00:00
|
|
|
negative_prompt=negative_prompt,
|
2023-01-07 21:05:29 +00:00
|
|
|
num_inference_steps=steps,
|
|
|
|
strength=strength,
|
|
|
|
).images[0]
|
|
|
|
|
2023-01-10 04:58:37 +00:00
|
|
|
(output_file, output_full) = make_output_path('img2img', seed,
|
|
|
|
(prompt, cfg, negative_prompt, steps, strength, height, width))
|
2023-01-07 21:05:29 +00:00
|
|
|
print("img2img output: %s" % output_full)
|
|
|
|
image.save(output_full)
|
|
|
|
|
|
|
|
return json_with_cors({
|
|
|
|
'output': output_file,
|
|
|
|
'params': {
|
|
|
|
'model': model,
|
|
|
|
'provider': provider,
|
|
|
|
'scheduler': scheduler.__name__,
|
2023-01-10 04:58:37 +00:00
|
|
|
'seed': seed,
|
|
|
|
'prompt': prompt,
|
2023-01-07 21:05:29 +00:00
|
|
|
'cfg': cfg,
|
2023-01-10 04:58:37 +00:00
|
|
|
'negativePrompt': negative_prompt,
|
2023-01-07 21:05:29 +00:00
|
|
|
'steps': steps,
|
|
|
|
'height': default_height,
|
|
|
|
'width': default_width,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/txt2img', methods=['POST'])
|
|
|
|
def txt2img():
|
2023-01-08 19:05:02 +00:00
|
|
|
(model, provider, scheduler, prompt, negative_prompt, cfg, steps, height,
|
2023-01-07 21:05:29 +00:00
|
|
|
width, seed, pipe) = pipeline_from_request(OnnxStableDiffusionPipeline)
|
|
|
|
|
|
|
|
latents = get_latents_from_seed(seed, width, height)
|
2023-01-08 15:31:22 +00:00
|
|
|
rng = np.random.RandomState(seed)
|
2023-01-07 21:05:29 +00:00
|
|
|
|
2023-01-05 17:19:42 +00:00
|
|
|
image = pipe(
|
|
|
|
prompt,
|
|
|
|
height,
|
|
|
|
width,
|
2023-01-08 19:05:02 +00:00
|
|
|
generator=rng,
|
2023-01-05 17:19:42 +00:00
|
|
|
guidance_scale=cfg,
|
2023-01-08 15:31:22 +00:00
|
|
|
latents=latents,
|
2023-01-08 19:05:02 +00:00
|
|
|
negative_prompt=negative_prompt,
|
|
|
|
num_inference_steps=steps,
|
2023-01-05 17:19:42 +00:00
|
|
|
).images[0]
|
|
|
|
|
2023-01-10 04:58:37 +00:00
|
|
|
(output_file, output_full) = make_output_path('txt2img',
|
|
|
|
seed, (prompt, cfg, negative_prompt, steps, height, width))
|
2023-01-06 02:32:46 +00:00
|
|
|
print("txt2img output: %s" % output_full)
|
|
|
|
image.save(output_full)
|
|
|
|
|
2023-01-06 03:54:40 +00:00
|
|
|
return json_with_cors({
|
2023-01-06 02:32:46 +00:00
|
|
|
'output': output_file,
|
|
|
|
'params': {
|
2023-01-06 16:43:58 +00:00
|
|
|
'model': model,
|
|
|
|
'provider': provider,
|
|
|
|
'scheduler': scheduler.__name__,
|
2023-01-10 04:58:37 +00:00
|
|
|
'seed': seed,
|
|
|
|
'prompt': prompt,
|
2023-01-06 02:32:46 +00:00
|
|
|
'cfg': cfg,
|
2023-01-10 04:58:37 +00:00
|
|
|
'negativePrompt': negative_prompt,
|
2023-01-06 02:32:46 +00:00
|
|
|
'steps': steps,
|
|
|
|
'height': height,
|
|
|
|
'width': width,
|
|
|
|
}
|
|
|
|
})
|
2023-01-05 23:24:33 +00:00
|
|
|
|
2023-01-06 02:32:46 +00:00
|
|
|
|
2023-01-09 00:11:34 +00:00
|
|
|
@app.route('/inpaint', methods=['POST'])
|
|
|
|
def inpaint():
|
|
|
|
source_file = request.files.get('source')
|
|
|
|
source_image = Image.open(BytesIO(source_file.read())).convert('RGB')
|
|
|
|
source_image.thumbnail((default_width, default_height))
|
|
|
|
|
|
|
|
mask_file = request.files.get('mask')
|
|
|
|
mask_image = Image.open(BytesIO(mask_file.read())).convert('RGB')
|
|
|
|
mask_image.thumbnail((default_width, default_height))
|
|
|
|
|
|
|
|
(model, provider, scheduler, prompt, negative_prompt, cfg, steps, height,
|
|
|
|
width, seed, pipe) = pipeline_from_request(OnnxStableDiffusionInpaintPipeline)
|
|
|
|
|
2023-01-09 00:16:51 +00:00
|
|
|
latents = get_latents_from_seed(seed, width, height)
|
2023-01-09 00:11:34 +00:00
|
|
|
rng = np.random.RandomState(seed)
|
2023-01-09 00:16:51 +00:00
|
|
|
|
2023-01-09 00:11:34 +00:00
|
|
|
image = pipe(
|
|
|
|
prompt,
|
|
|
|
generator=rng,
|
|
|
|
guidance_scale=cfg,
|
2023-01-09 00:16:51 +00:00
|
|
|
height=height,
|
2023-01-09 00:11:34 +00:00
|
|
|
image=source_image,
|
2023-01-09 00:16:51 +00:00
|
|
|
latents=latents,
|
2023-01-09 00:11:34 +00:00
|
|
|
mask_image=mask_image,
|
|
|
|
negative_prompt=negative_prompt,
|
|
|
|
num_inference_steps=steps,
|
2023-01-09 00:16:51 +00:00
|
|
|
width=width,
|
2023-01-09 00:11:34 +00:00
|
|
|
).images[0]
|
|
|
|
|
2023-01-10 04:58:37 +00:00
|
|
|
(output_file, output_full) = make_output_path(
|
|
|
|
'inpaint', (prompt, cfg, steps, height, width, seed))
|
2023-01-09 00:11:34 +00:00
|
|
|
print("inpaint output: %s" % output_full)
|
|
|
|
image.save(output_full)
|
|
|
|
|
|
|
|
return json_with_cors({
|
|
|
|
'output': output_file,
|
|
|
|
'params': {
|
|
|
|
'model': model,
|
|
|
|
'provider': provider,
|
|
|
|
'scheduler': scheduler.__name__,
|
2023-01-10 04:58:37 +00:00
|
|
|
'seed': seed,
|
|
|
|
'prompt': prompt,
|
2023-01-09 00:11:34 +00:00
|
|
|
'cfg': cfg,
|
2023-01-10 04:58:37 +00:00
|
|
|
'negativePrompt': negative_prompt,
|
2023-01-09 00:11:34 +00:00
|
|
|
'steps': steps,
|
|
|
|
'height': default_height,
|
|
|
|
'width': default_width,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2023-01-05 23:24:33 +00:00
|
|
|
@app.route('/output/<path:filename>')
|
2023-01-10 05:26:47 +00:00
|
|
|
def output(filename: str):
|
2023-01-06 23:11:02 +00:00
|
|
|
return send_from_directory(path.join('..', output_path), filename, as_attachment=False)
|