okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ba8745e13
main
1#!/usr/bin/env python3 2"""mkimg.py OUT_DIR: a 672x672 test picture (left half red, right half blue, a black disc in the middle), its DSI1 file and 3the prompt ids (chat mode, GGUF template) with the picture's span (16 x 17 + 2 = 274 positions of the image token) in them. 4Writes OUT_DIR/img.dsi and OUT_DIR/img_prompts.json in serve_prompts.py's format ("pic": file).""" 5import json ,os ,struct ,subprocess ,sys ,tempfile 6import jinja2 7from jinja2 .sandbox import ImmutableSandboxedEnvironment 8out ,template ,shard ,tokenize = sys .argv [1 :5 ] 9W = H = 672 10rows = bytearray () 11for y in range (H ): 12for x in range (W ): 13if (x - 336 )** 2 + (y - 336 )** 2 < 80 ** 2 :rows += bytes ((0 ,0 ,0 )) 14elif x < 336 :rows += bytes ((220 ,20 ,20 )) 15else :rows += bytes ((20 ,20 ,220 )) 16n_h = n_w = 672 // 42 17span = n_h * (n_w + 1 )+ 2 18os .makedirs (out ,exist_ok = True ) 19env = ImmutableSandboxedEnvironment (trim_blocks = True ,lstrip_blocks = True ) 20env .filters ["tojson" ]= lambda x ,** k :json .dumps (x ,ensure_ascii = False ) 21env .globals ["raise_exception" ]= lambda m : (_ for _ in ()).throw (jinja2 .exceptions .TemplateError (m )) 22tpl = env .from_string (open (template ,encoding = "utf-8" ).read ()) 23text = tpl .render (messages = [{"role" :"user" ,"content" : [{"type" :"image_url" ,"image_url" : {"url" :"x" }}, 24 {"type" :"text" ,"text" :"What colors are the left half and the right half of this picture? Answer in one short sentence." }]}], 25tools = None ,thinking_mode = "chat" ,reasoning_effort = None ,add_generation_prompt = True ) 26PH = "<\uff5cdeepseek_image\uff5c>" 27assert text .count (PH )== 1 ,text [:300 ] 28a ,b = text .split (PH ) 29def tok (t ): 30with tempfile .NamedTemporaryFile ("w" ,suffix = ".txt" ,delete = False ,encoding = "utf-8" )as f : 31f .write (t );p = f .name 32r = subprocess .run ([tokenize ,"-m" ,shard ,"-f" ,p ,"--ids" ,"--no-bos" ,"--no-escape" ],capture_output = True ,text = True ) 33os .unlink (p ) 34return json .loads ([l for l in r .stdout .splitlines ()if l .startswith ("[" )][- 1 ]) 35ia ,ib = tok (a ),tok (b ) 36ids = ia + [129264 ]* span + ib 37open (os .path .join (out ,"img.dsi" ),"wb" ).write (b"DSI1" + struct .pack ("<i" ,1 )+ struct .pack ("<qii" ,len (ia ),W ,H )+ bytes (rows )) 38json .dump ([{"id" :"image-colors" ,"mode" :"chat" ,"ids" :ids ,"max_tokens" :40 ,"expect_ids" : [], 39"pic" :os .path .join (out ,"img.dsi" )}],open (os .path .join (out ,"img_prompts.json" ),"w" )) 40"prompt" ,len (ids ),"tokens, picture span at" ,len (ia ),"length" ,span )