Spaces:
Paused
Paused
Update app/services/llm_extractor.py
Browse files- app/services/llm_extractor.py +37 -17
app/services/llm_extractor.py
CHANGED
|
@@ -60,37 +60,39 @@ Strict Rules:
|
|
| 60 |
|
| 61 |
@staticmethod
|
| 62 |
def get_extraction_prompt(clean_text: str) -> str:
|
| 63 |
-
"""Generate extraction prompt
|
| 64 |
-
return f"""You are
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
{{
|
|
|
|
|
|
|
| 68 |
"patient_id": "extracted or null",
|
| 69 |
"patient_name": "extracted or null",
|
| 70 |
-
"test_type": "type of pathology test",
|
| 71 |
"test_date": "date of test",
|
| 72 |
"findings": [
|
| 73 |
{{
|
| 74 |
-
"test_name": "name of test",
|
| 75 |
"value": "measured value",
|
| 76 |
"unit": "unit of measurement",
|
| 77 |
-
"reference_range": "normal range",
|
| 78 |
"is_abnormal": true/false
|
| 79 |
}}
|
| 80 |
],
|
| 81 |
-
"diagnosis": "primary diagnosis
|
| 82 |
-
"recommendations": "
|
| 83 |
-
"summary": "
|
| 84 |
}}
|
| 85 |
|
| 86 |
Strict Rules:
|
| 87 |
-
1. Return ONLY valid JSON, no
|
| 88 |
-
2. If
|
| 89 |
-
3.
|
| 90 |
-
4. Extract values, reference ranges, and units EXACTLY as they appear in the text.
|
| 91 |
-
5. DO NOT use standard biological or physiological reference ranges from your general knowledge base to "correct", override, or substitute the actual values, units, or ranges written in the report. Always extract exactly what is in the provided text, even if it looks misplaced, unusual, or clinically incorrect.
|
| 92 |
-
6. Pay close attention to column layout and spacing to avoid merging separate numbers from adjacent columns (e.g., do not combine a value column and a reference range column into a single number).
|
| 93 |
-
7. Do not hallucinate or guess any data.
|
| 94 |
|
| 95 |
Medical Report Text:
|
| 96 |
{clean_text}
|
|
@@ -116,6 +118,14 @@ Extract and return as JSON:"""
|
|
| 116 |
"data": None,
|
| 117 |
}
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
prompt = self.get_extraction_prompt(clean_text)
|
| 120 |
|
| 121 |
# High-throughput candidate models with Llama-3.3 primary
|
|
@@ -140,6 +150,16 @@ Extract and return as JSON:"""
|
|
| 140 |
response_text = response.choices[0].message.content or ""
|
| 141 |
extracted_data = json.loads(self._strip_code_fences(response_text))
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
return {
|
| 144 |
"status": "success",
|
| 145 |
"message": f"Extraction successful using open-source {model_name}",
|
|
|
|
| 60 |
|
| 61 |
@staticmethod
|
| 62 |
def get_extraction_prompt(clean_text: str) -> str:
|
| 63 |
+
"""Generate extraction prompt with medical pathology domain validation"""
|
| 64 |
+
return f"""You are an expert clinical medical intelligence validator and data extractor.
|
| 65 |
+
Your task is to:
|
| 66 |
+
1. FIRST, determine if the provided text represents a valid medical pathology or clinical diagnostic laboratory report (such as Complete Blood Count, Liver Function Test, Lipid Profile, Renal Function, Urine Routine, Histopathology, etc.).
|
| 67 |
+
2. If the text is NOT a medical pathology report (for example: a photo of nature/landscape, food, selfie, retail invoice, vehicle paper, random text, or gibberish), you MUST set "is_valid_medical_report": false.
|
| 68 |
+
3. If it IS a medical pathology report, set "is_valid_medical_report": true and extract the structured clinical fields.
|
| 69 |
+
|
| 70 |
+
Return ONLY valid JSON with the following structure:
|
| 71 |
{{
|
| 72 |
+
"is_valid_medical_report": true/false,
|
| 73 |
+
"rejection_reason": "Provide reason if false, or null if true",
|
| 74 |
"patient_id": "extracted or null",
|
| 75 |
"patient_name": "extracted or null",
|
| 76 |
+
"test_type": "type of pathology test (e.g. Complete Blood Count, Lipid Profile)",
|
| 77 |
"test_date": "date of test",
|
| 78 |
"findings": [
|
| 79 |
{{
|
| 80 |
+
"test_name": "name of test (e.g. Hemoglobin, WBC, Platelets, Total Cholesterol)",
|
| 81 |
"value": "measured value",
|
| 82 |
"unit": "unit of measurement",
|
| 83 |
+
"reference_range": "normal biological reference range",
|
| 84 |
"is_abnormal": true/false
|
| 85 |
}}
|
| 86 |
],
|
| 87 |
+
"diagnosis": "primary diagnosis or brief AI clinical evaluation based strictly on findings",
|
| 88 |
+
"recommendations": "medical advice or recommendations",
|
| 89 |
+
"summary": "brief summary of findings highlighting abnormal biomarkers"
|
| 90 |
}}
|
| 91 |
|
| 92 |
Strict Rules:
|
| 93 |
+
1. Return ONLY valid JSON, no markdown outside JSON fences.
|
| 94 |
+
2. If the text has no medical/laboratory content, set "is_valid_medical_report": false.
|
| 95 |
+
3. Extract values, reference ranges, and units EXACTLY as they appear in the text. Do not hallucinate or guess.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
Medical Report Text:
|
| 98 |
{clean_text}
|
|
|
|
| 118 |
"data": None,
|
| 119 |
}
|
| 120 |
|
| 121 |
+
if not clean_text or len(clean_text.strip()) < 15:
|
| 122 |
+
return {
|
| 123 |
+
"status": "error",
|
| 124 |
+
"error_type": "INVALID_DOCUMENT_TYPE",
|
| 125 |
+
"message": "Invalid Document: No readable pathology or laboratory text detected in this image. Please upload a clear diagnostic lab report.",
|
| 126 |
+
"data": None,
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
prompt = self.get_extraction_prompt(clean_text)
|
| 130 |
|
| 131 |
# High-throughput candidate models with Llama-3.3 primary
|
|
|
|
| 150 |
response_text = response.choices[0].message.content or ""
|
| 151 |
extracted_data = json.loads(self._strip_code_fences(response_text))
|
| 152 |
|
| 153 |
+
# Check medical validation guardrail
|
| 154 |
+
if extracted_data.get("is_valid_medical_report") is False:
|
| 155 |
+
reason = extracted_data.get("rejection_reason") or "The uploaded document is not a recognized medical pathology report."
|
| 156 |
+
return {
|
| 157 |
+
"status": "error",
|
| 158 |
+
"error_type": "INVALID_DOCUMENT_TYPE",
|
| 159 |
+
"message": f"Invalid Document: {reason}",
|
| 160 |
+
"data": None,
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
return {
|
| 164 |
"status": "success",
|
| 165 |
"message": f"Extraction successful using open-source {model_name}",
|