omega-tool-model
First-party Omega tool-use model source, serving harnesses, and verification records
Brought by drakestapleton
omega-tool-model / deployment / conversation / download.py
| 1 | import os |
| 2 | """External resumable download instrument; never a production dependency.""" |
| 3 | from pathlib import Path |
| 4 | import concurrent.futures,hashlib,json,os,signal,threading,time,urllib.request |
| 5 | R=Path(__file__).resolve().parent;meta=json.loads((R/'MODEL.json').read_text()) |
| 6 | dst=(Path(os.path.expanduser('~')) / '/.local/share/omega-tool-model/chat-model'.lstrip('/'))/meta['file'];part=dst.with_suffix('.gguf.part') |
| 7 | for proc in Path('/proc').iterdir(): |
| 8 | if not proc.name.isdigit():continue |
| 9 | try:args=(proc/'cmdline').read_bytes().split(b'\0') |
| 10 | except OSError:continue |
| 11 | if args and args[0]==b'curl' and str(part).encode() in args:os.kill(int(proc.name),signal.SIGTERM) |
| 12 | time.sleep(.2) |
| 13 | state=R/'download-progress.json' |
| 14 | if state.exists():progress=json.loads(state.read_text()) |
| 15 | else:progress={'prefix':part.stat().st_size if part.exists() else 0,'done':[],'started':time.time()} |
| 16 | fd=os.open(part,os.O_RDWR|os.O_CREAT,0o600);os.ftruncate(fd,meta['bytes']);lock=threading.Lock() |
| 17 | url=f'https://huggingface.co/{meta["repo"]}/resolve/{meta["revision"]}/{meta["file"]}' |
| 18 | size=32*1024*1024;chunks=[(a,min(a+size,meta['bytes'])) for a in range(progress['prefix'],meta['bytes'],size)] |
| 19 | def save(): |
| 20 | progress['downloaded_bytes']=progress['prefix']+sum(min(a+size,meta['bytes'])-a for a in progress['done']);progress['updated']=time.time() |
| 21 | tmp=state.with_suffix('.tmp');tmp.write_text(json.dumps(progress));tmp.replace(state) |
| 22 | save() |
| 23 | def get(pair): |
| 24 | a,b=pair |
| 25 | if a in progress['done']:return |
| 26 | for attempt in range(5): |
| 27 | try: |
| 28 | req=urllib.request.Request(url,headers={'Range':f'bytes={a}-{b-1}'}) |
| 29 | with urllib.request.urlopen(req,timeout=40) as r: |
| 30 | assert r.status==206 and r.headers.get('Content-Range')==f'bytes {a}-{b-1}/{meta["bytes"]}' |
| 31 | pos=a |
| 32 | while pos<b: |
| 33 | data=r.read(min(1024*1024,b-pos));assert data,'unexpected EOF' |
| 34 | sent=0 |
| 35 | while sent<len(data):sent+=os.pwrite(fd,data[sent:],pos+sent) |
| 36 | pos+=len(data) |
| 37 | with lock:progress['done'].append(a);save() |
| 38 | return |
| 39 | except Exception: |
| 40 | if attempt==4:raise |
| 41 | time.sleep(1+attempt) |
| 42 | try: |
| 43 | with concurrent.futures.ThreadPoolExecutor(max_workers=12) as pool:list(pool.map(get,chunks)) |
| 44 | os.fsync(fd) |
| 45 | finally:os.close(fd) |
| 46 | with part.open('rb') as f:digest=hashlib.file_digest(f,'sha256').hexdigest() |
| 47 | assert digest==meta['sha256'],digest |
| 48 | part.replace(dst);progress.update(complete=True,sha256=digest);save() |
| 49 | (dst.parent/'MODEL.json').write_text(json.dumps(meta,indent=2)+'\n') |
| 50 | print('Pinned conversational model download complete; full SHA-256 verified.',flush=True) |