Download scripts/dev/2.3_test_graph_operation.py from SlappAI/Singularity: direct link, hf CLI and curl.
- Browser
- Download file 2.53 kB
-
https://huggingface.co/SlappAI/Singularity/resolve/c8aa03645367f13b28133e72d9204fa09654b00a/scripts/dev/2.3_test_graph_operation.py
- Command line
-
hf download hf://SlappAI/Singularity@c8aa03645367f13b28133e72d9204fa09654b00a/scripts/dev/2.3_test_graph_operation.py
-
curl -L -o 2.3_test_graph_operation.py https://huggingface.co/SlappAI/Singularity/resolve/c8aa03645367f13b28133e72d9204fa09654b00a/scripts/dev/2.3_test_graph_operation.py
2.53 kB
| import requests | |
| def print_tree(node, prefix="", visited=None, root_id="Healthcare"): | |
| """Recursive function to print all relationships in a hierarchical tree format.""" | |
| if visited is None: | |
| visited = set() | |
| node_id = node.get("node_id") | |
| if not node_id: | |
| print(f"{prefix}(unknown node)") | |
| return | |
| if node_id in visited or (node_id == root_id and prefix != ""): | |
| print(f"{prefix}(already listed) {node_id}") | |
| return | |
| visited.add(node_id) | |
| relationship_label = f"({node.get('relationship', '')})" if node.get("relationship") else "" | |
| print(f"{prefix}{node_id} {relationship_label}") | |
| # Check if there are descendants to traverse | |
| descendants = node.get("descendants", []) | |
| if not descendants: | |
| print(f"{prefix}(no further descendants)") | |
| else: | |
| for i, child in enumerate(descendants): | |
| new_prefix = f"{prefix}βββ " if i < len(descendants) - 1 else f"{prefix}βββ " | |
| print_tree(child, new_prefix, visited, root_id) | |
| def inspect_and_print_relationships(node_id): | |
| """Inspect and display relationships for a specified node in a tree format.""" | |
| response = requests.get(f"{base_url}/traverse_node?node_id={node_id}&direction=down") | |
| traversal_hierarchy = response.json() | |
| print(f"\nTraversal Response for {node_id}:", traversal_hierarchy) # Debugging line | |
| # Ensure the root node structure matches expectations | |
| if "node_id" not in traversal_hierarchy: | |
| traversal_hierarchy = { | |
| "node_id": node_id, | |
| "descendants": traversal_hierarchy.get("descendants", []) | |
| } | |
| print(f"\nInspect Relationships for {node_id} (Full Hierarchy):") | |
| print_tree(traversal_hierarchy) | |
| # Test API Endpoints | |
| base_url = "http://localhost:5000" | |
| # Step 1: Load Graph | |
| print("\n--- Testing Graph Loading ---") | |
| response = requests.post(f"{base_url}/load_graph") | |
| print("Load Graph Response:", response.json()) | |
| # Step 2: Create Node | |
| print("\n--- Testing Node Creation ---") | |
| create_data = { | |
| "node_id": "patient_123", | |
| "data": {"name": "John Doe", "age": 45, "medical_conditions": ["hypertension", "diabetes"]}, | |
| "domain": "Healthcare", | |
| "type": "Patient" | |
| } | |
| response = requests.post(f"{base_url}/create_node", json=create_data) | |
| print("Create Node Response:", response.json()) | |
| # Step 3: Inspect Relationships for Node (Healthcare) - Full hierarchical tree | |
| print("\n--- Testing Inspect Relationships for Node (Healthcare) ---") | |
| inspect_and_print_relationships("Healthcare") |