CoolFace
Apppublic

BytecodeApps/docverse-api

sourceHugging Faceapache-2.0updated 7mo agoView on Hugging Face
0likes
structuring_engine.py49 linesDownload Raw Back to services
1from typing import Dict, Any, List2import json3 4class StructuringEngine:5    """6    Takes raw NLP entities and OCR text, mapping them into structured JSON7    that aligns with our PostgreSQL database schema (Medications, LabResults).8    """9 10    @staticmethod11    def construct_report_payload(raw_text: str, nlp_entities: Dict[str, Any], evaluation_flags: List[Dict]) -> str:12        """13        Constructs the final structured JSON object to be sent to the frontend14        and stored in the database.15        """16        17        # In a real scenario, this engine would use regex/heuristics to pair18        # extracted chemical entities with their specific dosages (e.g. "Lisinopril 10mg")19        # from the surrounding text in the OCR output.20        21        medications = []22        for chemical in nlp_entities.get("chemical_entity", []):23            medications.append({24                "drug_name": chemical,25                "dosage": "Unknown (Requires Manual Verification)", # Extracted from context heuristic26                "frequency": "Unknown",27                "instructions": "As prescribed by physician"28            })29            30        lab_results = nlp_entities.get("extracted_vitals", [])31 32        report = {33            "metadata": {34                "confidence_score": "High", # Based on OCR read certainty35                "source_text_length": len(raw_text)36            },37            "extracted_data": {38                "medications": medications,39                "conditions": nlp_entities.get("disease_entity", []),40                "lab_results": lab_results41            },42            "risk_analysis": {43                "flags": evaluation_flags,44                "summary": "High priority alerts detected." if any(f.get("severity") in ["EMERGENCY", "HIGH"] for f in evaluation_flags) else "No immediate risks detected."45            }46        }47 48        return json.dumps(report, indent=2)49