Spaces:
Running
Running
internomega-terrablue commited on
Commit ·
33a151e
1
Parent(s): a1da32d
Enabling logger for capturing storage error
Browse files- persistence/storage_service.py +28 -31
persistence/storage_service.py
CHANGED
|
@@ -2,15 +2,12 @@
|
|
| 2 |
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
-
import logging
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
from huggingface_hub import HfApi, hf_hub_download
|
| 9 |
|
| 10 |
from state import UserData
|
| 11 |
|
| 12 |
-
logger = logging.getLogger(__name__)
|
| 13 |
-
|
| 14 |
|
| 15 |
class StorageService:
|
| 16 |
REPO_ID = "Group-1-5010/notebooklm-data"
|
|
@@ -21,7 +18,7 @@ class StorageService:
|
|
| 21 |
def _get_token():
|
| 22 |
token = os.environ.get("HF_TOKEN")
|
| 23 |
if not token:
|
| 24 |
-
|
| 25 |
return token
|
| 26 |
|
| 27 |
@staticmethod
|
|
@@ -29,28 +26,27 @@ class StorageService:
|
|
| 29 |
"""Create the dataset repo if it doesn't already exist (once per process)."""
|
| 30 |
if StorageService._repo_ensured:
|
| 31 |
return
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
logger.info("Ensured dataset repo exists: %s", StorageService.REPO_ID)
|
| 41 |
-
except Exception as e:
|
| 42 |
-
logger.error("Failed to create dataset repo: %s", e)
|
| 43 |
|
| 44 |
@staticmethod
|
| 45 |
def save_user_data(user_data: UserData) -> None:
|
| 46 |
-
"""Serialize UserData to JSON and upload to HF Dataset repo.
|
|
|
|
|
|
|
|
|
|
| 47 |
token = StorageService._get_token()
|
| 48 |
-
if not token:
|
| 49 |
-
return
|
| 50 |
|
| 51 |
-
|
| 52 |
-
data_json = json.dumps(user_data.to_dict(), ensure_ascii=False, indent=2)
|
| 53 |
|
|
|
|
|
|
|
| 54 |
with tempfile.NamedTemporaryFile(
|
| 55 |
mode="w", suffix=".json", delete=False
|
| 56 |
) as tmp:
|
|
@@ -65,14 +61,13 @@ class StorageService:
|
|
| 65 |
repo_id=StorageService.REPO_ID,
|
| 66 |
repo_type=StorageService.REPO_TYPE,
|
| 67 |
)
|
| 68 |
-
|
| 69 |
-
except Exception as e:
|
| 70 |
-
logger.error("Failed to save user data: %s", e)
|
| 71 |
finally:
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
|
| 77 |
@staticmethod
|
| 78 |
def load_user_data(user_id: str, user_name: str) -> UserData | None:
|
|
@@ -80,8 +75,10 @@ class StorageService:
|
|
| 80 |
|
| 81 |
Returns None if the file doesn't exist (new user).
|
| 82 |
"""
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
return None
|
| 86 |
|
| 87 |
try:
|
|
@@ -93,9 +90,9 @@ class StorageService:
|
|
| 93 |
)
|
| 94 |
with open(path, "r") as f:
|
| 95 |
data = json.load(f)
|
| 96 |
-
|
| 97 |
return UserData.from_dict(data)
|
| 98 |
except Exception as e:
|
| 99 |
# EntryNotFoundError or network errors — treat as new user
|
| 100 |
-
|
| 101 |
return None
|
|
|
|
| 2 |
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
from huggingface_hub import HfApi, hf_hub_download
|
| 8 |
|
| 9 |
from state import UserData
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
class StorageService:
|
| 13 |
REPO_ID = "Group-1-5010/notebooklm-data"
|
|
|
|
| 18 |
def _get_token():
|
| 19 |
token = os.environ.get("HF_TOKEN")
|
| 20 |
if not token:
|
| 21 |
+
raise RuntimeError("HF_TOKEN not found in environment. Add it as a Secret in your HF Space settings.")
|
| 22 |
return token
|
| 23 |
|
| 24 |
@staticmethod
|
|
|
|
| 26 |
"""Create the dataset repo if it doesn't already exist (once per process)."""
|
| 27 |
if StorageService._repo_ensured:
|
| 28 |
return
|
| 29 |
+
api.create_repo(
|
| 30 |
+
repo_id=StorageService.REPO_ID,
|
| 31 |
+
repo_type=StorageService.REPO_TYPE,
|
| 32 |
+
private=True,
|
| 33 |
+
exist_ok=True,
|
| 34 |
+
)
|
| 35 |
+
StorageService._repo_ensured = True
|
| 36 |
+
print(f"[StorageService] Ensured dataset repo exists: {StorageService.REPO_ID}")
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
@staticmethod
|
| 39 |
def save_user_data(user_data: UserData) -> None:
|
| 40 |
+
"""Serialize UserData to JSON and upload to HF Dataset repo.
|
| 41 |
+
|
| 42 |
+
Raises on failure so the caller can show the error to the user.
|
| 43 |
+
"""
|
| 44 |
token = StorageService._get_token()
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
data_json = json.dumps(user_data.to_dict(), ensure_ascii=False, indent=2)
|
|
|
|
| 47 |
|
| 48 |
+
tmp_path = None
|
| 49 |
+
try:
|
| 50 |
with tempfile.NamedTemporaryFile(
|
| 51 |
mode="w", suffix=".json", delete=False
|
| 52 |
) as tmp:
|
|
|
|
| 61 |
repo_id=StorageService.REPO_ID,
|
| 62 |
repo_type=StorageService.REPO_TYPE,
|
| 63 |
)
|
| 64 |
+
print(f"[StorageService] Saved user data for '{user_data.user_id}'")
|
|
|
|
|
|
|
| 65 |
finally:
|
| 66 |
+
if tmp_path:
|
| 67 |
+
try:
|
| 68 |
+
os.unlink(tmp_path)
|
| 69 |
+
except Exception:
|
| 70 |
+
pass
|
| 71 |
|
| 72 |
@staticmethod
|
| 73 |
def load_user_data(user_id: str, user_name: str) -> UserData | None:
|
|
|
|
| 75 |
|
| 76 |
Returns None if the file doesn't exist (new user).
|
| 77 |
"""
|
| 78 |
+
try:
|
| 79 |
+
token = StorageService._get_token()
|
| 80 |
+
except RuntimeError:
|
| 81 |
+
print("[StorageService] HF_TOKEN not set — skipping load")
|
| 82 |
return None
|
| 83 |
|
| 84 |
try:
|
|
|
|
| 90 |
)
|
| 91 |
with open(path, "r") as f:
|
| 92 |
data = json.load(f)
|
| 93 |
+
print(f"[StorageService] Loaded user data for '{user_id}'")
|
| 94 |
return UserData.from_dict(data)
|
| 95 |
except Exception as e:
|
| 96 |
# EntryNotFoundError or network errors — treat as new user
|
| 97 |
+
print(f"[StorageService] No existing data for '{user_id}': {e}")
|
| 98 |
return None
|