Cistern commited on
Commit
2bf72cb
·
verified ·
1 Parent(s): 6ae3e2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -3
app.py CHANGED
@@ -13,7 +13,6 @@ from concurrent.futures import ThreadPoolExecutor
13
  # ----------------------------------------------------------------------
14
  # 1. ZEROGPU REQUIREMENT (MUST BE AT THE TOP)
15
  # ----------------------------------------------------------------------
16
- # ZeroGPU's AST scanner requires this to be declared early in the file
17
  import spaces
18
 
19
  @spaces.GPU(duration=10)
@@ -51,11 +50,15 @@ HF_REPO_TYPE = "dataset"
51
 
52
  LOCAL_TEMP_DIR = Path("./hf_mirror_temp")
53
  LOG_FILE = "mirror_activity.log"
 
54
  BATCH_SIZE_MB = 100
55
  MAX_COMMITS_PER_HOUR = 124
56
  DOWNLOAD_WORKERS = 4
57
  TOR_SOCKS_PORT = 9050
58
 
 
 
 
59
  # Reset log file on startup
60
  with open(LOG_FILE, "w") as f:
61
  f.write("System initialized. Starting background mirror...\n")
@@ -240,7 +243,15 @@ def download_with_aria2(url, dest_path):
240
  return False
241
 
242
  def crawl_onion_directory(url, rel_path=""):
243
- # Give the user feedback so it doesn't look like it's stuck during deep crawls
 
 
 
 
 
 
 
 
244
  log(f"📂 Scanning directory index: /{rel_path}")
245
 
246
  try:
@@ -268,10 +279,16 @@ def crawl_onion_directory(url, rel_path=""):
268
  else:
269
  yield (full_url, new_rel_path)
270
 
 
 
 
 
 
271
  # ----------------------------------------------------------------------
272
  # 8. MAIN MIRROR THREAD
273
  # ----------------------------------------------------------------------
274
  def run_mirror():
 
275
  try:
276
  dummy_gpu() # Execute once just in case the interceptor requires usage
277
  start_tor()
@@ -284,6 +301,14 @@ def run_mirror():
284
  verify_tor()
285
  LOCAL_TEMP_DIR.mkdir(parents=True, exist_ok=True)
286
 
 
 
 
 
 
 
 
 
287
  completed_files = get_remote_files()
288
  log(f"Resuming: {len(completed_files)} files already present.")
289
  log(f"Starting crawler at: {ONION_URL}")
@@ -299,7 +324,6 @@ def run_mirror():
299
  for file_url, rel_path in crawl_onion_directory(ONION_URL):
300
  if rel_path in completed_files:
301
  skipped_count += 1
302
- # Periodically log skipping so the user knows it's actively working
303
  if skipped_count % 100 == 0:
304
  log(f"⏩ Fast-forwarding: Skipped {skipped_count} files already on HF...")
305
  continue
 
13
  # ----------------------------------------------------------------------
14
  # 1. ZEROGPU REQUIREMENT (MUST BE AT THE TOP)
15
  # ----------------------------------------------------------------------
 
16
  import spaces
17
 
18
  @spaces.GPU(duration=10)
 
50
 
51
  LOCAL_TEMP_DIR = Path("./hf_mirror_temp")
52
  LOG_FILE = "mirror_activity.log"
53
+ COMPLETED_DIRS_FILE = Path("./completed_dirs.txt") # <-- NEW: checkpoint file
54
  BATCH_SIZE_MB = 100
55
  MAX_COMMITS_PER_HOUR = 124
56
  DOWNLOAD_WORKERS = 4
57
  TOR_SOCKS_PORT = 9050
58
 
59
+ # Global set to hold directory paths that were fully mirrored in previous runs
60
+ completed_dirs = set()
61
+
62
  # Reset log file on startup
63
  with open(LOG_FILE, "w") as f:
64
  f.write("System initialized. Starting background mirror...\n")
 
243
  return False
244
 
245
  def crawl_onion_directory(url, rel_path=""):
246
+ """
247
+ Recursively scan the onion directory and yield (file_url, relative_path).
248
+ Directories already present in completed_dirs are skipped entirely.
249
+ """
250
+ # Checkpoint: skip entire directory if already fully mirrored before
251
+ if rel_path in completed_dirs:
252
+ log(f"⏩ Skipping already mirrored directory: /{rel_path}")
253
+ return
254
+
255
  log(f"📂 Scanning directory index: /{rel_path}")
256
 
257
  try:
 
279
  else:
280
  yield (full_url, new_rel_path)
281
 
282
+ # After successfully processing all entries in this directory, mark it as completed
283
+ completed_dirs.add(rel_path)
284
+ with open(COMPLETED_DIRS_FILE, "a") as f:
285
+ f.write(rel_path + "\n")
286
+
287
  # ----------------------------------------------------------------------
288
  # 8. MAIN MIRROR THREAD
289
  # ----------------------------------------------------------------------
290
  def run_mirror():
291
+ global completed_dirs
292
  try:
293
  dummy_gpu() # Execute once just in case the interceptor requires usage
294
  start_tor()
 
301
  verify_tor()
302
  LOCAL_TEMP_DIR.mkdir(parents=True, exist_ok=True)
303
 
304
+ # Load previously completed directories (for instant resume)
305
+ if COMPLETED_DIRS_FILE.exists():
306
+ with open(COMPLETED_DIRS_FILE, "r") as f:
307
+ completed_dirs = set(line.strip() for line in f if line.strip())
308
+ log(f"⏩ Loaded {len(completed_dirs)} fully mirrored directories from previous run.")
309
+ else:
310
+ completed_dirs = set()
311
+
312
  completed_files = get_remote_files()
313
  log(f"Resuming: {len(completed_files)} files already present.")
314
  log(f"Starting crawler at: {ONION_URL}")
 
324
  for file_url, rel_path in crawl_onion_directory(ONION_URL):
325
  if rel_path in completed_files:
326
  skipped_count += 1
 
327
  if skipped_count % 100 == 0:
328
  log(f"⏩ Fast-forwarding: Skipped {skipped_count} files already on HF...")
329
  continue