Xandxtorm / app.py
Unfaithful's picture
Update app.py
fee153b verified
raw
history blame
5.4 kB
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()