Spaces:
Running
Running
Update cluster_insight.py
Browse files- cluster_insight.py +18 -3
cluster_insight.py
CHANGED
|
@@ -24,10 +24,17 @@ from sklearn.feature_extraction.text import TfidfVectorizer
|
|
| 24 |
import jieba
|
| 25 |
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# ========== 核心配置(和之前一致) ==========
|
| 28 |
FONT_FILE_PATH = "./SourceHanSansCN-Light.otf" # 字体文件在根目录
|
| 29 |
-
CHINESE_FONT = "Source Han Sans CN Light"
|
| 30 |
-
|
| 31 |
# ========== 关键:设置环境变量,让 Plotly/Kaleido 找到字体 ==========
|
| 32 |
os.environ["KALEIDO_FONT_SEARCH_PATH"] = os.getcwd() # 字体搜索路径 = 当前目录
|
| 33 |
print(f"🔧 字体搜索路径:{os.getcwd()}")
|
|
@@ -243,7 +250,15 @@ def cluster_and_visualize(
|
|
| 243 |
margin=dict(l=50, r=50, t=80, b=50) # 让 Plotly 内部也居中
|
| 244 |
)
|
| 245 |
|
| 246 |
-
img_bytes = fig.to_image(format="png", width=900, height=600, scale=2 ) # 新增这一行!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
b64 = base64.b64encode(img_bytes).decode('utf-8')
|
| 248 |
# print(f"{b64}解析成功!")
|
| 249 |
# return b64, stats
|
|
|
|
| 24 |
import jieba
|
| 25 |
|
| 26 |
|
| 27 |
+
# 第一步:在文件开头导入 plotly.io
|
| 28 |
+
import plotly.io as pio
|
| 29 |
+
|
| 30 |
+
# 第二步:设置默认引擎为 kaleido(旧版本 Plotly 也支持)
|
| 31 |
+
pio.kaleido.scope.default_format = "png"
|
| 32 |
+
print("✅ 已设置 kaleido 为默认图片引擎")
|
| 33 |
+
|
| 34 |
# ========== 核心配置(和之前一致) ==========
|
| 35 |
FONT_FILE_PATH = "./SourceHanSansCN-Light.otf" # 字体文件在根目录
|
| 36 |
+
# CHINESE_FONT = "Source Han Sans CN Light"
|
| 37 |
+
CHINESE_FONT = "SimHei, Microsoft YaHei, Arial Unicode MS, sans-serif"
|
| 38 |
# ========== 关键:设置环境变量,让 Plotly/Kaleido 找到字体 ==========
|
| 39 |
os.environ["KALEIDO_FONT_SEARCH_PATH"] = os.getcwd() # 字体搜索路径 = 当前目录
|
| 40 |
print(f"🔧 字体搜索路径:{os.getcwd()}")
|
|
|
|
| 250 |
margin=dict(l=50, r=50, t=80, b=50) # 让 Plotly 内部也居中
|
| 251 |
)
|
| 252 |
|
| 253 |
+
# img_bytes = fig.to_image(format="png", width=900, height=600, scale=2 ) # 新增这一行!
|
| 254 |
+
# 新代码:
|
| 255 |
+
img_bytes = pio.to_image(
|
| 256 |
+
fig,
|
| 257 |
+
format="png",
|
| 258 |
+
width=900,
|
| 259 |
+
height=600,
|
| 260 |
+
scale=2
|
| 261 |
+
)
|
| 262 |
b64 = base64.b64encode(img_bytes).decode('utf-8')
|
| 263 |
# print(f"{b64}解析成功!")
|
| 264 |
# return b64, stats
|