-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
332 lines (277 loc) · 10.5 KB
/
Copy pathmain.py
File metadata and controls
332 lines (277 loc) · 10.5 KB
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
from __future__ import annotations
import argparse
import sys
from pathlib import Path
from dotenv import load_dotenv
from revision.images import get_image_size
from revision.model import DEFAULT_MODEL, GeneratedCode, OpenAIModel
from revision.renderer import BrowserRenderer
from revision.workspace import Workspace
DEFAULT_VIEWPORT = "1440x900"
def parse_viewport(value: str) -> tuple[int, int]:
try:
width_text, height_text = value.lower().split("x", 1)
width = int(width_text)
height = int(height_text)
except ValueError as exc:
raise argparse.ArgumentTypeError("Viewport must use WIDTHxHEIGHT, for example 1440x900.") from exc
if width <= 0 or height <= 0:
raise argparse.ArgumentTypeError("Viewport width and height must be positive.")
return width, height
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Generate, render, critique, repair, loop, or evaluate a ReVision page.")
parser.add_argument(
"--target",
type=Path,
default=None,
help="Path to a PNG/JPG webpage screenshot. Also used to infer viewport size when --viewport is omitted.",
)
parser.add_argument(
"--html",
type=Path,
default=None,
help="Path to an HTML file to render. Defaults to workspace/index.html when --target is not provided.",
)
parser.add_argument(
"--output",
type=Path,
default=None,
help="Path where the screenshot should be saved. Defaults depend on the selected phase.",
)
parser.add_argument(
"--viewport",
type=parse_viewport,
default=None,
help=f"Browser viewport as WIDTHxHEIGHT. Defaults to the target image size, or {DEFAULT_VIEWPORT} without --target.",
)
parser.add_argument(
"--model",
default=None,
help=f"OpenAI model override for this run. Defaults to OPENAI_MODEL or {DEFAULT_MODEL}.",
)
parser.add_argument(
"--critique",
action="store_true",
help="Run Phase 2 visual critique. With --current, critiques existing screenshots; with --target only, critiques after generation.",
)
parser.add_argument(
"--current",
type=Path,
default=None,
help="Existing rendered screenshot to compare against --target. Defaults to output/iterations/iteration_0.png for --critique.",
)
parser.add_argument(
"--repair",
type=Path,
default=None,
help="Path to a critique JSON file. Runs Phase 3 one-shot repair using current output/index.html and output/style.css.",
)
parser.add_argument(
"--iteration",
type=int,
default=1,
help="Iteration number to save for --repair output. Defaults to 1.",
)
parser.add_argument(
"--loop",
type=int,
default=None,
help="Run Phase 4 from scratch with N repair cycles. Example: --loop 3 creates iteration_0 through iteration_3.",
)
parser.add_argument(
"--evaluate",
action="store_true",
help="Run Phase 5 local pixel-level evaluation over output/iterations/iteration_*.png. Requires --target.",
)
return parser
def main() -> None:
load_dotenv()
args = build_parser().parse_args()
workspace = Workspace(Path.cwd())
viewport = resolve_viewport(args.target, args.viewport)
renderer = BrowserRenderer(viewport=viewport)
if args.evaluate:
if args.target is None:
raise RuntimeError("--evaluate requires --target.")
evaluation_path = evaluate_existing_iterations(target_path=args.target, workspace=workspace)
print(f"Evaluation: {evaluation_path}")
return
if args.loop is not None:
if args.target is None:
raise RuntimeError("--loop requires --target.")
run_agent_loop(
target_path=args.target,
repair_cycles=args.loop,
workspace=workspace,
renderer=renderer,
model_name=args.model,
viewport=viewport,
)
return
if args.repair is not None:
html_path, screenshot_path = repair_iteration(
critique_path=args.repair,
iteration=args.iteration,
workspace=workspace,
renderer=renderer,
model_name=args.model,
output_path=args.output,
)
print(f"Viewport: {viewport[0]}x{viewport[1]}")
print(f"Repaired: {html_path}")
print(f"Screenshot: {screenshot_path}")
return
if args.critique and args.target is None:
raise RuntimeError("--critique requires --target so the critic can compare against the reference screenshot.")
if args.critique and args.current is not None:
critique_path = critique_iteration(
target_path=args.target,
current_path=args.current,
code=workspace.read_generated_code(),
workspace=workspace,
model_name=args.model,
iteration=0,
)
print(f"Viewport: {viewport[0]}x{viewport[1]}")
print(f"Critique: {critique_path}")
return
if args.target is not None:
html_path, screenshot_path = generate_initial_iteration(
target_path=args.target,
workspace=workspace,
renderer=renderer,
model_name=args.model,
output_path=args.output,
)
print(f"Viewport: {viewport[0]}x{viewport[1]}")
print(f"Rendered: {html_path}")
print(f"Screenshot: {screenshot_path}")
if args.critique:
critique_path = critique_iteration(
target_path=args.target,
current_path=screenshot_path,
code=workspace.read_generated_code(),
workspace=workspace,
model_name=args.model,
iteration=0,
)
print(f"Critique: {critique_path}")
return
html_path = args.html or workspace.ensure_sample_page()
screenshot_path = renderer.capture(
html_path=html_path,
output_path=args.output or workspace.output_dir / "screenshot.png",
)
print(f"Viewport: {viewport[0]}x{viewport[1]}")
print(f"Rendered: {html_path}")
print(f"Screenshot: {screenshot_path}")
def resolve_viewport(target_path: Path | None, viewport: tuple[int, int] | None) -> tuple[int, int]:
if viewport is not None:
return viewport
if target_path is not None:
return get_image_size(target_path)
return parse_viewport(DEFAULT_VIEWPORT)
def generate_initial_iteration(
target_path: Path,
workspace: Workspace,
renderer: BrowserRenderer,
model_name: str | None,
output_path: Path | None,
) -> tuple[Path, Path]:
model = OpenAIModel(model=model_name)
code = model.generate_page(target_path)
return save_and_render_iteration(code=code, iteration=0, workspace=workspace, renderer=renderer, output_path=output_path)
def critique_iteration(
target_path: Path,
current_path: Path,
code: GeneratedCode,
workspace: Workspace,
model_name: str | None,
iteration: int,
) -> Path:
model = OpenAIModel(model=model_name)
critique = model.critique(target_image_path=target_path, current_image_path=current_path, code=code)
return workspace.save_critique(iteration=iteration, critique=critique)
def repair_iteration(
critique_path: Path,
iteration: int,
workspace: Workspace,
renderer: BrowserRenderer,
model_name: str | None,
output_path: Path | None,
) -> tuple[Path, Path]:
if iteration < 1:
raise RuntimeError("--iteration must be 1 or greater for repair output.")
code = workspace.read_generated_code()
critique = workspace.read_critique(critique_path)
model = OpenAIModel(model=model_name)
repaired_code = model.repair(code=code, critique=critique)
return save_and_render_iteration(
code=repaired_code,
iteration=iteration,
workspace=workspace,
renderer=renderer,
output_path=output_path,
)
def run_agent_loop(
target_path: Path,
repair_cycles: int,
workspace: Workspace,
renderer: BrowserRenderer,
model_name: str | None,
viewport: tuple[int, int],
) -> None:
if repair_cycles < 0:
raise RuntimeError("--loop must be 0 or greater.")
model = OpenAIModel(model=model_name)
code = model.generate_page(target_path)
html_path, screenshot_path = save_and_render_iteration(
code=code,
iteration=0,
workspace=workspace,
renderer=renderer,
output_path=None,
)
print(f"Viewport: {viewport[0]}x{viewport[1]}")
print(f"Iteration 0 rendered: {screenshot_path}")
for iteration in range(repair_cycles):
critique = model.critique(target_image_path=target_path, current_image_path=screenshot_path, code=code)
critique_path = workspace.save_critique(iteration=iteration, critique=critique)
print(f"Iteration {iteration} critique: {critique_path}")
code = model.repair(code=code, critique=critique)
html_path, screenshot_path = save_and_render_iteration(
code=code,
iteration=iteration + 1,
workspace=workspace,
renderer=renderer,
output_path=None,
)
print(f"Iteration {iteration + 1} rendered: {screenshot_path}")
print(f"Final HTML: {html_path}")
print(f"Final screenshot: {screenshot_path}")
def evaluate_existing_iterations(target_path: Path, workspace: Workspace) -> Path:
from revision.evaluator import evaluate_iterations
results = evaluate_iterations(target_path=target_path, iterations_dir=workspace.iterations_dir)
if not results:
raise RuntimeError(f"No iteration screenshots found in {workspace.iterations_dir}")
return workspace.save_evaluation(results)
def save_and_render_iteration(
code: GeneratedCode,
iteration: int,
workspace: Workspace,
renderer: BrowserRenderer,
output_path: Path | None,
) -> tuple[Path, Path]:
html_path = workspace.write_generated_code(code)
workspace.save_iteration_code(iteration=iteration, code=code)
screenshot_path = renderer.capture(
html_path=html_path,
output_path=output_path or workspace.iterations_dir / f"iteration_{iteration}.png",
)
return html_path, screenshot_path
if __name__ == "__main__":
try:
main()
except RuntimeError as exc:
print(f"Error: {exc}", file=sys.stderr)
raise SystemExit(1) from None