Download scripts/dev/2.2_test_graph_operation.py from SlappAI/Singularity: direct link, hf CLI and curl.
- Browser
- Download file 2.4 kB
-
https://huggingface.co/SlappAI/Singularity/resolve/c8aa03645367f13b28133e72d9204fa09654b00a/scripts/dev/2.2_test_graph_operation.py
- Command line
-
hf download hf://SlappAI/Singularity@c8aa03645367f13b28133e72d9204fa09654b00a/scripts/dev/2.2_test_graph_operation.py
-
curl -L -o 2.2_test_graph_operation.py https://huggingface.co/SlappAI/Singularity/resolve/c8aa03645367f13b28133e72d9204fa09654b00a/scripts/dev/2.2_test_graph_operation.py
2.4 kB
| import requests | |
| def print_tree(node, prefix="", visited=None, is_inheritance=False): | |
| """Recursive function to print a tree structure with differentiation for inheritance vs. other relationships.""" | |
| if visited is None: | |
| visited = set() | |
| node_id = node["node_id"] | |
| # Avoid printing duplicate nodes by checking if node has been visited | |
| if node_id in visited: | |
| print(f"{prefix}(already listed) {node_id}") | |
| return | |
| visited.add(node_id) | |
| relationship_type = "inherited" if is_inheritance else "related" | |
| print(f"{prefix}{node_id} ({relationship_type})") | |
| # Traverse descendants (inheritance) | |
| descendants = [desc for desc in node.get("descendants", []) if desc["relationship"] == "inherits_from"] | |
| for i, child in enumerate(descendants): | |
| new_prefix = f"{prefix}βββ " if i < len(descendants) - 1 else f"{prefix}βββ " | |
| print_tree(child, new_prefix, visited, is_inheritance=True) | |
| # Display other relationships | |
| other_relations = [rel for rel in node.get("descendants", []) if rel["relationship"] != "inherits_from"] | |
| for j, rel in enumerate(other_relations): | |
| new_prefix = f"{prefix}βββ " if j < len(other_relations) - 1 else f"{prefix}βββ " | |
| print_tree(rel, new_prefix, visited, is_inheritance=False) | |
| # 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) | |
| print("\n--- Testing Inspect Relationships for Node (Healthcare) ---") | |
| response = requests.get(f"{base_url}/inspect_relationships?node_id=Healthcare") | |
| relationships = response.json() | |
| print("Inspect Relationships for Healthcare:") | |
| # Convert to tree-like structure | |
| root_node = { | |
| "node_id": relationships["node_id"], | |
| "descendants": relationships["relationships"]["child_relations"] | |
| } | |
| print_tree(root_node) |