authentication
Browse files- app.py +43 -21
- assets/title.html +1 -1
app.py
CHANGED
|
@@ -61,10 +61,12 @@ def generate_signature(key, did, timestamp):
|
|
| 61 |
return h.hexdigest()
|
| 62 |
|
| 63 |
|
| 64 |
-
def url_to_image(url, ip):
|
| 65 |
headers = {
|
| 66 |
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
| 67 |
-
'X-Forwarded-For': ip
|
|
|
|
|
|
|
| 68 |
}
|
| 69 |
try:
|
| 70 |
response = requests.get(url, headers=headers, timeout=30)
|
|
@@ -76,7 +78,7 @@ def url_to_image(url, ip):
|
|
| 76 |
return None
|
| 77 |
|
| 78 |
|
| 79 |
-
def start_task(task_id, did, ip):
|
| 80 |
timestamp = str(int(time.time()))
|
| 81 |
signature = generate_signature(
|
| 82 |
key=secret_key,
|
|
@@ -89,6 +91,8 @@ def start_task(task_id, did, ip):
|
|
| 89 |
'X-Signature': signature,
|
| 90 |
'X-Forwarded-For': ip,
|
| 91 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 92 |
}
|
| 93 |
data = {
|
| 94 |
"agentVersion": agent_version,
|
|
@@ -101,7 +105,7 @@ def start_task(task_id, did, ip):
|
|
| 101 |
return data, msg
|
| 102 |
|
| 103 |
|
| 104 |
-
def create_task(image_url, did, ip):
|
| 105 |
timestamp = str(int(time.time()))
|
| 106 |
signature = generate_signature(
|
| 107 |
key=secret_key,
|
|
@@ -114,6 +118,8 @@ def create_task(image_url, did, ip):
|
|
| 114 |
'X-Signature': signature,
|
| 115 |
'X-Forwarded-For': ip,
|
| 116 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 117 |
}
|
| 118 |
data = {
|
| 119 |
"agentVersion": agent_version,
|
|
@@ -125,7 +131,7 @@ def create_task(image_url, did, ip):
|
|
| 125 |
return data, msg
|
| 126 |
|
| 127 |
|
| 128 |
-
def query_task(task_id, execution_id, did, ip):
|
| 129 |
timestamp = str(int(time.time()))
|
| 130 |
signature = generate_signature(
|
| 131 |
key=secret_key,
|
|
@@ -138,6 +144,8 @@ def query_task(task_id, execution_id, did, ip):
|
|
| 138 |
'X-Signature': signature,
|
| 139 |
'X-Forwarded-For': ip,
|
| 140 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 141 |
}
|
| 142 |
data = {
|
| 143 |
"agentVersion": agent_version,
|
|
@@ -150,7 +158,7 @@ def query_task(task_id, execution_id, did, ip):
|
|
| 150 |
return data, msg
|
| 151 |
|
| 152 |
|
| 153 |
-
def upload_image(image, did, ip):
|
| 154 |
if image is None:
|
| 155 |
return None
|
| 156 |
image_format = image.format if image.format else "PNG"
|
|
@@ -171,13 +179,15 @@ def upload_image(image, did, ip):
|
|
| 171 |
'X-Signature': signature,
|
| 172 |
'X-Forwarded-For': ip,
|
| 173 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 174 |
}
|
| 175 |
response = requests.post(base_url + upload_image_url, files=files, headers=headers)
|
| 176 |
data, msg = parse_response(response)
|
| 177 |
return data, msg
|
| 178 |
|
| 179 |
|
| 180 |
-
def mask_image_save(task_id, mask, did, ip):
|
| 181 |
timestamp = str(int(time.time()))
|
| 182 |
signature = generate_signature(
|
| 183 |
key=secret_key,
|
|
@@ -190,6 +200,8 @@ def mask_image_save(task_id, mask, did, ip):
|
|
| 190 |
'X-Signature': signature,
|
| 191 |
'X-Forwarded-For': ip,
|
| 192 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 193 |
}
|
| 194 |
data = {
|
| 195 |
"taskId": task_id,
|
|
@@ -228,16 +240,16 @@ def image_to_base64(image):
|
|
| 228 |
|
| 229 |
def check_login_status(headers):
|
| 230 |
if not headers:
|
| 231 |
-
return
|
| 232 |
|
| 233 |
try:
|
| 234 |
text = headers.get(login_status_key)
|
| 235 |
if not text or "." not in text:
|
| 236 |
-
return
|
| 237 |
|
| 238 |
infos = text.split(".")
|
| 239 |
if len(infos) < 2:
|
| 240 |
-
return
|
| 241 |
|
| 242 |
info = infos[1]
|
| 243 |
cover = len(info) % 4
|
|
@@ -250,22 +262,22 @@ def check_login_status(headers):
|
|
| 250 |
|
| 251 |
data = datas.get(login_info_key)
|
| 252 |
if not data:
|
| 253 |
-
return
|
| 254 |
|
| 255 |
user_id = data.get("_id")
|
| 256 |
user_name = data.get("user")
|
| 257 |
-
return
|
| 258 |
|
| 259 |
except Exception as e:
|
| 260 |
print(f"An error occurred: {repr(e)}")
|
| 261 |
-
return
|
| 262 |
|
| 263 |
|
| 264 |
def generate_image(edit_image_infos, did, request: gr.Request):
|
| 265 |
if not did:
|
| 266 |
did = str(uuid.uuid4())
|
| 267 |
-
|
| 268 |
-
if not
|
| 269 |
m = "Please log in to your Hugging Face account to use the features of this application."
|
| 270 |
return gr.Warning(m), did
|
| 271 |
if edit_image_infos is None or not isinstance(edit_image_infos, dict):
|
|
@@ -296,7 +308,9 @@ def generate_image(edit_image_infos, did, request: gr.Request):
|
|
| 296 |
upload_image_data, upload_image_msg = upload_image(
|
| 297 |
image=main_image,
|
| 298 |
did=did,
|
| 299 |
-
ip=client_ip
|
|
|
|
|
|
|
| 300 |
)
|
| 301 |
if not upload_image_data:
|
| 302 |
return gr.Warning(upload_image_msg), did
|
|
@@ -307,7 +321,9 @@ def generate_image(edit_image_infos, did, request: gr.Request):
|
|
| 307 |
create_task_data, create_task_msg = create_task(
|
| 308 |
image_url=image_url,
|
| 309 |
did=did,
|
| 310 |
-
ip=client_ip
|
|
|
|
|
|
|
| 311 |
)
|
| 312 |
if not create_task_data:
|
| 313 |
return gr.Warning(create_task_msg), did
|
|
@@ -320,7 +336,9 @@ def generate_image(edit_image_infos, did, request: gr.Request):
|
|
| 320 |
task_id=task_id,
|
| 321 |
mask=image_to_base64(mask_image),
|
| 322 |
did=did,
|
| 323 |
-
ip=client_ip
|
|
|
|
|
|
|
| 324 |
)
|
| 325 |
if not mask_image_save_data:
|
| 326 |
return gr.Warning(mask_image_save_msg), did
|
|
@@ -328,7 +346,9 @@ def generate_image(edit_image_infos, did, request: gr.Request):
|
|
| 328 |
start_task_data, start_task_msg = start_task(
|
| 329 |
task_id=task_id,
|
| 330 |
did=did,
|
| 331 |
-
ip=client_ip
|
|
|
|
|
|
|
| 332 |
)
|
| 333 |
if not start_task_data:
|
| 334 |
return gr.Warning(start_task_msg), did
|
|
@@ -343,7 +363,9 @@ def generate_image(edit_image_infos, did, request: gr.Request):
|
|
| 343 |
task_id=task_id,
|
| 344 |
execution_id=execution_id,
|
| 345 |
did=did,
|
| 346 |
-
ip=client_ip
|
|
|
|
|
|
|
| 347 |
)
|
| 348 |
if not query_task_data:
|
| 349 |
return gr.Warning(query_task_msg), did
|
|
@@ -360,7 +382,7 @@ def generate_image(edit_image_infos, did, request: gr.Request):
|
|
| 360 |
elif status == "Success" or status == "Blocked":
|
| 361 |
img = results[0].get("image")
|
| 362 |
if img and str(img).strip() != "":
|
| 363 |
-
return url_to_image(img, ip=client_ip), did
|
| 364 |
end_time = int(time.time())
|
| 365 |
if end_time - start_time > 3600:
|
| 366 |
m = 'Query task timeout.'
|
|
|
|
| 61 |
return h.hexdigest()
|
| 62 |
|
| 63 |
|
| 64 |
+
def url_to_image(url, ip, user_id, user_name):
|
| 65 |
headers = {
|
| 66 |
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
| 67 |
+
'X-Forwarded-For': ip,
|
| 68 |
+
'X-HfUid': user_id,
|
| 69 |
+
'X-HfUserName': user_name,
|
| 70 |
}
|
| 71 |
try:
|
| 72 |
response = requests.get(url, headers=headers, timeout=30)
|
|
|
|
| 78 |
return None
|
| 79 |
|
| 80 |
|
| 81 |
+
def start_task(task_id, did, ip, user_id, user_name):
|
| 82 |
timestamp = str(int(time.time()))
|
| 83 |
signature = generate_signature(
|
| 84 |
key=secret_key,
|
|
|
|
| 91 |
'X-Signature': signature,
|
| 92 |
'X-Forwarded-For': ip,
|
| 93 |
'X-AppId': app_id,
|
| 94 |
+
'X-HfUid': user_id,
|
| 95 |
+
'X-HfUserName': user_name,
|
| 96 |
}
|
| 97 |
data = {
|
| 98 |
"agentVersion": agent_version,
|
|
|
|
| 105 |
return data, msg
|
| 106 |
|
| 107 |
|
| 108 |
+
def create_task(image_url, did, ip, user_id, user_name):
|
| 109 |
timestamp = str(int(time.time()))
|
| 110 |
signature = generate_signature(
|
| 111 |
key=secret_key,
|
|
|
|
| 118 |
'X-Signature': signature,
|
| 119 |
'X-Forwarded-For': ip,
|
| 120 |
'X-AppId': app_id,
|
| 121 |
+
'X-HfUid': user_id,
|
| 122 |
+
'X-HfUserName': user_name,
|
| 123 |
}
|
| 124 |
data = {
|
| 125 |
"agentVersion": agent_version,
|
|
|
|
| 131 |
return data, msg
|
| 132 |
|
| 133 |
|
| 134 |
+
def query_task(task_id, execution_id, did, ip, user_id, user_name):
|
| 135 |
timestamp = str(int(time.time()))
|
| 136 |
signature = generate_signature(
|
| 137 |
key=secret_key,
|
|
|
|
| 144 |
'X-Signature': signature,
|
| 145 |
'X-Forwarded-For': ip,
|
| 146 |
'X-AppId': app_id,
|
| 147 |
+
'X-HfUid': user_id,
|
| 148 |
+
'X-HfUserName': user_name,
|
| 149 |
}
|
| 150 |
data = {
|
| 151 |
"agentVersion": agent_version,
|
|
|
|
| 158 |
return data, msg
|
| 159 |
|
| 160 |
|
| 161 |
+
def upload_image(image, did, ip, user_id, user_name):
|
| 162 |
if image is None:
|
| 163 |
return None
|
| 164 |
image_format = image.format if image.format else "PNG"
|
|
|
|
| 179 |
'X-Signature': signature,
|
| 180 |
'X-Forwarded-For': ip,
|
| 181 |
'X-AppId': app_id,
|
| 182 |
+
'X-HfUid': user_id,
|
| 183 |
+
'X-HfUserName': user_name,
|
| 184 |
}
|
| 185 |
response = requests.post(base_url + upload_image_url, files=files, headers=headers)
|
| 186 |
data, msg = parse_response(response)
|
| 187 |
return data, msg
|
| 188 |
|
| 189 |
|
| 190 |
+
def mask_image_save(task_id, mask, did, ip, user_id, user_name):
|
| 191 |
timestamp = str(int(time.time()))
|
| 192 |
signature = generate_signature(
|
| 193 |
key=secret_key,
|
|
|
|
| 200 |
'X-Signature': signature,
|
| 201 |
'X-Forwarded-For': ip,
|
| 202 |
'X-AppId': app_id,
|
| 203 |
+
'X-HfUid': user_id,
|
| 204 |
+
'X-HfUserName': user_name,
|
| 205 |
}
|
| 206 |
data = {
|
| 207 |
"taskId": task_id,
|
|
|
|
| 240 |
|
| 241 |
def check_login_status(headers):
|
| 242 |
if not headers:
|
| 243 |
+
return None, None
|
| 244 |
|
| 245 |
try:
|
| 246 |
text = headers.get(login_status_key)
|
| 247 |
if not text or "." not in text:
|
| 248 |
+
return None, None
|
| 249 |
|
| 250 |
infos = text.split(".")
|
| 251 |
if len(infos) < 2:
|
| 252 |
+
return None, None
|
| 253 |
|
| 254 |
info = infos[1]
|
| 255 |
cover = len(info) % 4
|
|
|
|
| 262 |
|
| 263 |
data = datas.get(login_info_key)
|
| 264 |
if not data:
|
| 265 |
+
return None, None
|
| 266 |
|
| 267 |
user_id = data.get("_id")
|
| 268 |
user_name = data.get("user")
|
| 269 |
+
return user_id, user_name
|
| 270 |
|
| 271 |
except Exception as e:
|
| 272 |
print(f"An error occurred: {repr(e)}")
|
| 273 |
+
return None, None
|
| 274 |
|
| 275 |
|
| 276 |
def generate_image(edit_image_infos, did, request: gr.Request):
|
| 277 |
if not did:
|
| 278 |
did = str(uuid.uuid4())
|
| 279 |
+
user_id, user_name = check_login_status(request.request.headers)
|
| 280 |
+
if not user_id or not user_name:
|
| 281 |
m = "Please log in to your Hugging Face account to use the features of this application."
|
| 282 |
return gr.Warning(m), did
|
| 283 |
if edit_image_infos is None or not isinstance(edit_image_infos, dict):
|
|
|
|
| 308 |
upload_image_data, upload_image_msg = upload_image(
|
| 309 |
image=main_image,
|
| 310 |
did=did,
|
| 311 |
+
ip=client_ip,
|
| 312 |
+
user_id=user_id,
|
| 313 |
+
user_name=user_name
|
| 314 |
)
|
| 315 |
if not upload_image_data:
|
| 316 |
return gr.Warning(upload_image_msg), did
|
|
|
|
| 321 |
create_task_data, create_task_msg = create_task(
|
| 322 |
image_url=image_url,
|
| 323 |
did=did,
|
| 324 |
+
ip=client_ip,
|
| 325 |
+
user_id=user_id,
|
| 326 |
+
user_name=user_name
|
| 327 |
)
|
| 328 |
if not create_task_data:
|
| 329 |
return gr.Warning(create_task_msg), did
|
|
|
|
| 336 |
task_id=task_id,
|
| 337 |
mask=image_to_base64(mask_image),
|
| 338 |
did=did,
|
| 339 |
+
ip=client_ip,
|
| 340 |
+
user_id=user_id,
|
| 341 |
+
user_name=user_name
|
| 342 |
)
|
| 343 |
if not mask_image_save_data:
|
| 344 |
return gr.Warning(mask_image_save_msg), did
|
|
|
|
| 346 |
start_task_data, start_task_msg = start_task(
|
| 347 |
task_id=task_id,
|
| 348 |
did=did,
|
| 349 |
+
ip=client_ip,
|
| 350 |
+
user_id=user_id,
|
| 351 |
+
user_name=user_name
|
| 352 |
)
|
| 353 |
if not start_task_data:
|
| 354 |
return gr.Warning(start_task_msg), did
|
|
|
|
| 363 |
task_id=task_id,
|
| 364 |
execution_id=execution_id,
|
| 365 |
did=did,
|
| 366 |
+
ip=client_ip,
|
| 367 |
+
user_id=user_id,
|
| 368 |
+
user_name=user_name
|
| 369 |
)
|
| 370 |
if not query_task_data:
|
| 371 |
return gr.Warning(query_task_msg), did
|
|
|
|
| 382 |
elif status == "Success" or status == "Blocked":
|
| 383 |
img = results[0].get("image")
|
| 384 |
if img and str(img).strip() != "":
|
| 385 |
+
return url_to_image(img, ip=client_ip, user_id=user_id, user_name=user_name), did
|
| 386 |
end_time = int(time.time())
|
| 387 |
if end_time - start_time > 3600:
|
| 388 |
m = 'Query task timeout.'
|
assets/title.html
CHANGED
|
@@ -93,7 +93,7 @@
|
|
| 93 |
Explore our other applications on Hugging Face Spaces:
|
| 94 |
<span class="nav-links">
|
| 95 |
<a href="https://huggingface.co/spaces/WeShopAI/WeShopAI-Virtual-Try-On">WeShopAI Virtual Try-On</a>
|
| 96 |
-
<a href="https://huggingface.co/spaces/WeShopAI/WeShopAI-Pose-
|
| 97 |
</span>
|
| 98 |
</div>
|
| 99 |
<br>
|
|
|
|
| 93 |
Explore our other applications on Hugging Face Spaces:
|
| 94 |
<span class="nav-links">
|
| 95 |
<a href="https://huggingface.co/spaces/WeShopAI/WeShopAI-Virtual-Try-On">WeShopAI Virtual Try-On</a>
|
| 96 |
+
<a href="https://huggingface.co/spaces/WeShopAI/WeShopAI-Fashion-Model-Pose-Change">WeShopAI Fashion Model Pose Change</a>
|
| 97 |
</span>
|
| 98 |
</div>
|
| 99 |
<br>
|