Browse Source

模型下载问题

lxylxy123321 1 week ago
parent
commit
6f81590294
2 changed files with 8 additions and 8 deletions
  1. 6 7
      backend/app/services/model_service.py
  2. 2 1
      backend/requirements.txt

+ 6 - 7
backend/app/services/model_service.py

@@ -43,16 +43,15 @@ async def download_model(model_id: str, use_modelscope: bool = False) -> dict[st
     """从 HF 或 ModelScope 下载模型到本地缓存。"""
     try:
         if use_modelscope:
-            import subprocess
+            import asyncio
+            from modelscope import snapshot_download as ms_download
 
             download_dir = str(settings.models_dir / model_id.replace("/", "_"))
-            proc = subprocess.run(
-                ["modelscope", "download", "--model", model_id, "--local_dir", download_dir],
-                capture_output=True, text=True, timeout=3600,
+
+            # 在线程池中同步执行,避免与 asyncio 事件循环冲突
+            local_path = await asyncio.to_thread(
+                ms_download, model_id, cache_dir=download_dir
             )
-            if proc.returncode != 0:
-                raise RuntimeError(f"modelscope CLI failed: {proc.stderr}")
-            local_path = download_dir
         else:
             from huggingface_hub import snapshot_download
 

+ 2 - 1
backend/requirements.txt

@@ -20,4 +20,5 @@ pyarrow>=17.0.0
 addict>=2.4.0
 modelscope
 datasets
-huggingface_hub
+huggingface_hub
+aiohttp>=3.9.0