feat: AI patient onboarding backend and documentation
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import json
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.ai.gemini_client import GeminiClient
|
||||
from app.database.database import get_db
|
||||
from app.schemas.documents import DocumentExtractionResponse
|
||||
from app.services.onboarding_service import OnboardingService
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/v1/documents",
|
||||
tags=["Medical Documents"],
|
||||
)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/{session_id}/upload",
|
||||
response_model=DocumentExtractionResponse,
|
||||
)
|
||||
async def upload_medical_document(
|
||||
session_id: str,
|
||||
file: UploadFile = File(...),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
onboarding_service = OnboardingService()
|
||||
gemini_client = GeminiClient()
|
||||
|
||||
session = onboarding_service.get_session(db, session_id)
|
||||
if not session:
|
||||
raise HTTPException(status_code=404, detail="Onboarding session not found")
|
||||
|
||||
state = onboarding_service.get_state(db, session_id)
|
||||
file_bytes = await file.read()
|
||||
mime_type = file.content_type or "application/pdf"
|
||||
|
||||
patient_context = {
|
||||
"id": session.patient_id,
|
||||
"name": session.patient_name,
|
||||
"age": session.age,
|
||||
"gender": session.gender,
|
||||
"category": session.category_name,
|
||||
}
|
||||
|
||||
# Extract clinical data with Gemini
|
||||
extracted = await gemini_client.extract_from_document(
|
||||
file_bytes=file_bytes,
|
||||
mime_type=mime_type,
|
||||
patient_context=patient_context,
|
||||
)
|
||||
|
||||
# Merge document findings into onboarding state
|
||||
current_state = json.loads(state.state_json)
|
||||
|
||||
# Append medications if found
|
||||
meds_found = extracted.get("medications_found", [])
|
||||
if meds_found:
|
||||
existing_meds = current_state.get("medications", {})
|
||||
items = existing_meds.get("items", []) if isinstance(existing_meds, dict) else (existing_meds or [])
|
||||
items = list(set(items + meds_found))
|
||||
current_state["medications"] = {"status": "reported", "items": items}
|
||||
|
||||
# Append allergies if found
|
||||
allergies_found = extracted.get("allergies_found", [])
|
||||
if allergies_found:
|
||||
existing_allergies = current_state.get("allergies", {})
|
||||
items = existing_allergies.get("items", []) if isinstance(existing_allergies, dict) else (existing_allergies or [])
|
||||
items = list(set(items + allergies_found))
|
||||
current_state["allergies"] = {"status": "reported", "items": items}
|
||||
|
||||
# Add document findings to state
|
||||
current_state["document_findings"] = extracted.get("findings", [])
|
||||
|
||||
onboarding_service.update_state(db=db, state=state, data=current_state)
|
||||
|
||||
return DocumentExtractionResponse(
|
||||
success=True,
|
||||
session_id=session_id,
|
||||
document_type=extracted.get("document_type", "medical_report"),
|
||||
summary=extracted.get("summary", "Document analyzed successfully"),
|
||||
findings=extracted.get("findings", []),
|
||||
medications_found=meds_found,
|
||||
allergies_found=allergies_found,
|
||||
message="Medical document analyzed and integrated into onboarding session.",
|
||||
)
|
||||
Reference in New Issue
Block a user