ACCC1380's picture
download
raw
3.66 kB
from pathlib import Path
import importlib.util,json,base64,hashlib,shutil,time,html
from PIL import Image
root=Path('/marimo/nyako-generation');out=root/'batch';out.mkdir(exist_ok=True)
source=Path('/marimo/api_generate (4).py');spec=importlib.util.spec_from_file_location('user_generation',source);mod=importlib.util.module_from_spec(spec);spec.loader.exec_module(mod)
base='http://127.0.0.1:7860'
checkpoint=mod.select_checkpoint(mod.api_call(base,'/sdapi/v1/sd-models'))
modules=mod.select_modules(mod.api_call(base,'/sdapi/v1/sd-modules'))
files=sorted(Path('/data/nyako-training/artifacts/dataset').rglob('*.txt'));assert len(files)==24
items=[]
def write_status(stage,current=None):
d={'stage':stage,'completed':sum(x['state']=='complete' for x in items),'total':len(files),'current':current,'items':items}
(root/'batch-status.json').write_text(json.dumps(d,indent=2));print(json.dumps({k:v for k,v in d.items() if k!='items'}),flush=True)
cards=[]
for x in items:
if x['state']=='complete':
name=Path(x['image']).name
cards.append(f'<a href="{html.escape(name)}"><img loading="lazy" src="{html.escape(name)}"><span>{x["index"]:02d} · {html.escape(x["caption_file"])}</span></a>')
(out/'index.html').write_text('<!doctype html><html lang="zh"><meta charset="utf-8"><meta http-equiv="refresh" content="20"><title>Nyako 生成进度</title><style>body{background:#181818;color:white;font:16px sans-serif;margin:24px}.grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(250px,1fr));gap:20px}a{color:white;text-decoration:none}img{width:100%;display:block}span{display:block;padding:8px}</style><h1>Nyako:'+str(d['completed'])+'/24</h1><p>36 步 + 高清修复 32 步 · 1536×2304 · LoRA 1.0</p><div class="grid">'+''.join(cards)+'</div></html>')
for index,caption in enumerate(files,1):
p=out/f'{index:02d}_{caption.stem}.png'; raw=caption.read_bytes();assert raw.startswith(b'@nyako, ')
payload=mod.make_payload(checkpoint,modules);payload['prompt']=raw.decode('utf-8-sig').strip()+'\n<lora:nyako_style:1>'
item={'index':index,'caption_file':caption.name,'caption_sha256':hashlib.sha256(raw).hexdigest(),'image':str(p),'state':'pending'};items.append(item)
# Reuse the already verified first result with identical caption and script settings.
if index==1 and not p.exists():
receipt=json.loads((root/'hires-status.json').read_text())
assert receipt['caption_sha256']==item['caption_sha256']
assert receipt['source_script_sha256']==hashlib.sha256(source.read_bytes()).hexdigest()
shutil.copy2(receipt['images'][0],p)
shutil.copy2(root/'hires-response-info.json',p.with_suffix('.info.json'))
if p.exists():
with Image.open(p) as im:assert im.size==(1536,2304);im.verify()
item['state']='complete';write_status('generating');continue
p.with_suffix('.request.json').write_text(json.dumps(payload,ensure_ascii=False,indent=2))
for attempt in range(1,4):
try:
write_status('generating',index)
result=mod.api_call(base,'/sdapi/v1/txt2img',payload,timeout=3600);assert result.get('images')
temp=p.with_suffix('.tmp');temp.write_bytes(base64.b64decode(result['images'][-1].split(',')[-1]))
with Image.open(temp) as im:assert im.size==(1536,2304);im.verify()
temp.replace(p);p.with_suffix('.info.json').write_text(result.get('info','{}'));item['state']='complete';write_status('generating');break
except Exception as exc:
p.with_suffix('.error.txt').write_text(str(exc));item['error_type']=type(exc).__name__
if attempt==3:item['state']='failed';write_status('generating');break
time.sleep(5)
write_status('complete' if all(x['state']=='complete' for x in items) else 'needs_retry')

Xet Storage Details

Size:
3.66 kB
·
Xet hash:
e1593a8f4813e1cfe9cf8fe5b456f33c3fc747a0b2d81d6954d6e9a858a18cf6

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.