huibao/backend/manual_analyze.py

69 lines
2.4 KiB
Python
Raw Permalink Normal View History

"""
强制触发深度AI分析并实时打印进度
"""
import asyncio
import sys
import logging
sys.path.insert(0, ".")
# 配置日志输出到控制台
logging.basicConfig(level=logging.INFO, format='%(asctime)s | %(levelname)s | %(message)s')
from app.database import AsyncSessionLocal, init_db
from app.models import Report
from app.services.ai_analyzer import ai_analyzer
from app.services.pdf_extractor import pdf_extractor
from sqlalchemy import select
from sqlalchemy.orm import selectinload
async def force_analyze_latest():
print("=" * 60)
print("🚀 强制启动深度AI分析 (电商实战版)")
print("=" * 60)
await init_db()
async with AsyncSessionLocal() as db:
# 1. 找一份报告(优先找博士眼镜,因为是标杆)
stmt = select(Report).where(Report.title.like("%2024年半年度报告%")) \
.options(selectinload(Report.company)) \
.limit(1)
result = await db.execute(stmt)
report = result.scalar_one_or_none()
if not report:
print("✗ 未找到目标报告")
return
print(f"📄 目标报告: {report.title} (ID: {report.id})")
print(f"🏢 所属公司: {report.company.short_name}")
# 2. 提取内容
if not report.is_extracted:
print("\n[阶段1] 正在提取PDF内容...")
contents = await pdf_extractor.extract_and_save(db, report)
print(f"✓ 提取到 {len(contents)} 个关键章节")
else:
print("\n[阶段1] 内容已提取,跳过")
# 3. 强制分析
print("\n[阶段2] 正在进行AI深度分析...")
print(" 正在挖掘【分销渠道】、【新品类】、【数字化】线索...")
print(" (由于分析维度较多,大约需要 1-2 分钟,请耐心等待...)")
# 强制重置状态以触发重新分析
report.is_analyzed = False
await db.commit()
success = await ai_analyzer.analyze_report(db, report)
if success:
print("\n✅ 分析完成!")
print("请在前端刷新页面查看《电商突围与数字化内参》")
else:
print("\n✗ 分析失败,请检查后端日志")
if __name__ == "__main__":
asyncio.run(force_analyze_latest())