Repository navigation
Expand file tree
/
Copy pathrenderer.py
More file actions
45 lines (37 loc) · 1.77 KB
/
Copy pathrenderer.py
File metadata and controls
45 lines (37 loc) · 1.77 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
"""ASCII tree renderer for EvaluationGraph DAGs using Rich Tree."""
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from minival.dag.graph import EvaluationGraph
def render_dag(graph: "EvaluationGraph") -> str:
from rich.console import Console
from rich.tree import Tree
import io
from minival.dag.nodes import TaskNode, BinaryJudgeNode, NonBinaryJudgeNode, VerdictNode
def add_node(tree: Tree, node: object) -> None:
if isinstance(node, TaskNode):
branch = tree.add(f"[bold cyan]TaskNode[/] — {node.instructions[:60]!r} → [{node.output_label}]")
for child in node.children:
add_node(branch, child)
elif isinstance(node, BinaryJudgeNode):
branch = tree.add(f"[bold yellow]BinaryJudgeNode[/] — {node.criteria[:60]!r}")
for child in node.children:
add_node(branch, child)
elif isinstance(node, NonBinaryJudgeNode):
branch = tree.add(f"[bold magenta]NonBinaryJudgeNode[/] — {node.criteria[:60]!r}")
for child in node.children:
add_node(branch, child)
elif isinstance(node, VerdictNode):
if node.score is not None:
tree.add(f"[green]VerdictNode[/] verdict={node.verdict!r} score={node.score}/10")
else:
branch = tree.add(f"[green]VerdictNode[/] verdict={node.verdict!r} → delegates to child")
add_node(branch, node.child)
root = Tree("[bold]EvaluationGraph[/]")
for i, node in enumerate(graph.root_nodes):
subtree = root.add(f"Root {i + 1}")
add_node(subtree, node)
buf = io.StringIO()
console = Console(file=buf, highlight=False)
console.print(root)
return buf.getvalue()