Spaces:
Paused
Paused
Update app/routes/llm_extraction.py
Browse files- app/routes/llm_extraction.py +51 -0
app/routes/llm_extraction.py
CHANGED
|
@@ -181,3 +181,54 @@ async def approve_save(request: ApproveSaveRequest, db: Session = Depends(get_db
|
|
| 181 |
raise HTTPException(
|
| 182 |
status_code=500, detail=f"Failed to save approved report: {str(e)}"
|
| 183 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
raise HTTPException(
|
| 182 |
status_code=500, detail=f"Failed to save approved report: {str(e)}"
|
| 183 |
)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
class BatchApproveSaveRequest(BaseModel):
|
| 187 |
+
reports: List[ApproveSaveRequest]
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
@router.post("/batch-save")
|
| 191 |
+
async def batch_approve_save(request: BatchApproveSaveRequest, db: Session = Depends(get_db)):
|
| 192 |
+
"""Commit multiple verified pathology reports to PostgreSQL EMR in batch"""
|
| 193 |
+
saved_ids = []
|
| 194 |
+
errors = []
|
| 195 |
+
|
| 196 |
+
for item in request.reports:
|
| 197 |
+
try:
|
| 198 |
+
extraction_data = item.dict()
|
| 199 |
+
report = await asyncio.to_thread(
|
| 200 |
+
DatabaseService.save_pathology_report,
|
| 201 |
+
db, item.file_id, extraction_data
|
| 202 |
+
)
|
| 203 |
+
saved_ids.append(report.id)
|
| 204 |
+
|
| 205 |
+
# Generate and index BioLORD embeddings
|
| 206 |
+
doc = await asyncio.to_thread(DatabaseService.get_document, db, item.file_id)
|
| 207 |
+
if doc:
|
| 208 |
+
text_to_embed = doc.cleaned_text or doc.raw_text or item.summary
|
| 209 |
+
if text_to_embed:
|
| 210 |
+
try:
|
| 211 |
+
from app.services.embedding_service import EmbeddingService
|
| 212 |
+
embed_service = EmbeddingService()
|
| 213 |
+
embed_res = await asyncio.to_thread(
|
| 214 |
+
embed_service.generate_embedding, text_to_embed
|
| 215 |
+
)
|
| 216 |
+
if embed_res.get("status") == "success":
|
| 217 |
+
await asyncio.to_thread(
|
| 218 |
+
DatabaseService.save_embedding,
|
| 219 |
+
db,
|
| 220 |
+
item.file_id,
|
| 221 |
+
embed_res["embedding"],
|
| 222 |
+
text_to_embed[:500],
|
| 223 |
+
)
|
| 224 |
+
except Exception as emb_err:
|
| 225 |
+
print(f"[WARN] Batch embedding skipped for {item.file_id}: {emb_err}")
|
| 226 |
+
except Exception as e:
|
| 227 |
+
errors.append({"file_id": item.file_id, "error": str(e)})
|
| 228 |
+
|
| 229 |
+
return {
|
| 230 |
+
"status": "success",
|
| 231 |
+
"saved_count": len(saved_ids),
|
| 232 |
+
"report_ids": saved_ids,
|
| 233 |
+
"errors": errors,
|
| 234 |
+
}
|