Spaces:
Running
Running
fix: actually start background analysis task and implement process_analysis in AnalysisService
Browse files- app/api/routes/analysis.py +10 -2
- app/services/analysis_service.py +60 -0
app/api/routes/analysis.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
"""
|
| 2 |
Routes analyses K2 Think
|
| 3 |
"""
|
| 4 |
-
from fastapi import APIRouter, Depends, HTTPException, status
|
| 5 |
from sqlalchemy.orm import Session
|
| 6 |
from app.dependencies import get_db, get_current_user
|
| 7 |
from app.services.analysis_service import AnalysisService
|
|
@@ -18,7 +18,6 @@ from app.models.schemas import ScientificDocument, AnalysisRequest as K2Analysis
|
|
| 18 |
from app.services.export_service import ExportService
|
| 19 |
from app.services.arxiv_service import ArXivService
|
| 20 |
from app.services.openalex_service import OpenAlexService
|
| 21 |
-
from fastapi.responses import Response, FileResponse
|
| 22 |
from app.core.logging import logger
|
| 23 |
|
| 24 |
router = APIRouter()
|
|
@@ -49,10 +48,12 @@ async def get_user_analysis_history(
|
|
| 49 |
return results
|
| 50 |
|
| 51 |
|
|
|
|
| 52 |
@router.post("/{project_id}", response_model=dict, status_code=status.HTTP_202_ACCEPTED)
|
| 53 |
async def start_project_analysis(
|
| 54 |
project_id: str,
|
| 55 |
request: AnalysisRequest,
|
|
|
|
| 56 |
current_user = Depends(get_current_user),
|
| 57 |
db: Session = Depends(get_db)
|
| 58 |
):
|
|
@@ -90,6 +91,13 @@ async def start_project_analysis(
|
|
| 90 |
project_id=target_project_id,
|
| 91 |
model_used=request.model or "k2-think-pro"
|
| 92 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
return {
|
| 95 |
"message": "Analysis started successfully",
|
|
|
|
| 1 |
"""
|
| 2 |
Routes analyses K2 Think
|
| 3 |
"""
|
| 4 |
+
from fastapi import APIRouter, Depends, HTTPException, status, BackgroundTasks, Response, FileResponse
|
| 5 |
from sqlalchemy.orm import Session
|
| 6 |
from app.dependencies import get_db, get_current_user
|
| 7 |
from app.services.analysis_service import AnalysisService
|
|
|
|
| 18 |
from app.services.export_service import ExportService
|
| 19 |
from app.services.arxiv_service import ArXivService
|
| 20 |
from app.services.openalex_service import OpenAlexService
|
|
|
|
| 21 |
from app.core.logging import logger
|
| 22 |
|
| 23 |
router = APIRouter()
|
|
|
|
| 48 |
return results
|
| 49 |
|
| 50 |
|
| 51 |
+
|
| 52 |
@router.post("/{project_id}", response_model=dict, status_code=status.HTTP_202_ACCEPTED)
|
| 53 |
async def start_project_analysis(
|
| 54 |
project_id: str,
|
| 55 |
request: AnalysisRequest,
|
| 56 |
+
background_tasks: BackgroundTasks,
|
| 57 |
current_user = Depends(get_current_user),
|
| 58 |
db: Session = Depends(get_db)
|
| 59 |
):
|
|
|
|
| 91 |
project_id=target_project_id,
|
| 92 |
model_used=request.model or "k2-think-pro"
|
| 93 |
)
|
| 94 |
+
|
| 95 |
+
# START THE ACTUAL PROCESSING
|
| 96 |
+
background_tasks.add_task(
|
| 97 |
+
service.process_analysis,
|
| 98 |
+
str(analysis.id),
|
| 99 |
+
request
|
| 100 |
+
)
|
| 101 |
|
| 102 |
return {
|
| 103 |
"message": "Analysis started successfully",
|
app/services/analysis_service.py
CHANGED
|
@@ -33,6 +33,7 @@ class AnalysisService:
|
|
| 33 |
def complete_analysis(
|
| 34 |
self,
|
| 35 |
analysis_id: UUID,
|
|
|
|
| 36 |
status: str = "COMPLETED"
|
| 37 |
):
|
| 38 |
"""Marque une analyse comme complétée"""
|
|
@@ -41,6 +42,65 @@ class AnalysisService:
|
|
| 41 |
if analysis:
|
| 42 |
analysis.status = status
|
| 43 |
analysis.completed_at = datetime.utcnow()
|
|
|
|
|
|
|
| 44 |
self.analysis_repo.update(analysis_id, analysis)
|
| 45 |
|
| 46 |
return analysis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def complete_analysis(
|
| 34 |
self,
|
| 35 |
analysis_id: UUID,
|
| 36 |
+
result: dict = None,
|
| 37 |
status: str = "COMPLETED"
|
| 38 |
):
|
| 39 |
"""Marque une analyse comme complétée"""
|
|
|
|
| 42 |
if analysis:
|
| 43 |
analysis.status = status
|
| 44 |
analysis.completed_at = datetime.utcnow()
|
| 45 |
+
if result:
|
| 46 |
+
analysis.result_data = json.dumps(result)
|
| 47 |
self.analysis_repo.update(analysis_id, analysis)
|
| 48 |
|
| 49 |
return analysis
|
| 50 |
+
|
| 51 |
+
async def process_analysis(self, analysis_id: str, request):
|
| 52 |
+
"""Tâche de fond pour traiter l'analyse"""
|
| 53 |
+
from app.services.k2_think_engine import K2ThinkEngine
|
| 54 |
+
from app.db.repositories.paper_repo import PaperRepository
|
| 55 |
+
from app.models.schemas import K2AnalysisRequest, ScientificDocument
|
| 56 |
+
from app.db.models.reasoning_trace import ReasoningTrace
|
| 57 |
+
|
| 58 |
+
db = self.analysis_repo.db
|
| 59 |
+
try:
|
| 60 |
+
# 1. Fetch papers from DB
|
| 61 |
+
paper_repo = PaperRepository(db)
|
| 62 |
+
db_papers = [paper_repo.get_by_id(pid) for pid in request.paper_ids]
|
| 63 |
+
|
| 64 |
+
docs = []
|
| 65 |
+
for p in db_papers:
|
| 66 |
+
if p:
|
| 67 |
+
docs.append(ScientificDocument(
|
| 68 |
+
id=str(p.id),
|
| 69 |
+
title=p.title,
|
| 70 |
+
abstract=p.summary or "",
|
| 71 |
+
content=p.summary or "", # TODO: Full text if available
|
| 72 |
+
url=p.pdf_path or ""
|
| 73 |
+
))
|
| 74 |
+
|
| 75 |
+
# 2. Prepare K2 Request
|
| 76 |
+
k2_req = K2AnalysisRequest(
|
| 77 |
+
documents=docs,
|
| 78 |
+
user_id=request.user_id,
|
| 79 |
+
user_profile=request.user_profile,
|
| 80 |
+
reasoning_depth=request.reasoning_depth,
|
| 81 |
+
ethics_rigor=request.ethics_rigor,
|
| 82 |
+
info_density=request.info_density
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# 3. Call K2 Engine
|
| 86 |
+
engine = K2ThinkEngine()
|
| 87 |
+
result = await engine.process_analysis_request(k2_req)
|
| 88 |
+
|
| 89 |
+
# 4. Save Reasoning Trace
|
| 90 |
+
trace = ReasoningTrace(
|
| 91 |
+
analysis_id=UUID(analysis_id),
|
| 92 |
+
trace_data=json.dumps(result.get("reasoning_steps", [])),
|
| 93 |
+
final_conclusion=result.get("strategic_recommendations", ""),
|
| 94 |
+
tokens_used=0
|
| 95 |
+
)
|
| 96 |
+
db.add(trace)
|
| 97 |
+
|
| 98 |
+
# 5. Complete Analysis
|
| 99 |
+
self.complete_analysis(UUID(analysis_id), result=result)
|
| 100 |
+
db.commit()
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
from app.core.logging import logger
|
| 104 |
+
logger.error(f"Background analysis error for {analysis_id}: {e}")
|
| 105 |
+
self.complete_analysis(UUID(analysis_id), status="FAILED")
|
| 106 |
+
db.commit()
|