feat(api): use hash of params as output filename
This commit is contained in:
parent
067a9b3981
commit
e82379c61f
|
@ -17,6 +17,7 @@ from diffusers import (
|
||||||
OnnxStableDiffusionImg2ImgPipeline,
|
OnnxStableDiffusionImg2ImgPipeline,
|
||||||
)
|
)
|
||||||
from flask import Flask, jsonify, request, send_from_directory, url_for
|
from flask import Flask, jsonify, request, send_from_directory, url_for
|
||||||
|
from hashlib import sha256
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from stringcase import spinalcase
|
from stringcase import spinalcase
|
||||||
|
@ -139,6 +140,18 @@ def json_with_cors(data, origin='*'):
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def make_output_path(type, params):
|
||||||
|
sha = sha256()
|
||||||
|
sha.update(type)
|
||||||
|
for param in params:
|
||||||
|
sha.update(param)
|
||||||
|
|
||||||
|
output_file = 'txt2img_%s_%s.png' % (params[0], sha.hexdigest())
|
||||||
|
output_full = safer_join(output_path, output_file)
|
||||||
|
|
||||||
|
return (output_file, output_full)
|
||||||
|
|
||||||
|
|
||||||
def safer_join(base, tail):
|
def safer_join(base, tail):
|
||||||
safer_path = path.relpath(path.normpath(path.join('/', tail)), '/')
|
safer_path = path.relpath(path.normpath(path.join('/', tail)), '/')
|
||||||
return path.join(base, safer_path)
|
return path.join(base, safer_path)
|
||||||
|
@ -249,8 +262,7 @@ def img2img():
|
||||||
generator=rng,
|
generator=rng,
|
||||||
).images[0]
|
).images[0]
|
||||||
|
|
||||||
output_file = 'img2img_%s_%s.png' % (seed, spinalcase(prompt[0:64]))
|
(output_file, output_full) = make_output_path('img2img', (prompt, cfg, steps, height, width, seed))
|
||||||
output_full = safer_join(output_path, output_file)
|
|
||||||
print("img2img output: %s" % output_full)
|
print("img2img output: %s" % output_full)
|
||||||
image.save(output_full)
|
image.save(output_full)
|
||||||
|
|
||||||
|
@ -288,8 +300,7 @@ def txt2img():
|
||||||
generator=rng,
|
generator=rng,
|
||||||
).images[0]
|
).images[0]
|
||||||
|
|
||||||
output_file = 'txt2img_%s_%s.png' % (seed, spinalcase(prompt[0:64]))
|
(output_file, output_full) = make_output_path('txt2img', (prompt, cfg, steps, height, width, seed))
|
||||||
output_full = safer_join(output_path, output_file)
|
|
||||||
print("txt2img output: %s" % output_full)
|
print("txt2img output: %s" % output_full)
|
||||||
image.save(output_full)
|
image.save(output_full)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue