48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
|
import random
|
||
|
import websocket #NOTE: websocket-client (https://github.com/websocket-client/websocket-client)
|
||
|
import uuid
|
||
|
import json
|
||
|
import urllib.request
|
||
|
import urllib.parse
|
||
|
|
||
|
server_address = "127.0.0.1:8188"
|
||
|
|
||
|
def queue_prompt(prompt, client_id):
|
||
|
p = {"prompt": prompt, "client_id": client_id}
|
||
|
data = json.dumps(p).encode('utf-8')
|
||
|
req = urllib.request.Request("http://{}/prompt".format(server_address), data=data)
|
||
|
return json.loads(urllib.request.urlopen(req).read())
|
||
|
|
||
|
|
||
|
def get_image(filename, subfolder, folder_type):
|
||
|
data = {"filename": filename, "subfolder": subfolder, "type": folder_type}
|
||
|
url_values = urllib.parse.urlencode(data)
|
||
|
with urllib.request.urlopen("http://{}/view?{}".format(server_address, url_values)) as response:
|
||
|
return response.read()
|
||
|
|
||
|
def get_history(prompt_id):
|
||
|
with urllib.request.urlopen("http://{}/history/{}".format(server_address, prompt_id)) as response:
|
||
|
return json.loads(response.read())
|
||
|
|
||
|
def get_images(ws, prompt, client_id):
|
||
|
prompt_id = queue_prompt(prompt, client_id)['prompt_id']
|
||
|
output_images = {}
|
||
|
current_node = ""
|
||
|
while True:
|
||
|
out = ws.recv()
|
||
|
if isinstance(out, str):
|
||
|
message = json.loads(out)
|
||
|
if message['type'] == 'executing':
|
||
|
data = message['data']
|
||
|
if data['prompt_id'] == prompt_id:
|
||
|
if data['node'] is None:
|
||
|
break #Execution is done
|
||
|
else:
|
||
|
current_node = data['node']
|
||
|
else:
|
||
|
if current_node == 'save_image_websocket_node':
|
||
|
images_output = output_images.get(current_node, [])
|
||
|
images_output.append(out[8:])
|
||
|
output_images[current_node] = images_output
|
||
|
|
||
|
return output_images
|