Spaces:
Runtime error
Runtime error
| import os | |
| from pyvis.network import Network | |
| def visualize_reasoned_graph(memories, relationships): | |
| """ | |
| Generates an interactive graph of your memory network. | |
| Safe for headless environments like Hugging Face Spaces. | |
| """ | |
| net = Network(height="700px", width="100%", bgcolor="#0d0d0d", font_color="white", notebook=False) | |
| net.barnes_hut(gravity=-30000, central_gravity=0.3, spring_length=150, spring_strength=0.05) | |
| for i, memory in enumerate(memories): | |
| net.add_node(i, label=memory, title=memory, color="#4fc3f7") | |
| for rel in relationships: | |
| src, tgt, sim = rel | |
| src_idx = memories.index(src) | |
| tgt_idx = memories.index(tgt) | |
| width = max(1, sim * 10) | |
| color = "#00e676" if sim > 0.5 else "#ffeb3b" if sim > 0.3 else "#ff1744" | |
| net.add_edge(src_idx, tgt_idx, value=sim, color=color, width=width, title=f"Similarity: {sim:.2f}") | |
| output_dir = "/mnt/data" | |
| os.makedirs(output_dir, exist_ok=True) | |
| output_html = os.path.join(output_dir, "graph_reasoned.html") | |
| # ✅ write_html instead of show() | |
| net.write_html(output_html) | |
| print(f"✅ Visualization generated successfully at: {output_html}") | |
| return output_html |