forked from piktid/superid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperid_api.py
284 lines (204 loc) · 9.1 KB
/
superid_api.py
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import json
import base64
import requests
from io import BytesIO
from requests_toolbelt import MultipartEncoder
from PIL import Image, ImageCms
# -----------READ/WRITE FUNCTIONS------------
def open_image_from_url(url):
response = requests.get(url, stream=True)
if not response.ok:
print(response)
image = Image.open(BytesIO(response.content))
return image
def open_image_from_path(path):
f = open(path, 'rb')
buffer = BytesIO(f.read())
image = Image.open(buffer)
return image
return BytesIO(response.content)
def im_2_B(image):
# Convert Image to buffer
buff = BytesIO()
if image.mode == 'CMYK':
image = ImageCms.profileToProfile(image, 'ISOcoated_v2_eci.icc', 'sRGB Color Space Profile.icm', renderingIntent=0, outputMode='RGB')
image.save(buff, format='PNG',icc_profile=image.info.get('icc_profile'))
img_str = buff.getvalue()
return img_str
def im_2_buffer(image):
# Convert Image to bytes
buff = BytesIO()
if image.mode == 'CMYK':
image = ImageCms.profileToProfile(image, 'ISOcoated_v2_eci.icc', 'sRGB Color Space Profile.icm', renderingIntent=0, outputMode='RGB')
image.save(buff, format='PNG',icc_profile=image.info.get('icc_profile'))
return buff
def b64_2_img(data):
# Convert Base64 to Image
buff = BytesIO(base64.b64decode(data))
return Image.open(buff)
def im_2_b64(image):
# Convert Image
buff = BytesIO()
image.save(buff, format='PNG')
img_str = base64.b64encode(buff.getvalue()).decode('utf-8')
return img_str
# ---QUICK UTILS---
def extract_link(data_list, id_image, id_project):
for item in data_list:
if item['data']['id_image'] == id_image and item['data']['id_project'] == id_project:
return item['data']['link']['l']
return None
# -----------PROCESSING FUNCTIONS------------
def start_call(email, password):
# Get token
URL_API = 'https://api.piktid.com/api'
print(f'Logging to: {URL_API}')
response = requests.post(URL_API+'/tokens', data={}, auth=(email, password))
response_json = json.loads(response.text)
ACCESS_TOKEN = response_json['access_token']
REFRESH_TOKEN = response_json['refresh_token']
return {'access_token': ACCESS_TOKEN, 'refresh_token': REFRESH_TOKEN, 'url_api': URL_API}
def refresh_call(TOKEN_DICTIONARY):
# Get token using only access and refresh tokens, no mail and psw
URL_API = TOKEN_DICTIONARY.get('url_api')
response = requests.put(URL_API+'/tokens', json=TOKEN_DICTIONARY)
response_json = json.loads(response.text)
ACCESS_TOKEN = response_json['access_token']
REFRESH_TOKEN = response_json['refresh_token']
return {'access_token': ACCESS_TOKEN, 'refresh_token': REFRESH_TOKEN, 'url_api': URL_API}
# UPLOAD
def upload_superid_call(src_img, TOKEN_DICTIONARY):
# Upload the image into PiktID's servers and get the id of the project and of the image
TOKEN = TOKEN_DICTIONARY.get('access_token', '')
URL_API = TOKEN_DICTIONARY.get('url_api')
src_img_B = im_2_buffer(src_img)
options = '0'
m = MultipartEncoder(
fields={'options': options,
'file': ('file', src_img_B, 'text/plain')}
)
response = requests.post(URL_API+'/upload_extra',
headers={
"Content-Type": m.content_type,
'Authorization': 'Bearer '+TOKEN},
data=m,
)
response_json = json.loads(response.text)
# print(response_json)
id_project = response_json.get('id_project')
id_image = response_json.get('id_image')
return id_project, id_image
def update_data_upscaling_call(data, PARAM_DICTIONARY):
# update the json data first
GUIDANCE_SCALE = PARAM_DICTIONARY.get('GUIDANCE_SCALE')
PROMPT_STRENGTH = PARAM_DICTIONARY.get('PROMPT_STRENGTH')
CONTROLNET_SCALE = PARAM_DICTIONARY.get('CONTROLNET_SCALE')
if GUIDANCE_SCALE is not None:
data.update({'guidance_scale': GUIDANCE_SCALE})
if PROMPT_STRENGTH is not None:
data.update({'prompt_strength': PROMPT_STRENGTH})
if CONTROLNET_SCALE is not None:
data.update({'controlnet_conditioning_scale': CONTROLNET_SCALE})
# extra options
FACE_FIXER = PARAM_DICTIONARY.get('FACE_FIXER')
DENOISE_INPUT = PARAM_DICTIONARY.get('DENOISE_INPUT')
OPTIONS_DICT = {}
if FACE_FIXER is not None:
OPTIONS_DICT = {**OPTIONS_DICT, 'face_enhancer': FACE_FIXER}
if DENOISE_INPUT is not None:
OPTIONS_DICT = {**OPTIONS_DICT, 'denoise_input': DENOISE_INPUT}
OPTIONS = json.dumps(OPTIONS_DICT)
extra_options = {'options': OPTIONS}
data.update(extra_options)
return data
def upscaling_call(PARAM_DICTIONARY, TOKEN_DICTIONARY):
id_project = PARAM_DICTIONARY.get('PROJECT_ID')
id_image = PARAM_DICTIONARY.get('IMAGE_ID')
seed = PARAM_DICTIONARY.get('SEED')
scale_factor = PARAM_DICTIONARY.get('SCALE_FACTOR')
upscaler_type = PARAM_DICTIONARY.get('UPSCALER_TYPE', '4')
upscaling_mode = PARAM_DICTIONARY.get('UPSCALING_MODE', 'super')
flag_email = PARAM_DICTIONARY.get('FLAG_EMAIL')
output_format = PARAM_DICTIONARY.get('OUTPUT_FORMAT')
data = {'id_project': id_project,
'id_image': id_image,
'scale_factor': scale_factor,
'upscaler_type': upscaler_type,
'upscaling_mode': upscaling_mode,
'flag_email': flag_email,
'output_format': output_format,
'seed': seed
}
data = update_data_upscaling_call(data, PARAM_DICTIONARY)
print(f'data to send to upscale: {data}')
TOKEN = TOKEN_DICTIONARY.get('access_token', '')
URL_API = TOKEN_DICTIONARY.get('url_api')
response = requests.post(URL_API+'/superid',
headers={'Authorization': 'Bearer '+TOKEN},
json=data,
)
response_json = json.loads(response.text)
return response_json
def upscaling_fast_call(PARAM_DICTIONARY, TOKEN_DICTIONARY):
id_project = PARAM_DICTIONARY.get('PROJECT_ID')
id_image = PARAM_DICTIONARY.get('IMAGE_ID')
seed = PARAM_DICTIONARY.get('SEED')
scale_factor = PARAM_DICTIONARY.get('SCALE_FACTOR')
flag_email = PARAM_DICTIONARY.get('FLAG_EMAIL')
output_format = PARAM_DICTIONARY.get('OUTPUT_FORMAT')
data = {'id_project': id_project,
'id_image': id_image,
'scale_factor': scale_factor,
'flag_email': flag_email,
'output_format': output_format,
'seed': seed
}
print(f'data to send to fast upscale: {data}')
TOKEN = TOKEN_DICTIONARY.get('access_token', '')
URL_API = TOKEN_DICTIONARY.get('url_api')
response = requests.post(URL_API+'/superid_fast',
headers={'Authorization': 'Bearer '+TOKEN},
json=data,
)
# print(response.content)
response_json = json.loads(response.text)
return response_json
# NOTIFICATION FUNCTIONS
def get_notification_call(PARAM_DICTIONARY, TOKEN_DICTIONARY):
TOKEN = TOKEN_DICTIONARY.get('access_token', '')
URL_API = TOKEN_DICTIONARY.get('url_api')
response = requests.post(URL_API+'/notification_by_name_json',
headers={'Authorization': 'Bearer '+TOKEN},
json={'name_list': 'superid'}
)
response_json = json.loads(response.text)
return response_json.get('notifications_list')
def get_superid_info(PARAM_DICTIONARY, TOKEN_DICTIONARY):
# get ETA and credits needed for upscaling
id_project = PARAM_DICTIONARY.get('PROJECT_ID')
id_image = PARAM_DICTIONARY.get('IMAGE_ID')
scale_factor = PARAM_DICTIONARY.get('SCALE_FACTOR')
upscaling_mode = PARAM_DICTIONARY.get('UPSCALING_MODE', 'super')
strength = PARAM_DICTIONARY.get('PROMPT_STRENGTH', '0.35')
data = {'id_project': id_project, 'id_image': id_image, 'scale_factor': scale_factor, 'upscaling_mode': upscaling_mode,
'strength': strength, 'num_inference_steps': 20}
TOKEN = TOKEN_DICTIONARY.get('access_token', '')
URL_API = TOKEN_DICTIONARY.get('url_api')
response = requests.get(URL_API+'/superid_info',
headers={'Authorization': 'Bearer '+TOKEN},
json=data,
)
response_json = json.loads(response.text)
eta = response_json.get('eta')
credits = response_json.get('required_credits')
width = response_json.get('width')
height = response_json.get('height')
return eta, credits, width, height
def get_superid_link(PARAM_DICTIONARY, TOKEN_DICTIONARY):
# get info on the upscaled image
id_project = PARAM_DICTIONARY.get('PROJECT_ID')
id_image = PARAM_DICTIONARY.get('IMAGE_ID')
# extract notifications
notifications_list = get_notification_call(PARAM_DICTIONARY, TOKEN_DICTIONARY)
link = extract_link(notifications_list, id_image, id_project)
return link