Spaces:
Running
on
Zero
Running
on
Zero
Update user_history.py
Browse files- user_history.py +0 -70
user_history.py
CHANGED
@@ -15,7 +15,6 @@ Useful links:
|
|
15 |
- Source file: https://huggingface.co/spaces/Wauplin/gradio-user-history/blob/main/user_history.py
|
16 |
- Discussions: https://huggingface.co/spaces/Wauplin/gradio-user-history/discussions
|
17 |
"""
|
18 |
-
import hashlib
|
19 |
import json
|
20 |
import os
|
21 |
import shutil
|
@@ -38,12 +37,6 @@ def setup(folder_path: str | Path | None = None) -> None:
|
|
38 |
user_history.folder_path = _resolve_folder_path(folder_path)
|
39 |
user_history.initialized = True
|
40 |
|
41 |
-
# Clean duplicates
|
42 |
-
try:
|
43 |
-
_clean_duplicates()
|
44 |
-
except Exception as e:
|
45 |
-
print(f"Failed to clean duplicates: {e}")
|
46 |
-
|
47 |
|
48 |
def render() -> None:
|
49 |
user_history = _UserHistory()
|
@@ -428,66 +421,3 @@ def _fetch_admins() -> List[str]:
|
|
428 |
if response.status_code == 200:
|
429 |
return sorted((member["user"] for member in response.json()), key=lambda x: x.lower())
|
430 |
return [namespace]
|
431 |
-
|
432 |
-
|
433 |
-
#######
|
434 |
-
#######
|
435 |
-
|
436 |
-
# TODO: remove this once from IllusionDiffusion once cleaned
|
437 |
-
|
438 |
-
|
439 |
-
def _clean_duplicates() -> None:
|
440 |
-
user_history = _UserHistory()
|
441 |
-
if not (user_history.initialized and user_history.folder_path.exists()):
|
442 |
-
# Must be initialized correctly
|
443 |
-
return
|
444 |
-
|
445 |
-
_lock = user_history.folder_path / "_clean_duplicates.lock"
|
446 |
-
_is_done_file = user_history.folder_path / "_clean_duplicates_is_done" # Only 1 replica will do it, once for all
|
447 |
-
|
448 |
-
with FileLock(_lock):
|
449 |
-
if _is_done_file.exists(): # if True, another replica already did it
|
450 |
-
return
|
451 |
-
|
452 |
-
for subpath in user_history.folder_path.iterdir():
|
453 |
-
if subpath.is_file():
|
454 |
-
continue
|
455 |
-
|
456 |
-
history_file = subpath / "history.jsonl"
|
457 |
-
if not history_file.exists():
|
458 |
-
continue
|
459 |
-
|
460 |
-
# Read history
|
461 |
-
images = [json.loads(line) for line in history_file.read_text().splitlines()]
|
462 |
-
|
463 |
-
# Select unique images
|
464 |
-
curated_images = []
|
465 |
-
seen_hashes = set()
|
466 |
-
seen_paths = set()
|
467 |
-
for image in images:
|
468 |
-
image_hash = _file_hash(Path(image["path"]))
|
469 |
-
if image_hash is None:
|
470 |
-
continue
|
471 |
-
if image_hash in seen_hashes:
|
472 |
-
continue
|
473 |
-
seen_hashes.add(image_hash)
|
474 |
-
seen_paths.add(Path(image["path"]))
|
475 |
-
curated_images.append(image)
|
476 |
-
|
477 |
-
# Remove duplicates + save history
|
478 |
-
for path in subpath.glob("images/*"):
|
479 |
-
if path not in seen_paths:
|
480 |
-
try:
|
481 |
-
path.unlink()
|
482 |
-
except OSError:
|
483 |
-
pass
|
484 |
-
history_file.write_text("\n".join(json.dumps(image) for image in curated_images))
|
485 |
-
|
486 |
-
_is_done_file.touch()
|
487 |
-
|
488 |
-
|
489 |
-
def _file_hash(path: Path) -> str | None:
|
490 |
-
"""Return the hash of a file. No need to read by chunks."""
|
491 |
-
if path.is_file():
|
492 |
-
return hashlib.md5(path.read_bytes()).hexdigest()
|
493 |
-
return None
|
|
|
15 |
- Source file: https://huggingface.co/spaces/Wauplin/gradio-user-history/blob/main/user_history.py
|
16 |
- Discussions: https://huggingface.co/spaces/Wauplin/gradio-user-history/discussions
|
17 |
"""
|
|
|
18 |
import json
|
19 |
import os
|
20 |
import shutil
|
|
|
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()
|
|
|
421 |
if response.status_code == 200:
|
422 |
return sorted((member["user"] for member in response.json()), key=lambda x: x.lower())
|
423 |
return [namespace]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|