lxylxy123321 пре 1 недеља
родитељ
комит
293a33528e
3 измењених фајлова са 139 додато и 31 уклоњено
  1. 2 1
      backend/app/core/db.py
  2. 1 5
      backend/app/services/model_service.py
  3. 136 25
      result.txt

+ 2 - 1
backend/app/core/db.py

@@ -33,7 +33,8 @@ def _get_session():
     return _async_session
 
 
-async_session = _get_session
+# async_session 是 async_sessionmaker 实例,调用它返回 AsyncSession
+async_session = _get_session()
 
 
 async def init_db():

+ 1 - 5
backend/app/services/model_service.py

@@ -80,9 +80,7 @@ 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))
 
         # 写入数据库(如果已存在则更新)
-        session_factory = async_session()
-        session = session_factory()
-        try:
+        async with async_session() as session:
             result = await session.execute(select(ModelCache).where(ModelCache.id == model_id))
             existing = result.scalar_one_or_none()
             if existing:
@@ -104,8 +102,6 @@ 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}

+ 136 - 25
result.txt

@@ -1,27 +1,140 @@
-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:
+  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 88, in __call__
+    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 13, in list_models
+    models = await model_service.list_cached_models()
+  File "/app/app/services/model_service.py", line 126, in list_cached_models
+    async with async_session() as session:
+AttributeError: __aenter__
+INFO:     172.20.0.4:55612 - "GET /api/v1/models/ 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
+  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 88, in __call__
+    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 13, in list_models
+    models = await model_service.list_cached_models()
+  File "/app/app/services/model_service.py", line 126, in list_cached_models
     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
+INFO:     172.20.0.4:55622 - "GET /api/v1/models/ 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
+  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 88, in __call__
+    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 13, in list_models
+    models = await model_service.list_cached_models()
+  File "/app/app/services/model_service.py", line 126, in list_cached_models
     async with async_session() as session:
 AttributeError: __aenter__
-
-During handling of the above exception, another exception occurred:
-
+INFO:     172.20.0.4:55624 - "GET /api/v1/models/ HTTP/1.0" 500 Internal Server Error
+ERROR:    Exception in ASGI application
 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]
@@ -35,9 +148,7 @@ Traceback (most recent call last):
     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
+  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/cors.py", line 88, in __call__
     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)
@@ -65,8 +176,8 @@ Traceback (most recent call last):
     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
+  File "/app/app/api/models.py", line 13, in list_models
+    models = await model_service.list_cached_models()
+  File "/app/app/services/model_service.py", line 126, in list_cached_models
+    async with async_session() as session:
+AttributeError: __aenter__