Check user login status
Browse files
app.py
CHANGED
|
@@ -7,10 +7,8 @@ import requests
|
|
| 7 |
from io import BytesIO
|
| 8 |
from PIL import Image
|
| 9 |
import uuid
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# load_dotenv()
|
| 14 |
|
| 15 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
| 16 |
clothing_list = os.listdir(os.path.join(example_path, "clothing"))
|
|
@@ -28,6 +26,9 @@ secret_key = os.getenv('secret_key')
|
|
| 28 |
agent_version = os.getenv('agent_version')
|
| 29 |
agent_name = os.getenv('agent_name')
|
| 30 |
app_id = os.getenv('app_id')
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def parse_response(response):
|
| 33 |
data = {}
|
|
@@ -209,9 +210,48 @@ def load_description(file_path):
|
|
| 209 |
return content
|
| 210 |
|
| 211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
def generate_image(main_image, background_image, did, request: gr.Request):
|
| 213 |
if not did:
|
| 214 |
did = str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
if main_image is None or background_image is None:
|
| 216 |
m = "Please upload both the main image and the background reference image before generating."
|
| 217 |
return gr.Warning(m), did
|
|
|
|
| 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 |
clothing_list = os.listdir(os.path.join(example_path, "clothing"))
|
|
|
|
| 26 |
agent_version = os.getenv('agent_version')
|
| 27 |
agent_name = os.getenv('agent_name')
|
| 28 |
app_id = os.getenv('app_id')
|
| 29 |
+
login_status_key = os.getenv('login_status_key')
|
| 30 |
+
login_info_key = os.getenv('login_info_key')
|
| 31 |
+
|
| 32 |
|
| 33 |
def parse_response(response):
|
| 34 |
data = {}
|
|
|
|
| 210 |
return content
|
| 211 |
|
| 212 |
|
| 213 |
+
def check_login_status(headers):
|
| 214 |
+
if not headers:
|
| 215 |
+
return False
|
| 216 |
+
|
| 217 |
+
try:
|
| 218 |
+
text = headers.get(login_status_key)
|
| 219 |
+
if not text or "." not in text:
|
| 220 |
+
return False
|
| 221 |
+
|
| 222 |
+
infos = text.split(".")
|
| 223 |
+
if len(infos) < 2:
|
| 224 |
+
return False
|
| 225 |
+
|
| 226 |
+
info = infos[1]
|
| 227 |
+
cover = len(info) % 4
|
| 228 |
+
if cover != 0:
|
| 229 |
+
info += "=" * (4 - cover)
|
| 230 |
+
|
| 231 |
+
decoded_bytes = base64.b64decode(info)
|
| 232 |
+
decoded_str = decoded_bytes.decode('utf-8')
|
| 233 |
+
datas = json.loads(decoded_str)
|
| 234 |
+
|
| 235 |
+
data = datas.get(login_info_key)
|
| 236 |
+
if not data:
|
| 237 |
+
return False
|
| 238 |
+
|
| 239 |
+
user_id = data.get("_id")
|
| 240 |
+
user_name = data.get("user")
|
| 241 |
+
return bool(user_id and user_name)
|
| 242 |
+
|
| 243 |
+
except Exception as e:
|
| 244 |
+
print(f"An error occurred: {repr(e)}")
|
| 245 |
+
return False
|
| 246 |
+
|
| 247 |
+
|
| 248 |
def generate_image(main_image, background_image, did, request: gr.Request):
|
| 249 |
if not did:
|
| 250 |
did = str(uuid.uuid4())
|
| 251 |
+
login_status = check_login_status(request.request.headers)
|
| 252 |
+
if not login_status:
|
| 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:
|
| 256 |
m = "Please upload both the main image and the background reference image before generating."
|
| 257 |
return gr.Warning(m), did
|