add gen-styles

This commit is contained in:
Vladimir Mandic
2023-10-04 14:30:58 -04:00
parent 30fe75f205
commit d95236c927
3 changed files with 72 additions and 2 deletions
+1 -1
View File
@@ -7,7 +7,7 @@
- Add FreeU for *backend:diffusers*
for *backend:original* use extension: <https://github.com/ljleb/sd-webui-freeu>
- Add HyperTile: <https://github.com/tfernd/HyperTile>
- Fix override settings
- Implement Styles extra field
This is a big one, with some major changes and new functionality...
And probably the biggest release since introduction of **Diffusers**
+70
View File
@@ -0,0 +1,70 @@
#!/bin/env python
import io
import sys
import json
import base64
import requests
from PIL import Image
url = 'http://127.0.0.1:7860'
size = 128
quality = 35
prompt = 'woman walking in a city'
options = {
"negative_prompt": "",
"steps": 20,
"batch_size": 1,
"n_iter": 1,
"seed": -1,
"sampler_name": "UniPC",
"cfg_scale": 6,
"width": 512,
"height": 512,
"save_images": False,
"send_images": True,
}
styles = []
def pil_to_b64(img: Image):
img = img.convert('RGB')
img = img.resize((size, size))
buffer = io.BytesIO()
img.save(buffer, format="JPEG", quality=quality)
b64encoded = base64.b64encode(buffer.getvalue()).decode("utf-8")
return f'data:image/jpeg;base64,{b64encoded}'
def post(endpoint: str, dct: dict = None):
req = requests.post(f'{url}{endpoint}', json = dct, timeout=300, verify=False)
if req.status_code != 200:
return { 'error': req.status_code, 'reason': req.reason, 'url': req.url }
else:
return req.json()
if __name__ == '__main__':
sys.argv.pop(0)
if len(sys.argv) < 2:
print('gen-styles.py <input text file> <output json file>')
sys.exit(1)
with open(sys.argv[0], encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
line = line.strip().replace('\n', '')
print(line)
options['prompt'] = f'{line} {prompt}'
data = post('/sdapi/v1/txt2img', options)
b64str = data['images'][0].split(',',1)[0]
image = Image.open(io.BytesIO(base64.b64decode(b64str)))
styles.append({
'name': line,
'prompt': line + ' {prompt}',
'negative': '',
'extra': '',
'preview': pil_to_b64(image)
})
with open(sys.argv[1], 'w', encoding='utf-8') as outfile:
json.dump(styles, outfile, indent=2)
+1 -1
View File
@@ -242,7 +242,7 @@ table.settings-value-table td { padding: 0.4em; border: 1px solid #ccc; max-widt
.extra-networks .search { flex: 1; }
.extra-networks .description { flex: 3; }
.extra-networks .tab-nav > button { margin-right: 0; height: 24px; padding: 2px 4px 2px 4px; }
.extra-networks .buttons { position: absolute; right: 0; margin: -4px; }
.extra-networks .buttons { position: absolute; right: 0; margin: -4px; background: var(--background-color); }
.extra-networks .custom-button { width: 120px; width: 100%; background: none; justify-content: left; text-align: left; padding: 2px 8px 2px 16px; text-indent: -8px; box-shadow: none; line-break: auto; }
.extra-networks .custom-button:hover { background: var(--button-primary-background-fill) }
.extra-networks-tab { padding: 0 !important; }