Spaces:
Sleeping
Sleeping
Commit ·
5f3014b
1
Parent(s): 2c0a58f
history display
Browse files- storage.py +15 -7
storage.py
CHANGED
|
@@ -64,28 +64,36 @@ def load_history(user_password=""):
|
|
| 64 |
print("[+] Security Pass: Credentials verified. Loading environment histories...")
|
| 65 |
|
| 66 |
try:
|
|
|
|
| 67 |
repo_info = api.repo_info(repo_id=REPO_ID, repo_type="dataset", files_metadata=True)
|
|
|
|
| 68 |
chat_files_meta = []
|
| 69 |
if repo_info and hasattr(repo_info, 'siblings'):
|
| 70 |
for f in repo_info.siblings:
|
| 71 |
-
|
| 72 |
-
if
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
last_modified = getattr(f, 'last_modified', None)
|
| 75 |
chat_files_meta.append((chat_id, last_modified))
|
| 76 |
|
|
|
|
| 77 |
chat_files_meta.sort(key=lambda x: x[1] if x[1] is not None else x[0], reverse=True)
|
| 78 |
return [[item[0]] for item in chat_files_meta]
|
|
|
|
| 79 |
except Exception as metadata_error:
|
| 80 |
-
|
|
|
|
| 81 |
|
|
|
|
| 82 |
try:
|
| 83 |
-
|
| 84 |
-
files = fallback_api.list_repo_files(repo_id=REPO_ID, repo_type="dataset")
|
| 85 |
chat_files = [f.split("/")[-1].replace(".json", "") for f in files if f.startswith("chats/")]
|
| 86 |
return [[f] for f in sorted(chat_files, reverse=True)]
|
| 87 |
except Exception as fallback_error:
|
| 88 |
-
print(f"[!!]
|
| 89 |
return []
|
| 90 |
|
| 91 |
def save_chat(chat_id, history):
|
|
|
|
| 64 |
print("[+] Security Pass: Credentials verified. Loading environment histories...")
|
| 65 |
|
| 66 |
try:
|
| 67 |
+
# 1. Attempt to get rich metadata for chronological sorting
|
| 68 |
repo_info = api.repo_info(repo_id=REPO_ID, repo_type="dataset", files_metadata=True)
|
| 69 |
+
|
| 70 |
chat_files_meta = []
|
| 71 |
if repo_info and hasattr(repo_info, 'siblings'):
|
| 72 |
for f in repo_info.siblings:
|
| 73 |
+
# Safe startswith check
|
| 74 |
+
if f.rfind('chats/') == 0:
|
| 75 |
+
filename_idx = f.rfind('/') + 1
|
| 76 |
+
chat_id = f[filename_idx:].replace(".json", "")
|
| 77 |
+
|
| 78 |
+
# Safely extract last_modified timestamp if it exists
|
| 79 |
last_modified = getattr(f, 'last_modified', None)
|
| 80 |
chat_files_meta.append((chat_id, last_modified))
|
| 81 |
|
| 82 |
+
# Sort by timestamp if available; otherwise fall back to string name
|
| 83 |
chat_files_meta.sort(key=lambda x: x[1] if x[1] is not None else x[0], reverse=True)
|
| 84 |
return [[item[0]] for item in chat_files_meta]
|
| 85 |
+
|
| 86 |
except Exception as metadata_error:
|
| 87 |
+
# Log the error so it's visible in your HF Space logs instead of swallowing it
|
| 88 |
+
print(f"[!] Metadata fetch failed, switching to fallback string sort: {metadata_error}")
|
| 89 |
|
| 90 |
+
# 2. Fallback Layer: If metadata calls fail, immediately run original working string method
|
| 91 |
try:
|
| 92 |
+
files = api.list_repo_files(repo_id=REPO_ID, repo_type="dataset")
|
|
|
|
| 93 |
chat_files = [f.split("/")[-1].replace(".json", "") for f in files if f.startswith("chats/")]
|
| 94 |
return [[f] for f in sorted(chat_files, reverse=True)]
|
| 95 |
except Exception as fallback_error:
|
| 96 |
+
print(f"[!!] Total storage access failure: {fallback_error}")
|
| 97 |
return []
|
| 98 |
|
| 99 |
def save_chat(chat_id, history):
|