Spaces:
Sleeping
Sleeping
phamngoctukts
commited on
Commit
•
3b66cf0
1
Parent(s):
5c25115
Update render.py
Browse files
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 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
out
|
40 |
-
|
41 |
-
message
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
percentage
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
data
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
image_data
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
buffer =
|
71 |
-
|
72 |
-
base64_string
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
ws_url
|
78 |
-
|
79 |
-
data =
|
80 |
-
data["
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
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 |
+
|
|