Spaces:
Sleeping
Sleeping
Update lineage_visualizer.py
Browse files- lineage_visualizer.py +30 -12
lineage_visualizer.py
CHANGED
|
@@ -4,22 +4,24 @@
|
|
| 4 |
from lineage_tracker import get_ancestors, get_descendants
|
| 5 |
|
| 6 |
def _row_positions(n: int, width: int, y: int):
|
| 7 |
-
if n <= 0:
|
|
|
|
| 8 |
step = width // (n + 1)
|
| 9 |
return [(step * (i + 1), y) for i in range(n)]
|
| 10 |
|
| 11 |
def render_lineage_tree(agent_id: str, overlay_color_hue: int = 260, max_depth: int = 6) -> str:
|
| 12 |
-
width, height = 980,
|
| 13 |
svg = [f"<svg width='{width}' height='{height}' xmlns='http://www.w3.org/2000/svg'>"]
|
| 14 |
svg.append("<style>text{font-family:monospace}</style>")
|
| 15 |
svg.append(f"<text x='12' y='24' font-size='16'>RFT Lineage: {agent_id}</text>")
|
| 16 |
|
| 17 |
-
root_x, root_y = width//2, 170
|
| 18 |
|
|
|
|
| 19 |
ancestors = get_ancestors(agent_id)
|
| 20 |
if ancestors:
|
| 21 |
pos = _row_positions(len(ancestors), width, 80)
|
| 22 |
-
for (x,y), aid in zip(pos, ancestors):
|
| 23 |
svg.append(f"<circle cx='{x}' cy='{y}' r='14' fill='hsl({overlay_color_hue},60%,50%)'/>")
|
| 24 |
svg.append(f"<text x='{x-28}' y='{y+32}' font-size='11'>{aid}</text>")
|
| 25 |
x_last, y_last = pos[-1]
|
|
@@ -27,26 +29,42 @@ def render_lineage_tree(agent_id: str, overlay_color_hue: int = 260, max_depth:
|
|
| 27 |
else:
|
| 28 |
svg.append("<text x='12' y='80' font-size='12' fill='#888'>No ancestors</text>")
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
svg.append(f"<text x='{root_x-40}' y='{root_y+40}' font-size='12'>{agent_id}</text>")
|
| 33 |
|
|
|
|
| 34 |
levels = get_descendants(agent_id, depth=max_depth)
|
| 35 |
y = 260
|
| 36 |
gap = 90
|
| 37 |
for d in range(1, max_depth + 1):
|
| 38 |
nodes = levels.get(d, [])
|
| 39 |
-
if not nodes:
|
|
|
|
| 40 |
pos = _row_positions(len(nodes), width, y)
|
| 41 |
svg.append(f"<text x='12' y='{y-26}' font-size='12' fill='#888'>Gen {d}</text>")
|
| 42 |
for (x, yy), item in zip(pos, nodes):
|
| 43 |
hue = 40 + (15 * d)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
svg.append(f"<text x='{x-60}' y='{yy+32}' font-size='11'>{label}</text>")
|
| 49 |
-
svg.append(
|
|
|
|
|
|
|
|
|
|
| 50 |
y += gap
|
| 51 |
|
| 52 |
svg.append("</svg>")
|
|
|
|
| 4 |
from lineage_tracker import get_ancestors, get_descendants
|
| 5 |
|
| 6 |
def _row_positions(n: int, width: int, y: int):
|
| 7 |
+
if n <= 0:
|
| 8 |
+
return []
|
| 9 |
step = width // (n + 1)
|
| 10 |
return [(step * (i + 1), y) for i in range(n)]
|
| 11 |
|
| 12 |
def render_lineage_tree(agent_id: str, overlay_color_hue: int = 260, max_depth: int = 6) -> str:
|
| 13 |
+
width, height = 980, 480 # reduced height to avoid cutoff
|
| 14 |
svg = [f"<svg width='{width}' height='{height}' xmlns='http://www.w3.org/2000/svg'>"]
|
| 15 |
svg.append("<style>text{font-family:monospace}</style>")
|
| 16 |
svg.append(f"<text x='12' y='24' font-size='16'>RFT Lineage: {agent_id}</text>")
|
| 17 |
|
| 18 |
+
root_x, root_y = width // 2, 170
|
| 19 |
|
| 20 |
+
# Ancestors
|
| 21 |
ancestors = get_ancestors(agent_id)
|
| 22 |
if ancestors:
|
| 23 |
pos = _row_positions(len(ancestors), width, 80)
|
| 24 |
+
for (x, y), aid in zip(pos, ancestors):
|
| 25 |
svg.append(f"<circle cx='{x}' cy='{y}' r='14' fill='hsl({overlay_color_hue},60%,50%)'/>")
|
| 26 |
svg.append(f"<text x='{x-28}' y='{y+32}' font-size='11'>{aid}</text>")
|
| 27 |
x_last, y_last = pos[-1]
|
|
|
|
| 29 |
else:
|
| 30 |
svg.append("<text x='12' y='80' font-size='12' fill='#888'>No ancestors</text>")
|
| 31 |
|
| 32 |
+
# Root agent
|
| 33 |
+
svg.append(
|
| 34 |
+
f"<circle cx='{root_x}' cy='{root_y}' r='18' fill='#00b894'>"
|
| 35 |
+
"<animate attributeName='r' values='18;22;18' dur='2s' repeatCount='indefinite'/>"
|
| 36 |
+
"</circle>"
|
| 37 |
+
)
|
| 38 |
svg.append(f"<text x='{root_x-40}' y='{root_y+40}' font-size='12'>{agent_id}</text>")
|
| 39 |
|
| 40 |
+
# Descendants
|
| 41 |
levels = get_descendants(agent_id, depth=max_depth)
|
| 42 |
y = 260
|
| 43 |
gap = 90
|
| 44 |
for d in range(1, max_depth + 1):
|
| 45 |
nodes = levels.get(d, [])
|
| 46 |
+
if not nodes:
|
| 47 |
+
break
|
| 48 |
pos = _row_positions(len(nodes), width, y)
|
| 49 |
svg.append(f"<text x='12' y='{y-26}' font-size='12' fill='#888'>Gen {d}</text>")
|
| 50 |
for (x, yy), item in zip(pos, nodes):
|
| 51 |
hue = 40 + (15 * d)
|
| 52 |
+
child_id = item.get("child_id", "?")
|
| 53 |
+
tier = item.get("tier", "?")
|
| 54 |
+
overlay = item.get("overlay", "?")
|
| 55 |
+
svg.append(
|
| 56 |
+
f"<circle cx='{x}' cy='{yy}' r='14' fill='hsl({hue},70%,55%)'>"
|
| 57 |
+
"<animate attributeName='fill' "
|
| 58 |
+
f"values='hsl({hue},70%,55%);hsl({hue},70%,65%);hsl({hue},70%,55%)' "
|
| 59 |
+
"dur='2s' repeatCount='indefinite'/>"
|
| 60 |
+
"</circle>"
|
| 61 |
+
)
|
| 62 |
+
label = f"{child_id} · {tier} · {overlay}"
|
| 63 |
svg.append(f"<text x='{x-60}' y='{yy+32}' font-size='11'>{label}</text>")
|
| 64 |
+
svg.append(
|
| 65 |
+
f"<path d='M {root_x} {root_y+20} Q {(root_x+x)//2} {(root_y+yy)//2} {x} {yy-14}' "
|
| 66 |
+
"stroke='rgba(0,0,0,.35)' fill='none'/>"
|
| 67 |
+
)
|
| 68 |
y += gap
|
| 69 |
|
| 70 |
svg.append("</svg>")
|