fix docker issue
This commit is contained in:
49
app/main.py
49
app/main.py
@@ -107,7 +107,7 @@ bot_manager = BotManager()
|
||||
async def lifespan(_app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
"""
|
||||
应用生命周期管理:
|
||||
- 启动时:初始化数据目录、验证数据库连接、预热嵌入服务、初始化 BM25 索引
|
||||
- 启动时:初始化数据目录、验证数据库连接、预热嵌入服务
|
||||
- 关闭时:释放数据库连接
|
||||
"""
|
||||
_logger = logging.getLogger(__name__)
|
||||
@@ -126,53 +126,6 @@ async def lifespan(_app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
except Exception as exc:
|
||||
_logger.warning("嵌入服务预热失败(将在首次使用时重试): %s", exc)
|
||||
|
||||
# ── 初始化 BM25 索引 ──
|
||||
try:
|
||||
from app.services.search_engine import BM25Index
|
||||
import app.api.v1.images as images_module
|
||||
|
||||
_images_bm25 = BM25Index()
|
||||
|
||||
# 加载数据库中已有的 OCR 记录到索引
|
||||
from app.database import async_session_factory
|
||||
from app.models.base import OCRImage
|
||||
from sqlalchemy import select
|
||||
|
||||
async with async_session_factory() as session:
|
||||
result = await session.execute(
|
||||
select(OCRImage).where(OCRImage.status == "completed")
|
||||
)
|
||||
records = result.scalars().all()
|
||||
for record in records:
|
||||
if record.ocr_text:
|
||||
index_text = record.ocr_text
|
||||
if record.tags:
|
||||
try:
|
||||
tags = json.loads(record.tags)
|
||||
if tags:
|
||||
index_text += "\n" + " ".join(tags)
|
||||
except Exception:
|
||||
pass
|
||||
if record.story_summary:
|
||||
index_text += "\n" + record.story_summary
|
||||
_images_bm25.add_document(
|
||||
doc_id=record.id,
|
||||
text=index_text,
|
||||
metadata={
|
||||
"file_path": record.file_path,
|
||||
"tags": json.loads(record.tags) if record.tags else [],
|
||||
"story_summary": record.story_summary or "",
|
||||
"confidence": record.confidence,
|
||||
"provider": record.provider,
|
||||
"created_at": str(record.created_at),
|
||||
},
|
||||
)
|
||||
|
||||
images_module.bm25_index = _images_bm25
|
||||
_logger.info("BM25 索引加载完成: %d 条记录", _images_bm25.size)
|
||||
except Exception as exc:
|
||||
_logger.warning("BM25 索引初始化失败: %s", exc)
|
||||
|
||||
yield
|
||||
|
||||
# ── 关闭 ──
|
||||
|
||||
Reference in New Issue
Block a user