Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API Usage Example #101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions example/api_example.py
Original file line number Diff line number Diff line change
@@ -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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be useful to add a comment to each of these arguments to explain what it actually does.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes


# 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)