Spaces:
Running on Zero
Running on Zero
Improve pending QR recovery and dashboard sizing
Browse files- app.py +7 -4
- hf_dashboard.py +43 -1
app.py
CHANGED
|
@@ -6620,11 +6620,11 @@ with gr.Blocks(delete_cache=(3600, 3600)) as demo:
|
|
| 6620 |
my_qr_gallery = gr.Gallery(
|
| 6621 |
label="My QR Generations",
|
| 6622 |
value=[],
|
| 6623 |
-
columns=
|
| 6624 |
rows=2,
|
| 6625 |
-
height=
|
| 6626 |
-
allow_preview=
|
| 6627 |
-
object_fit="
|
| 6628 |
show_download_button=False,
|
| 6629 |
)
|
| 6630 |
with gr.Row():
|
|
@@ -6633,6 +6633,9 @@ with gr.Blocks(delete_cache=(3600, 3600)) as demo:
|
|
| 6633 |
label="Selected QR",
|
| 6634 |
visible=True,
|
| 6635 |
interactive=False,
|
|
|
|
|
|
|
|
|
|
| 6636 |
)
|
| 6637 |
with gr.Column(scale=1):
|
| 6638 |
my_qr_detail_markdown = gr.Markdown(
|
|
|
|
| 6620 |
my_qr_gallery = gr.Gallery(
|
| 6621 |
label="My QR Generations",
|
| 6622 |
value=[],
|
| 6623 |
+
columns=4,
|
| 6624 |
rows=2,
|
| 6625 |
+
height=260,
|
| 6626 |
+
allow_preview=False,
|
| 6627 |
+
object_fit="contain",
|
| 6628 |
show_download_button=False,
|
| 6629 |
)
|
| 6630 |
with gr.Row():
|
|
|
|
| 6633 |
label="Selected QR",
|
| 6634 |
visible=True,
|
| 6635 |
interactive=False,
|
| 6636 |
+
height=360,
|
| 6637 |
+
width=360,
|
| 6638 |
+
show_download_button=False,
|
| 6639 |
)
|
| 6640 |
with gr.Column(scale=1):
|
| 6641 |
my_qr_detail_markdown = gr.Markdown(
|
hf_dashboard.py
CHANGED
|
@@ -226,6 +226,23 @@ def _build_anon_id(request: Any | None, source: str = "ui", fallback: str = "ano
|
|
| 226 |
return hashlib.sha256(raw.encode("utf-8")).hexdigest()[:24]
|
| 227 |
|
| 228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
def get_current_user_context(request: Any | None) -> dict[str, Any] | None:
|
| 230 |
oauth_info = _session_oauth_info(request)
|
| 231 |
raw_userinfo = oauth_info.get("userinfo")
|
|
@@ -391,6 +408,22 @@ def _claim_anonymous_generations(request: Any | None, app_user_id: str) -> int:
|
|
| 391 |
total += len([row for row in rows if str(row.get("id") or "") not in claimed_ids])
|
| 392 |
else:
|
| 393 |
total += len(rows)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
|
| 395 |
return total
|
| 396 |
|
|
@@ -459,6 +492,7 @@ def persist_generation(
|
|
| 459 |
metadata["pending_claim"] = {
|
| 460 |
"anon_id": anon_id,
|
| 461 |
"session_hash": str(getattr(request, "session_hash", "") or "") or None,
|
|
|
|
| 462 |
}
|
| 463 |
|
| 464 |
row = {
|
|
@@ -494,13 +528,21 @@ def list_my_generations(request: Any | None) -> dict[str, Any]:
|
|
| 494 |
user = ensure_app_user_for_request(request)
|
| 495 |
if user is None:
|
| 496 |
session_hash = str(getattr(request, "session_hash", "") or "").strip() if request is not None else ""
|
|
|
|
| 497 |
pending_rows: list[dict[str, Any]] = []
|
|
|
|
| 498 |
if session_hash:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
pending_query = (
|
| 500 |
"select=id,created_at,updated_at,status,input_type,qr_mode,prompt,destination_url_original,destination_url_normalized,"
|
| 501 |
"effective_qr_text,shortener_applied,short_link_id,short_code,short_url,shortener_expires_at,completed_image_url,thumbnail_url,metadata"
|
|
|
|
| 502 |
"&app_user_id=is.null"
|
| 503 |
-
f"&metadata->pending_claim->>session_hash=eq.{_quote_filter(session_hash)}"
|
| 504 |
"&order=created_at.desc"
|
| 505 |
)
|
| 506 |
pending_rows = _table_select("dashboard_generation_summaries", query=pending_query)
|
|
|
|
| 226 |
return hashlib.sha256(raw.encode("utf-8")).hexdigest()[:24]
|
| 227 |
|
| 228 |
|
| 229 |
+
def _build_browser_fingerprint(request: Any | None, source: str = "ui") -> str | None:
|
| 230 |
+
if request is None:
|
| 231 |
+
return None
|
| 232 |
+
headers = _request_headers(request)
|
| 233 |
+
raw = "|".join(
|
| 234 |
+
[
|
| 235 |
+
source or "unknown",
|
| 236 |
+
str(headers.get("x-forwarded-for", "")),
|
| 237 |
+
str(headers.get("user-agent", "")),
|
| 238 |
+
str(headers.get("accept-language", "")),
|
| 239 |
+
]
|
| 240 |
+
)
|
| 241 |
+
if raw.count("|") == 3 and all(not part for part in raw.split("|")):
|
| 242 |
+
return None
|
| 243 |
+
return hashlib.sha256(raw.encode("utf-8")).hexdigest()[:24]
|
| 244 |
+
|
| 245 |
+
|
| 246 |
def get_current_user_context(request: Any | None) -> dict[str, Any] | None:
|
| 247 |
oauth_info = _session_oauth_info(request)
|
| 248 |
raw_userinfo = oauth_info.get("userinfo")
|
|
|
|
| 408 |
total += len([row for row in rows if str(row.get("id") or "") not in claimed_ids])
|
| 409 |
else:
|
| 410 |
total += len(rows)
|
| 411 |
+
claimed_ids.update(str(row.get("id") or "") for row in rows)
|
| 412 |
+
|
| 413 |
+
browser_fingerprint = _build_browser_fingerprint(request)
|
| 414 |
+
if browser_fingerprint:
|
| 415 |
+
rows = _patch_rows(
|
| 416 |
+
"generations",
|
| 417 |
+
filters=(
|
| 418 |
+
f"metadata->pending_claim->>browser_fingerprint=eq.{_quote_filter(browser_fingerprint)}"
|
| 419 |
+
"&app_user_id=is.null"
|
| 420 |
+
),
|
| 421 |
+
values={
|
| 422 |
+
"app_user_id": app_user_id,
|
| 423 |
+
"updated_at": now_iso,
|
| 424 |
+
},
|
| 425 |
+
)
|
| 426 |
+
total += len([row for row in rows if str(row.get("id") or "") not in claimed_ids])
|
| 427 |
|
| 428 |
return total
|
| 429 |
|
|
|
|
| 492 |
metadata["pending_claim"] = {
|
| 493 |
"anon_id": anon_id,
|
| 494 |
"session_hash": str(getattr(request, "session_hash", "") or "") or None,
|
| 495 |
+
"browser_fingerprint": _build_browser_fingerprint(request),
|
| 496 |
}
|
| 497 |
|
| 498 |
row = {
|
|
|
|
| 528 |
user = ensure_app_user_for_request(request)
|
| 529 |
if user is None:
|
| 530 |
session_hash = str(getattr(request, "session_hash", "") or "").strip() if request is not None else ""
|
| 531 |
+
browser_fingerprint = _build_browser_fingerprint(request)
|
| 532 |
pending_rows: list[dict[str, Any]] = []
|
| 533 |
+
pending_filters = ["app_user_id=is.null"]
|
| 534 |
if session_hash:
|
| 535 |
+
pending_filters.append(f"metadata->pending_claim->>session_hash=eq.{_quote_filter(session_hash)}")
|
| 536 |
+
if browser_fingerprint:
|
| 537 |
+
pending_filters.append(
|
| 538 |
+
f"metadata->pending_claim->>browser_fingerprint=eq.{_quote_filter(browser_fingerprint)}"
|
| 539 |
+
)
|
| 540 |
+
if len(pending_filters) > 1:
|
| 541 |
pending_query = (
|
| 542 |
"select=id,created_at,updated_at,status,input_type,qr_mode,prompt,destination_url_original,destination_url_normalized,"
|
| 543 |
"effective_qr_text,shortener_applied,short_link_id,short_code,short_url,shortener_expires_at,completed_image_url,thumbnail_url,metadata"
|
| 544 |
+
f"&or=({','.join(pending_filters[1:])})"
|
| 545 |
"&app_user_id=is.null"
|
|
|
|
| 546 |
"&order=created_at.desc"
|
| 547 |
)
|
| 548 |
pending_rows = _table_select("dashboard_generation_summaries", query=pending_query)
|