Check user login status
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ from io import BytesIO
|
|
| 8 |
from PIL import Image
|
| 9 |
import uuid
|
| 10 |
import base64
|
|
|
|
| 11 |
|
| 12 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
| 13 |
clothing_list = os.listdir(os.path.join(example_path, "clothing"))
|
|
@@ -23,6 +24,8 @@ secret_key = os.getenv('secret_key')
|
|
| 23 |
agent_version = os.getenv('agent_version')
|
| 24 |
agent_name = os.getenv('agent_name')
|
| 25 |
app_id = os.getenv('app_id')
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
def parse_response(response, state='default'):
|
|
@@ -223,9 +226,48 @@ def image_to_base64(image):
|
|
| 223 |
return f"data:image/png;base64,{img_base64}"
|
| 224 |
|
| 225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
def generate_image(edit_image_infos, did, request: gr.Request):
|
| 227 |
if not did:
|
| 228 |
did = str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
if edit_image_infos is None or not isinstance(edit_image_infos, dict):
|
| 230 |
m = "Please upload the main image before generating."
|
| 231 |
return gr.Warning(m), did
|
|
|
|
| 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 |
clothing_list = os.listdir(os.path.join(example_path, "clothing"))
|
|
|
|
| 24 |
agent_version = os.getenv('agent_version')
|
| 25 |
agent_name = os.getenv('agent_name')
|
| 26 |
app_id = os.getenv('app_id')
|
| 27 |
+
login_status_key = os.getenv('login_status_key')
|
| 28 |
+
login_info_key = os.getenv('login_info_key')
|
| 29 |
|
| 30 |
|
| 31 |
def parse_response(response, state='default'):
|
|
|
|
| 226 |
return f"data:image/png;base64,{img_base64}"
|
| 227 |
|
| 228 |
|
| 229 |
+
def check_login_status(headers):
|
| 230 |
+
if not headers:
|
| 231 |
+
return False
|
| 232 |
+
|
| 233 |
+
try:
|
| 234 |
+
text = headers.get(login_status_key)
|
| 235 |
+
if not text or "." not in text:
|
| 236 |
+
return False
|
| 237 |
+
|
| 238 |
+
infos = text.split(".")
|
| 239 |
+
if len(infos) < 2:
|
| 240 |
+
return False
|
| 241 |
+
|
| 242 |
+
info = infos[1]
|
| 243 |
+
cover = len(info) % 4
|
| 244 |
+
if cover != 0:
|
| 245 |
+
info += "=" * (4 - cover)
|
| 246 |
+
|
| 247 |
+
decoded_bytes = base64.b64decode(info)
|
| 248 |
+
decoded_str = decoded_bytes.decode('utf-8')
|
| 249 |
+
datas = json.loads(decoded_str)
|
| 250 |
+
|
| 251 |
+
data = datas.get(login_info_key)
|
| 252 |
+
if not data:
|
| 253 |
+
return False
|
| 254 |
+
|
| 255 |
+
user_id = data.get("_id")
|
| 256 |
+
user_name = data.get("user")
|
| 257 |
+
return bool(user_id and user_name)
|
| 258 |
+
|
| 259 |
+
except Exception as e:
|
| 260 |
+
print(f"An error occurred: {repr(e)}")
|
| 261 |
+
return False
|
| 262 |
+
|
| 263 |
+
|
| 264 |
def generate_image(edit_image_infos, did, request: gr.Request):
|
| 265 |
if not did:
|
| 266 |
did = str(uuid.uuid4())
|
| 267 |
+
login_status = check_login_status(request.request.headers)
|
| 268 |
+
if not login_status:
|
| 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):
|
| 272 |
m = "Please upload the main image before generating."
|
| 273 |
return gr.Warning(m), did
|