phamngoctukts commited on
Commit
3b66cf0
1 Parent(s): 5c25115

Update render.py

Browse files
Files changed (1) hide show
  1. render.py +89 -90
render.py CHANGED
@@ -1,90 +1,89 @@
1
- import websocket # websocket-client
2
- import uuid
3
- import json
4
- import urllib.request
5
- import urllib.parse
6
- import random
7
- from PIL import Image
8
- import io
9
- import base64
10
- import io
11
- import os
12
-
13
- server_address = os.environ.get("URL_API")
14
- json_data=os.environ.get("JSON_API")
15
- client_id = str(uuid.uuid4())
16
-
17
- def queue_prompt(prompt):
18
- p = {"prompt": prompt, "client_id": client_id}
19
- data = json.dumps(p, indent=4).encode('utf-8') # Prettify JSON for print
20
- req = urllib.request.Request(f"http://{server_address}/prompt", data=data)
21
- return json.loads(urllib.request.urlopen(req).read())
22
-
23
- def get_image(filename, subfolder, folder_type):
24
- data = {"filename": filename, "subfolder": subfolder, "type": folder_type}
25
- url_values = urllib.parse.urlencode(data)
26
- with urllib.request.urlopen(f"http://{server_address}/view?{url_values}") as response:
27
- return response.read()
28
-
29
- def get_history(prompt_id):
30
- print(colored(f"Fetching history for prompt ID: {prompt_id}.", "cyan"))
31
- with urllib.request.urlopen(f"http://{server_address}/history/{prompt_id}") as response:
32
- return json.loads(response.read())
33
-
34
- def get_images(ws, prompt):
35
- prompt_id = queue_prompt(prompt)['prompt_id']
36
- output_images = {}
37
- last_reported_percentage = 0
38
- while True:
39
- out = ws.recv()
40
- if isinstance(out, str):
41
- message = json.loads(out)
42
- if message['type'] == 'progress':
43
- data = message['data']
44
- current_progress = data['value']
45
- max_progress = data['max']
46
- percentage = int((current_progress / max_progress) * 100)
47
- if percentage >= last_reported_percentage + 10:
48
- last_reported_percentage = percentage
49
-
50
- elif message['type'] == 'executing':
51
- data = message['data']
52
- if data['node'] is None and data['prompt_id'] == prompt_id:
53
- break # Execution is done
54
- else:
55
- continue # Previews are binary data
56
-
57
- history = get_history(prompt_id)[prompt_id]
58
- for o in history['outputs']:
59
- for node_id in history['outputs']:
60
- node_output = history['outputs'][node_id]
61
- if 'images' in node_output:
62
- images_output = []
63
- for image in node_output['images']:
64
- image_data = get_image(image['filename'], image['subfolder'], image['type'])
65
- images_output.append(image_data)
66
- output_images[node_id] = images_output
67
- return output_images
68
-
69
- def pil_to_base64(image):
70
- buffer = io.BytesIO()
71
- image.save(buffer, format="PNG")
72
- base64_string=base64.b64encode(buffer.getvalue()).decode("utf-8")
73
- return f"data:image/png;base64,{base64_string}"
74
-
75
- def generate_images(positive_prompt, image):
76
- ws = websocket.WebSocket()
77
- ws_url = f"ws://{server_address}/ws?clientId={client_id}"
78
- ws.connect(ws_url)
79
- data = json.loads(json_data)
80
- data["49"]["inputs"]["text"] = positive_prompt
81
- data["90"]["inputs"]["images"]["base64"] = [pil_to_base64(image)]
82
- seed = random.randint(1, 1000000000)
83
- data["47"]["inputs"]["noise_seed"] = seed
84
- images = get_images(ws, data)
85
- ws.close()
86
- for node_id in images:
87
- for image_data in images[node_id]:
88
- image = Image.open(io.BytesIO(image_data))
89
- return image
90
-
 
1
+ import websocket # websocket-client
2
+ import uuid
3
+ import json
4
+ import urllib.request
5
+ import urllib.parse
6
+ import random
7
+ from PIL import Image
8
+ import io
9
+ import base64
10
+ import io
11
+ import os
12
+
13
+ server_address = os.environ.get("URL_API")
14
+ json_data=os.environ.get("JSON_API")
15
+ client_id = str(uuid.uuid4())
16
+
17
+ def queue_prompt(prompt):
18
+ p = {"prompt": prompt, "client_id": client_id}
19
+ data = json.dumps(p, indent=4).encode('utf-8') # Prettify JSON for print
20
+ req = urllib.request.Request(f"http://{server_address}/prompt", data=data)
21
+ return json.loads(urllib.request.urlopen(req).read())
22
+
23
+ def get_image(filename, subfolder, folder_type):
24
+ data = {"filename": filename, "subfolder": subfolder, "type": folder_type}
25
+ url_values = urllib.parse.urlencode(data)
26
+ with urllib.request.urlopen(f"http://{server_address}/view?{url_values}") as response:
27
+ return response.read()
28
+
29
+ def get_history(prompt_id):
30
+ with urllib.request.urlopen(f"http://{server_address}/history/{prompt_id}") as response:
31
+ return json.loads(response.read())
32
+
33
+ def get_images(ws, prompt):
34
+ prompt_id = queue_prompt(prompt)['prompt_id']
35
+ output_images = {}
36
+ last_reported_percentage = 0
37
+ while True:
38
+ out = ws.recv()
39
+ if isinstance(out, str):
40
+ message = json.loads(out)
41
+ if message['type'] == 'progress':
42
+ data = message['data']
43
+ current_progress = data['value']
44
+ max_progress = data['max']
45
+ percentage = int((current_progress / max_progress) * 100)
46
+ if percentage >= last_reported_percentage + 10:
47
+ last_reported_percentage = percentage
48
+
49
+ elif message['type'] == 'executing':
50
+ data = message['data']
51
+ if data['node'] is None and data['prompt_id'] == prompt_id:
52
+ break # Execution is done
53
+ else:
54
+ continue # Previews are binary data
55
+
56
+ history = get_history(prompt_id)[prompt_id]
57
+ for o in history['outputs']:
58
+ for node_id in history['outputs']:
59
+ node_output = history['outputs'][node_id]
60
+ if 'images' in node_output:
61
+ images_output = []
62
+ for image in node_output['images']:
63
+ image_data = get_image(image['filename'], image['subfolder'], image['type'])
64
+ images_output.append(image_data)
65
+ output_images[node_id] = images_output
66
+ return output_images
67
+
68
+ def pil_to_base64(image):
69
+ buffer = io.BytesIO()
70
+ image.save(buffer, format="PNG")
71
+ base64_string=base64.b64encode(buffer.getvalue()).decode("utf-8")
72
+ return f"data:image/png;base64,{base64_string}"
73
+
74
+ def generate_images(positive_prompt, image):
75
+ ws = websocket.WebSocket()
76
+ ws_url = f"ws://{server_address}/ws?clientId={client_id}"
77
+ ws.connect(ws_url)
78
+ data = json.loads(json_data)
79
+ data["49"]["inputs"]["text"] = positive_prompt
80
+ data["90"]["inputs"]["images"]["base64"] = [pil_to_base64(image)]
81
+ seed = random.randint(1, 1000000000)
82
+ data["47"]["inputs"]["noise_seed"] = seed
83
+ images = get_images(ws, data)
84
+ ws.close()
85
+ for node_id in images:
86
+ for image_data in images[node_id]:
87
+ image = Image.open(io.BytesIO(image_data))
88
+ return image
89
+