Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -519,15 +519,30 @@ def generate_kg_image(entities, relations):
|
|
519 |
|
520 |
# 创建网络图
|
521 |
G = nx.DiGraph()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
522 |
for entity in entities:
|
523 |
-
|
|
|
|
|
|
|
524 |
for relation in relations:
|
525 |
-
|
|
|
526 |
|
527 |
# 绘制网络图
|
528 |
-
pos = nx.spring_layout(G)
|
529 |
-
plt.figure(figsize=(
|
530 |
-
|
|
|
531 |
nx.draw_networkx_edge_labels(G, pos, edge_labels={(u, v): d["label"] for u, v, d in G.edges(data=True)})
|
532 |
|
533 |
# 确保保存目录存在
|
|
|
519 |
|
520 |
# 创建网络图
|
521 |
G = nx.DiGraph()
|
522 |
+
|
523 |
+
# 定义实体类型颜色
|
524 |
+
entity_colors = {
|
525 |
+
"Person": "red",
|
526 |
+
"Location": "green",
|
527 |
+
"Organization": "blue",
|
528 |
+
"Industry": "purple"
|
529 |
+
}
|
530 |
+
|
531 |
+
# 添加实体节点
|
532 |
for entity in entities:
|
533 |
+
if "text" in entity and "type" in entity:
|
534 |
+
G.add_node(entity["text"], label=entity["type"], color=entity_colors.get(entity["type"], "gray"))
|
535 |
+
|
536 |
+
# 添加关系边
|
537 |
for relation in relations:
|
538 |
+
if "head" in relation and "tail" in relation and "relation" in relation:
|
539 |
+
G.add_edge(relation["head"], relation["tail"], label=relation["relation"])
|
540 |
|
541 |
# 绘制网络图
|
542 |
+
pos = nx.spring_layout(G) # 使用 spring 布局
|
543 |
+
plt.figure(figsize=(12, 8))
|
544 |
+
node_colors = [G.nodes[node].get("color", "gray") for node in G.nodes]
|
545 |
+
nx.draw(G, pos, with_labels=True, node_color=node_colors, font_size=10, font_weight="bold")
|
546 |
nx.draw_networkx_edge_labels(G, pos, edge_labels={(u, v): d["label"] for u, v, d in G.edges(data=True)})
|
547 |
|
548 |
# 确保保存目录存在
|