Datasets:
Upload source/upload_big.py with huggingface_hub
Browse files- source/upload_big.py +135 -0
source/upload_big.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Upload daemon that accumulates crawl chunks until >= 1GB, then combines and uploads."""
|
| 3 |
+
import os, time, glob, datetime, json, shutil
|
| 4 |
+
from huggingface_hub import HfApi
|
| 5 |
+
|
| 6 |
+
TOKEN = 'HF_TOKEN_REDACTED'
|
| 7 |
+
REPO = 'OpenTransformer/web-crawl-2026'
|
| 8 |
+
MIN_BATCH_SIZE = 1 * 1024 * 1024 * 1024 # 1GB minimum before uploading
|
| 9 |
+
MAX_AGE_HOURS = 24 # force upload if oldest file > 24h old even if < 1GB
|
| 10 |
+
STALE_SEC = 300 # file must be untouched for 5 min to be considered complete
|
| 11 |
+
POLL_SEC = 120 # check every 2 minutes
|
| 12 |
+
DIRS = ['/workspace/scraped_data_go', '/workspace/scraped_data', '/workspace/staging', '/workspace/scraped_data_rust']
|
| 13 |
+
STATE_FILE = '/workspace/upload_big_state.json'
|
| 14 |
+
|
| 15 |
+
api = HfApi(token=TOKEN)
|
| 16 |
+
|
| 17 |
+
def log(msg):
|
| 18 |
+
ts = datetime.datetime.utcnow().isoformat()
|
| 19 |
+
print(f'{ts} {msg}', flush=True)
|
| 20 |
+
|
| 21 |
+
def load_state():
|
| 22 |
+
try:
|
| 23 |
+
with open(STATE_FILE) as f:
|
| 24 |
+
return json.load(f)
|
| 25 |
+
except:
|
| 26 |
+
return {'uploaded': [], 'total_uploaded_bytes': 0}
|
| 27 |
+
|
| 28 |
+
def save_state(state):
|
| 29 |
+
with open(STATE_FILE, 'w') as f:
|
| 30 |
+
json.dump(state, f, indent=2)
|
| 31 |
+
|
| 32 |
+
def find_ready_files():
|
| 33 |
+
"""Find .gz files that are stale (not being written to)."""
|
| 34 |
+
ready = []
|
| 35 |
+
now = time.time()
|
| 36 |
+
state = load_state()
|
| 37 |
+
uploaded_set = set(state.get('uploaded', []))
|
| 38 |
+
for d in DIRS:
|
| 39 |
+
if not os.path.isdir(d):
|
| 40 |
+
continue
|
| 41 |
+
for f in sorted(glob.glob(os.path.join(d, '*.gz'))):
|
| 42 |
+
basename = os.path.basename(f)
|
| 43 |
+
if basename in uploaded_set:
|
| 44 |
+
continue
|
| 45 |
+
try:
|
| 46 |
+
sz = os.path.getsize(f)
|
| 47 |
+
age = now - os.path.getmtime(f)
|
| 48 |
+
except OSError:
|
| 49 |
+
continue
|
| 50 |
+
if sz < 1024 * 1024: # skip < 1MB
|
| 51 |
+
continue
|
| 52 |
+
if age < STALE_SEC: # still being written
|
| 53 |
+
continue
|
| 54 |
+
ready.append((f, sz, age))
|
| 55 |
+
return ready
|
| 56 |
+
|
| 57 |
+
def combine_files(files, output_path):
|
| 58 |
+
"""Concatenate gzip files into one."""
|
| 59 |
+
with open(output_path, 'wb') as out:
|
| 60 |
+
for fpath, _, _ in files:
|
| 61 |
+
with open(fpath, 'rb') as inp:
|
| 62 |
+
shutil.copyfileobj(inp, out, length=8*1024*1024)
|
| 63 |
+
return os.path.getsize(output_path)
|
| 64 |
+
|
| 65 |
+
def main():
|
| 66 |
+
log('Big upload daemon started (1GB batches, 2min poll)')
|
| 67 |
+
while True:
|
| 68 |
+
ready = find_ready_files()
|
| 69 |
+
if not ready:
|
| 70 |
+
log('No ready files, sleeping...')
|
| 71 |
+
time.sleep(POLL_SEC)
|
| 72 |
+
continue
|
| 73 |
+
|
| 74 |
+
total_ready = sum(sz for _, sz, _ in ready)
|
| 75 |
+
max_age_h = max(age for _, _, age in ready) / 3600
|
| 76 |
+
log(f'Found {len(ready)} ready files, total {total_ready/(1024*1024):.0f}MB, oldest {max_age_h:.1f}h')
|
| 77 |
+
|
| 78 |
+
# Upload if total >= 1GB OR oldest file > 24h
|
| 79 |
+
if total_ready < MIN_BATCH_SIZE and max_age_h < MAX_AGE_HOURS:
|
| 80 |
+
log(f'Waiting for more data ({total_ready/(1024*1024*1024):.2f}GB / 1GB minimum, oldest {max_age_h:.1f}h / {MAX_AGE_HOURS}h max)')
|
| 81 |
+
time.sleep(POLL_SEC)
|
| 82 |
+
continue
|
| 83 |
+
|
| 84 |
+
# Combine and upload
|
| 85 |
+
ts = datetime.datetime.utcnow().strftime('%Y%m%d_%H%M%S')
|
| 86 |
+
combined_path = f'/workspace/crawl_batch_{ts}.jsonl.gz'
|
| 87 |
+
|
| 88 |
+
log(f'Combining {len(ready)} files into {combined_path}...')
|
| 89 |
+
combined_size = combine_files(ready, combined_path)
|
| 90 |
+
combined_gb = combined_size / (1024*1024*1024)
|
| 91 |
+
|
| 92 |
+
remote_name = f'crawl_batch_{ts}_{combined_gb:.1f}GB.jsonl.gz'
|
| 93 |
+
remote_path = f'crawl/combined/{remote_name}'
|
| 94 |
+
|
| 95 |
+
log(f'Uploading {combined_gb:.2f}GB -> {remote_path}')
|
| 96 |
+
try:
|
| 97 |
+
api.upload_file(
|
| 98 |
+
path_or_fileobj=combined_path,
|
| 99 |
+
path_in_repo=remote_path,
|
| 100 |
+
repo_id=REPO,
|
| 101 |
+
repo_type='dataset',
|
| 102 |
+
commit_message=f'Crawl batch {ts} ({combined_gb:.1f}GB, {len(ready)} chunks, {sum(1 for _ in ready)} files)',
|
| 103 |
+
)
|
| 104 |
+
log(f'Upload complete! Cleaning up local files...')
|
| 105 |
+
|
| 106 |
+
state = load_state()
|
| 107 |
+
for fpath, sz, _ in ready:
|
| 108 |
+
basename = os.path.basename(fpath)
|
| 109 |
+
state['uploaded'].append(basename)
|
| 110 |
+
state['total_uploaded_bytes'] = state.get('total_uploaded_bytes', 0) + sz
|
| 111 |
+
try:
|
| 112 |
+
os.remove(fpath)
|
| 113 |
+
log(f' Deleted {basename}')
|
| 114 |
+
except:
|
| 115 |
+
pass
|
| 116 |
+
save_state(state)
|
| 117 |
+
|
| 118 |
+
# Remove combined file
|
| 119 |
+
try:
|
| 120 |
+
os.remove(combined_path)
|
| 121 |
+
except:
|
| 122 |
+
pass
|
| 123 |
+
|
| 124 |
+
log(f'Batch upload done! Total uploaded so far: {state[total_uploaded_bytes]/(1024*1024*1024):.2f}GB')
|
| 125 |
+
except Exception as e:
|
| 126 |
+
log(f'Upload FAILED: {e}')
|
| 127 |
+
try:
|
| 128 |
+
os.remove(combined_path)
|
| 129 |
+
except:
|
| 130 |
+
pass
|
| 131 |
+
|
| 132 |
+
time.sleep(POLL_SEC)
|
| 133 |
+
|
| 134 |
+
if __name__ == '__main__':
|
| 135 |
+
main()
|