chen666-666 commited on
Commit
72013e5
·
verified ·
1 Parent(s): ffb7b95

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -22
app.py CHANGED
@@ -510,25 +510,30 @@ def process_text(text, model_type="bert"):
510
  # ======================== 知识图谱可视化 ========================
511
  def generate_kg_image(entities, relations):
512
  """
513
- Hugging Face 专用中文知识图谱生成函数
514
- 已测试通过,确保显示中文标签
515
  """
516
  try:
517
  import matplotlib.pyplot as plt
518
  import networkx as nx
519
  import tempfile
520
  import os
 
521
  from matplotlib import font_manager
522
 
523
- # === 1. 强制使用系统内置中文字体 ===
524
- plt.rcParams['font.sans-serif'] = ['Noto Sans CJK SC', 'SimHei', 'Microsoft YaHei'] # 多字体回退
 
 
 
 
 
 
 
 
 
525
  plt.rcParams['axes.unicode_minus'] = False
526
 
527
- # === 2. 检查可用字体(调试用)===
528
- if os.environ.get('DISPLAY_FONTS', '0') == '1': # 设置环境变量DISPLAY_FONTS=1启用
529
- print("可用字体:", [f.name for f in font_manager.fontManager.ttflist if 'CJK' in f.name or 'Hei' in f.name])
530
-
531
- # === 3. 创建图谱 ===
532
  G = nx.DiGraph()
533
  entity_colors = {
534
  'PER': '#FF6B6B', # 人物-红色
@@ -537,15 +542,13 @@ def generate_kg_image(entities, relations):
537
  'TIME': '#96CEB4' # 时间-绿色
538
  }
539
 
540
- # 添加节点(实体)
541
  for entity in entities:
542
  G.add_node(
543
  entity["text"],
544
- label=f"{entity['text']} ({entity['type']})", # 保留英文类型
545
  color=entity_colors.get(entity['type'], '#D3D3D3')
546
  )
547
 
548
- # 添加边(关系)
549
  for relation in relations:
550
  if relation["head"] in G.nodes and relation["tail"] in G.nodes:
551
  G.add_edge(
@@ -554,11 +557,10 @@ def generate_kg_image(entities, relations):
554
  label=relation["relation"]
555
  )
556
 
557
- # === 4. 绘图配置 ===
558
- plt.figure(figsize=(12, 8), dpi=120) # 降低DPI节省内存
559
- pos = nx.spring_layout(G, k=0.7, seed=42) # 固定随机种子
560
 
561
- # 绘制节点和边
562
  nx.draw_networkx_nodes(
563
  G, pos,
564
  node_color=[G.nodes[n]['color'] for n in G.nodes],
@@ -573,29 +575,27 @@ def generate_kg_image(entities, relations):
573
  arrowsize=20
574
  )
575
 
576
- # === 5. 关键修改:确保中文显示 ===
577
  node_labels = {n: G.nodes[n]['label'] for n in G.nodes}
578
  nx.draw_networkx_labels(
579
  G, pos,
580
  labels=node_labels,
581
  font_size=10,
582
- font_family='Noto Sans CJK SC', # 强制指定字体
583
  font_weight='bold'
584
  )
585
-
586
- # 边标签(关系)
587
  edge_labels = nx.get_edge_attributes(G, 'label')
588
  nx.draw_networkx_edge_labels(
589
  G, pos,
590
  edge_labels=edge_labels,
591
  font_size=8,
592
- font_family='Noto Sans CJK SC' # 强制指定字体
593
  )
594
 
595
  plt.axis('off')
596
  plt.tight_layout()
597
 
598
- # === 6. 保存图片 ===
599
  temp_dir = tempfile.mkdtemp()
600
  output_path = os.path.join(temp_dir, "kg.png")
601
  plt.savefig(output_path, bbox_inches='tight', pad_inches=0.1)
@@ -608,6 +608,7 @@ def generate_kg_image(entities, relations):
608
  return None
609
 
610
 
 
611
  def process_file(file, model_type="bert"):
612
  try:
613
  with open(file.name, 'rb') as f:
 
510
  # ======================== 知识图谱可视化 ========================
511
  def generate_kg_image(entities, relations):
512
  """
513
+ 中文知识图谱生成函数,支持自动匹配系统中的中文字体,避免中文显示为方框。
 
514
  """
515
  try:
516
  import matplotlib.pyplot as plt
517
  import networkx as nx
518
  import tempfile
519
  import os
520
+ import logging
521
  from matplotlib import font_manager
522
 
523
+ # === 1. 自动查找可用中文字体 ===
524
+ def find_chinese_font():
525
+ preferred_fonts = ['Noto Sans CJK SC', 'Microsoft YaHei', 'SimHei', 'WenQuanYi Zen Hei', 'PingFang SC']
526
+ available_fonts = set(f.name for f in font_manager.fontManager.ttflist)
527
+ for font in preferred_fonts:
528
+ if font in available_fonts:
529
+ return font
530
+ return 'sans-serif' # fallback
531
+
532
+ chinese_font = find_chinese_font()
533
+ plt.rcParams['font.sans-serif'] = [chinese_font]
534
  plt.rcParams['axes.unicode_minus'] = False
535
 
536
+ # === 2. 创建图谱 ===
 
 
 
 
537
  G = nx.DiGraph()
538
  entity_colors = {
539
  'PER': '#FF6B6B', # 人物-红色
 
542
  'TIME': '#96CEB4' # 时间-绿色
543
  }
544
 
 
545
  for entity in entities:
546
  G.add_node(
547
  entity["text"],
548
+ label=f"{entity['text']} ({entity['type']})",
549
  color=entity_colors.get(entity['type'], '#D3D3D3')
550
  )
551
 
 
552
  for relation in relations:
553
  if relation["head"] in G.nodes and relation["tail"] in G.nodes:
554
  G.add_edge(
 
557
  label=relation["relation"]
558
  )
559
 
560
+ # === 3. 绘图配置 ===
561
+ plt.figure(figsize=(12, 8), dpi=150)
562
+ pos = nx.spring_layout(G, k=0.7, seed=42)
563
 
 
564
  nx.draw_networkx_nodes(
565
  G, pos,
566
  node_color=[G.nodes[n]['color'] for n in G.nodes],
 
575
  arrowsize=20
576
  )
577
 
 
578
  node_labels = {n: G.nodes[n]['label'] for n in G.nodes}
579
  nx.draw_networkx_labels(
580
  G, pos,
581
  labels=node_labels,
582
  font_size=10,
583
+ font_family=chinese_font,
584
  font_weight='bold'
585
  )
586
+
 
587
  edge_labels = nx.get_edge_attributes(G, 'label')
588
  nx.draw_networkx_edge_labels(
589
  G, pos,
590
  edge_labels=edge_labels,
591
  font_size=8,
592
+ font_family=chinese_font
593
  )
594
 
595
  plt.axis('off')
596
  plt.tight_layout()
597
 
598
+ # === 4. 保存图片 ===
599
  temp_dir = tempfile.mkdtemp()
600
  output_path = os.path.join(temp_dir, "kg.png")
601
  plt.savefig(output_path, bbox_inches='tight', pad_inches=0.1)
 
608
  return None
609
 
610
 
611
+
612
  def process_file(file, model_type="bert"):
613
  try:
614
  with open(file.name, 'rb') as f: