File size: 3,367 Bytes
110ce02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import networkx as nx
import rapidjson
import warnings
import os

from pyvis.network import Network

warnings.filterwarnings("ignore")

# Load the GraphML file
file_path = "./cache/graph_chunk_entity_relation.graphml"

assert os.path.exists(file_path), f"File {file_path} does not exist."
G = nx.read_graphml(file_path)

def create_graph(json_data):
    """
    Create interactive knowledge graph using pyvis
    """

    G = nx.Graph()

    # Add nodes with tooltips and error handling for missing keys
    for node in json_data['nodes']:
        # Get node type with fallback
        type = node.get("type", "Entity")

        # Get detailed type with fallback
        detailed_type = node.get("detailed_type", type)
        
        # Use node ID and type info for the tooltip
        G.add_node(node['id'], title=f"{type}: {detailed_type}")

    # Add edges with labels
    for edge in json_data['edges']:
        # Check if the required keys exist
        if 'from' in edge and 'to' in edge:
            label = edge.get('label', 'related')
            G.add_edge(edge['from'], edge['to'], title=label, label=label)

    # Create network visualization
    network = Network(
        width="100%",
        height="100vh",
        notebook=False,
        bgcolor="#f8fafc", 
        font_color="#1e293b"
    )
    
    # Configure network display
    network.from_nx(G)
    
    # Customize node appearance
    for node in network.nodes:
        node['color'] = {'background': '#e0e7ff', 'border': '#6366f1', 'highlight': {'background': '#c7d2fe', 'border': '#4f46e5'}}
        node['font'] = {'size': 14, 'color': '#1e293b'}
        node['shape'] = 'dot'
        node['size'] = 20
    
    # Customize edge appearance
    for edge in network.edges:
        edge['width'] = 4
        edge['color'] = {'color': '#6366f1', 'highlight': '#4f46e5'}
        edge['font'] = {'size': 12, 'color': '#4b5563', 'face': 'Arial'}
    
    # Save and display the network
    filename_out = "knowledge_graph.html"
    network.show(filename_out)
    print(f"Knowledge graph saved to {filename_out}")

# Convert the graph to node-link data format
js_graph = nx.node_link_data(G)
js_data = rapidjson.loads(rapidjson.dumps(js_graph))
# print(js_data)

create_graph(js_data)

# # Create a Pyvis network
# network = Network(width="100%",
#                   height="100vh",
#                   notebook=True,
#                   bgcolor="#f8fafc",
#                   font_color="#1e293b")

# # Convert NetworkX graph to Pyvis network
# network.from_nx(G)

# # Add colors and title to nodes
# for node in network.nodes:
#     if "description" in node:
#         node["title"] = node["description"]

#     node['color'] = {'background': '#e0e7ff', 'border': '#6366f1', 'highlight': {'background': '#c7d2fe', 'border': '#4f46e5'}}
#     node['font'] = {'size': 14, 'color': '#1e293b'}
#     node['shape'] = 'dot'
#     node['size'] = 20

# # Add title to edges
# for edge in network.edges:
#     if "description" in edge:
#         edge["title"] = edge["description"]

#     edge['width'] = 4
#     edge['color'] = {'color': '#6366f1', 'highlight': '#4f46e5'}
#     edge['font'] = {'size': 12, 'color': '#4b5563', 'face': 'Arial'}

# # Save and display the network
# filename_out = "knowledge_graph.html"
# network.show(filename_out)
# print(f"Knowledge graph saved to {filename_out}")