File size: 5,395 Bytes
14e7f18
 
 
fee153b
14e7f18
 
fee153b
 
 
 
14e7f18
 
fee153b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14e7f18
 
fee153b
14e7f18
fee153b
 
 
 
 
 
 
 
 
 
 
 
 
14e7f18
fee153b
 
 
14e7f18
 
fee153b
 
 
 
 
 
 
 
 
 
 
14e7f18
 
 
fee153b
 
 
 
14e7f18
 
 
 
fee153b
 
 
 
 
 
14e7f18
 
fee153b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14e7f18
 
 
 
 
 
 
 
 
 
 
 
 
fee153b
 
 
 
 
14e7f18
 
 
fee153b
14e7f18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae70076
14e7f18
 
 
 
 
ae70076
14e7f18
ae70076
14e7f18
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
import gradio as gr
from transformers import pipeline
from graphviz import Digraph
import json
import os

# File-based persistence
DB_FILE = "fractal_tree.json"

# AI Model Initialization
ai_model = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")


class Node:
    def __init__(self, node_id, data, module, parent_id=None):
        self.node_id = node_id
        self.data = data
        self.module = module
        self.parent_id = parent_id
        self.children = []

    def to_dict(self):
        """Convert Node to dictionary for JSON storage."""
        return {
            "node_id": self.node_id,
            "data": self.data,
            "module": self.module,
            "parent_id": self.parent_id,
            "children": [child.to_dict() for child in self.children],
        }

    @staticmethod
    def from_dict(node_dict):
        """Create Node from dictionary."""
        node = Node(
            node_dict["node_id"],
            node_dict["data"],
            node_dict["module"],
            node_dict.get("parent_id"),
        )
        node.children = [Node.from_dict(child) for child in node_dict["children"]]
        return node


class FractalTree:
    def __init__(self):
        self.tree = None
        self.current_node = None
        self.load_tree()

    def save_tree(self):
        """Save the tree to a JSON file."""
        with open(DB_FILE, "w") as f:
            json.dump(self.tree.to_dict(), f, indent=4)

    def load_tree(self):
        """Load the tree from a JSON file."""
        if os.path.exists(DB_FILE):
            with open(DB_FILE, "r") as f:
                self.tree = Node.from_dict(json.load(f))
                self.current_node = self.tree
        else:
            self.tree = Node("0", "Root", "Root Module")
            self.current_node = self.tree
            self.save_tree()

    def add_node(self, data):
        """Add a node as a child of the current node."""
        suggestion = ai_model(data)[0]["label"]
        new_node = Node(
            node_id=str(len(self.tree.to_dict())),  # Generate unique ID
            data=data,
            module=suggestion,
            parent_id=self.current_node.node_id,
        )
        self.current_node.children.append(new_node)
        self.save_tree()
        return f"Added node with data: '{data}' and module: '{suggestion}'"

    def go_to_child(self, index):
        """Navigate to a child node by index."""
        if 0 <= index < len(self.current_node.children):
            self.current_node = self.current_node.children[index]
            self.save_tree()
            return f"Moved to child node: {self.current_node.data}"
        return "Invalid child index."

    def go_to_parent(self):
        """Navigate to the parent node."""
        if self.current_node.parent_id:
            parent_node = self.find_node(self.tree, self.current_node.parent_id)
            if parent_node:
                self.current_node = parent_node
                self.save_tree()
                return f"Moved to parent node: {self.current_node.data}"
        return "Already at the root node."

    def find_node(self, node, node_id):
        """Recursively find a node by ID."""
        if node.node_id == node_id:
            return node
        for child in node.children:
            result = self.find_node(child, node_id)
            if result:
                return result
        return None

    def build_graph(self):
        """Build a graph visualization of the tree."""
        def traverse(node, graph, node_id="0"):
            graph.node(node_id, f"{node.data} ({node.module})")
            for idx, child in enumerate(node.children):
                child_id = f"{node_id}-{idx}"
                graph.edge(node_id, child_id)
                traverse(child, graph, child_id)

        graph = Digraph()
        traverse(self.tree, graph)
        return graph


# Initialize Fractal Tree
fractal_tree = FractalTree()

# Gradio Functions
def add_node(data):
    return fractal_tree.add_node(data)

def go_to_child(index):
    return fractal_tree.go_to_child(int(index))

def go_to_parent():
    return fractal_tree.go_to_parent()

def view_tree():
    graph = fractal_tree.build_graph()
    graph_path = "/tmp/fractal_tree"
    graph.render(graph_path, format="png", cleanup=True)
    return graph_path + ".png"


# Gradio Interface
with gr.Blocks() as app:
    gr.Markdown("# Emergent Fractal App with JSON Persistence")

    # Add Node Section
    with gr.Row():
        data_input = gr.Textbox(label="Enter Node Data")
        add_button = gr.Button("Add Node")

    output_text = gr.Textbox(label="Output", interactive=False)

    # Navigation Section
    with gr.Row():
        child_index = gr.Number(label="Child Index", value=0, precision=0)
        child_button = gr.Button("Go to Child")
        parent_button = gr.Button("Go to Parent")

    # Tree Visualization
    tree_view_button = gr.Button("View Tree")
    tree_view = gr.Image(label="Tree Structure")

    # Connect Functions
    add_button.click(add_node, inputs=[data_input], outputs=[output_text])
    child_button.click(go_to_child, inputs=[child_index], outputs=[output_text])
    parent_button.click(go_to_parent, outputs=[output_text])
    tree_view_button.click(view_tree, outputs=[tree_view])

# Launch App
if __name__ == "__main__":
    app.launch()