| 41 | |
| 42 | |
| 43 | def _download(url: str, root: str): |
| 44 | os.makedirs(root, exist_ok=True) |
| 45 | filename = os.path.basename(url) |
| 46 | |
| 47 | expected_sha256 = url.split("/")[-2] |
| 48 | download_target = os.path.join(root, filename) |
| 49 | |
| 50 | if os.path.exists(download_target) and not os.path.isfile(download_target): |
| 51 | raise RuntimeError(f"{download_target} exists and is not a regular file") |
| 52 | |
| 53 | if os.path.isfile(download_target): |
| 54 | if insecure_hashlib.sha256(open(download_target, "rb").read()).hexdigest() == expected_sha256: |
| 55 | return download_target |
| 56 | else: |
| 57 | warnings.warn(f"{download_target} exists, but the SHA256 checksum does not match; re-downloading the file") |
| 58 | |
| 59 | with urllib.request.urlopen(url) as source, open(download_target, "wb") as output: |
| 60 | with tqdm( |
| 61 | total=int(source.info().get("Content-Length")), |
| 62 | ncols=80, |
| 63 | unit="iB", |
| 64 | unit_scale=True, |
| 65 | unit_divisor=1024, |
| 66 | ) as loop: |
| 67 | while True: |
| 68 | buffer = source.read(8192) |
| 69 | if not buffer: |
| 70 | break |
| 71 | |
| 72 | output.write(buffer) |
| 73 | loop.update(len(buffer)) |
| 74 | |
| 75 | if insecure_hashlib.sha256(open(download_target, "rb").read()).hexdigest() != expected_sha256: |
| 76 | raise RuntimeError("Model has been downloaded but the SHA256 checksum does not not match") |
| 77 | |
| 78 | return download_target |
| 79 | |
| 80 | |
| 81 | class ConsistencyDecoder: |