From 4122c6fbce08b1e3b9d38b8fe3a75edc693f2e75 Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Tue, 18 Jun 2024 08:37:14 -0400 Subject: [PATCH] fix api ip-adapter --- CHANGELOG.md | 3 ++- cli/api-json.py | 55 +++++++++++++++++++++++++++++++++++++++++++ cli/image-encode.py | 32 +++++++++++++++++++++++++ modules/api/models.py | 4 ++-- wiki | 2 +- 5 files changed, 92 insertions(+), 4 deletions(-) create mode 100755 cli/api-json.py create mode 100755 cli/image-encode.py diff --git a/CHANGELOG.md b/CHANGELOG.md index ce05525c3..363020797 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,6 @@ Other than SD3, highlight is (finally) new release of **Torch-DirectML** as well - fix unsaturated outputs, force apply vae config on model load - fix hidiffusion handling of non-square aspect ratios, thanks @ShenZhang-Shin! - fix control second pass resize -- fix api face-hires - fix **hunyuandit** set attention processor - fix civitai download without name - fix compatibility with latest adetailer @@ -54,6 +53,8 @@ Other than SD3, highlight is (finally) new release of **Torch-DirectML** as well - fix saving style without name provided - fix t2i-color adapter - fix sdxl "has been incorrectly initialized" +- fix api face-hires +- fix api ip-adapter - restructure api examples: `cli/api-*` - handle theme fallback when invalid theme is specified - remove obsolete training code leftovers diff --git a/cli/api-json.py b/cli/api-json.py new file mode 100755 index 000000000..889b70d01 --- /dev/null +++ b/cli/api-json.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +# curl -vX POST http://localhost:7860/sdapi/v1/txt2img --header "Content-Type: application/json" -d @3261.json +import os +import json +import logging +import argparse +import requests +import urllib3 + + +sd_url = os.environ.get('SDAPI_URL', "http://127.0.0.1:7860") +sd_username = os.environ.get('SDAPI_USR', None) +sd_password = os.environ.get('SDAPI_PWD', None) +options = { + "save_images": True, + "send_images": True, +} + +logging.basicConfig(level = logging.INFO, format = '%(asctime)s %(levelname)s: %(message)s') +log = logging.getLogger(__name__) +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) + + +def auth(): + if sd_username is not None and sd_password is not None: + return requests.auth.HTTPBasicAuth(sd_username, sd_password) + return None + + +def post(endpoint: str, payload: dict = None): + if 'sdapi' not in endpoint: + endpoint = f'sdapi/v1/{endpoint}' + if 'http' not in endpoint: + endpoint = f'{sd_url}/{endpoint}' + req = requests.post(endpoint, json = payload, timeout=300, verify=False, auth=auth()) + return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } if req.status_code != 200 else req.json() + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description = 'api-txt2img') + parser.add_argument('endpoint', nargs=1, help='endpoint') + parser.add_argument('json', nargs=1, help='json data or file') + args = parser.parse_args() + log.info(f'api-json: {args}') + if os.path.isfile(args.json[0]): + with open(args.json[0], 'r', encoding='ascii') as f: + txt = f.read() + txt = txt.encode('ascii') + print('HERE', txt) + dct = json.loads(txt) + else: + dct = json.loads(args.json[0]) + res = post(endpoint=args.endpoint[0], payload=dct) + print(res) diff --git a/cli/image-encode.py b/cli/image-encode.py new file mode 100755 index 000000000..0769c2544 --- /dev/null +++ b/cli/image-encode.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +import io +import os +import sys +import base64 +from PIL import Image +from rich import print # pylint: disable=redefined-builtin + + +def encode(file: str): + image = Image.open(file) if os.path.exists(file) else None + print(f'Input: file={file} image={image}') + if image is None: + return None + if image.mode != 'RGB': + image = image.convert('RGB') + with io.BytesIO() as stream: + image.save(stream, 'JPEG') + image.close() + values = stream.getvalue() + encoded = base64.b64encode(values).decode() + return encoded + + +if __name__ == "__main__": + sys.argv.pop(0) + fn = sys.argv[0] if len(sys.argv) > 0 else '' + b64 = encode(fn) + print('=== BEGIN ===') + print(f'{b64}') + print('=== END ===') + diff --git a/modules/api/models.py b/modules/api/models.py index a0cb2562d..8437e91ba 100644 --- a/modules/api/models.py +++ b/modules/api/models.py @@ -152,8 +152,8 @@ class ItemIPAdapter(BaseModel): adapter: str = Field(title="Adapter", default="Base", description="") images: List[str] = Field(title="Image", default=[], description="") masks: Optional[List[str]] = Field(title="Mask", default=[], description="") - scale: float = Field(title="Scale", default=0.5, gt=0, le=1, description="") - start: float = Field(title="Start", default=0.0, gt=0, le=1, description="") + scale: float = Field(title="Scale", default=0.5, ge=0, le=1, description="") + start: float = Field(title="Start", default=0.0, ge=0, le=1, description="") end: float = Field(title="End", default=1.0, gt=0, le=1, description="") class ItemFace(BaseModel): diff --git a/wiki b/wiki index 4e01da914..c5c9e8998 160000 --- a/wiki +++ b/wiki @@ -1 +1 @@ -Subproject commit 4e01da914a578ba0db26907da53640a49d7ecb2b +Subproject commit c5c9e89981c8bd35b51823315418a4a4864bb5e1