From e3bbbd1c150d30e6184314b45a37dde2797bbd19 Mon Sep 17 00:00:00 2001 From: yeeyou Date: Mon, 26 Jun 2023 08:54:11 +0800 Subject: [PATCH] Add API Usage --- README.md | 5 +++++ example/api_example.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 example/api_example.py diff --git a/README.md b/README.md index eb7db3e0..18f74113 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,11 @@ For rest of the errors, use google. Good luck. 1. Under "roop" drop-down menu, import an image containing a face. 2. Turn on the "Enable" checkbox 3. That's it, now the generated result will have the face you selected +You can use it with Webui API: +1. Check the [SD Web API Wiki](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/API) for how to use API +2. Call requests.get(url=f'{address}/sdapi/v1/script-info') to find the args that roop need. +3. Define roop script args and add like this "alwayson_scripts": {"roop":{"args":args}} in the payload. +4. Call the API, there's an [full usage example](./example/api_example.py) in example folder ## Tips #### Getting good quality results diff --git a/example/api_example.py b/example/api_example.py new file mode 100644 index 00000000..9aa5f350 --- /dev/null +++ b/example/api_example.py @@ -0,0 +1,35 @@ +import base64 +import io +import requests + +address = 'http://127.0.0.1:7860' +image_file = "path/to/local/image/file" +im = Image.open(image_file) + +img_bytes = io.BytesIO() +im.save(img_bytes, format='PNG') +img_base64 = base64.b64encode(img_bytes.getvalue()).decode('utf-8') + +# the most important is this one +args=[img_base64,True,'0','F:\stable-diffusion-webui\models/roop\inswapper_128.onnx','CodeFormer',1,None,1,'None',False,True] + +# The args for roop can be found by +# requests.get(url=f'{address}/sdapi/v1/script-info') + +prompt = "(8k, best quality, masterpiece, ultra highres:1.2),Realistic image style,Vertical orientation, Girl,White short hair,Shoulder-length, tousled White blossom pink hair,Clothing" +neg = "ng_deepnegative_v1_75t, (worst quality:2), (low quality:2), (normal quality:2), lowres, bad anatomy, normal quality, ((monochrome)), ((grayscale)), (verybadimagenegative_v1.3:0.8), negative_hand-neg, (lamp), badhandv4" +payload = { + "prompt": prompt, + "negative_prompt": neg, + "seed": -1, + "sampler_name": "DPM++ SDE Karras", + "steps": 20, + "cfg_scale": 7, + "width": 512, + "height": 768, + "restore_faces": True, + #"script_name":"roop", # this doesn't work + #"script_args":args, # this doesn't work + "alwayson_scripts": {"roop":{"args":args}} # This is the args. +} +result = requests.post(url=f'{address}/sdapi/v1/txt2img', json=payload) \ No newline at end of file