ChristophSchuhmann's picture
Document 500 planned people, ten reference expressions and WebDataset inference
9bc4c0e verified
Raw History Blame
2.88 kB
"""Generate one planned person and ten expression samples directly into a TAR."""
import argparse
import hashlib
import io
import json
from pathlib import Path
import tarfile
import time
from urllib.request import urlopen
from PIL import Image
import torch
from native_flux_inference import create_native_pipeline, generate_native_image, encode_text
from background_production import add_member
from character_pilot import metadata
def main():
p=argparse.ArgumentParser();p.add_argument('--person',default='person-0253')
p.add_argument('--plan',type=Path);p.add_argument('--base-path',type=Path)
p.add_argument('--output',type=Path,default=Path('person-emotions.tar'))
p.add_argument('--revision',default='main',help='Dataset plan revision; model revisions are separately pinned')
p.add_argument('--no-compile',action='store_true');a=p.parse_args()
if a.plan:plan=json.loads(a.plan.read_text())
else:
url=f'https://huggingface.co/datasets/laion/Vivarium-Greenscreen-Characters-FLUX-9B/resolve/{a.revision}/metadata/person-plan.json'
with urlopen(url,timeout=90) as f:plan=json.load(f)
person=next(x for x in plan if x['personality_id']==a.person)
pipe=create_native_pipeline('characters',base_path=a.base_path,compile_mode=None if a.no_compile else 'default')
prompts={e['id']:encode_text(pipe,e['prompt']) for e in person['expressions']}
reference=None;reference_sha='';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:
for i,e in enumerate(person['expressions']):
torch.cuda.synchronize();start=time.perf_counter()
image=generate_native_image(pipe,prompt_embeds=prompts[e['id']],seed=e['seed'],steps=50,guidance=4.,
negative_prompt=e['negative_prompt'],image=reference if i else None,width=832,height=1248)[0]
torch.cuda.synchronize();seconds=time.perf_counter()-start
b=io.BytesIO();image.save(b,format='PNG',compress_level=4);png=b.getvalue()
m=metadata(person,e,png,reference_sha,seconds,0.,a.output.name,'portable-inference')
m['inference']['compile_mode']=None if a.no_compile else 'default'
m['runtime']['positive_text_encoding_seconds']=None
m['runtime']['includes_compile_warmup']=not a.no_compile and i<2
add_member(tar,e['id']+'.png',png);add_member(tar,e['id']+'.json',json.dumps(m,indent=2,ensure_ascii=False).encode())
add_member(tar,e['id']+'.txt',e['prompt'].encode())
if i==0:reference=image.resize((416,624),Image.Resampling.BICUBIC);reference_sha=hashlib.sha256(png).hexdigest()
print(e['id'],round(seconds,2),'seconds',flush=True)
temporary.replace(a.output)
if __name__=='__main__':main()