File size: 8,011 Bytes
610364e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Module 1 schema: typed Davidsonian-style scene graph extracted from the OG image.

The VLM must emit JSON that validates against `DSGSceneSchema`. Validators are
lenient on surface form (grid-cell spelling, list lengths) but strict on
structure, so the downstream compiler always receives a well-formed graph.
"""

from __future__ import annotations

import re
from typing import Literal

from pydantic import BaseModel, Field, field_validator, model_validator

GridCell = Literal[
    "Top-Left", "Top-Center", "Top-Right",
    "Mid-Left", "Center", "Mid-Right",
    "Bot-Left", "Bot-Center", "Bot-Right",
]
GRID_ORDER: tuple[str, ...] = GridCell.__args__  # reading order: left-to-right, top-to-bottom

GRID_PHRASE = {
    "Top-Left": "top-left", "Top-Center": "top-center", "Top-Right": "top-right",
    "Mid-Left": "middle-left", "Center": "center", "Mid-Right": "middle-right",
    "Bot-Left": "bottom-left", "Bot-Center": "bottom-center", "Bot-Right": "bottom-right",
}

MAX_NODES = 12
MAX_ATTRIBUTES = 4
MAX_RELATIONS = 3
MAX_PALETTE = 8
MAX_EXTRA_CHECKS = 6


def normalize_grid_cell(value: str) -> str:
    """Map spellings like 'top left', 'middle-right', 'bottom center' onto GridCell."""
    s = re.sub(r"[\s_]+", "-", str(value).strip().lower())
    s = s.replace("middle", "mid").replace("bottom", "bot").replace("centre", "center")
    if s in {"center", "mid-center", "center-center", "mid-mid", "mid"}:
        return "Center"
    for cell in GRID_ORDER:
        if s == cell.lower():
            return cell
    raise ValueError(f"unknown grid cell {value!r}; use one of {', '.join(GRID_ORDER)}")


def _slug(value: str) -> str:
    return re.sub(r"[^a-z0-9]+", "_", value.lower()).strip("_") or "node"


_KEY_PREFIX = re.compile(r"^[A-Za-z][A-Za-z /_-]{1,24}:\s+")  # 'color: dark red' -> 'dark red'


def _clean_list(items: list[str], limit: int, strip_keys: bool = False) -> list[str]:
    if isinstance(items, str):
        items = [items]
    seen, out = set(), []
    for item in items:
        text = " ".join(str(item).split()).strip(" .;,")
        if strip_keys:
            text = _KEY_PREFIX.sub("", text)
        if text and text.lower() not in seen:
            seen.add(text.lower())
            out.append(text)
    return out[:limit]


class Relation(BaseModel):
    predicate: str = Field(
        description="Spatial or physical relation phrased so '<subject> <predicate> <target>' reads as a sentence, "
        "e.g. 'is left of', 'overlaps', 'is above', 'is partially behind', 'touches'."
    )
    target_id: str = Field(description="`id` of another node in this graph.")


class SpatialNode(BaseModel):
    id: str = Field(description="Unique snake_case identifier, e.g. 'red_circle'.")
    grid_cell: GridCell = Field(
        description="Cell of the 3x3 grid holding the node's visual center. One of: " + ", ".join(GRID_ORDER)
    )
    entity: str = Field(
        description="Objective visual form/object as an indefinite noun phrase (e.g. 'a large circle', "
        "'a sign reading \"PALACE\"'), without unverified era or style buzzwords."
    )
    attributes: list[str] = Field(
        default_factory=list,
        description=f"Up to {MAX_ATTRIBUTES} exact visual properties as bare phrases (e.g. 'crimson red', "
        "'sharp edges', 'rotated 45 degrees', 'about one third of the frame width'), never 'key: value'. "
        "Do not repeat what the entity noun already says.",
    )
    relations: list[Relation] = Field(
        default_factory=list,
        description=f"Up to {MAX_RELATIONS} spatial/physical relations to other nodes.",
    )

    @field_validator("grid_cell", mode="before")
    @classmethod
    def _grid(cls, v):
        return normalize_grid_cell(v)

    @field_validator("id", mode="before")
    @classmethod
    def _id(cls, v):
        return _slug(str(v))

    @field_validator("entity", mode="before")
    @classmethod
    def _entity(cls, v):
        text = " ".join(str(v).split()).strip(" .")
        if not text:
            raise ValueError("entity must be non-empty")
        return text

    @field_validator("attributes", mode="before")
    @classmethod
    def _attrs(cls, v):
        return _clean_list(v or [], MAX_ATTRIBUTES, strip_keys=True)

    @field_validator("relations", mode="after")
    @classmethod
    def _rels(cls, v):
        return v[:MAX_RELATIONS]


MAX_MEDIUM_FACETS = 4


class DSGSceneSchema(BaseModel):
    medium_and_optics: list[str] = Field(
        description=f"1-{MAX_MEDIUM_FACETS} short, separately verifiable facets of the physical medium, surface "
        "and optics, e.g. ['flat gouache on matte paper', 'zero drop shadows', 'deep depth of field']. "
        "Observable facts only; no guesses about how the image was made."
    )
    background: str = Field(description="The background/ground as a noun phrase, e.g. 'a plain off-white background'.")
    color_palette: list[str] = Field(description=f"3-{MAX_PALETTE} dominant colors as precise color names.")
    nodes: list[SpatialNode] = Field(
        description=f"1-{MAX_NODES} distinct visible entities, each anchored to one 3x3 grid cell."
    )
    extra_checks: list[str] = Field(
        default_factory=list,
        description=f"0-{MAX_EXTRA_CHECKS} additional atomic, binary-verifiable visual facts not captured by nodes "
        "(e.g. counts: 'exactly three circles'; visible text). Noun-phrase or short-clause form.",
    )

    @field_validator("medium_and_optics", mode="before")
    @classmethod
    def _medium(cls, v):
        # A plain string is split on semicolons only: commas often join adjectives ("a flat, matte image").
        if isinstance(v, str):
            v = v.split(";")
        out = _clean_list(v or [], MAX_MEDIUM_FACETS)
        if not out:
            raise ValueError("medium_and_optics must contain at least one facet")
        return out

    @field_validator("background", mode="before")
    @classmethod
    def _text(cls, v):
        return " ".join(str(v).split()).strip(" .")

    @field_validator("color_palette", mode="before")
    @classmethod
    def _palette(cls, v):
        return _clean_list(v or [], MAX_PALETTE)

    @field_validator("extra_checks", mode="before")
    @classmethod
    def _extra(cls, v):
        return _clean_list(v or [], MAX_EXTRA_CHECKS)

    @model_validator(mode="after")
    def _graph_integrity(self):
        if not self.nodes:
            raise ValueError("nodes must contain at least one entity")
        self.nodes = self.nodes[:MAX_NODES]
        # Make ids unique, then drop relations whose target is missing or self-referential.
        seen: dict[str, int] = {}
        for node in self.nodes:
            base = node.id
            if base in seen:
                seen[base] += 1
                node.id = f"{base}_{seen[base]}"
            else:
                seen[base] = 1
        ids = {n.id for n in self.nodes}
        for node in self.nodes:
            node.relations = [r for r in node.relations if r.target_id in ids and r.target_id != node.id]
        return self

    def node(self, node_id: str) -> SpatialNode:
        return next(n for n in self.nodes if n.id == node_id)


class TextEdit(BaseModel):
    original: str = Field(description="An exact, verbatim substring of the current prompt to replace.")
    revised: str = Field(description="Replacement text for that span.")


class RewritePatch(BaseModel):
    """Targeted diff: only spans tied to failed predicates are touched."""

    edits: list[TextEdit] = Field(default_factory=list, description="At most 6 span replacements.")
    additions: list[str] = Field(
        default_factory=list,
        description="At most 3 short new sentences for failed facts that no existing span covers.",
    )

    @field_validator("edits", mode="after")
    @classmethod
    def _cap_edits(cls, v):
        return v[:6]

    @field_validator("additions", mode="before")
    @classmethod
    def _cap_additions(cls, v):
        return _clean_list(v or [], 3)