Spaces:
Running
Running
File size: 13,426 Bytes
56bef59 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | #!/usr/bin/env python3
from __future__ import annotations
import argparse
import hashlib
import json
import math
import re
import shutil
import time
from collections import Counter
from pathlib import Path
from typing import Dict, List, Tuple
try:
from PIL import Image, ImageDraw, ImageFont
except ImportError:
raise SystemExit("Missing pillow. Install with: pip install pillow")
HOME = Path.home()
DEFAULT_ROOTS = [
HOME / "kaggle",
HOME / "arc3_glyphmatic",
HOME / "arc_agi3_data",
HOME / "arc_tasks",
HOME,
]
KEYWORDS = [
"arc",
"agi3",
"kaggle",
"submission",
"final",
"dual",
"plan",
"score",
"optimized",
"agent",
"notebook",
"v5",
"v6",
"v30",
"forge",
"glyph",
]
FONT_CANDIDATES = [
"/data/data/com.termux/files/usr/share/fonts/TTF/DejaVuSansMono.ttf",
"/data/data/com.termux/files/usr/share/fonts/TTF/DejaVuSans.ttf",
"/system/fonts/RobotoMono-Regular.ttf",
"/system/fonts/DroidSansMono.ttf",
]
def sha_text(s: str) -> str:
return hashlib.sha256(s.encode("utf-8")).hexdigest()
def safe_rel(p: Path) -> str:
try:
return str(p.relative_to(HOME))
except Exception:
return str(p)
def find_notebooks(roots: List[Path]) -> List[Path]:
seen = set()
out = []
skip_parts = {
".git",
".cache",
"node_modules",
"venv",
".venv",
"tok-venv",
"vilenv",
"site-packages",
"__pycache__",
}
for root in roots:
if not root.exists():
continue
for p in root.rglob("*.ipynb"):
parts = set(p.parts)
if parts & skip_parts:
continue
rp = p.resolve()
if str(rp) in seen:
continue
seen.add(str(rp))
out.append(rp)
return out
def score_notebook(path: Path) -> float:
s = str(path).lower()
score = 0.0
for kw in KEYWORDS:
if kw in s:
score += 10.0
try:
size = path.stat().st_size
mtime = path.stat().st_mtime
except Exception:
return score
# Prefer real notebooks but avoid huge output-only notebooks.
score += min(size / 50_000, 20)
# Recency boost.
age_days = max(0, (time.time() - mtime) / 86400)
score += max(0, 30 - age_days)
# Penalize checkpoints.
if ".ipynb_checkpoints" in s:
score -= 100
return score
def load_notebook_code(path: Path) -> Tuple[str, List[str]]:
try:
obj = json.loads(path.read_text(encoding="utf-8", errors="ignore"))
except Exception:
return "", []
cells = obj.get("cells", [])
blocks = []
for i, cell in enumerate(cells):
if cell.get("cell_type") != "code":
continue
src = cell.get("source", "")
if isinstance(src, list):
text = "".join(src)
else:
text = str(src)
text = text.rstrip()
if not text.strip():
continue
blocks.append(f"# ---- CELL {i} ----\n{text}")
code = "\n\n".join(blocks).strip() + "\n"
lines = code.splitlines()
return code, lines
def token_name(i: int) -> str:
# OCR-stable ASCII glyph token.
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
a = alphabet[(i // 26) % 26]
b = alphabet[i % 26]
return f"G{a}{b}"
def build_dictionary(all_lines: List[str], max_tokens: int = 512) -> Dict[str, str]:
counts = Counter(all_lines)
candidates = []
for line, count in counts.items():
stripped = line.strip()
if count < 2:
continue
if len(stripped) < 12:
continue
if stripped.startswith("# ---- CELL"):
continue
# Estimated savings: repeated literal length minus token length.
savings = (len(line) - 5) * (count - 1)
if savings > 0:
candidates.append((savings, count, line))
candidates.sort(reverse=True, key=lambda x: (x[0], x[1], len(x[2])))
dictionary = {}
for i, (_, _, line) in enumerate(candidates[:max_tokens]):
dictionary[token_name(i)] = line
return dictionary
def escape_literal(s: str) -> str:
# JSON string gives exact text while staying visible.
return json.dumps(s, ensure_ascii=False)
def unescape_literal(s: str) -> str:
return json.loads(s)
def encode_notebooks(notebooks: List[Path], out_dir: Path, max_tokens: int) -> Dict:
out_dir.mkdir(parents=True, exist_ok=True)
records = []
all_lines = []
for p in notebooks:
code, lines = load_notebook_code(p)
if not code.strip():
continue
rec = {
"path": safe_rel(p),
"source_file": str(p),
"code_sha256": sha_text(code),
"code_bytes": len(code.encode("utf-8")),
"line_count": len(lines),
"lines": lines,
}
records.append(rec)
all_lines.extend(lines)
dictionary = build_dictionary(all_lines, max_tokens=max_tokens)
reverse = {v: k for k, v in dictionary.items()}
glyphlines = []
glyphlines.append("GMARC3V1")
glyphlines.append("MODE FUNCTIONAL_CODE_ONLY")
glyphlines.append("RULE GLYPHMATICS_STRUCTURAL_COMPRESSION_ONLY")
glyphlines.append("NO_BASE64")
glyphlines.append("NO_BYTE_WRAPPER")
glyphlines.append("NO_HIDDEN_PAYLOAD")
glyphlines.append(f"NOTEBOOKS {len(records)}")
glyphlines.append(f"DICT {len(dictionary)}")
for tok, line in dictionary.items():
glyphlines.append(f"D {tok} {escape_literal(line)}")
for idx, rec in enumerate(records):
glyphlines.append(f"N {idx} {escape_literal(rec['path'])} {rec['code_sha256']} {rec['code_bytes']} {rec['line_count']}")
for line in rec["lines"]:
if line in reverse:
glyphlines.append(f"T {reverse[line]}")
else:
glyphlines.append(f"L {escape_literal(line)}")
glyphlines.append("ENDN")
glyph_text = "\n".join(glyphlines) + "\n"
glyph_sha = sha_text(glyph_text)
raw_code_bytes = sum(r["code_bytes"] for r in records)
glyph_bytes = len(glyph_text.encode("utf-8"))
ratio = raw_code_bytes / glyph_bytes if glyph_bytes else 0.0
reduction = 100.0 - ((glyph_bytes / raw_code_bytes) * 100.0) if raw_code_bytes else 0.0
manifest = {
"format": "GlyphMatics ARCAGI3 Notebook Structural Glyphpack",
"version": "0.1.0",
"glyphstring": "pARC3Zvhz",
"mode": "functional_code_only",
"notebook_count": len(records),
"dictionary_entries": len(dictionary),
"raw_code_bytes": raw_code_bytes,
"glyphline_bytes": glyph_bytes,
"semantic_compression_ratio": ratio,
"semantic_reduction_percent": reduction,
"glyphline_sha256": glyph_sha,
"records": [
{
"path": r["path"],
"code_sha256": r["code_sha256"],
"code_bytes": r["code_bytes"],
"line_count": r["line_count"],
}
for r in records
],
}
(out_dir / "arcagi3_top_notebooks.glyphlines.txt").write_text(glyph_text, encoding="utf-8")
(out_dir / "arcagi3_top_notebooks_manifest.json").write_text(json.dumps(manifest, indent=2), encoding="utf-8")
return {
"manifest": manifest,
"glyph_text": glyph_text,
"dictionary": dictionary,
"records": records,
}
def decode_glyphlines(glyph_text: str, out_dir: Path) -> Dict:
out_dir.mkdir(parents=True, exist_ok=True)
dictionary = {}
current = None
outputs = []
for raw in glyph_text.splitlines():
line = raw.rstrip("\n")
if not line:
continue
if line.startswith("D "):
_, tok, literal = line.split(" ", 2)
dictionary[tok] = unescape_literal(literal)
continue
if line.startswith("N "):
parts = line.split(" ", 5)
idx = parts[1]
path_literal = parts[2]
expected_sha = parts[3]
expected_bytes = int(parts[4])
expected_lines = int(parts[5])
current = {
"idx": idx,
"path": unescape_literal(path_literal),
"expected_sha": expected_sha,
"expected_bytes": expected_bytes,
"expected_lines": expected_lines,
"lines": [],
}
continue
if line == "ENDN":
if current is None:
continue
code = "\n".join(current["lines"]) + "\n"
actual_sha = sha_text(code)
ok = actual_sha == current["expected_sha"]
name = Path(current["path"]).name
if name.endswith(".ipynb"):
name = name[:-6]
out_path = out_dir / f"{current['idx']}_{name}.py"
out_path.write_text(code, encoding="utf-8")
outputs.append({
"path": current["path"],
"out": str(out_path),
"expected_sha": current["expected_sha"],
"actual_sha": actual_sha,
"ok": ok,
"bytes": len(code.encode("utf-8")),
})
current = None
continue
if current is not None:
if line.startswith("T "):
tok = line.split(" ", 1)[1]
current["lines"].append(dictionary[tok])
elif line.startswith("L "):
current["lines"].append(unescape_literal(line[2:]))
return {
"restored": outputs,
"ok": all(x["ok"] for x in outputs),
"count": len(outputs),
}
def choose_font(font_size: int):
for fp in FONT_CANDIDATES:
p = Path(fp)
if p.exists():
try:
return ImageFont.truetype(str(p), font_size)
except Exception:
pass
return ImageFont.load_default()
def render_text_image(text: str, out_path: Path, font_size: int = 16, margin: int = 28, line_gap: int = 4, max_width_px: int = 1800) -> Dict:
font = choose_font(font_size)
# Wrap long visible lines so image remains readable/OCR-able.
wrapped = []
for line in text.splitlines():
if len(line) <= 180:
wrapped.append(line)
else:
# Use continuation prefix. Decoder from OCR is not implemented for wrapped lines,
# but the text file remains exact. Image is transport/inspection artifact.
for i in range(0, len(line), 180):
prefix = "" if i == 0 else "↪"
wrapped.append(prefix + line[i:i+180])
dummy = Image.new("L", (10, 10), 255)
d = ImageDraw.Draw(dummy)
line_h = math.ceil(font_size * 1.28)
width = min(max_width_px, max(600, max(int(d.textlength(x, font=font)) for x in wrapped) + margin * 2))
height = max(200, len(wrapped) * (line_h + line_gap) + margin * 2)
img = Image.new("L", (width, height), 255)
draw = ImageDraw.Draw(img)
y = margin
for line in wrapped:
draw.text((margin, y), line, fill=0, font=font)
y += line_h + line_gap
out_path.parent.mkdir(parents=True, exist_ok=True)
img.save(out_path)
return {
"image": str(out_path),
"width": width,
"height": height,
"rendered_lines": len(wrapped),
}
def main():
ap = argparse.ArgumentParser(description="Encode top ARCAGI3 notebooks into GlyphMatics structural glyphlines and a single image.")
ap.add_argument("--top", type=int, default=12)
ap.add_argument("--max-tokens", type=int, default=512)
ap.add_argument("--out", default=str(HOME / "arcagi3_glyph_encoded"))
ap.add_argument("--render", action="store_true")
ap.add_argument("--decode-test", action="store_true")
args = ap.parse_args()
out_dir = Path(args.out).expanduser()
notebooks = find_notebooks(DEFAULT_ROOTS)
ranked = sorted(notebooks, key=score_notebook, reverse=True)
selected = ranked[:args.top]
if not selected:
raise SystemExit("No .ipynb files found.")
result = encode_notebooks(selected, out_dir, max_tokens=args.max_tokens)
manifest = result["manifest"]
image_info = None
if args.render:
image_info = render_text_image(
result["glyph_text"],
out_dir / "arcagi3_top_notebooks_glyphline_image.png",
font_size=16,
)
decode_info = None
if args.decode_test:
decode_info = decode_glyphlines(
result["glyph_text"],
out_dir / "restored_code",
)
print(json.dumps({
"status": "encoded",
"out_dir": str(out_dir),
"glyphlines": str(out_dir / "arcagi3_top_notebooks.glyphlines.txt"),
"manifest": str(out_dir / "arcagi3_top_notebooks_manifest.json"),
"image": image_info,
"decode_test": decode_info,
"notebooks": manifest["notebook_count"],
"raw_code_bytes": manifest["raw_code_bytes"],
"glyphline_bytes": manifest["glyphline_bytes"],
"semantic_compression_ratio": manifest["semantic_compression_ratio"],
"semantic_reduction_percent": manifest["semantic_reduction_percent"],
"selected": manifest["records"],
}, indent=2, ensure_ascii=False))
if __name__ == "__main__":
main()
|