authentication
Browse files- app.py +46 -22
- assets/title.html +1 -1
app.py
CHANGED
|
@@ -57,10 +57,12 @@ def generate_signature(key, did, timestamp):
|
|
| 57 |
return h.hexdigest()
|
| 58 |
|
| 59 |
|
| 60 |
-
def url_to_image(url, ip):
|
| 61 |
headers = {
|
| 62 |
"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",
|
| 63 |
-
'X-Forwarded-For': ip
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
try:
|
| 66 |
response = requests.get(url, headers=headers, timeout=30)
|
|
@@ -72,7 +74,7 @@ def url_to_image(url, ip):
|
|
| 72 |
return None
|
| 73 |
|
| 74 |
|
| 75 |
-
def start_task(task_id, did, ip):
|
| 76 |
timestamp = str(int(time.time()))
|
| 77 |
signature = generate_signature(
|
| 78 |
key=secret_key,
|
|
@@ -85,6 +87,8 @@ def start_task(task_id, did, ip):
|
|
| 85 |
'X-Signature': signature,
|
| 86 |
'X-Forwarded-For': ip,
|
| 87 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 88 |
}
|
| 89 |
data = {
|
| 90 |
"agentVersion": agent_version,
|
|
@@ -97,7 +101,7 @@ def start_task(task_id, did, ip):
|
|
| 97 |
return data, msg
|
| 98 |
|
| 99 |
|
| 100 |
-
def create_task(image_url, did, ip):
|
| 101 |
timestamp = str(int(time.time()))
|
| 102 |
signature = generate_signature(
|
| 103 |
key=secret_key,
|
|
@@ -110,6 +114,8 @@ def create_task(image_url, did, ip):
|
|
| 110 |
'X-Signature': signature,
|
| 111 |
'X-Forwarded-For': ip,
|
| 112 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
data = {
|
| 115 |
"agentVersion": agent_version,
|
|
@@ -121,7 +127,7 @@ def create_task(image_url, did, ip):
|
|
| 121 |
return data, msg
|
| 122 |
|
| 123 |
|
| 124 |
-
def save_task(image_url, show_image, task_id, location_id, did, ip):
|
| 125 |
timestamp = str(int(time.time()))
|
| 126 |
signature = generate_signature(
|
| 127 |
key=secret_key,
|
|
@@ -134,6 +140,8 @@ def save_task(image_url, show_image, task_id, location_id, did, ip):
|
|
| 134 |
'X-Signature': signature,
|
| 135 |
'X-Forwarded-For': ip,
|
| 136 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 137 |
}
|
| 138 |
data = {
|
| 139 |
"agentVersion": agent_version,
|
|
@@ -148,7 +156,7 @@ def save_task(image_url, show_image, task_id, location_id, did, ip):
|
|
| 148 |
return data, msg
|
| 149 |
|
| 150 |
|
| 151 |
-
def query_task(task_id, execution_id, did, ip):
|
| 152 |
timestamp = str(int(time.time()))
|
| 153 |
signature = generate_signature(
|
| 154 |
key=secret_key,
|
|
@@ -161,6 +169,8 @@ def query_task(task_id, execution_id, did, ip):
|
|
| 161 |
'X-Signature': signature,
|
| 162 |
'X-Forwarded-For': ip,
|
| 163 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 164 |
}
|
| 165 |
data = {
|
| 166 |
"agentVersion": agent_version,
|
|
@@ -173,7 +183,7 @@ def query_task(task_id, execution_id, did, ip):
|
|
| 173 |
return data, msg
|
| 174 |
|
| 175 |
|
| 176 |
-
def upload_image(image, upload_type, did, ip):
|
| 177 |
if image is None:
|
| 178 |
return None
|
| 179 |
if upload_type == 'image':
|
|
@@ -198,6 +208,8 @@ def upload_image(image, upload_type, did, ip):
|
|
| 198 |
'X-Signature': signature,
|
| 199 |
'X-Forwarded-For': ip,
|
| 200 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 201 |
}
|
| 202 |
response = requests.post(base_url + upload_url, files=files, headers=headers)
|
| 203 |
data, msg = parse_response(response)
|
|
@@ -212,16 +224,16 @@ def load_description(file_path):
|
|
| 212 |
|
| 213 |
def check_login_status(headers):
|
| 214 |
if not headers:
|
| 215 |
-
return
|
| 216 |
|
| 217 |
try:
|
| 218 |
text = headers.get(login_status_key)
|
| 219 |
if not text or "." not in text:
|
| 220 |
-
return
|
| 221 |
|
| 222 |
infos = text.split(".")
|
| 223 |
if len(infos) < 2:
|
| 224 |
-
return
|
| 225 |
|
| 226 |
info = infos[1]
|
| 227 |
cover = len(info) % 4
|
|
@@ -234,22 +246,22 @@ def check_login_status(headers):
|
|
| 234 |
|
| 235 |
data = datas.get(login_info_key)
|
| 236 |
if not data:
|
| 237 |
-
return
|
| 238 |
|
| 239 |
user_id = data.get("_id")
|
| 240 |
user_name = data.get("user")
|
| 241 |
-
return
|
| 242 |
|
| 243 |
except Exception as e:
|
| 244 |
print(f"An error occurred: {repr(e)}")
|
| 245 |
-
return
|
| 246 |
|
| 247 |
|
| 248 |
def generate_image(main_image, background_image, did, request: gr.Request):
|
| 249 |
if not did:
|
| 250 |
did = str(uuid.uuid4())
|
| 251 |
-
|
| 252 |
-
if not
|
| 253 |
m = "Please log in to your Hugging Face account to use the features of this application."
|
| 254 |
return gr.Warning(m), did
|
| 255 |
if main_image is None or background_image is None:
|
|
@@ -265,7 +277,9 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 265 |
image=main_image,
|
| 266 |
upload_type='image',
|
| 267 |
did=did,
|
| 268 |
-
ip=client_ip
|
|
|
|
|
|
|
| 269 |
)
|
| 270 |
if not upload_image_data:
|
| 271 |
return gr.Warning(upload_image_msg), did
|
|
@@ -276,7 +290,9 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 276 |
create_task_data, create_task_msg = create_task(
|
| 277 |
image_url=image_url,
|
| 278 |
did=did,
|
| 279 |
-
ip=client_ip
|
|
|
|
|
|
|
| 280 |
)
|
| 281 |
if not create_task_data:
|
| 282 |
return gr.Warning(create_task_msg), did
|
|
@@ -289,7 +305,9 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 289 |
image=background_image,
|
| 290 |
upload_type='background_image',
|
| 291 |
did=did,
|
| 292 |
-
ip=client_ip
|
|
|
|
|
|
|
| 293 |
)
|
| 294 |
if not upload_image_data:
|
| 295 |
return gr.Warning(upload_image_msg), did
|
|
@@ -300,7 +318,9 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 300 |
task_id=task_id,
|
| 301 |
location_id=upload_image_data,
|
| 302 |
did=did,
|
| 303 |
-
ip=client_ip
|
|
|
|
|
|
|
| 304 |
)
|
| 305 |
if not save_task_data:
|
| 306 |
return gr.Warning(save_task_msg), did
|
|
@@ -311,7 +331,9 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 311 |
start_task_data, start_task_msg = start_task(
|
| 312 |
task_id=save_task_id,
|
| 313 |
did=did,
|
| 314 |
-
ip=client_ip
|
|
|
|
|
|
|
| 315 |
)
|
| 316 |
if not start_task_data:
|
| 317 |
return gr.Warning(start_task_msg), did
|
|
@@ -326,7 +348,9 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 326 |
task_id=save_task_id,
|
| 327 |
execution_id=execution_id,
|
| 328 |
did=did,
|
| 329 |
-
ip=client_ip
|
|
|
|
|
|
|
| 330 |
)
|
| 331 |
if not query_task_data:
|
| 332 |
return gr.Warning(query_task_msg), did
|
|
@@ -343,7 +367,7 @@ def generate_image(main_image, background_image, did, request: gr.Request):
|
|
| 343 |
elif status == "Success" or status == "Blocked":
|
| 344 |
img = results[0].get("image")
|
| 345 |
if img and str(img).strip() != "":
|
| 346 |
-
return url_to_image(img, ip=client_ip), did
|
| 347 |
end_time = int(time.time())
|
| 348 |
if end_time - start_time > 3600:
|
| 349 |
m = 'Query task timeout.'
|
|
|
|
| 57 |
return h.hexdigest()
|
| 58 |
|
| 59 |
|
| 60 |
+
def url_to_image(url, ip, user_id, user_name):
|
| 61 |
headers = {
|
| 62 |
"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",
|
| 63 |
+
'X-Forwarded-For': ip,
|
| 64 |
+
'X-HfUid': user_id,
|
| 65 |
+
'X-HfUserName': user_name,
|
| 66 |
}
|
| 67 |
try:
|
| 68 |
response = requests.get(url, headers=headers, timeout=30)
|
|
|
|
| 74 |
return None
|
| 75 |
|
| 76 |
|
| 77 |
+
def start_task(task_id, did, ip, user_id, user_name):
|
| 78 |
timestamp = str(int(time.time()))
|
| 79 |
signature = generate_signature(
|
| 80 |
key=secret_key,
|
|
|
|
| 87 |
'X-Signature': signature,
|
| 88 |
'X-Forwarded-For': ip,
|
| 89 |
'X-AppId': app_id,
|
| 90 |
+
'X-HfUid': user_id,
|
| 91 |
+
'X-HfUserName': user_name,
|
| 92 |
}
|
| 93 |
data = {
|
| 94 |
"agentVersion": agent_version,
|
|
|
|
| 101 |
return data, msg
|
| 102 |
|
| 103 |
|
| 104 |
+
def create_task(image_url, did, ip, user_id, user_name):
|
| 105 |
timestamp = str(int(time.time()))
|
| 106 |
signature = generate_signature(
|
| 107 |
key=secret_key,
|
|
|
|
| 114 |
'X-Signature': signature,
|
| 115 |
'X-Forwarded-For': ip,
|
| 116 |
'X-AppId': app_id,
|
| 117 |
+
'X-HfUid': user_id,
|
| 118 |
+
'X-HfUserName': user_name,
|
| 119 |
}
|
| 120 |
data = {
|
| 121 |
"agentVersion": agent_version,
|
|
|
|
| 127 |
return data, msg
|
| 128 |
|
| 129 |
|
| 130 |
+
def save_task(image_url, show_image, task_id, location_id, did, ip, user_id, user_name):
|
| 131 |
timestamp = str(int(time.time()))
|
| 132 |
signature = generate_signature(
|
| 133 |
key=secret_key,
|
|
|
|
| 140 |
'X-Signature': signature,
|
| 141 |
'X-Forwarded-For': ip,
|
| 142 |
'X-AppId': app_id,
|
| 143 |
+
'X-HfUid': user_id,
|
| 144 |
+
'X-HfUserName': user_name,
|
| 145 |
}
|
| 146 |
data = {
|
| 147 |
"agentVersion": agent_version,
|
|
|
|
| 156 |
return data, msg
|
| 157 |
|
| 158 |
|
| 159 |
+
def query_task(task_id, execution_id, did, ip, user_id, user_name):
|
| 160 |
timestamp = str(int(time.time()))
|
| 161 |
signature = generate_signature(
|
| 162 |
key=secret_key,
|
|
|
|
| 169 |
'X-Signature': signature,
|
| 170 |
'X-Forwarded-For': ip,
|
| 171 |
'X-AppId': app_id,
|
| 172 |
+
'X-HfUid': user_id,
|
| 173 |
+
'X-HfUserName': user_name,
|
| 174 |
}
|
| 175 |
data = {
|
| 176 |
"agentVersion": agent_version,
|
|
|
|
| 183 |
return data, msg
|
| 184 |
|
| 185 |
|
| 186 |
+
def upload_image(image, upload_type, did, ip, user_id, user_name):
|
| 187 |
if image is None:
|
| 188 |
return None
|
| 189 |
if upload_type == 'image':
|
|
|
|
| 208 |
'X-Signature': signature,
|
| 209 |
'X-Forwarded-For': ip,
|
| 210 |
'X-AppId': app_id,
|
| 211 |
+
'X-HfUid': user_id,
|
| 212 |
+
'X-HfUserName': user_name,
|
| 213 |
}
|
| 214 |
response = requests.post(base_url + upload_url, files=files, headers=headers)
|
| 215 |
data, msg = parse_response(response)
|
|
|
|
| 224 |
|
| 225 |
def check_login_status(headers):
|
| 226 |
if not headers:
|
| 227 |
+
return None, None
|
| 228 |
|
| 229 |
try:
|
| 230 |
text = headers.get(login_status_key)
|
| 231 |
if not text or "." not in text:
|
| 232 |
+
return None, None
|
| 233 |
|
| 234 |
infos = text.split(".")
|
| 235 |
if len(infos) < 2:
|
| 236 |
+
return None, None
|
| 237 |
|
| 238 |
info = infos[1]
|
| 239 |
cover = len(info) % 4
|
|
|
|
| 246 |
|
| 247 |
data = datas.get(login_info_key)
|
| 248 |
if not data:
|
| 249 |
+
return None, None
|
| 250 |
|
| 251 |
user_id = data.get("_id")
|
| 252 |
user_name = data.get("user")
|
| 253 |
+
return user_id, user_name
|
| 254 |
|
| 255 |
except Exception as e:
|
| 256 |
print(f"An error occurred: {repr(e)}")
|
| 257 |
+
return None, None
|
| 258 |
|
| 259 |
|
| 260 |
def generate_image(main_image, background_image, did, request: gr.Request):
|
| 261 |
if not did:
|
| 262 |
did = str(uuid.uuid4())
|
| 263 |
+
user_id, user_name = check_login_status(request.request.headers)
|
| 264 |
+
if not user_id or not user_name:
|
| 265 |
m = "Please log in to your Hugging Face account to use the features of this application."
|
| 266 |
return gr.Warning(m), did
|
| 267 |
if main_image is None or background_image is None:
|
|
|
|
| 277 |
image=main_image,
|
| 278 |
upload_type='image',
|
| 279 |
did=did,
|
| 280 |
+
ip=client_ip,
|
| 281 |
+
user_id=user_id,
|
| 282 |
+
user_name=user_name
|
| 283 |
)
|
| 284 |
if not upload_image_data:
|
| 285 |
return gr.Warning(upload_image_msg), did
|
|
|
|
| 290 |
create_task_data, create_task_msg = create_task(
|
| 291 |
image_url=image_url,
|
| 292 |
did=did,
|
| 293 |
+
ip=client_ip,
|
| 294 |
+
user_id=user_id,
|
| 295 |
+
user_name=user_name
|
| 296 |
)
|
| 297 |
if not create_task_data:
|
| 298 |
return gr.Warning(create_task_msg), did
|
|
|
|
| 305 |
image=background_image,
|
| 306 |
upload_type='background_image',
|
| 307 |
did=did,
|
| 308 |
+
ip=client_ip,
|
| 309 |
+
user_id=user_id,
|
| 310 |
+
user_name=user_name
|
| 311 |
)
|
| 312 |
if not upload_image_data:
|
| 313 |
return gr.Warning(upload_image_msg), did
|
|
|
|
| 318 |
task_id=task_id,
|
| 319 |
location_id=upload_image_data,
|
| 320 |
did=did,
|
| 321 |
+
ip=client_ip,
|
| 322 |
+
user_id=user_id,
|
| 323 |
+
user_name=user_name
|
| 324 |
)
|
| 325 |
if not save_task_data:
|
| 326 |
return gr.Warning(save_task_msg), did
|
|
|
|
| 331 |
start_task_data, start_task_msg = start_task(
|
| 332 |
task_id=save_task_id,
|
| 333 |
did=did,
|
| 334 |
+
ip=client_ip,
|
| 335 |
+
user_id=user_id,
|
| 336 |
+
user_name=user_name
|
| 337 |
)
|
| 338 |
if not start_task_data:
|
| 339 |
return gr.Warning(start_task_msg), did
|
|
|
|
| 348 |
task_id=save_task_id,
|
| 349 |
execution_id=execution_id,
|
| 350 |
did=did,
|
| 351 |
+
ip=client_ip,
|
| 352 |
+
user_id=user_id,
|
| 353 |
+
user_name=user_name
|
| 354 |
)
|
| 355 |
if not query_task_data:
|
| 356 |
return gr.Warning(query_task_msg), did
|
|
|
|
| 367 |
elif status == "Success" or status == "Blocked":
|
| 368 |
img = results[0].get("image")
|
| 369 |
if img and str(img).strip() != "":
|
| 370 |
+
return url_to_image(img, ip=client_ip, user_id=user_id, user_name=user_name), did
|
| 371 |
end_time = int(time.time())
|
| 372 |
if end_time - start_time > 3600:
|
| 373 |
m = 'Query task timeout.'
|
assets/title.html
CHANGED
|
@@ -92,7 +92,7 @@
|
|
| 92 |
<div class="content-text">
|
| 93 |
Explore our other applications on Hugging Face Spaces:
|
| 94 |
<span class="nav-links">
|
| 95 |
-
<a href="https://huggingface.co/spaces/WeShopAI/WeShopAI-Pose-
|
| 96 |
<a href="https://huggingface.co/spaces/WeShopAI/WeShopAI-Bad-Hand-Fixer">WeShopAI Bad Hand Fixer</a>
|
| 97 |
</span>
|
| 98 |
</div>
|
|
|
|
| 92 |
<div class="content-text">
|
| 93 |
Explore our other applications on Hugging Face Spaces:
|
| 94 |
<span class="nav-links">
|
| 95 |
+
<a href="https://huggingface.co/spaces/WeShopAI/WeShopAI-Fashion-Model-Pose-Change">WeShopAI Fashion Model Pose Change</a>
|
| 96 |
<a href="https://huggingface.co/spaces/WeShopAI/WeShopAI-Bad-Hand-Fixer">WeShopAI Bad Hand Fixer</a>
|
| 97 |
</span>
|
| 98 |
</div>
|