Spaces:
Sleeping
Sleeping
fix: 8394gfqy8i93hf8qy348o9fq38ghfiuy8o134
Browse files
app.py
CHANGED
|
@@ -291,79 +291,97 @@ def download_pkuseg_models() -> bool:
|
|
| 291 |
logger.info(f"pkuseg 模型已存在: {pkuseg_home}")
|
| 292 |
return True
|
| 293 |
|
| 294 |
-
# 查找 MFA 环境的 Python
|
| 295 |
-
mfa_env = Path("/tmp/micromamba/envs/mfa")
|
| 296 |
-
python_path = mfa_env / "bin" / "python"
|
| 297 |
-
|
| 298 |
-
if not python_path.exists():
|
| 299 |
-
logger.error(f"MFA Python 不存在: {python_path}")
|
| 300 |
-
return False
|
| 301 |
-
|
| 302 |
logger.info(f"下载 pkuseg 中文分词模型到 {pkuseg_home}...")
|
| 303 |
|
| 304 |
-
# 使用
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
]
|
| 311 |
|
| 312 |
-
for
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
import os
|
| 317 |
-
os.environ["PKUSEG_HOME"] = "{pkuseg_home}"
|
| 318 |
-
|
| 319 |
-
from spacy_pkuseg.config import config
|
| 320 |
-
from spacy_pkuseg.download import download_model
|
| 321 |
-
|
| 322 |
-
print(f"PKUSEG_HOME: {{config.pkuseg_home}}")
|
| 323 |
-
|
| 324 |
-
# 设置镜像 URL
|
| 325 |
-
config.model_urls["spacy_ontonotes"] = "{mirror_prefix}/explosion/spacy-pkuseg/releases/download/v0.0.26/spacy_ontonotes.zip"
|
| 326 |
-
config.model_urls["postag"] = "{mirror_prefix}/lancopku/pkuseg-python/releases/download/v0.0.16/postag.zip"
|
| 327 |
-
|
| 328 |
-
print(f"下载 spacy_ontonotes: {{config.model_urls['spacy_ontonotes']}}")
|
| 329 |
-
download_model(config.model_urls["spacy_ontonotes"], config.pkuseg_home, config.model_hash["spacy_ontonotes"])
|
| 330 |
-
|
| 331 |
-
print(f"下载 postag: {{config.model_urls['postag']}}")
|
| 332 |
-
download_model(config.model_urls["postag"], config.pkuseg_home, config.model_hash["postag"])
|
| 333 |
-
|
| 334 |
-
print("pkuseg models downloaded successfully")
|
| 335 |
-
'''
|
| 336 |
|
| 337 |
-
|
| 338 |
-
|
|
|
|
| 339 |
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
env=env
|
| 345 |
-
)
|
| 346 |
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
else:
|
| 357 |
-
logger.warning(f"
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
|
| 368 |
|
| 369 |
def download_silero_vad_model() -> bool:
|
|
|
|
| 291 |
logger.info(f"pkuseg 模型已存在: {pkuseg_home}")
|
| 292 |
return True
|
| 293 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
logger.info(f"下载 pkuseg 中文分词模型到 {pkuseg_home}...")
|
| 295 |
|
| 296 |
+
# 直接使用 curl + unzip 下载,不依赖 spacy_pkuseg 的下载函数
|
| 297 |
+
models = [
|
| 298 |
+
{
|
| 299 |
+
"name": "spacy_ontonotes",
|
| 300 |
+
"urls": [
|
| 301 |
+
"https://ghfast.top/https://github.com/explosion/spacy-pkuseg/releases/download/v0.0.26/spacy_ontonotes.zip",
|
| 302 |
+
"https://gh-proxy.com/https://github.com/explosion/spacy-pkuseg/releases/download/v0.0.26/spacy_ontonotes.zip",
|
| 303 |
+
"https://github.com/explosion/spacy-pkuseg/releases/download/v0.0.26/spacy_ontonotes.zip",
|
| 304 |
+
],
|
| 305 |
+
"check_file": "unigram_word.txt",
|
| 306 |
+
},
|
| 307 |
+
{
|
| 308 |
+
"name": "postag",
|
| 309 |
+
"urls": [
|
| 310 |
+
"https://ghfast.top/https://github.com/lancopku/pkuseg-python/releases/download/v0.0.16/postag.zip",
|
| 311 |
+
"https://gh-proxy.com/https://github.com/lancopku/pkuseg-python/releases/download/v0.0.16/postag.zip",
|
| 312 |
+
"https://github.com/lancopku/pkuseg-python/releases/download/v0.0.16/postag.zip",
|
| 313 |
+
],
|
| 314 |
+
"check_file": "features.pkl",
|
| 315 |
+
},
|
| 316 |
]
|
| 317 |
|
| 318 |
+
for model in models:
|
| 319 |
+
model_name = model["name"]
|
| 320 |
+
model_dir = pkuseg_home / model_name
|
| 321 |
+
check_file = model_dir / model["check_file"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
+
if check_file.exists():
|
| 324 |
+
logger.info(f"{model_name} 已存在,跳过")
|
| 325 |
+
continue
|
| 326 |
|
| 327 |
+
downloaded = False
|
| 328 |
+
for url in model["urls"]:
|
| 329 |
+
logger.info(f"下载 {model_name}: {url}")
|
| 330 |
+
zip_path = pkuseg_home / f"{model_name}.zip"
|
|
|
|
|
|
|
| 331 |
|
| 332 |
+
try:
|
| 333 |
+
# 下载
|
| 334 |
+
result = subprocess.run(
|
| 335 |
+
["curl", "-fsSL", "-o", str(zip_path), url],
|
| 336 |
+
capture_output=True, text=True, timeout=120
|
| 337 |
+
)
|
| 338 |
+
|
| 339 |
+
if result.returncode != 0:
|
| 340 |
+
logger.warning(f"curl 下载失败: {result.stderr}")
|
| 341 |
+
continue
|
| 342 |
+
|
| 343 |
+
if not zip_path.exists() or zip_path.stat().st_size < 1000:
|
| 344 |
+
logger.warning(f"下载文件无效或太小")
|
| 345 |
+
continue
|
| 346 |
+
|
| 347 |
+
logger.info(f"下载完成,文件大小: {zip_path.stat().st_size} bytes")
|
| 348 |
+
|
| 349 |
+
# 解压
|
| 350 |
+
result = subprocess.run(
|
| 351 |
+
["unzip", "-o", "-q", str(zip_path), "-d", str(pkuseg_home)],
|
| 352 |
+
capture_output=True, text=True, timeout=60
|
| 353 |
+
)
|
| 354 |
+
|
| 355 |
+
if result.returncode != 0:
|
| 356 |
+
logger.warning(f"unzip 解压失败: {result.stderr}")
|
| 357 |
+
continue
|
| 358 |
+
|
| 359 |
+
# 删除 zip 文件
|
| 360 |
+
zip_path.unlink(missing_ok=True)
|
| 361 |
+
|
| 362 |
+
# 验证
|
| 363 |
+
if check_file.exists():
|
| 364 |
+
logger.info(f"{model_name} 下载并解压成功")
|
| 365 |
+
downloaded = True
|
| 366 |
+
break
|
| 367 |
else:
|
| 368 |
+
logger.warning(f"解压后文件不存在: {check_file}")
|
| 369 |
+
# 列出目录内容帮助调试
|
| 370 |
+
if model_dir.exists():
|
| 371 |
+
files = list(model_dir.iterdir())[:10]
|
| 372 |
+
logger.info(f"{model_name} 目录内容: {[f.name for f in files]}")
|
| 373 |
+
|
| 374 |
+
except subprocess.TimeoutExpired:
|
| 375 |
+
logger.warning(f"下载超时: {url}")
|
| 376 |
+
except Exception as e:
|
| 377 |
+
logger.warning(f"下载异常: {e}")
|
| 378 |
+
|
| 379 |
+
if not downloaded:
|
| 380 |
+
logger.error(f"{model_name} 所有镜像下载失败")
|
| 381 |
+
return False
|
| 382 |
+
|
| 383 |
+
logger.info("pkuseg 模型下载完成")
|
| 384 |
+
return True
|
| 385 |
|
| 386 |
|
| 387 |
def download_silero_vad_model() -> bool:
|