Datasets:
Upload source/auto_upload.py with huggingface_hub
Browse files- source/auto_upload.py +80 -0
source/auto_upload.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Auto-upload completed crawl chunks to HuggingFace.
|
| 3 |
+
Runs in a loop, checks every 30 min for finished chunks >= 100MB.
|
| 4 |
+
A chunk is 'finished' if it hasn't been modified in the last 5 minutes.
|
| 5 |
+
"""
|
| 6 |
+
import os, time, subprocess, glob, sys
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
HF_TOKEN = 'HF_TOKEN_REDACTED'
|
| 10 |
+
REPO = 'OpenTransformer/web-crawl-2026'
|
| 11 |
+
MIN_SIZE_MB = 100
|
| 12 |
+
STALE_SECS = 300 # 5 min no modification = finished
|
| 13 |
+
CHECK_INTERVAL = 1800 # 30 min
|
| 14 |
+
|
| 15 |
+
DIRS = {
|
| 16 |
+
'/workspace/scraped_data/': 'data/',
|
| 17 |
+
'/workspace/staging/': 'data/',
|
| 18 |
+
'/workspace/scraped_data_go/': 'data/',
|
| 19 |
+
'/workspace/scraped_data_rust/': 'data/',
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
def log(msg):
|
| 23 |
+
print(f'{datetime.utcnow().isoformat()} {msg}', flush=True)
|
| 24 |
+
|
| 25 |
+
def upload_file(local_path, hf_path):
|
| 26 |
+
"""Upload a single file to HF using huggingface_hub."""
|
| 27 |
+
from huggingface_hub import HfApi
|
| 28 |
+
api = HfApi(token=HF_TOKEN)
|
| 29 |
+
size_mb = os.path.getsize(local_path) / (1024*1024)
|
| 30 |
+
log(f'Uploading {os.path.basename(local_path)} ({size_mb:.0f}MB) -> {hf_path}')
|
| 31 |
+
api.upload_file(
|
| 32 |
+
path_or_fileobj=local_path,
|
| 33 |
+
path_in_repo=hf_path,
|
| 34 |
+
repo_id=REPO,
|
| 35 |
+
repo_type='dataset',
|
| 36 |
+
)
|
| 37 |
+
log(f'Upload complete: {hf_path}')
|
| 38 |
+
return True
|
| 39 |
+
|
| 40 |
+
def check_and_upload():
|
| 41 |
+
now = time.time()
|
| 42 |
+
uploaded = 0
|
| 43 |
+
for local_dir, hf_prefix in DIRS.items():
|
| 44 |
+
if not os.path.isdir(local_dir):
|
| 45 |
+
continue
|
| 46 |
+
for f in glob.glob(os.path.join(local_dir, '*.jsonl.gz')):
|
| 47 |
+
size = os.path.getsize(f)
|
| 48 |
+
mtime = os.path.getmtime(f)
|
| 49 |
+
age = now - mtime
|
| 50 |
+
size_mb = size / (1024*1024)
|
| 51 |
+
|
| 52 |
+
# Skip if too small or still being written
|
| 53 |
+
if size_mb < MIN_SIZE_MB:
|
| 54 |
+
log(f'Skip {os.path.basename(f)}: {size_mb:.0f}MB < {MIN_SIZE_MB}MB min')
|
| 55 |
+
continue
|
| 56 |
+
if age < STALE_SECS:
|
| 57 |
+
log(f'Skip {os.path.basename(f)}: still active ({age:.0f}s since mod)')
|
| 58 |
+
continue
|
| 59 |
+
|
| 60 |
+
# Upload
|
| 61 |
+
hf_path = hf_prefix + os.path.basename(f)
|
| 62 |
+
try:
|
| 63 |
+
upload_file(f, hf_path)
|
| 64 |
+
# Delete after successful upload to save disk
|
| 65 |
+
os.remove(f)
|
| 66 |
+
log(f'Deleted local: {f}')
|
| 67 |
+
uploaded += 1
|
| 68 |
+
except Exception as e:
|
| 69 |
+
log(f'ERROR uploading {f}: {e}')
|
| 70 |
+
return uploaded
|
| 71 |
+
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
log('Auto-upload daemon started')
|
| 74 |
+
while True:
|
| 75 |
+
try:
|
| 76 |
+
n = check_and_upload()
|
| 77 |
+
log(f'Check complete: {n} files uploaded')
|
| 78 |
+
except Exception as e:
|
| 79 |
+
log(f'ERROR in check cycle: {e}')
|
| 80 |
+
time.sleep(CHECK_INTERVAL)
|