From bc02d6668da7f85f515110b2f81910e892831fae Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Tue, 16 May 2023 10:52:38 -0400 Subject: [PATCH] update cli script examples --- cli/simple-img2txt.py | 62 ++++++++++++++++++++++++++++ cli/{simple.py => simple-txt2txt.py} | 0 modules/api/api.py | 1 + modules/scripts.py | 2 + 4 files changed, 65 insertions(+) create mode 100755 cli/simple-img2txt.py rename cli/{simple.py => simple-txt2txt.py} (100%) diff --git a/cli/simple-img2txt.py b/cli/simple-img2txt.py new file mode 100755 index 000000000..ceb89fd81 --- /dev/null +++ b/cli/simple-img2txt.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +import io +import sys +import base64 +import logging +import requests +from PIL import Image + +logging.basicConfig(level = logging.INFO, format = '%(asctime)s %(levelname)s: %(message)s') +log = logging.getLogger(__name__) +sd_url = "http://127.0.0.1:7860" +options = { + "init_images": [], + "prompt": "city at night", + "negative_prompt": "foggy, blurry", + "steps": 1, + "batch_size": 1, + "n_iter": 1, + "seed": -1, + "sampler_name": "Euler a", + "cfg_scale": 6, + "width": 512, + "height": 512, + "save_images": False, + "send_images": True, +} + +def post(endpoint: str, dct: dict = None): + req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300) + if req.status_code != 200: + return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } + else: + return req.json() + +def encode(f): + image = Image.open(f) + if image.mode == 'RGBA': + image = image.convert('RGB') + with io.BytesIO() as stream: + image.save(stream, 'JPEG') + values = stream.getvalue() + encoded = base64.b64encode(values).decode() + return encoded + +def generate(num: int = 0): + log.info(f'sending generate request: {num+1} {options}') + options['init_images'] = [encode('../html/logo.png')] + data = post('/sdapi/v1/img2img', options) + if 'images' in data: + for i in range(len(data['images'])): + b64 = data['images'][i].split(',',1)[0] + image = Image.open(io.BytesIO(base64.b64decode(b64))) + log.info(f'received image: {image.size}') + else: + log.warning(f'no images received: {data}') + +if __name__ == "__main__": + sys.argv.pop(0) + repeats = int(''.join(sys.argv) or '1') + log.info(f'repeats: {repeats}') + for n in range(repeats): + generate(n) diff --git a/cli/simple.py b/cli/simple-txt2txt.py similarity index 100% rename from cli/simple.py rename to cli/simple-txt2txt.py diff --git a/modules/api/api.py b/modules/api/api.py index 95c4bf094..a9bd608c2 100644 --- a/modules/api/api.py +++ b/modules/api/api.py @@ -57,6 +57,7 @@ def decode_base64_to_image(encoding): image = Image.open(BytesIO(base64.b64decode(encoding))) return image except Exception as e: + shared.log.warning(f'API cannot decode image: {e}') raise HTTPException(status_code=500, detail="Invalid encoded image") from e def encode_pil_to_base64(image): diff --git a/modules/scripts.py b/modules/scripts.py index 8735a30a7..3c168d2d5 100644 --- a/modules/scripts.py +++ b/modules/scripts.py @@ -370,8 +370,10 @@ class ScriptRunner: log.debug(f'Script process: {[s.title() for s in self.alwayson_scripts]}') for script in self.alwayson_scripts: try: + # log.debug(f'Script process start: {script.title()}') args = p.per_script_args.get(script.title(), p.script_args[script.args_from:script.args_to]) script.process(p, *args, **kwargs) + # log.debug(f'Script process end : {script.title()}') except Exception as e: errors.display(e, f'Running script process: {script.filename}')