signature change
Browse files
app.py
CHANGED
|
@@ -8,6 +8,10 @@ from io import BytesIO
|
|
| 8 |
from PIL import Image
|
| 9 |
import uuid
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
| 12 |
clothing_list = os.listdir(os.path.join(example_path, "clothing"))
|
| 13 |
clothing_list_path = [os.path.join(example_path, "clothing", clothing) for clothing in clothing_list]
|
|
@@ -23,7 +27,7 @@ query_task_url = os.getenv('query_task_url')
|
|
| 23 |
secret_key = os.getenv('secret_key')
|
| 24 |
agent_version = os.getenv('agent_version')
|
| 25 |
agent_name = os.getenv('agent_name')
|
| 26 |
-
|
| 27 |
|
| 28 |
def parse_response(response):
|
| 29 |
data = {}
|
|
@@ -67,12 +71,19 @@ def url_to_image(url, ip):
|
|
| 67 |
return None
|
| 68 |
|
| 69 |
|
| 70 |
-
def start_task(task_id,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
headers = {
|
| 72 |
'Did': did,
|
| 73 |
'X-Timestamp': timestamp,
|
| 74 |
'X-Signature': signature,
|
| 75 |
-
'X-Forwarded-For': ip
|
|
|
|
| 76 |
}
|
| 77 |
data = {
|
| 78 |
"agentVersion": agent_version,
|
|
@@ -85,12 +96,19 @@ def start_task(task_id, timestamp, signature, did, ip):
|
|
| 85 |
return data, msg
|
| 86 |
|
| 87 |
|
| 88 |
-
def create_task(image_url,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
headers = {
|
| 90 |
'Did': did,
|
| 91 |
'X-Timestamp': timestamp,
|
| 92 |
'X-Signature': signature,
|
| 93 |
-
'X-Forwarded-For': ip
|
|
|
|
| 94 |
}
|
| 95 |
data = {
|
| 96 |
"agentVersion": agent_version,
|
|
@@ -102,12 +120,19 @@ def create_task(image_url, timestamp, signature, did, ip):
|
|
| 102 |
return data, msg
|
| 103 |
|
| 104 |
|
| 105 |
-
def save_task(image_url,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
headers = {
|
| 107 |
'Did': did,
|
| 108 |
'X-Timestamp': timestamp,
|
| 109 |
'X-Signature': signature,
|
| 110 |
-
'X-Forwarded-For': ip
|
|
|
|
| 111 |
}
|
| 112 |
data = {
|
| 113 |
"agentVersion": agent_version,
|
|
@@ -122,12 +147,19 @@ def save_task(image_url, timestamp, signature, show_image, task_id, location_id,
|
|
| 122 |
return data, msg
|
| 123 |
|
| 124 |
|
| 125 |
-
def query_task(task_id, execution_id,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
headers = {
|
| 127 |
'Did': did,
|
| 128 |
'X-Timestamp': timestamp,
|
| 129 |
'X-Signature': signature,
|
| 130 |
-
'X-Forwarded-For': ip
|
|
|
|
| 131 |
}
|
| 132 |
data = {
|
| 133 |
"agentVersion": agent_version,
|
|
@@ -140,7 +172,7 @@ def query_task(task_id, execution_id, timestamp, signature, did, ip):
|
|
| 140 |
return data, msg
|
| 141 |
|
| 142 |
|
| 143 |
-
def upload_image(image,
|
| 144 |
if image is None:
|
| 145 |
return None
|
| 146 |
if upload_type == 'image':
|
|
@@ -149,6 +181,12 @@ def upload_image(image, signature, timestamp, upload_type, did, ip):
|
|
| 149 |
upload_url = upload_background_image_url
|
| 150 |
image_format = image.format if image.format else "PNG"
|
| 151 |
mime_type = f"image/{image_format.lower()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
with BytesIO() as m_img:
|
| 153 |
image.save(m_img, format=image_format)
|
| 154 |
m_img.seek(0)
|
|
@@ -157,7 +195,8 @@ def upload_image(image, signature, timestamp, upload_type, did, ip):
|
|
| 157 |
'Did': did,
|
| 158 |
'X-Timestamp': timestamp,
|
| 159 |
'X-Signature': signature,
|
| 160 |
-
'X-Forwarded-For': ip
|
|
|
|
| 161 |
}
|
| 162 |
response = requests.post(base_url + upload_url, files=files, headers=headers)
|
| 163 |
data, msg = parse_response(response)
|
|
@@ -182,16 +221,8 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 182 |
if x_forwarded_for:
|
| 183 |
client_ip = x_forwarded_for
|
| 184 |
|
| 185 |
-
timestamp = str(int(time.time()))
|
| 186 |
-
signature = generate_signature(
|
| 187 |
-
key=secret_key,
|
| 188 |
-
did=did,
|
| 189 |
-
timestamp=timestamp
|
| 190 |
-
)
|
| 191 |
upload_image_data, upload_image_msg = upload_image(
|
| 192 |
image=main_image,
|
| 193 |
-
signature=signature,
|
| 194 |
-
timestamp=timestamp,
|
| 195 |
upload_type='image',
|
| 196 |
did=did,
|
| 197 |
ip=client_ip
|
|
@@ -204,8 +235,6 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 204 |
return gr.Warning(m), did
|
| 205 |
create_task_data, create_task_msg = create_task(
|
| 206 |
image_url=image_url,
|
| 207 |
-
timestamp=timestamp,
|
| 208 |
-
signature=signature,
|
| 209 |
did=did,
|
| 210 |
ip=client_ip
|
| 211 |
)
|
|
@@ -218,8 +247,6 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 218 |
return gr.Warning(m), did
|
| 219 |
upload_image_data, upload_image_msg = upload_image(
|
| 220 |
image=background_image,
|
| 221 |
-
signature=signature,
|
| 222 |
-
timestamp=timestamp,
|
| 223 |
upload_type='background_image',
|
| 224 |
did=did,
|
| 225 |
ip=client_ip
|
|
@@ -229,8 +256,6 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 229 |
|
| 230 |
save_task_data, save_task_msg = save_task(
|
| 231 |
image_url=image_url,
|
| 232 |
-
timestamp=timestamp,
|
| 233 |
-
signature=signature,
|
| 234 |
show_image=show_image,
|
| 235 |
task_id=task_id,
|
| 236 |
location_id=upload_image_data,
|
|
@@ -245,8 +270,6 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 245 |
return gr.Warning('Save task failed'), did
|
| 246 |
start_task_data, start_task_msg = start_task(
|
| 247 |
task_id=save_task_id,
|
| 248 |
-
timestamp=timestamp,
|
| 249 |
-
signature=signature,
|
| 250 |
did=did,
|
| 251 |
ip=client_ip
|
| 252 |
)
|
|
@@ -262,8 +285,6 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 262 |
query_task_data, query_task_msg = query_task(
|
| 263 |
task_id=save_task_id,
|
| 264 |
execution_id=execution_id,
|
| 265 |
-
timestamp=timestamp,
|
| 266 |
-
signature=signature,
|
| 267 |
did=did,
|
| 268 |
ip=client_ip
|
| 269 |
)
|
|
|
|
| 8 |
from PIL import Image
|
| 9 |
import uuid
|
| 10 |
|
| 11 |
+
# from dotenv import load_dotenv
|
| 12 |
+
|
| 13 |
+
# load_dotenv()
|
| 14 |
+
|
| 15 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
| 16 |
clothing_list = os.listdir(os.path.join(example_path, "clothing"))
|
| 17 |
clothing_list_path = [os.path.join(example_path, "clothing", clothing) for clothing in clothing_list]
|
|
|
|
| 27 |
secret_key = os.getenv('secret_key')
|
| 28 |
agent_version = os.getenv('agent_version')
|
| 29 |
agent_name = os.getenv('agent_name')
|
| 30 |
+
app_id = os.getenv('app_id')
|
| 31 |
|
| 32 |
def parse_response(response):
|
| 33 |
data = {}
|
|
|
|
| 71 |
return None
|
| 72 |
|
| 73 |
|
| 74 |
+
def start_task(task_id, did, ip):
|
| 75 |
+
timestamp = str(int(time.time()))
|
| 76 |
+
signature = generate_signature(
|
| 77 |
+
key=secret_key,
|
| 78 |
+
did=did,
|
| 79 |
+
timestamp=timestamp
|
| 80 |
+
)
|
| 81 |
headers = {
|
| 82 |
'Did': did,
|
| 83 |
'X-Timestamp': timestamp,
|
| 84 |
'X-Signature': signature,
|
| 85 |
+
'X-Forwarded-For': ip,
|
| 86 |
+
'X-AppId': app_id,
|
| 87 |
}
|
| 88 |
data = {
|
| 89 |
"agentVersion": agent_version,
|
|
|
|
| 96 |
return data, msg
|
| 97 |
|
| 98 |
|
| 99 |
+
def create_task(image_url, did, ip):
|
| 100 |
+
timestamp = str(int(time.time()))
|
| 101 |
+
signature = generate_signature(
|
| 102 |
+
key=secret_key,
|
| 103 |
+
did=did,
|
| 104 |
+
timestamp=timestamp
|
| 105 |
+
)
|
| 106 |
headers = {
|
| 107 |
'Did': did,
|
| 108 |
'X-Timestamp': timestamp,
|
| 109 |
'X-Signature': signature,
|
| 110 |
+
'X-Forwarded-For': ip,
|
| 111 |
+
'X-AppId': app_id,
|
| 112 |
}
|
| 113 |
data = {
|
| 114 |
"agentVersion": agent_version,
|
|
|
|
| 120 |
return data, msg
|
| 121 |
|
| 122 |
|
| 123 |
+
def save_task(image_url, show_image, task_id, location_id, did, ip):
|
| 124 |
+
timestamp = str(int(time.time()))
|
| 125 |
+
signature = generate_signature(
|
| 126 |
+
key=secret_key,
|
| 127 |
+
did=did,
|
| 128 |
+
timestamp=timestamp
|
| 129 |
+
)
|
| 130 |
headers = {
|
| 131 |
'Did': did,
|
| 132 |
'X-Timestamp': timestamp,
|
| 133 |
'X-Signature': signature,
|
| 134 |
+
'X-Forwarded-For': ip,
|
| 135 |
+
'X-AppId': app_id,
|
| 136 |
}
|
| 137 |
data = {
|
| 138 |
"agentVersion": agent_version,
|
|
|
|
| 147 |
return data, msg
|
| 148 |
|
| 149 |
|
| 150 |
+
def query_task(task_id, execution_id, did, ip):
|
| 151 |
+
timestamp = str(int(time.time()))
|
| 152 |
+
signature = generate_signature(
|
| 153 |
+
key=secret_key,
|
| 154 |
+
did=did,
|
| 155 |
+
timestamp=timestamp
|
| 156 |
+
)
|
| 157 |
headers = {
|
| 158 |
'Did': did,
|
| 159 |
'X-Timestamp': timestamp,
|
| 160 |
'X-Signature': signature,
|
| 161 |
+
'X-Forwarded-For': ip,
|
| 162 |
+
'X-AppId': app_id,
|
| 163 |
}
|
| 164 |
data = {
|
| 165 |
"agentVersion": agent_version,
|
|
|
|
| 172 |
return data, msg
|
| 173 |
|
| 174 |
|
| 175 |
+
def upload_image(image, upload_type, did, ip):
|
| 176 |
if image is None:
|
| 177 |
return None
|
| 178 |
if upload_type == 'image':
|
|
|
|
| 181 |
upload_url = upload_background_image_url
|
| 182 |
image_format = image.format if image.format else "PNG"
|
| 183 |
mime_type = f"image/{image_format.lower()}"
|
| 184 |
+
timestamp = str(int(time.time()))
|
| 185 |
+
signature = generate_signature(
|
| 186 |
+
key=secret_key,
|
| 187 |
+
did=did,
|
| 188 |
+
timestamp=timestamp
|
| 189 |
+
)
|
| 190 |
with BytesIO() as m_img:
|
| 191 |
image.save(m_img, format=image_format)
|
| 192 |
m_img.seek(0)
|
|
|
|
| 195 |
'Did': did,
|
| 196 |
'X-Timestamp': timestamp,
|
| 197 |
'X-Signature': signature,
|
| 198 |
+
'X-Forwarded-For': ip,
|
| 199 |
+
'X-AppId': app_id,
|
| 200 |
}
|
| 201 |
response = requests.post(base_url + upload_url, files=files, headers=headers)
|
| 202 |
data, msg = parse_response(response)
|
|
|
|
| 221 |
if x_forwarded_for:
|
| 222 |
client_ip = x_forwarded_for
|
| 223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
upload_image_data, upload_image_msg = upload_image(
|
| 225 |
image=main_image,
|
|
|
|
|
|
|
| 226 |
upload_type='image',
|
| 227 |
did=did,
|
| 228 |
ip=client_ip
|
|
|
|
| 235 |
return gr.Warning(m), did
|
| 236 |
create_task_data, create_task_msg = create_task(
|
| 237 |
image_url=image_url,
|
|
|
|
|
|
|
| 238 |
did=did,
|
| 239 |
ip=client_ip
|
| 240 |
)
|
|
|
|
| 247 |
return gr.Warning(m), did
|
| 248 |
upload_image_data, upload_image_msg = upload_image(
|
| 249 |
image=background_image,
|
|
|
|
|
|
|
| 250 |
upload_type='background_image',
|
| 251 |
did=did,
|
| 252 |
ip=client_ip
|
|
|
|
| 256 |
|
| 257 |
save_task_data, save_task_msg = save_task(
|
| 258 |
image_url=image_url,
|
|
|
|
|
|
|
| 259 |
show_image=show_image,
|
| 260 |
task_id=task_id,
|
| 261 |
location_id=upload_image_data,
|
|
|
|
| 270 |
return gr.Warning('Save task failed'), did
|
| 271 |
start_task_data, start_task_msg = start_task(
|
| 272 |
task_id=save_task_id,
|
|
|
|
|
|
|
| 273 |
did=did,
|
| 274 |
ip=client_ip
|
| 275 |
)
|
|
|
|
| 285 |
query_task_data, query_task_msg = query_task(
|
| 286 |
task_id=save_task_id,
|
| 287 |
execution_id=execution_id,
|
|
|
|
|
|
|
| 288 |
did=did,
|
| 289 |
ip=client_ip
|
| 290 |
)
|