-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsync_example_base.py
More file actions
148 lines (133 loc) · 4.33 KB
/
sync_example_base.py
File metadata and controls
148 lines (133 loc) · 4.33 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
"""
post example when deploy the service name as base
set --use_transalte and upload the translate model to post chinese prompt
translate model: https://www.modelscope.cn/models/damo/nlp_csanmt_translation_zh2en/summary
"""
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'
}
func_list = ['t2i','i2i','inpaint','outpaint']
def decode_base64(image_base64, save_file):
img = Image.open(BytesIO(base64.urlsafe_b64decode(image_base64)))
img.save(save_file)
def select_data(func_name):
if func_name == 't2i':
datas = json.dumps({
'task_id':
func_name,
'prompt':
'一只可爱的小猫',
'func_name':
func_name, # or default is t2i
'negative_prompt':
'NSFW',
'steps':
50,
'image_num':
1,
'width':
512,
'height':
512,
'lora_path':
['lora/animeLineartMangaLike_v30MangaLike.safetensors'],
'lora_attn':
1.0
})
elif func_name == 'i2i':
datas = json.dumps({
'image_link':
'https://converter-offline-installer.oss-cn-hangzhou.aliyuncs.com/zxy/image.png',
# 'image_base64': base64.b64encode(open('/mnt/xinyi.zxy/diffuser/models/bosi2/result/a001_20230602_075912_12.png', 'rb').read()).decode(ENCODING),
'task_id':
func_name,
'prompt':
'a cat',
'func_name':
func_name,
'negative_prompt':
'NSFW',
'steps':
50,
'image_num':
1,
'width':
512,
'height':
512,
'lora_path':
['lora/animeLineartMangaLike_v30MangaLike.safetensors']
})
elif func_name == 'inpaint':
datas = json.dumps({
'image_link':
'https://converter-offline-installer.oss-cn-hangzhou.aliyuncs.com/zxy/image.png',
'mask_link':
'https://converter-offline-installer.oss-cn-hangzhou.aliyuncs.com/zxy/mask.png',
'task_id':
func_name,
'prompt':
'a cat',
'func_name':
func_name,
'negative_prompt':
'NSFW',
'steps':
50,
'image_num':
1,
'width':
512,
'height':
512,
'lora_path':
['lora/animeLineartMangaLike_v30MangaLike.safetensors']
})
elif func_name == 'outpaint':
datas = json.dumps({
'image_link':
'https://converter-offline-installer.oss-cn-hangzhou.aliyuncs.com/zxy/image.png',
# 'image_base64': base64.b64encode(open('/mnt/xinyi.zxy/diffuser/models/bosi2/result/a001_20230602_075912_12.png', 'rb').read()).decode(ENCODING),
'task_id': func_name,
'prompt': 'a cat',
'func_name': func_name,
'negative_prompt': 'NSFW',
'steps': 50,
'image_num': 1,
'width': 512,
'height': 512,
'expand': [256, 256, 0, 0], # [left,right,up,down]
'expand_type': 'copy', # or 'reflect',
'denoising_strength': 0.6
# 'lora_path': ['path_to_your_lora_model']
})
else:
raise ValueError('Invalid process_func value')
return datas
for func_name in func_list:
datas = select_data(func_name)
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, './result_{}.png'.format(str(i)))
else:
print(data['error_msg'])