Download MTLCC/tools/patch_fetch.py from Dhruv1000/TSRDA: direct link, hf CLI and curl.
- Browser
- Download file 3.07 kB
-
https://huggingface.co/Dhruv1000/TSRDA/resolve/main/MTLCC/tools/patch_fetch.py
- Command line
-
hf download hf://Dhruv1000/TSRDA/MTLCC/tools/patch_fetch.py
-
curl -L -o patch_fetch.py https://huggingface.co/Dhruv1000/TSRDA/resolve/main/MTLCC/tools/patch_fetch.py
3.07 kB
| s=open('fetch_mtlcc.py').read() | |
| s=s.replace('''sel = [e for e in ents if e[0].startswith(PREFIX) and not e[0].endswith("/")] | |
| is17 = lambda n: "/data17/" in n | |
| sel = [e for e in sel if (is17(e[0]) if phase == "b" else not is17(e[0]))] | |
| sel.sort(key=lambda e: e[4]) | |
| ''','''is17 = lambda n: "/data17/" in n | |
| allp = sorted([e for e in ents if e[0].startswith(PREFIX)], key=lambda e: e[4]) | |
| want = lambda e: (not e[0].endswith("/")) and (is17(e[0]) if phase == "b" else not is17(e[0])) | |
| sel = [e for e in allp if want(e)] | |
| ''') | |
| s=s.replace('''todo = [e for e in sel if not (os.path.exists(dest(e[0])) and os.path.getsize(dest(e[0])) == e[3])] | |
| print(f"phase {phase}: {len(sel)} members, {len(todo)} to fetch, " | |
| f"{sum(e[2] for e in todo)/1e9:.2f} GB compressed", flush=True)''','''todo_set = set(e[0] for e in sel if not (os.path.exists(dest(e[0])) and os.path.getsize(dest(e[0])) == e[3])) | |
| todo = [e for e in allp if e[0].endswith("/") or e[0] in todo_set] | |
| print(f"phase {phase}: {len(sel)} members, {len(todo_set)} to fetch, " | |
| f"{sum(e[2] for e in todo if e[0] in todo_set)/1e9:.2f} GB compressed", flush=True)''') | |
| old=s[s.index('def worker(chunk):'):s.index('# split by')] | |
| new='''def worker(chunk): | |
| i = 0 | |
| while i < len(chunk) and not stop.is_set(): | |
| first = chunk[i] | |
| try: | |
| req = urllib.request.Request(URL, headers={"Range": f"bytes={first[4]}-{chunk[-1][4] + 4096 + chunk[-1][2]}"}) | |
| with urllib.request.urlopen(req, timeout=120) as r: | |
| pos = first[4] | |
| while i < len(chunk) and not stop.is_set(): | |
| name, cm, cs, us, lo = chunk[i] | |
| if lo != pos: break # gap -> reconnect at lo | |
| hdr = r.read(30) | |
| sig, = struct.unpack("<I", hdr[:4]) | |
| assert sig == 0x04034b50, "bad local header" | |
| fnl, exl = struct.unpack("<HH", hdr[26:30]) | |
| r.read(fnl + exl) | |
| raw = b"" | |
| while len(raw) < cs: | |
| b = r.read(min(1 << 20, cs - len(raw))) | |
| if not b: raise IOError("short read") | |
| raw += b | |
| pos = lo + 30 + fnl + exl + cs | |
| if not name.endswith("/"): | |
| data = raw if cm == 0 else zlib.decompress(raw, -15) | |
| assert len(data) == us, f"size mismatch {name}" | |
| if disk_pct() > LIMIT_PCT: | |
| print("DISK LIMIT REACHED - aborting", flush=True); stop.set(); return | |
| p = dest(name); os.makedirs(os.path.dirname(p), exist_ok=True) | |
| with open(p + ".part", "wb") as f: f.write(data) | |
| os.replace(p + ".part", p) | |
| with lock: done_bytes[0] += cs | |
| i += 1 | |
| except Exception as ex: | |
| print(f"retry seg after {type(ex).__name__}: {ex}", flush=True); time.sleep(5) | |
| ''' | |
| open('fetch_mtlcc.py','w').write(s.replace(old,new)) | |