Spaces:
Running
Running
Download source/studio/horde.py from Ethscriptions/eths: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/spaces/Ethscriptions/eths/resolve/main/source/studio/horde.py
- Command line
-
hf download hf://spaces/Ethscriptions/eths/source/studio/horde.py
-
curl -L -o horde.py https://huggingface.co/spaces/Ethscriptions/eths/resolve/main/source/studio/horde.py
2.64 kB
| import json, sys, time, urllib.request, base64 | |
| API = 'https://stablehorde.net/api/v2' | |
| H = {'apikey': '0000000000', 'Content-Type': 'application/json', 'Client-Agent': 'live-words:1.0:anon'} | |
| def req(path, body=None): | |
| r = urllib.request.Request(API + path, data=json.dumps(body).encode() if body else None, headers=H, method='POST' if body else 'GET') | |
| return json.load(urllib.request.urlopen(r, timeout=60)) | |
| def gen(prompt, out, model, w, h, seed, steps=8, cfg=1.0, sampler='k_euler'): | |
| body = {'prompt': prompt, 'params': {'width': w, 'height': h, 'steps': steps, 'cfg_scale': cfg, 'sampler_name': sampler, 'seed': str(seed), 'n': 1}, | |
| 'models': [model], 'r2': True, 'nsfw': False, 'censor_nsfw': True, 'slow_workers': True} | |
| j = req('/generate/async', body); rid = j['id']; print('queued', rid, j.get('kudos'), flush=True) | |
| while True: | |
| time.sleep(6) | |
| s = req('/generate/check/' + rid) | |
| if s.get('done'): break | |
| if s.get('faulted'): raise RuntimeError('faulted') | |
| print('wait', s.get('queue_position'), s.get('wait_time'), flush=True) | |
| st = req('/generate/status/' + rid) | |
| g = st['generations'][0] | |
| url = g['img'] | |
| data = urllib.request.urlopen(url, timeout=120).read() if url.startswith('http') else base64.b64decode(url) | |
| open(out, 'wb').write(data); print('saved', out, g.get('model'), g.get('worker_name'), flush=True) | |
| if __name__ == '__main__': | |
| gen(sys.argv[1], sys.argv[2], sys.argv[3], int(sys.argv[4]), int(sys.argv[5]), int(sys.argv[6])) | |
| def gen_pp(prompt, out, seed, model='Z-Image-Turbo', w=768, h=1024): | |
| body = {'prompt': prompt, 'params': {'width': w, 'height': h, 'steps': 8, 'cfg_scale': 1.0, 'sampler_name': 'k_euler', | |
| 'seed': str(seed), 'n': 1, 'post_processing': ['RealESRGAN_x2plus']}, | |
| 'models': [model], 'r2': True, 'nsfw': False, 'censor_nsfw': True, 'slow_workers': True} | |
| j = req('/generate/async', body); rid = j['id']; print('queued', out, j.get('kudos'), flush=True) | |
| t0 = time.time() | |
| while True: | |
| time.sleep(8) | |
| s = req('/generate/check/' + rid) | |
| if s.get('done'): break | |
| if s.get('faulted') or (not s.get('is_possible', True)): raise RuntimeError('faulted/impossible') | |
| if time.time() - t0 > 900: raise RuntimeError('timeout') | |
| st = req('/generate/status/' + rid) | |
| g = st['generations'][0] | |
| url = g['img'] | |
| data = urllib.request.urlopen(url, timeout=120).read() if url.startswith('http') else base64.b64decode(url) | |
| open(out, 'wb').write(data); print('saved', out, g.get('model'), g.get('worker_name'), round(time.time()-t0), flush=True) | |