-
Notifications
You must be signed in to change notification settings - Fork 24
/
sdapi.py
executable file
·65 lines (58 loc) · 2.09 KB
/
sdapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
# -*- coding: utf-8 -*-
## record.py
##
## Hands-free voice audio recording to mp3, wav, other types
##
## Usage: sdapi.py [prompt] [output image]
##
## Copyright 2023 Henry Kroll <[email protected]>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
## MA 02110-1301, USA.
##
import sys
import json
import requests
import io
import base64
from PIL import Image
# Requires stable-diffusion web UI, optionally configured for low memory usage
# re. https://techtactician.com/stable-diffusion-low-vram-memory-errors-fix/
# Start it with --api option, e.g.: webui.sh --api --medvram
url = "http://127.0.0.1:7860"
def draw(prompt, output="output.png"):
payload = {
"prompt": prompt,
"steps": 1,
"cfg_scale": 1
}
try:
response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
r = response.json()
image = Image.open(io.BytesIO(base64.b64decode(r['images'][0])))
image.save(output)
image.show()
except Exception as e:
sys.stderr.write("SD API had a problem. Here's the error message.")
sys.stderr.write(str(e))
if __name__ == '__main__':
# Draw an image from a prompt supplied on the command line.
if len(sys.argv) == 2:
draw(sys.argv[1])
if len(sys.argv) == 3: # Provide a name for the image.
draw(sys.argv[1], sys.argv[2])
else:
sys.stderr.write(f"Usage: {sys.argv[0]} \"a horse riding an elephant\" horse_phant.png")