"""Portable one-outfit inference with the dataset's neutral reference in memory.""" import argparse import hashlib import io import json from pathlib import Path import tarfile import time from urllib.request import urlopen,Request from PIL import Image import torch from background_production import add_member from character_pilot import metadata from native_flux_inference import create_native_pipeline,generate_native_image,encode_text REPO='laion/Vivarium-Greenscreen-Characters-FLUX-9B' def main(): p=argparse.ArgumentParser();p.add_argument('--id',default='person-0281-job-001');p.add_argument('--plan',type=Path) p.add_argument('--revision',default='main');p.add_argument('--reference',type=Path);p.add_argument('--base-path',type=Path) p.add_argument('--output',type=Path,default=Path('job-outfit.tar'));p.add_argument('--no-compile',action='store_true');a=p.parse_args() root=f'https://huggingface.co/datasets/{REPO}/resolve/{a.revision}/' plan=json.loads(a.plan.read_text()) if a.plan else json.load(urlopen(root+'metadata/job-outfits/image-plan.json',timeout=90)) r=next(r for r in plan if r['id']==a.id) if a.reference:data=a.reference.read_bytes() else: index_member='metadata/sample-index.json' if r['reference_source_phase']=='character-production' else 'metadata/extension/sample-index.json' entries=json.load(urlopen(root+index_member,timeout=90));entry=next(x for x in entries if x['id']==r['reference_id']) start=entry['image_offset'];end=start+entry['image_bytes']-1;req=Request(root+entry['repo_path'],headers={'Range':f'bytes={start}-{end}'}) with urlopen(req,timeout=90) as response: raw=response.read();data=raw if response.status==206 else raw[start:end+1] assert hashlib.sha256(data).hexdigest()==entry['image_sha256'] digest=hashlib.sha256(data).hexdigest() with Image.open(io.BytesIO(data)) as im:im.load();reference=im.convert('RGB').resize((416,624),Image.Resampling.BICUBIC) pipe=create_native_pipeline('characters',base_path=a.base_path,compile_mode=None if a.no_compile else 'default',adapter_scale=r['lora_scale']) emb=encode_text(pipe,r['prompt']);torch.cuda.synchronize();t=time.perf_counter() im=generate_native_image(pipe,prompt_embeds=emb,seed=r['seed'],steps=50,guidance=4.,negative_prompt=r['negative_prompt'],image=reference,width=832,height=1248)[0] torch.cuda.synchronize();seconds=time.perf_counter()-t;b=io.BytesIO();im.save(b,format='PNG',compress_level=4);pixels=b.getvalue() m=metadata(r,dict(id=r['id'],seed=r['seed']),pixels,digest,seconds,0.,a.output.name,'portable-job-inference') m['inference']['compile_mode']=None if a.no_compile else 'default';m['runtime']['includes_compile_warmup']=not a.no_compile a.output.parent.mkdir(parents=True,exist_ok=True);temporary=a.output.with_name(a.output.name+'.partial') with tarfile.open(temporary,'w',format=tarfile.USTAR_FORMAT) as tar: add_member(tar,r['id']+'.png',pixels);add_member(tar,r['id']+'.json',json.dumps(m,indent=2).encode());add_member(tar,r['id']+'.txt',r['prompt'].encode()) temporary.replace(a.output);print(r['id'],round(seconds,2),'seconds') if __name__=='__main__':main()