-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsync_example_control.py
More file actions
172 lines (154 loc) · 5.65 KB
/
sync_example_control.py
File metadata and controls
172 lines (154 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
post example when deploy the service name as controlnet
"""
import base64
import json
import os
import sys
from io import BytesIO
import requests
from PIL import Image, PngImagePlugin
ENCODING = 'utf-8'
hosts = 'http://xxx.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/service_name'
head = {
'Authorization': 'xxx'
}
def decode_base64(image_base64, save_file):
img = Image.open(BytesIO(base64.urlsafe_b64decode(image_base64)))
img.save(save_file)
def select_data(process_func):
if process_func == 'canny':
datas = json.dumps({
'task_id': 'canny',
'steps': 50,
'image_num': 1,
'width': 512,
'height': 512,
'image_link':
'https://huggingface.co/lllyasviel/sd-controlnet-hed/resolve/main/images/man.png',
'prompt': 'man',
'process_func': 'canny',
})
elif process_func == 'depth':
datas = json.dumps({
'task_id': 'depth',
'steps': 50,
'image_num': 1,
'width': 512,
'height': 512,
'image_link':
'https://huggingface.co/lllyasviel/sd-controlnet-depth/resolve/main/images/stormtrooper.png',
'prompt': "Stormtrooper's lecture",
'controlnet_path':
'new_controlnet/models--lllyasviel--sd-controlnet-depth/diffusion_pytorch_model.safetensors', # use to change the controlnet path
'process_func': 'depth',
})
elif process_func == 'hed':
datas = json.dumps({
'task_id': 'hed',
'steps': 50,
'image_num': 1,
'width': 512,
'height': 512,
'image_link':
'https://huggingface.co/lllyasviel/sd-controlnet-hed/resolve/main/images/man.png',
'prompt': 'oil painting of handsome old man, masterpiece',
'controlnet_path':
'new_controlnet/models--lllyasviel--sd-controlnet-hed/diffusion_pytorch_model.safetensors',
'process_func': 'hed',
})
elif process_func == 'mlsd':
datas = json.dumps({
'task_id': 'mlsd',
'steps': 50,
'image_num': 1,
'width': 512,
'height': 512,
'image_link':
'https://huggingface.co/lllyasviel/sd-controlnet-mlsd/resolve/main/images/room.png',
'prompt': 'room',
'controlnet_path':
'new_controlnet/models--lllyasviel--sd-controlnet-mlsd/diffusion_pytorch_model.safetensors',
'process_func': 'mlsd',
})
elif process_func == 'normal':
datas = json.dumps({
'task_id': 'normal',
'steps': 50,
'image_num': 1,
'width': 512,
'height': 512,
'image_link':
'https://huggingface.co/lllyasviel/sd-controlnet-normal/resolve/main/images/toy.png',
'prompt': 'cute toy',
'controlnet_path':
'new_controlnet/models--fusing--stable-diffusion-v1-5-controlnet-normal/diffusion_pytorch_model.safetensors',
'process_func': 'normal',
})
elif process_func == 'openpose':
datas = json.dumps({
'task_id': 'openpose',
'steps': 50,
'image_num': 1,
'width': 512,
'height': 512,
'image_link':
'https://huggingface.co/lllyasviel/sd-controlnet-openpose/resolve/main/images/pose.png',
'prompt': 'chef in the kitchen',
'controlnet_path':
'new_controlnet/models--lllyasviel--sd-controlnet-openpose/diffusion_pytorch_model.safetensors',
'process_func': 'openpose',
})
elif process_func == 'scribble':
datas = json.dumps({
'task_id': 'scribble',
'steps': 50,
'image_num': 1,
'width': 512,
'height': 512,
'image_link':
'https://huggingface.co/lllyasviel/sd-controlnet-scribble/resolve/main/images/bag.png',
'prompt': 'bag',
'controlnet_path':
'new_controlnet/models--lllyasviel--sd-controlnet-scribble/diffusion_pytorch_model.safetensors',
'process_func': 'scribble',
})
elif process_func == 'seg':
datas = json.dumps({
'task_id': 'seg',
'steps': 50,
'image_num': 1,
'width': 512,
'height': 512,
'image_link':
'https://huggingface.co/lllyasviel/sd-controlnet-seg/resolve/main/images/house.png',
'prompt': 'house',
'controlnet_path':
'new_controlnet/models--lllyasviel--sd-controlnet-seg/diffusion_pytorch_model.safetensors',
'process_func': 'seg',
})
else:
raise ValueError('Invalid process_func value')
return datas
process_func_list = [
'canny', 'depth', 'hed', 'mlsd', 'normal', 'openpose', 'scribble', 'seg'
]
for process_func in process_func_list:
datas = select_data(process_func)
r = requests.post(hosts, data=datas, headers=head)
# r = requests.post("http://0.0.0.0:8000/test", data=datas, timeout=1500)
data = json.loads(r.content.decode('utf-8'))
print(data.keys())
if data['success']:
print(data['image_url'])
print(data['oss_url'])
print(data['task_id'])
print(data['use_blade'])
print(data['seed'])
print(data['is_nsfw'])
if 'images_base64' in data.keys():
for i, image_base64 in enumerate(data['images_base64']):
decode_base64(image_base64,
'./decode_ldm_base64_{}.png'.format(str(i)))
else:
print(data['error_msg'])