Download scripts/evren_import.py from KeremTT/hemato: direct link, hf CLI and curl.
- Browser
- Download file 2.68 kB
-
https://huggingface.co/datasets/KeremTT/hemato/resolve/main/scripts/evren_import.py
- Command line
-
hf download hf://datasets/KeremTT/hemato/scripts/evren_import.py
-
curl -L -o evren_import.py https://huggingface.co/datasets/KeremTT/hemato/resolve/main/scripts/evren_import.py
2.68 kB
| """Import ZIP parts into an Evren dataset via the Platform API chunked import flow. | |
| Usage: EVREN_KEY=... python3 evren_import.py DATASET_ID zip1 [zip2 ...]""" | |
| import json, os, sys, time | |
| import requests | |
| API = 'https://api.ssyz.org.tr/api/v1' | |
| KEY = os.environ['EVREN_KEY'] | |
| H = {'X-API-Key': KEY} | |
| CHUNK = 32 * 1024 * 1024 | |
| ds = sys.argv[1] | |
| def req(method, url, **kw): | |
| for attempt in range(6): | |
| try: | |
| r = requests.request(method, url, headers={**H, **kw.pop('headers', {})}, timeout=300, **kw) | |
| if r.status_code in (429, 502, 503, 504): | |
| time.sleep(int(r.headers.get('Retry-After', 2 ** attempt))); continue | |
| return r | |
| except requests.RequestException as e: | |
| print(' retry', attempt, e, flush=True); time.sleep(2 ** attempt) | |
| raise RuntimeError(f'{method} {url} failed') | |
| for path in sys.argv[2:]: | |
| size = os.path.getsize(path); name = os.path.basename(path) | |
| r = req('POST', f'{API}/datasets/{ds}/import/init', json={'filename': name, 'total_size': size, 'chunk_size': CHUNK, 'deduplicate': os.environ.get('DEDUP', '1') == '1'}) | |
| if not r.ok: sys.exit(f'init failed {r.status_code}: {r.text[:300]}') | |
| j = r.json(); iid, n = j['import_id'], j['total_chunks'] | |
| t0 = time.time() | |
| with open(path, 'rb') as f: | |
| for i in range(1, n + 1): | |
| data = f.read(CHUNK) | |
| r = req('PUT', f'{API}/datasets/{ds}/import/{iid}/chunk/{i}', data=data, headers={'Content-Type': 'application/octet-stream'}) | |
| if not r.ok: sys.exit(f'chunk {i} failed {r.status_code}: {r.text[:300]}') | |
| r = req('POST', f'{API}/datasets/{ds}/import/{iid}/complete') | |
| if not r.ok: sys.exit(f'complete failed {r.status_code}: {r.text[:300]}') | |
| print(f'{name}: uploaded {size/1e6:.0f} MB in {time.time()-t0:.0f}s, processing...', flush=True) | |
| last = None | |
| while True: | |
| time.sleep(10) | |
| r = req('GET', f'{API}/datasets/{ds}/import/{iid}/status') | |
| if r.status_code == 404: print(f'{name}: status 404 (probably finished)'); break | |
| s = r.json() | |
| msg = (s.get('state'), s.get('progress'), s.get('waiting')) | |
| if msg != last: print(f' {name}: {msg}', flush=True); last = msg | |
| if s.get('state') == 'done': | |
| res = s.get('result') or {} | |
| print(f'{name}: DONE {json.dumps(res, ensure_ascii=False)[:600]}', flush=True) | |
| if os.environ.get('EXPECT_FORMAT') and res.get('format') != os.environ['EXPECT_FORMAT']: | |
| sys.exit(f'{name}: UNEXPECTED FORMAT {res.get("format")} - stopping') | |
| break | |
| if s.get('state') == 'error': | |
| sys.exit(f'{name}: ERROR {s.get("error")}') | |