Răsfoiți Sursa

修改入库方法

lxylxy123321 1 săptămână în urmă
părinte
comite
4d70b0f4fc
3 a modificat fișierele cu 81 adăugiri și 24 ștergeri
  1. 0 3
      backend/app/api/models.py
  2. 9 2
      backend/app/services/model_service.py
  3. 72 19
      result.txt

+ 0 - 3
backend/app/api/models.py

@@ -28,10 +28,7 @@ async def list_models():
 @router.post("/download", response_model=ModelDownloadResponse, status_code=200)
 async def download_model(req: ModelDownloadRequest):
     """从 HuggingFace 或 ModelScope 下载模型。"""
-    from app.core.logging import logger
-    logger.info(f"[DEBUG] download request: model_id={req.model_id}, use_modelscope={req.use_modelscope}, type={type(req.use_modelscope)}")
     result = await model_service.download_model(req.model_id, req.use_modelscope)
-    logger.info(f"[DEBUG] download result: {result}")
 
     if result["status"] == "failed":
         raise HTTPException(status_code=400, detail=result.get("error", "Download failed"))

+ 9 - 2
backend/app/services/model_service.py

@@ -80,7 +80,9 @@ async def download_model(model_id: str, use_modelscope: bool = False) -> dict[st
             context_length = cfg.get("max_position_embeddings", cfg.get("max_sequence_length", 2048))
 
         # 写入数据库(如果已存在则更新)
-        async with async_session() as session:
+        session_factory = async_session()
+        session = session_factory()
+        try:
             result = await session.execute(select(ModelCache).where(ModelCache.id == model_id))
             existing = result.scalar_one_or_none()
             if existing:
@@ -102,15 +104,20 @@ async def download_model(model_id: str, use_modelscope: bool = False) -> dict[st
                 )
                 session.add(record)
             await session.commit()
+        finally:
+            await session.close()
 
         logger.info(f"Model downloaded: {model_id} -> {local_path}")
         return {"model_id": model_id, "status": "completed", "path": local_path}
     except Exception as e:
         import traceback
-
         tb = traceback.format_exc()
         logger.error(f"Model download failed: {type(e).__name__}: {e}")
         logger.error(f"Traceback:\n{tb}")
+        error_msg = str(e)
+        if "Connection" in error_msg or "timeout" in error_msg.lower() or "network" in error_msg.lower():
+            error_msg += "\n提示: 可能是 HuggingFace 网络问题。尝试使用 ModelScope 下载。"
+        return {"model_id": model_id, "status": "failed", "error": error_msg}
         return {"model_id": model_id, "status": "failed", "error": error_msg}
 
 

+ 72 - 19
result.txt

@@ -1,19 +1,72 @@
-(base) [root@localhost backend]# sshpass -p 'lq123456!' ssh -o StrictHostKeyChecking=no lq@192.168.92.151 "ls -la /home/lq/Fine-tuning/"
-total 104
-drwxrwxr-x 5 lq lq  4096 May 19 03:50 .
-drwxr-xr-x 8 lq lq  4096 May 19 08:05 ..
-drwxrwxr-x 4 lq lq  4096 May 19 03:59 backend
--rw-rw-r-- 1 lq lq  5050 May 19 03:49 CLAUDE.md
--rw-rw-r-- 1 lq lq  2031 May 19 03:50 DEPLOY.md
--rw-rw-r-- 1 lq lq  1892 May 19 03:50 docker-compose.yml
--rw-rw-r-- 1 lq lq    86 May 19 03:49 .dockerignore
--rw-rw-r-- 1 lq lq   543 May 19 03:49 .env
--rw-rw-r-- 1 lq lq   375 May 19 03:49 .env.example
-drwxrwxr-x 3 lq lq  4096 May 19 03:49 frontend
-drwxrwxr-x 8 lq lq  4096 May 19 03:50 .git
--rw-rw-r-- 1 lq lq   210 May 19 03:49 .gitignore
--rw-rw-r-- 1 lq lq     0 May 19 03:49 peft-finetune-frontend@0.1.0
--rw-rw-r-- 1 lq lq  3485 May 19 03:49 README.md
--rw-rw-r-- 1 lq lq  7036 May 19 03:49 result.txt
--rw-rw-r-- 1 lq lq 15237 May 19 03:50 样本中心提供API接口文档_外部.md
--rw-rw-r-- 1 lq lq 20943 May 19 03:50 统一认证平台接入流程及API接口文档(1)(1).md
+lq@lq:~/Fine-tuning$ sudo docker logs -f finetune-backend
+INFO:     Started server process [1]
+INFO:     Waiting for application startup.
+2026-05-19 14:38:30 | INFO     | peft-platform | JobQueue started with 2 workers
+INFO:     Application startup complete.
+INFO:     Uvicorn running on http://0.0.0.0:8010 (Press CTRL+C to quit)
+INFO:     127.0.0.1:59670 - "GET /health HTTP/1.1" 200 OK
+2026-05-19 14:38:36 | INFO     | peft-platform | [DEBUG] download request: model_id=Qwen/Qwen3.5-0.8B, use_modelscope=True, type=<class 'bool'>
+2026-05-19 14:38:37 | ERROR    | peft-platform | Model download failed: AttributeError: __aenter__
+2026-05-19 14:38:37 | ERROR    | peft-platform | Traceback:
+Traceback (most recent call last):
+  File "/app/app/services/model_service.py", line 83, in download_model
+    async with async_session() as session:
+AttributeError: __aenter__
+
+INFO:     172.20.0.4:37748 - "POST /api/v1/models/download HTTP/1.0" 500 Internal Server Error
+ERROR:    Exception in ASGI application
+Traceback (most recent call last):
+  File "/app/app/services/model_service.py", line 83, in download_model
+    async with async_session() as session:
+AttributeError: __aenter__
+
+During handling of the above exception, another exception occurred:
+
+Traceback (most recent call last):
+  File "/usr/local/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py", line 421, in run_asgi
+    result = await app(  # type: ignore[func-returns-value]
+  File "/usr/local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 56, in __call__
+    return await self.app(scope, receive, send)
+  File "/usr/local/lib/python3.10/site-packages/fastapi/applications.py", line 1159, in __call__
+    await super().__call__(scope, receive, send)
+  File "/usr/local/lib/python3.10/site-packages/starlette/applications.py", line 90, in __call__
+    await self.middleware_stack(scope, receive, send)
+  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 186, in __call__
+    raise exc
+  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 164, in __call__
+    await self.app(scope, receive, _send)
+  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/cors.py", line 96, in __call__
+    await self.simple_response(scope, receive, send, request_headers=headers)
+  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/cors.py", line 154, in simple_response
+    await self.app(scope, receive, send)
+  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 63, in __call__
+    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
+  File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
+    raise exc
+  File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
+    await app(scope, receive, sender)
+  File "/usr/local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
+    await self.app(scope, receive, send)
+  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 660, in __call__
+    await self.middleware_stack(scope, receive, send)
+  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 680, in app
+    await route.handle(scope, receive, send)
+  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
+    await self.app(scope, receive, send)
+  File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 134, in app
+    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
+  File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
+    raise exc
+  File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
+    await app(scope, receive, sender)
+  File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 120, in app
+    response = await f(request)
+  File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 674, in app
+    raw_response = await run_endpoint_function(
+  File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 328, in run_endpoint_function
+    return await dependant.call(**values)
+  File "/app/app/api/models.py", line 33, in download_model
+    result = await model_service.download_model(req.model_id, req.use_modelscope)
+  File "/app/app/services/model_service.py", line 114, in download_model
+    return {"model_id": model_id, "status": "failed", "error": error_msg}
+NameError: name 'error_msg' is not defined