-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_generator.py
More file actions
107 lines (93 loc) · 3.84 KB
/
Copy pathpdf_generator.py
File metadata and controls
107 lines (93 loc) · 3.84 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
import asyncio
import json
from pathlib import Path
from report_generator import generate_teardown_report
def main() -> None:
flow_data = json.loads(Path("flow_analysis.json").read_text(encoding="utf-8"))
issues = flow_data.get("issues", [])
scrape_data = {}
scrape_path = Path("scrape_results.json")
if scrape_path.exists():
scrape_data = json.loads(scrape_path.read_text(encoding="utf-8"))
fallback_images = sorted(Path("page_screenshots").glob("*.png"))
image_paths = [str(path) for path in fallback_images]
if not image_paths:
raise FileNotFoundError("No screenshot found. Ensure page_screenshots has images.")
bounding_boxes = []
flow_analysis = {
"Friction": "",
"Legitimacy": "",
"Offer Clarity": "",
"Willingness to Buy": ""
}
# Start with all scraped text/button boxes so the full page can be highlighted.
all_box_index: dict[tuple[int, int, int, int], int] = {}
box_source = scrape_data.get("collect_text_and_button_boxes", {})
def add_box(box: dict) -> None:
x = box.get("x")
y = box.get("y")
width = box.get("width")
height = box.get("height")
if x is None or y is None or width is None or height is None:
return
try:
key = (
int(round(float(x))),
int(round(float(y))),
int(round(float(width))),
int(round(float(height))),
)
except (ValueError, TypeError):
return
if key in all_box_index:
return
all_box_index[key] = len(bounding_boxes)
bounding_boxes.append(
{
"x": float(x),
"y": float(y),
"width": float(width),
"height": float(height),
}
)
if isinstance(box_source, dict) and "pages" in box_source and isinstance(box_source.get("pages"), list):
for page in box_source["pages"]:
for box in page.get("button_boxes", []):
if isinstance(box, dict):
add_box(box)
for box in page.get("text_boxes", []):
if isinstance(box, dict):
add_box(box)
elif isinstance(box_source, dict):
for box in box_source.get("button_boxes", []):
if isinstance(box, dict):
add_box(box)
for box in box_source.get("text_boxes", []):
if isinstance(box, dict):
add_box(box)
for issue in issues:
bbox = issue.get("bounding_box")
if bbox:
try:
issue_key = (
int(round(float(bbox.get("x")))),
int(round(float(bbox.get("y")))),
int(round(float(bbox.get("width")))),
int(round(float(bbox.get("height")))),
)
except (ValueError, TypeError, AttributeError):
issue_key = None
if issue_key is not None and issue_key in all_box_index:
bounding_boxes[all_box_index[issue_key]]["score"] = issue.get("score")
else:
bbox_copy = bbox.copy()
bbox_copy["score"] = issue.get("score")
bounding_boxes.append(bbox_copy)
cat = issue.get("category")
if cat in flow_analysis:
elem = issue.get("element_name", "Element").replace("→", "->").encode("latin-1", "ignore").decode("latin-1")
score = issue.get("score", "-")
fix = issue.get("suggested_text_fix", "").replace("→", "->").encode("latin-1", "ignore").decode("latin-1")
flow_analysis[cat] += f"- {elem} (Score: {score}/10): {fix}\n"
report_path = generate_teardown_report(image_paths, bounding_boxes, flow_analysis)
print(f"Teardown report generated at: {report_path}")