Spaces:
Runtime error
Runtime error
Update user_history.py
Browse files- user_history.py +1 -65
user_history.py
CHANGED
@@ -37,9 +37,6 @@ def setup(folder_path: str | Path | None = None) -> None:
|
|
37 |
user_history.folder_path = _resolve_folder_path(folder_path)
|
38 |
user_history.initialized = True
|
39 |
|
40 |
-
# TODO: remove this section once all Spaces have migrated
|
41 |
-
_migrate_history()
|
42 |
-
|
43 |
|
44 |
def render() -> None:
|
45 |
user_history = _UserHistory()
|
@@ -423,65 +420,4 @@ def _fetch_admins() -> List[str]:
|
|
423 |
response = requests.get(f"https://huggingface.co/api/organizations/{namespace}/members")
|
424 |
if response.status_code == 200:
|
425 |
return sorted((member["user"] for member in response.json()), key=lambda x: x.lower())
|
426 |
-
return [namespace]
|
427 |
-
|
428 |
-
|
429 |
-
################################################################
|
430 |
-
# Legacy helpers to migrate image structure to new data format #
|
431 |
-
################################################################
|
432 |
-
# TODO: remove this section once all Spaces have migrated
|
433 |
-
|
434 |
-
|
435 |
-
def _migrate_history():
|
436 |
-
"""Script to migrate user history from v0 to v1."""
|
437 |
-
legacy_history_path = _legacy_get_history_folder_path()
|
438 |
-
if not legacy_history_path.exists():
|
439 |
-
return
|
440 |
-
|
441 |
-
error_count = 0
|
442 |
-
for json_path in legacy_history_path.glob("*.json"):
|
443 |
-
username = json_path.stem
|
444 |
-
print(f"Migrating history for user {username}...")
|
445 |
-
error_count += _legacy_move_user_history(username)
|
446 |
-
print("Done.")
|
447 |
-
print(f"Migration complete. {error_count} error(s) happened.")
|
448 |
-
|
449 |
-
if error_count == 0:
|
450 |
-
shutil.rmtree(legacy_history_path, ignore_errors=True)
|
451 |
-
|
452 |
-
|
453 |
-
def _legacy_move_user_history(username: str) -> int:
|
454 |
-
history = _legacy_read_user_history(username)
|
455 |
-
error_count = 0
|
456 |
-
for image, prompt in reversed(history):
|
457 |
-
try:
|
458 |
-
save_image(label=prompt, image=image, profile={"preferred_username": username})
|
459 |
-
except Exception as e:
|
460 |
-
print("Issue while migrating image:", e)
|
461 |
-
error_count += 1
|
462 |
-
return error_count
|
463 |
-
|
464 |
-
|
465 |
-
def _legacy_get_history_folder_path() -> Path:
|
466 |
-
_folder = os.environ.get("HISTORY_FOLDER")
|
467 |
-
if _folder is None:
|
468 |
-
_folder = Path(__file__).parent / "history"
|
469 |
-
return Path(_folder)
|
470 |
-
|
471 |
-
|
472 |
-
def _legacy_read_user_history(username: str) -> List[Tuple[str, str]]:
|
473 |
-
"""Return saved history for that user."""
|
474 |
-
with _legacy_user_lock(username):
|
475 |
-
path = _legacy_user_history_path(username)
|
476 |
-
if path.exists():
|
477 |
-
return json.loads(path.read_text())
|
478 |
-
return [] # No history yet
|
479 |
-
|
480 |
-
|
481 |
-
def _legacy_user_history_path(username: str) -> Path:
|
482 |
-
return _legacy_get_history_folder_path() / f"{username}.json"
|
483 |
-
|
484 |
-
|
485 |
-
def _legacy_user_lock(username: str) -> FileLock:
|
486 |
-
"""Ensure history is not corrupted if concurrent calls."""
|
487 |
-
return FileLock(f"{_legacy_user_history_path(username)}.lock")
|
|
|
37 |
user_history.folder_path = _resolve_folder_path(folder_path)
|
38 |
user_history.initialized = True
|
39 |
|
|
|
|
|
|
|
40 |
|
41 |
def render() -> None:
|
42 |
user_history = _UserHistory()
|
|
|
420 |
response = requests.get(f"https://huggingface.co/api/organizations/{namespace}/members")
|
421 |
if response.status_code == 200:
|
422 |
return sorted((member["user"] for member in response.json()), key=lambda x: x.lower())
|
423 |
+
return [namespace]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|