Spaces:
Running
Running
bobbypaton commited on
Commit ·
465da9c
1
Parent(s): de4f5fa
fix: run visit logging in background thread to prevent worker timeout
Browse files- bde_prediction/visit_counter.py +17 -10
bde_prediction/visit_counter.py
CHANGED
|
@@ -17,6 +17,7 @@ import os
|
|
| 17 |
import csv
|
| 18 |
import io
|
| 19 |
from datetime import datetime, timezone
|
|
|
|
| 20 |
|
| 21 |
try:
|
| 22 |
from huggingface_hub import HfApi, hf_hub_download
|
|
@@ -35,8 +36,12 @@ SPACE_NAME = "patonlab/alfabet_bde" # label stored in each row
|
|
| 35 |
TOKEN = os.environ.get("HF_TOKEN") # set as a Space secret
|
| 36 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 37 |
|
| 38 |
-
|
| 39 |
def log_visit(space: str = SPACE_NAME):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
"""Append one row to visits.csv in the analytics dataset repo."""
|
| 41 |
if not TOKEN:
|
| 42 |
print("[visit_counter] HF_TOKEN not set — skipping visit log.")
|
|
@@ -69,15 +74,17 @@ def log_visit(space: str = SPACE_NAME):
|
|
| 69 |
writer.writerows(existing_rows)
|
| 70 |
|
| 71 |
# 4. Push back to dataset repo
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
| 82 |
def get_visit_counts() -> dict:
|
| 83 |
"""Return a dict of {space_name: visit_count} from the analytics repo."""
|
|
|
|
| 17 |
import csv
|
| 18 |
import io
|
| 19 |
from datetime import datetime, timezone
|
| 20 |
+
import threading
|
| 21 |
|
| 22 |
try:
|
| 23 |
from huggingface_hub import HfApi, hf_hub_download
|
|
|
|
| 36 |
TOKEN = os.environ.get("HF_TOKEN") # set as a Space secret
|
| 37 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 38 |
|
|
|
|
| 39 |
def log_visit(space: str = SPACE_NAME):
|
| 40 |
+
"""Fire-and-forget visit logging in a background thread."""
|
| 41 |
+
thread = threading.Thread(target=_do_log_visit, args=(space,), daemon=True)
|
| 42 |
+
thread.start()
|
| 43 |
+
|
| 44 |
+
def _do_log_visit(space: str):
|
| 45 |
"""Append one row to visits.csv in the analytics dataset repo."""
|
| 46 |
if not TOKEN:
|
| 47 |
print("[visit_counter] HF_TOKEN not set — skipping visit log.")
|
|
|
|
| 74 |
writer.writerows(existing_rows)
|
| 75 |
|
| 76 |
# 4. Push back to dataset repo
|
| 77 |
+
try:
|
| 78 |
+
api.upload_file(
|
| 79 |
+
path_or_fileobj=buf.getvalue().encode(),
|
| 80 |
+
path_in_repo=ANALYTICS_FILE,
|
| 81 |
+
repo_id=ANALYTICS_REPO,
|
| 82 |
+
repo_type="dataset",
|
| 83 |
+
commit_message=f"visit: {space} @ {now}",
|
| 84 |
+
)
|
| 85 |
+
print(f"[visit_counter] Logged visit for {space} at {now}")
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"[visit_counter] Warning: could not log visit: {e}")
|
| 88 |
|
| 89 |
def get_visit_counts() -> dict:
|
| 90 |
"""Return a dict of {space_name: visit_count} from the analytics repo."""
|