Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -542,18 +542,19 @@ def generate_kg_image(entities, relations):
|
|
542 |
font_prop = font_manager.FontProperties(fname=chinese_font)
|
543 |
plt.rcParams['font.family'] = font_prop.get_name()
|
544 |
else:
|
|
|
545 |
logging.warning("Using default font")
|
546 |
-
|
|
|
547 |
plt.rcParams['axes.unicode_minus'] = False
|
548 |
|
549 |
# === 2. 创建图谱 ===
|
550 |
G = nx.DiGraph()
|
551 |
entity_colors = {
|
552 |
-
'PER': '#FF6B6B',
|
553 |
-
'ORG': '#4ECDC4',
|
554 |
-
'LOC': '#45B7D1',
|
555 |
-
'TIME': '#96CEB4'
|
556 |
-
'TITLE': '#D3D3D3'
|
557 |
}
|
558 |
|
559 |
for entity in entities:
|
@@ -594,7 +595,7 @@ def generate_kg_image(entities, relations):
|
|
594 |
G, pos,
|
595 |
labels=node_labels,
|
596 |
font_size=10,
|
597 |
-
|
598 |
font_weight='bold'
|
599 |
)
|
600 |
|
@@ -603,15 +604,17 @@ def generate_kg_image(entities, relations):
|
|
603 |
G, pos,
|
604 |
edge_labels=edge_labels,
|
605 |
font_size=8,
|
606 |
-
|
607 |
)
|
608 |
|
609 |
plt.axis('off')
|
610 |
plt.tight_layout()
|
611 |
|
612 |
# === 4. 保存图片 ===
|
613 |
-
temp_dir = tempfile.mkdtemp()
|
614 |
output_path = os.path.join(temp_dir, "kg.png")
|
|
|
|
|
615 |
logging.info(f"Saving graph image to {output_path}")
|
616 |
|
617 |
plt.savefig(output_path, bbox_inches='tight', pad_inches=0.1)
|
@@ -624,7 +627,6 @@ def generate_kg_image(entities, relations):
|
|
624 |
return None
|
625 |
|
626 |
|
627 |
-
|
628 |
# ======================== 文件处理 ========================
|
629 |
def process_file(file, model_type="bert"):
|
630 |
try:
|
|
|
542 |
font_prop = font_manager.FontProperties(fname=chinese_font)
|
543 |
plt.rcParams['font.family'] = font_prop.get_name()
|
544 |
else:
|
545 |
+
# 如果字体路径未找到,使用默认字体(DejaVu Sans)
|
546 |
logging.warning("Using default font")
|
547 |
+
plt.rcParams['font.family'] = ['DejaVu Sans']
|
548 |
+
|
549 |
plt.rcParams['axes.unicode_minus'] = False
|
550 |
|
551 |
# === 2. 创建图谱 ===
|
552 |
G = nx.DiGraph()
|
553 |
entity_colors = {
|
554 |
+
'PER': '#FF6B6B', # 人物-红色
|
555 |
+
'ORG': '#4ECDC4', # 组织-青色
|
556 |
+
'LOC': '#45B7D1', # 地点-蓝色
|
557 |
+
'TIME': '#96CEB4' # 时间-绿色
|
|
|
558 |
}
|
559 |
|
560 |
for entity in entities:
|
|
|
595 |
G, pos,
|
596 |
labels=node_labels,
|
597 |
font_size=10,
|
598 |
+
font_family=font_prop.get_name() if chinese_font else 'SimHei',
|
599 |
font_weight='bold'
|
600 |
)
|
601 |
|
|
|
604 |
G, pos,
|
605 |
edge_labels=edge_labels,
|
606 |
font_size=8,
|
607 |
+
font_family=font_prop.get_name() if chinese_font else 'SimHei'
|
608 |
)
|
609 |
|
610 |
plt.axis('off')
|
611 |
plt.tight_layout()
|
612 |
|
613 |
# === 4. 保存图片 ===
|
614 |
+
temp_dir = tempfile.mkdtemp() # 确保在 Docker 容器中有权限写入
|
615 |
output_path = os.path.join(temp_dir, "kg.png")
|
616 |
+
|
617 |
+
# 打印路径以方便调试
|
618 |
logging.info(f"Saving graph image to {output_path}")
|
619 |
|
620 |
plt.savefig(output_path, bbox_inches='tight', pad_inches=0.1)
|
|
|
627 |
return None
|
628 |
|
629 |
|
|
|
630 |
# ======================== 文件处理 ========================
|
631 |
def process_file(file, model_type="bert"):
|
632 |
try:
|