"""500 identity groups with neutral references and ten paired TAR samples each.""" import argparse import hashlib import io import json import os from pathlib import Path import sqlite3 import tarfile import time from datetime import datetime,timezone,timedelta from background_production import add_member,sha,dump ROOT=Path(__file__).resolve().parents[1] STATE=ROOT/'experiments/character-production' BUFFER=Path('/tmp/vivarium-character-production') REPO='laion/Vivarium-Greenscreen-Characters-FLUX-9B' def utc():return datetime.now(timezone.utc).isoformat() def connect(): STATE.mkdir(parents=True,exist_ok=True) con=sqlite3.connect(STATE/'queue.sqlite',timeout=120);con.row_factory=sqlite3.Row con.execute('PRAGMA journal_mode=WAL');con.execute('PRAGMA busy_timeout=120000') return con def init(): con=connect() con.executescript(''' CREATE TABLE IF NOT EXISTS people (id TEXT PRIMARY KEY,position INTEGER,record TEXT,status TEXT DEFAULT 'queued',worker INTEGER,started TEXT,finished TEXT); CREATE TABLE IF NOT EXISTS images (id TEXT PRIMARY KEY,person_id TEXT,emotion TEXT,shard TEXT,metadata TEXT); CREATE TABLE IF NOT EXISTS shards (name TEXT PRIMARY KEY,worker INTEGER,count INTEGER,bytes INTEGER,sha256 TEXT,status TEXT,revision TEXT,created TEXT,uploaded TEXT); CREATE TABLE IF NOT EXISTS events (time TEXT,worker INTEGER,kind TEXT,detail TEXT); ''') plan=json.loads((STATE/'person-plan.json').read_text());groups={} for person in plan:groups.setdefault((person['age_group'],person['gender']),[]).append(person) order=[] for cycle in range(12): for first in [True,False]: for cell,(key,people) in enumerate(groups.items()): index=2*cycle+(cell%2 if first else 1-cell%2) if index=p.started""").fetchone()[0] return dict(utc=utc(),people=people,shards=shards,images_in_closed_shards=committed, images_generated=committed+max(open_count,0),target_people=500,target_images=5000, images_uploaded=con.execute("SELECT COALESCE(SUM(count),0) FROM shards WHERE status='uploaded'").fetchone()[0], buffer_free_gb=os.statvfs('/tmp').f_bavail*os.statvfs('/tmp').f_frsize/1e9) def ingest_pilot(): assert (STATE/'pilot-quality-passed.json').is_file(),'Review reference identity,emotion and outfits before accepting pilot' con=connect();plan={p['personality_id']:p for p in json.loads((STATE/'person-plan.json').read_text())} for path in sorted(BUFFER.glob('pilot-person-*.tar')): if con.execute('SELECT name FROM shards WHERE name=?',(path.name,)).fetchone():continue records=[] bg=sqlite3.connect(ROOT/'experiments/background-production/queue.sqlite') end=bg.execute("SELECT MAX(time) FROM events WHERE worker=7 AND kind='complete'").fetchone()[0];bg.close() assert end,'Background GPU7 must have completed before pilot ingestion' corrected=path.with_name(path.name+'.runtimefix') with tarfile.open(path,'r:') as src,tarfile.open(corrected,'w',format=tarfile.USTAR_FORMAT) as dst: for member in src: data=src.extractfile(member).read() if member.name.endswith('.json'): m=json.loads(data) start=datetime.fromisoformat(m['generated_utc'])-timedelta(seconds=m['runtime']['generation_seconds']) m['runtime']['gpu_shared_with_backgrounds']=start