Skip to content

Commit

Permalink
lint(api): helper to get borders from request
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jan 16, 2023
1 parent 53fcc0b commit 604cdf3
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions api/onnx_web/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ def pipeline_from_request() -> Tuple[BaseParams, Size]:
return (params, size)


def border_from_request() -> Border:
left = get_and_clamp_int(request.args, 'left', 0,
config_params.get('width').get('max'), 0)
right = get_and_clamp_int(request.args, 'right',
0, config_params.get('width').get('max'), 0)
top = get_and_clamp_int(request.args, 'top', 0,
config_params.get('height').get('max'), 0)
bottom = get_and_clamp_int(
request.args, 'bottom', 0, config_params.get('height').get('max'), 0)

return Border(left, right, top, bottom)


def check_paths():
if not path.exists(model_path):
raise RuntimeError('model path must exist')
Expand Down Expand Up @@ -264,14 +277,14 @@ def img2img():
source_file = request.files.get('source')
source_image = Image.open(BytesIO(source_file.read())).convert('RGB')

params, size = pipeline_from_request()

strength = get_and_clamp_float(
request.args,
'strength',
config_params.get('strength').get('default'),
config_params.get('strength').get('max'))

params, size = pipeline_from_request()

output = make_output_name(
'img2img',
params,
Expand Down Expand Up @@ -320,16 +333,7 @@ def inpaint():
mask_image = Image.open(BytesIO(mask_file.read())).convert('RGB')

params, size = pipeline_from_request()

left = get_and_clamp_int(request.args, 'left', 0,
config_params.get('width').get('max'), 0)
right = get_and_clamp_int(request.args, 'right',
0, config_params.get('width').get('max'), 0)
top = get_and_clamp_int(request.args, 'top', 0,
config_params.get('height').get('max'), 0)
bottom = get_and_clamp_int(
request.args, 'bottom', 0, config_params.get('height').get('max'), 0)
expand = Border(left, right, top, bottom)
expand = border_from_request()

mask_filter = get_from_map(request.args, 'filter', mask_filters, 'none')
noise_source = get_from_map(
Expand All @@ -341,10 +345,10 @@ def inpaint():
params,
size,
extras=(
left,
right,
top,
bottom,
expand.left,
expand.right,
expand.top,
expand.bottom,
mask_filter.__name__,
noise_source.__name__,
)
Expand Down

0 comments on commit 604cdf3

Please sign in to comment.