Skip to content

Commit

Permalink
fix(api): clamp im2img strength
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jan 8, 2023
1 parent 33fd5f1 commit 282a7cf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/onnx_web/__init__.py
@@ -1 +1 @@
from onnx_web.serve import get_and_clamp, get_from_map, get_latents_from_seed, load_pipeline
from onnx_web.serve import get_and_clamp_float, get_and_clamp_int, get_from_map, get_latents_from_seed, load_pipeline
17 changes: 11 additions & 6 deletions api/onnx_web/serve.py
Expand Up @@ -32,6 +32,7 @@
default_steps = 20
default_height = 512
default_width = 512
default_strength = 0.5

max_cfg = 30
max_steps = 150
Expand Down Expand Up @@ -71,7 +72,11 @@
}


def get_and_clamp(args, key, default_value, max_value, min_value=1):
def get_and_clamp_float(args, key, default_value, max_value, min_value=0.0):
return min(max(float(args.get(key, default_value)), min_value), max_value)


def get_and_clamp_int(args, key, default_value, max_value, min_value=1):
return min(max(int(args.get(key, default_value)), min_value), max_value)


Expand Down Expand Up @@ -207,10 +212,10 @@ def pipeline_from_request(pipeline):

# image params
prompt = request.args.get('prompt', default_prompt)
cfg = get_and_clamp(request.args, 'cfg', default_cfg, max_cfg, 0)
steps = get_and_clamp(request.args, 'steps', default_steps, max_steps)
height = get_and_clamp(request.args, 'height', default_height, max_height)
width = get_and_clamp(request.args, 'width', default_width, max_width)
cfg = get_and_clamp_int(request.args, 'cfg', default_cfg, max_cfg, 0)
steps = get_and_clamp_int(request.args, 'steps', default_steps, max_steps)
height = get_and_clamp_int(request.args, 'height', default_height, max_height)
width = get_and_clamp_int(request.args, 'width', default_width, max_width)

seed = int(request.args.get('seed', -1))
if seed == -1:
Expand All @@ -229,7 +234,7 @@ def img2img():
input_image = Image.open(BytesIO(input_file.read())).convert('RGB')
input_image.thumbnail((default_width, default_height))

strength = float(request.args.get('strength', 0.8))
strength = get_and_clamp_float(request.args, 'strength', 0.5, 1.0)

(model, provider, scheduler, prompt, cfg, steps, height,
width, seed, pipe) = pipeline_from_request(OnnxStableDiffusionImg2ImgPipeline)
Expand Down

0 comments on commit 282a7cf

Please sign in to comment.