Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- Dockerfile +3 -2
- app.py +23 -18
Dockerfile
CHANGED
@@ -5,9 +5,10 @@ FROM python:3.10-slim
|
|
5 |
RUN apt-get update && \
|
6 |
apt-get install -y --no-install-recommends \
|
7 |
fonts-noto-cjk \ # 中文字体
|
|
|
8 |
python3-pip \ # pip
|
9 |
-
git \ # git
|
10 |
-
curl \ # curl
|
11 |
&& apt-get clean && \
|
12 |
rm -rf /var/lib/apt/lists/*
|
13 |
|
|
|
5 |
RUN apt-get update && \
|
6 |
apt-get install -y --no-install-recommends \
|
7 |
fonts-noto-cjk \ # 中文字体
|
8 |
+
fontconfig \ # 字体配置工具
|
9 |
python3-pip \ # pip
|
10 |
+
git \ # git(如果有需要拉取代码)
|
11 |
+
curl \ # curl(如果有需要下载文件)
|
12 |
&& apt-get clean && \
|
13 |
rm -rf /var/lib/apt/lists/*
|
14 |
|
app.py
CHANGED
@@ -508,29 +508,33 @@ def process_text(text, model_type="bert"):
|
|
508 |
return ent_text, rel_text, kg_text, f"{total_duration:.2f} 秒"
|
509 |
|
510 |
# ======================== 知识图谱可视化 ========================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
511 |
def generate_kg_image(entities, relations):
|
512 |
"""
|
513 |
中文知识图谱生成函数,支持自动匹配系统中的中文字体,避免中文显示为方框。
|
514 |
"""
|
515 |
try:
|
516 |
-
|
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 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
return 'sans-serif' # fallback
|
531 |
|
532 |
chinese_font = find_chinese_font()
|
533 |
-
|
|
|
|
|
|
|
|
|
|
|
534 |
plt.rcParams['axes.unicode_minus'] = False
|
535 |
|
536 |
# === 2. 创建图谱 ===
|
@@ -580,7 +584,7 @@ def generate_kg_image(entities, relations):
|
|
580 |
G, pos,
|
581 |
labels=node_labels,
|
582 |
font_size=10,
|
583 |
-
font_family=chinese_font,
|
584 |
font_weight='bold'
|
585 |
)
|
586 |
|
@@ -589,7 +593,7 @@ def generate_kg_image(entities, relations):
|
|
589 |
G, pos,
|
590 |
edge_labels=edge_labels,
|
591 |
font_size=8,
|
592 |
-
font_family=chinese_font
|
593 |
)
|
594 |
|
595 |
plt.axis('off')
|
@@ -611,7 +615,8 @@ def generate_kg_image(entities, relations):
|
|
611 |
logging.error(f"[ERROR] 图谱生成失败: {str(e)}")
|
612 |
return None
|
613 |
|
614 |
-
|
|
|
615 |
# ======================== 文件处理 ========================
|
616 |
def process_file(file, model_type="bert"):
|
617 |
try:
|
|
|
508 |
return ent_text, rel_text, kg_text, f"{total_duration:.2f} 秒"
|
509 |
|
510 |
# ======================== 知识图谱可视化 ========================
|
511 |
+
import matplotlib.pyplot as plt
|
512 |
+
import networkx as nx
|
513 |
+
import tempfile
|
514 |
+
import os
|
515 |
+
import logging
|
516 |
+
from matplotlib import font_manager
|
517 |
+
|
518 |
def generate_kg_image(entities, relations):
|
519 |
"""
|
520 |
中文知识图谱生成函数,支持自动匹配系统中的中文字体,避免中文显示为方框。
|
521 |
"""
|
522 |
try:
|
523 |
+
# === 1. 确保使用合适的中文字体 ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
524 |
def find_chinese_font():
|
525 |
+
# 使用字体路径而非字体名,这里硬编码路径
|
526 |
+
font_path = "/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc"
|
527 |
+
if os.path.exists(font_path):
|
528 |
+
return font_path
|
529 |
+
return None
|
|
|
530 |
|
531 |
chinese_font = find_chinese_font()
|
532 |
+
if chinese_font:
|
533 |
+
plt.rcParams['font.sans-serif'] = [chinese_font]
|
534 |
+
else:
|
535 |
+
# 如果字体路径未找到,使用默认字体
|
536 |
+
plt.rcParams['font.sans-serif'] = ['SimHei']
|
537 |
+
|
538 |
plt.rcParams['axes.unicode_minus'] = False
|
539 |
|
540 |
# === 2. 创建图谱 ===
|
|
|
584 |
G, pos,
|
585 |
labels=node_labels,
|
586 |
font_size=10,
|
587 |
+
font_family=chinese_font or 'SimHei', # 若字体路径为空,使用 SimHei 作为备选
|
588 |
font_weight='bold'
|
589 |
)
|
590 |
|
|
|
593 |
G, pos,
|
594 |
edge_labels=edge_labels,
|
595 |
font_size=8,
|
596 |
+
font_family=chinese_font or 'SimHei' # 同上
|
597 |
)
|
598 |
|
599 |
plt.axis('off')
|
|
|
615 |
logging.error(f"[ERROR] 图谱生成失败: {str(e)}")
|
616 |
return None
|
617 |
|
618 |
+
|
619 |
+
|
620 |
# ======================== 文件处理 ========================
|
621 |
def process_file(file, model_type="bert"):
|
622 |
try:
|