WeShop commited on
Commit
d951baa
·
1 Parent(s): 97eb3aa

Check user login status

Browse files
Files changed (1) hide show
  1. app.py +52 -12
app.py CHANGED
@@ -7,10 +7,8 @@ import requests
7
  from io import BytesIO
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
  human_list = os.listdir(os.path.join(example_path, "human"))
@@ -25,6 +23,9 @@ secret_key = os.getenv('secret_key')
25
  app_id = os.getenv('app_id')
26
  agent_version = os.getenv('agent_version')
27
  agent_name = os.getenv('agent_name')
 
 
 
28
 
29
  def parse_response(response):
30
  data = {}
@@ -203,9 +204,48 @@ def load_description(file_path):
203
  return content
204
 
205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
  def generate_image(main_image, text_description, did, request: gr.Request):
207
  if not did:
208
  did = str(uuid.uuid4())
 
 
 
 
209
  if main_image is None:
210
  m = "Please upload both the main image and the background reference image before generating."
211
  return gr.Warning(m), did
@@ -214,7 +254,7 @@ def generate_image(main_image, text_description, did, request: gr.Request):
214
  x_forwarded_for = request.headers.get('x-forwarded-for')
215
  if x_forwarded_for:
216
  client_ip = x_forwarded_for
217
-
218
  upload_image_data, upload_image_msg = upload_image(
219
  image=main_image,
220
  did=did,
@@ -384,7 +424,7 @@ with gr.Blocks(css=css) as WeShop:
384
  "Low angle upward shot",
385
  ],
386
  outputs=text_description_input
387
- )
388
  with gr.Column():
389
  with gr.Row():
390
  with gr.Column():
@@ -406,22 +446,22 @@ with gr.Blocks(css=css) as WeShop:
406
  show_case = gr.Examples(
407
  examples=[
408
  [
409
- "assets/human/07.png",
410
- "Walking angle",
411
  "assets/examples/result_07_01.jpeg",
412
  ],
413
  [
414
- "assets/human/01.png",
415
  "Following camera angle",
416
  "assets/examples/result_01_01.jpeg"
417
  ],
418
  [
419
- "assets/human/06.png",
420
  "Change pose",
421
  "assets/examples/result_06_01.jpeg"
422
  ],
423
  [
424
- "assets/human/04.png",
425
  "Keep the background unchanged, only change the person's pose",
426
  "assets/examples/result_04_01.jpeg"
427
  ],
@@ -434,4 +474,4 @@ with gr.Blocks(css=css) as WeShop:
434
  inputs=[main_image_input],
435
  outputs=main_image_input
436
  )
437
- WeShop.queue(api_open=False).launch(show_api=False)
 
7
  from io import BytesIO
8
  from PIL import Image
9
  import uuid
10
+ import base64
11
+ import json
 
 
12
 
13
  example_path = os.path.join(os.path.dirname(__file__), 'assets')
14
  human_list = os.listdir(os.path.join(example_path, "human"))
 
23
  app_id = os.getenv('app_id')
24
  agent_version = os.getenv('agent_version')
25
  agent_name = os.getenv('agent_name')
26
+ login_status_key = os.getenv('login_status_key')
27
+ login_info_key = os.getenv('login_info_key')
28
+
29
 
30
  def parse_response(response):
31
  data = {}
 
204
  return content
205
 
206
 
207
+ def check_login_status(headers):
208
+ if not headers:
209
+ return False
210
+
211
+ try:
212
+ text = headers.get(login_status_key)
213
+ if not text or "." not in text:
214
+ return False
215
+
216
+ infos = text.split(".")
217
+ if len(infos) < 2:
218
+ return False
219
+
220
+ info = infos[1]
221
+ cover = len(info) % 4
222
+ if cover != 0:
223
+ info += "=" * (4 - cover)
224
+
225
+ decoded_bytes = base64.b64decode(info)
226
+ decoded_str = decoded_bytes.decode('utf-8')
227
+ datas = json.loads(decoded_str)
228
+
229
+ data = datas.get(login_info_key)
230
+ if not data:
231
+ return False
232
+
233
+ user_id = data.get("_id")
234
+ user_name = data.get("user")
235
+ return bool(user_id and user_name)
236
+
237
+ except Exception as e:
238
+ print(f"An error occurred: {repr(e)}")
239
+ return False
240
+
241
+
242
  def generate_image(main_image, text_description, did, request: gr.Request):
243
  if not did:
244
  did = str(uuid.uuid4())
245
+ login_status = check_login_status(request.request.headers)
246
+ if not login_status:
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:
250
  m = "Please upload both the main image and the background reference image before generating."
251
  return gr.Warning(m), did
 
254
  x_forwarded_for = request.headers.get('x-forwarded-for')
255
  if x_forwarded_for:
256
  client_ip = x_forwarded_for
257
+
258
  upload_image_data, upload_image_msg = upload_image(
259
  image=main_image,
260
  did=did,
 
424
  "Low angle upward shot",
425
  ],
426
  outputs=text_description_input
427
+ )
428
  with gr.Column():
429
  with gr.Row():
430
  with gr.Column():
 
446
  show_case = gr.Examples(
447
  examples=[
448
  [
449
+ "assets/human/07.png",
450
+ "Walking angle",
451
  "assets/examples/result_07_01.jpeg",
452
  ],
453
  [
454
+ "assets/human/01.png",
455
  "Following camera angle",
456
  "assets/examples/result_01_01.jpeg"
457
  ],
458
  [
459
+ "assets/human/06.png",
460
  "Change pose",
461
  "assets/examples/result_06_01.jpeg"
462
  ],
463
  [
464
+ "assets/human/04.png",
465
  "Keep the background unchanged, only change the person's pose",
466
  "assets/examples/result_04_01.jpeg"
467
  ],
 
474
  inputs=[main_image_input],
475
  outputs=main_image_input
476
  )
477
+ WeShop.queue(api_open=False).launch(show_api=False)