authentication
Browse files
app.py
CHANGED
|
@@ -54,10 +54,12 @@ def generate_signature(key, did, timestamp):
|
|
| 54 |
return h.hexdigest()
|
| 55 |
|
| 56 |
|
| 57 |
-
def url_to_image(url, ip):
|
| 58 |
headers = {
|
| 59 |
"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",
|
| 60 |
-
'X-Forwarded-For': ip
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
try:
|
| 63 |
response = requests.get(url, headers=headers, timeout=30)
|
|
@@ -69,7 +71,7 @@ def url_to_image(url, ip):
|
|
| 69 |
return None
|
| 70 |
|
| 71 |
|
| 72 |
-
def start_task(task_id, did, ip):
|
| 73 |
timestamp = str(int(time.time()))
|
| 74 |
signature = generate_signature(
|
| 75 |
key=secret_key,
|
|
@@ -82,6 +84,8 @@ def start_task(task_id, did, ip):
|
|
| 82 |
'X-Signature': signature,
|
| 83 |
'X-Forwarded-For': ip,
|
| 84 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
data = {
|
| 87 |
"agentVersion": agent_version,
|
|
@@ -94,7 +98,7 @@ def start_task(task_id, did, ip):
|
|
| 94 |
return data, msg
|
| 95 |
|
| 96 |
|
| 97 |
-
def create_task(image_url, did, ip):
|
| 98 |
timestamp = str(int(time.time()))
|
| 99 |
signature = generate_signature(
|
| 100 |
key=secret_key,
|
|
@@ -107,6 +111,8 @@ def create_task(image_url, did, ip):
|
|
| 107 |
'X-Signature': signature,
|
| 108 |
'X-Forwarded-For': ip,
|
| 109 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 110 |
}
|
| 111 |
data = {
|
| 112 |
"agentVersion": agent_version,
|
|
@@ -118,7 +124,7 @@ def create_task(image_url, did, ip):
|
|
| 118 |
return data, msg
|
| 119 |
|
| 120 |
|
| 121 |
-
def save_task(image_url, show_image, task_id, text_description, did, ip):
|
| 122 |
timestamp = str(int(time.time()))
|
| 123 |
signature = generate_signature(
|
| 124 |
key=secret_key,
|
|
@@ -131,6 +137,8 @@ def save_task(image_url, show_image, task_id, text_description, did, ip):
|
|
| 131 |
'X-Signature': signature,
|
| 132 |
'X-Forwarded-For': ip,
|
| 133 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 134 |
}
|
| 135 |
data = {
|
| 136 |
"agentVersion": agent_version,
|
|
@@ -145,7 +153,7 @@ def save_task(image_url, show_image, task_id, text_description, did, ip):
|
|
| 145 |
return data, msg
|
| 146 |
|
| 147 |
|
| 148 |
-
def query_task(task_id, execution_id, did, ip):
|
| 149 |
timestamp = str(int(time.time()))
|
| 150 |
signature = generate_signature(
|
| 151 |
key=secret_key,
|
|
@@ -158,6 +166,8 @@ def query_task(task_id, execution_id, did, ip):
|
|
| 158 |
'X-Signature': signature,
|
| 159 |
'X-Forwarded-For': ip,
|
| 160 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 161 |
}
|
| 162 |
data = {
|
| 163 |
"agentVersion": agent_version,
|
|
@@ -170,7 +180,7 @@ def query_task(task_id, execution_id, did, ip):
|
|
| 170 |
return data, msg
|
| 171 |
|
| 172 |
|
| 173 |
-
def upload_image(image, did, ip):
|
| 174 |
if image is None:
|
| 175 |
return None
|
| 176 |
timestamp = str(int(time.time()))
|
|
@@ -192,6 +202,8 @@ def upload_image(image, did, ip):
|
|
| 192 |
'X-Signature': signature,
|
| 193 |
'X-Forwarded-For': ip,
|
| 194 |
'X-AppId': app_id,
|
|
|
|
|
|
|
| 195 |
}
|
| 196 |
response = requests.post(base_url + upload_url, files=files, headers=headers)
|
| 197 |
data, msg = parse_response(response)
|
|
@@ -206,16 +218,16 @@ def load_description(file_path):
|
|
| 206 |
|
| 207 |
def check_login_status(headers):
|
| 208 |
if not headers:
|
| 209 |
-
return
|
| 210 |
|
| 211 |
try:
|
| 212 |
text = headers.get(login_status_key)
|
| 213 |
if not text or "." not in text:
|
| 214 |
-
return
|
| 215 |
|
| 216 |
infos = text.split(".")
|
| 217 |
if len(infos) < 2:
|
| 218 |
-
return
|
| 219 |
|
| 220 |
info = infos[1]
|
| 221 |
cover = len(info) % 4
|
|
@@ -228,22 +240,22 @@ def check_login_status(headers):
|
|
| 228 |
|
| 229 |
data = datas.get(login_info_key)
|
| 230 |
if not data:
|
| 231 |
-
return
|
| 232 |
|
| 233 |
user_id = data.get("_id")
|
| 234 |
user_name = data.get("user")
|
| 235 |
-
return
|
| 236 |
|
| 237 |
except Exception as e:
|
| 238 |
print(f"An error occurred: {repr(e)}")
|
| 239 |
-
return
|
| 240 |
|
| 241 |
|
| 242 |
def generate_image(main_image, text_description, did, request: gr.Request):
|
| 243 |
if not did:
|
| 244 |
did = str(uuid.uuid4())
|
| 245 |
-
|
| 246 |
-
if not
|
| 247 |
m = "Please log in to your Hugging Face account to use the features of this application."
|
| 248 |
return gr.Warning(m), did
|
| 249 |
if main_image is None:
|
|
@@ -259,6 +271,8 @@ def generate_image(main_image, text_description, did, request: gr.Request):
|
|
| 259 |
image=main_image,
|
| 260 |
did=did,
|
| 261 |
ip=client_ip,
|
|
|
|
|
|
|
| 262 |
)
|
| 263 |
if not upload_image_data:
|
| 264 |
return gr.Warning(upload_image_msg), did
|
|
@@ -270,6 +284,8 @@ def generate_image(main_image, text_description, did, request: gr.Request):
|
|
| 270 |
image_url=image_url,
|
| 271 |
did=did,
|
| 272 |
ip=client_ip,
|
|
|
|
|
|
|
| 273 |
)
|
| 274 |
if not create_task_data:
|
| 275 |
return gr.Warning(create_task_msg), did
|
|
@@ -285,6 +301,8 @@ def generate_image(main_image, text_description, did, request: gr.Request):
|
|
| 285 |
text_description=text_description,
|
| 286 |
did=did,
|
| 287 |
ip=client_ip,
|
|
|
|
|
|
|
| 288 |
)
|
| 289 |
if not save_task_data:
|
| 290 |
return gr.Warning(save_task_msg), did
|
|
@@ -296,6 +314,8 @@ def generate_image(main_image, text_description, did, request: gr.Request):
|
|
| 296 |
task_id=save_task_id,
|
| 297 |
did=did,
|
| 298 |
ip=client_ip,
|
|
|
|
|
|
|
| 299 |
)
|
| 300 |
if not start_task_data:
|
| 301 |
return gr.Warning(start_task_msg), did
|
|
@@ -311,6 +331,8 @@ def generate_image(main_image, text_description, did, request: gr.Request):
|
|
| 311 |
execution_id=execution_id,
|
| 312 |
did=did,
|
| 313 |
ip=client_ip,
|
|
|
|
|
|
|
| 314 |
)
|
| 315 |
print("Query task data:", query_task_data)
|
| 316 |
print("Query task msg:", query_task_msg)
|
|
@@ -329,7 +351,7 @@ def generate_image(main_image, text_description, did, request: gr.Request):
|
|
| 329 |
elif status == "Success" or status == "Blocked":
|
| 330 |
img = results[0].get("image")
|
| 331 |
if img and str(img).strip() != "":
|
| 332 |
-
return url_to_image(img, ip=client_ip), did
|
| 333 |
end_time = int(time.time())
|
| 334 |
if end_time - start_time > 3600:
|
| 335 |
m = 'Query task timeout.'
|
|
|
|
| 54 |
return h.hexdigest()
|
| 55 |
|
| 56 |
|
| 57 |
+
def url_to_image(url, ip, user_id, user_name):
|
| 58 |
headers = {
|
| 59 |
"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",
|
| 60 |
+
'X-Forwarded-For': ip,
|
| 61 |
+
'X-HfUid': user_id,
|
| 62 |
+
'X-HfUserName': user_name,
|
| 63 |
}
|
| 64 |
try:
|
| 65 |
response = requests.get(url, headers=headers, timeout=30)
|
|
|
|
| 71 |
return None
|
| 72 |
|
| 73 |
|
| 74 |
+
def start_task(task_id, did, ip, user_id, user_name):
|
| 75 |
timestamp = str(int(time.time()))
|
| 76 |
signature = generate_signature(
|
| 77 |
key=secret_key,
|
|
|
|
| 84 |
'X-Signature': signature,
|
| 85 |
'X-Forwarded-For': ip,
|
| 86 |
'X-AppId': app_id,
|
| 87 |
+
'X-HfUid': user_id,
|
| 88 |
+
'X-HfUserName': user_name,
|
| 89 |
}
|
| 90 |
data = {
|
| 91 |
"agentVersion": agent_version,
|
|
|
|
| 98 |
return data, msg
|
| 99 |
|
| 100 |
|
| 101 |
+
def create_task(image_url, did, ip, user_id, user_name):
|
| 102 |
timestamp = str(int(time.time()))
|
| 103 |
signature = generate_signature(
|
| 104 |
key=secret_key,
|
|
|
|
| 111 |
'X-Signature': signature,
|
| 112 |
'X-Forwarded-For': ip,
|
| 113 |
'X-AppId': app_id,
|
| 114 |
+
'X-HfUid': user_id,
|
| 115 |
+
'X-HfUserName': user_name,
|
| 116 |
}
|
| 117 |
data = {
|
| 118 |
"agentVersion": agent_version,
|
|
|
|
| 124 |
return data, msg
|
| 125 |
|
| 126 |
|
| 127 |
+
def save_task(image_url, show_image, task_id, text_description, did, ip, user_id, user_name):
|
| 128 |
timestamp = str(int(time.time()))
|
| 129 |
signature = generate_signature(
|
| 130 |
key=secret_key,
|
|
|
|
| 137 |
'X-Signature': signature,
|
| 138 |
'X-Forwarded-For': ip,
|
| 139 |
'X-AppId': app_id,
|
| 140 |
+
'X-HfUid': user_id,
|
| 141 |
+
'X-HfUserName': user_name,
|
| 142 |
}
|
| 143 |
data = {
|
| 144 |
"agentVersion": agent_version,
|
|
|
|
| 153 |
return data, msg
|
| 154 |
|
| 155 |
|
| 156 |
+
def query_task(task_id, execution_id, did, ip, user_id, user_name):
|
| 157 |
timestamp = str(int(time.time()))
|
| 158 |
signature = generate_signature(
|
| 159 |
key=secret_key,
|
|
|
|
| 166 |
'X-Signature': signature,
|
| 167 |
'X-Forwarded-For': ip,
|
| 168 |
'X-AppId': app_id,
|
| 169 |
+
'X-HfUid': user_id,
|
| 170 |
+
'X-HfUserName': user_name,
|
| 171 |
}
|
| 172 |
data = {
|
| 173 |
"agentVersion": agent_version,
|
|
|
|
| 180 |
return data, msg
|
| 181 |
|
| 182 |
|
| 183 |
+
def upload_image(image, did, ip, user_id, user_name):
|
| 184 |
if image is None:
|
| 185 |
return None
|
| 186 |
timestamp = str(int(time.time()))
|
|
|
|
| 202 |
'X-Signature': signature,
|
| 203 |
'X-Forwarded-For': ip,
|
| 204 |
'X-AppId': app_id,
|
| 205 |
+
'X-HfUid': user_id,
|
| 206 |
+
'X-HfUserName': user_name,
|
| 207 |
}
|
| 208 |
response = requests.post(base_url + upload_url, files=files, headers=headers)
|
| 209 |
data, msg = parse_response(response)
|
|
|
|
| 218 |
|
| 219 |
def check_login_status(headers):
|
| 220 |
if not headers:
|
| 221 |
+
return None, None
|
| 222 |
|
| 223 |
try:
|
| 224 |
text = headers.get(login_status_key)
|
| 225 |
if not text or "." not in text:
|
| 226 |
+
return None, None
|
| 227 |
|
| 228 |
infos = text.split(".")
|
| 229 |
if len(infos) < 2:
|
| 230 |
+
return None, None
|
| 231 |
|
| 232 |
info = infos[1]
|
| 233 |
cover = len(info) % 4
|
|
|
|
| 240 |
|
| 241 |
data = datas.get(login_info_key)
|
| 242 |
if not data:
|
| 243 |
+
return None, None
|
| 244 |
|
| 245 |
user_id = data.get("_id")
|
| 246 |
user_name = data.get("user")
|
| 247 |
+
return user_id, user_name
|
| 248 |
|
| 249 |
except Exception as e:
|
| 250 |
print(f"An error occurred: {repr(e)}")
|
| 251 |
+
return None, None
|
| 252 |
|
| 253 |
|
| 254 |
def generate_image(main_image, text_description, did, request: gr.Request):
|
| 255 |
if not did:
|
| 256 |
did = str(uuid.uuid4())
|
| 257 |
+
user_id, user_name = check_login_status(request.request.headers)
|
| 258 |
+
if not user_id or not user_name:
|
| 259 |
m = "Please log in to your Hugging Face account to use the features of this application."
|
| 260 |
return gr.Warning(m), did
|
| 261 |
if main_image is None:
|
|
|
|
| 271 |
image=main_image,
|
| 272 |
did=did,
|
| 273 |
ip=client_ip,
|
| 274 |
+
user_id=user_id,
|
| 275 |
+
user_name=user_name
|
| 276 |
)
|
| 277 |
if not upload_image_data:
|
| 278 |
return gr.Warning(upload_image_msg), did
|
|
|
|
| 284 |
image_url=image_url,
|
| 285 |
did=did,
|
| 286 |
ip=client_ip,
|
| 287 |
+
user_id=user_id,
|
| 288 |
+
user_name=user_name
|
| 289 |
)
|
| 290 |
if not create_task_data:
|
| 291 |
return gr.Warning(create_task_msg), did
|
|
|
|
| 301 |
text_description=text_description,
|
| 302 |
did=did,
|
| 303 |
ip=client_ip,
|
| 304 |
+
user_id=user_id,
|
| 305 |
+
user_name=user_name
|
| 306 |
)
|
| 307 |
if not save_task_data:
|
| 308 |
return gr.Warning(save_task_msg), did
|
|
|
|
| 314 |
task_id=save_task_id,
|
| 315 |
did=did,
|
| 316 |
ip=client_ip,
|
| 317 |
+
user_id=user_id,
|
| 318 |
+
user_name=user_name
|
| 319 |
)
|
| 320 |
if not start_task_data:
|
| 321 |
return gr.Warning(start_task_msg), did
|
|
|
|
| 331 |
execution_id=execution_id,
|
| 332 |
did=did,
|
| 333 |
ip=client_ip,
|
| 334 |
+
user_id=user_id,
|
| 335 |
+
user_name=user_name
|
| 336 |
)
|
| 337 |
print("Query task data:", query_task_data)
|
| 338 |
print("Query task msg:", query_task_msg)
|
|
|
|
| 351 |
elif status == "Success" or status == "Blocked":
|
| 352 |
img = results[0].get("image")
|
| 353 |
if img and str(img).strip() != "":
|
| 354 |
+
return url_to_image(img, ip=client_ip, user_id=user_id, user_name=user_name), did
|
| 355 |
end_time = int(time.time())
|
| 356 |
if end_time - start_time > 3600:
|
| 357 |
m = 'Query task timeout.'
|