Spaces:
Sleeping
Sleeping
Download storage_selftest.py from ohgnues/image-editing-study: direct link, hf CLI and curl.
- Browser
- Download file 2.27 kB
-
https://huggingface.co/spaces/ohgnues/image-editing-study/resolve/main/storage_selftest.py
- Command line
-
hf download hf://spaces/ohgnues/image-editing-study/storage_selftest.py
-
curl -L -o storage_selftest.py https://huggingface.co/spaces/ohgnues/image-editing-study/resolve/main/storage_selftest.py
2.27 kB
| """Opt-in live test. Writes synthetic records outside responses/, never participant votes.""" | |
| import os, json, uuid, hashlib | |
| from concurrent.futures import ThreadPoolExecutor | |
| from pathlib import Path | |
| def run_selftest(save_payload, repo): | |
| from huggingface_hub import HfApi, hf_hub_download | |
| token = os.environ['HF_TOKEN'] | |
| api = HfApi(token=token) | |
| if not api.repo_info(repo, repo_type='dataset').private: | |
| raise RuntimeError('Response Dataset must be private') | |
| run = str(uuid.uuid4()) | |
| prefix = f'storage_selftest/{run}/' | |
| expected = {} | |
| for i in range(12): | |
| record = {'synthetic_storage_test': True, 'run': run, 'ordinal': i, | |
| 'participant_id': str(uuid.uuid4()), 'answers': {'instruction':'A','physics':'B','overall':'C'}} | |
| payload = json.dumps(record, sort_keys=True).encode() | |
| expected[prefix + hashlib.sha256(payload).hexdigest() + '.json'] = payload | |
| jobs = list(expected.items()) | |
| jobs += jobs[:6] # exact retries must not create six additional records | |
| try: | |
| with ThreadPoolExecutor(max_workers=12) as pool: | |
| list(pool.map(lambda item: save_payload(item[1], item[0]), jobs)) | |
| found = {p for p in api.list_repo_files(repo, repo_type='dataset') if p.startswith(prefix)} | |
| if found != set(expected): | |
| raise RuntimeError('Stored record count does not match') | |
| for name, payload in expected.items(): | |
| p = hf_hub_download(repo_id=repo, repo_type='dataset', filename=name, token=token, force_download=True) | |
| if Path(p).read_bytes() != payload: | |
| raise RuntimeError('Stored record integrity mismatch') | |
| except Exception: | |
| # Never print exception strings: an upstream exception can contain a URL or credentials. | |
| print('LIVE_STORAGE_SELFTEST FAILED; live storage not verified', flush=True) | |
| raise RuntimeError('Live storage verification failed; inspect private Dataset and retry') from None | |
| result = {'status': 'PASS', 'parallel_clients':12, 'requests':18, 'unique_records':12, | |
| 'exact_retries':6, 'download_hashes':'12/12 match', 'dataset_private': True} | |
| print('LIVE_STORAGE_SELFTEST ' + json.dumps(result, sort_keys=True), flush=True) | |
| return result | |