Spaces:
Sleeping
Sleeping
Delete src/core/pdf_processor.py
Browse files- src/core/pdf_processor.py +0 -283
src/core/pdf_processor.py
DELETED
|
@@ -1,283 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Human+ PDF Processor
|
| 3 |
-
Extracts biomarkers from lab PDF using Gemini vision.
|
| 4 |
-
Returns structured list of biomarker dicts ready for UI rendering.
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
import json
|
| 8 |
-
import re
|
| 9 |
-
from typing import Optional
|
| 10 |
-
from core.ai_client import call_gemini, pdf_bytes_to_part, TEMP_EXTRACTION
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# ── Biomarker extraction prompt ──────────────────────────────────────
|
| 14 |
-
_EXTRACTION_PROMPT = """
|
| 15 |
-
Kamu adalah AI lab analyst dari Human+ Bali.
|
| 16 |
-
|
| 17 |
-
Tugasmu: Ekstrak SEMUA biomarker dari hasil lab PDF ini.
|
| 18 |
-
|
| 19 |
-
PENTING — Kembalikan HANYA valid JSON array, tanpa teks lain, tanpa markdown code block.
|
| 20 |
-
|
| 21 |
-
Format setiap item:
|
| 22 |
-
{
|
| 23 |
-
"name": "Nama biomarker (gunakan nama standar, contoh: hs-CRP, Vitamin D, HbA1c)",
|
| 24 |
-
"value": "Nilai numerik sebagai string (contoh: '2.4', '22', '5.6')",
|
| 25 |
-
"unit": "Satuan (contoh: mg/L, ng/mL, %)",
|
| 26 |
-
"reference_range": "Range referensi dari lab (contoh: 0.0-5.0 mg/L)",
|
| 27 |
-
"lab_flag": "normal | high | low | critical_high | critical_low"
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
Prioritas biomarker (wajib cari jika ada):
|
| 31 |
-
- Inflammation: hs-CRP, Homocysteine, Ferritin
|
| 32 |
-
- Metabolic: HbA1c, Fasting Glucose, Fasting Insulin
|
| 33 |
-
- Hormones: Total Testosterone, Free Testosterone, Vitamin D, DHEA-S, Cortisol, TSH
|
| 34 |
-
- Nutrients: Vitamin B12, Magnesium, Zinc, Folate, Vitamin B6
|
| 35 |
-
- Cardiovascular: ApoB, Triglycerides, HDL, LDL, Total Cholesterol
|
| 36 |
-
- Blood: Hemoglobin, Hematocrit, WBC, Platelets
|
| 37 |
-
- Liver: ALT, AST, GGT, Bilirubin
|
| 38 |
-
- Kidney: Creatinine, eGFR, Uric Acid
|
| 39 |
-
|
| 40 |
-
Jika nilai tidak terbaca atau tidak ada, SKIP biomarker tersebut.
|
| 41 |
-
Kembalikan HANYA JSON array, tidak ada teks lain.
|
| 42 |
-
"""
|
| 43 |
-
|
| 44 |
-
# ── Human+ optimal ranges (untuk status mapping) ─────────────────────
|
| 45 |
-
_OPTIMAL_RANGES: dict[str, dict] = {
|
| 46 |
-
"hs-crp": {"optimal": (None, 1.0), "warning": (1.0, 3.0)},
|
| 47 |
-
"homocysteine": {"optimal": (None, 8.0), "warning": (8.0, 12.0)},
|
| 48 |
-
"ferritin_m": {"optimal": (50, 150), "warning": (30, 50)}, # pria
|
| 49 |
-
"ferritin_f": {"optimal": (30, 100), "warning": (15, 30)}, # wanita
|
| 50 |
-
"hba1c": {"optimal": (4.8, 5.2), "warning": (5.2, 5.6)},
|
| 51 |
-
"fasting glucose": {"optimal": (75, 86), "warning": (70, 100)},
|
| 52 |
-
"fasting insulin": {"optimal": (None, 7.0), "warning": (7.0, 10.0)},
|
| 53 |
-
"total testosterone": {"optimal": (600, None), "warning": (400, 600)},
|
| 54 |
-
"vitamin d": {"optimal": (50, 80), "warning": (30, 50)},
|
| 55 |
-
"dhea-s": {"optimal": None, "warning": None}, # age-dependent
|
| 56 |
-
"vitamin b12": {"optimal": (500, None), "warning": (300, 500)},
|
| 57 |
-
"magnesium": {"optimal": (2.2, None), "warning": (1.8, 2.2)},
|
| 58 |
-
"zinc": {"optimal": (90, 120), "warning": (70, 90)},
|
| 59 |
-
"folate": {"optimal": (10, None), "warning": (5, 10)},
|
| 60 |
-
"apob": {"optimal": (None, 80), "warning": (80, 100)},
|
| 61 |
-
"triglycerides": {"optimal": (None, 90), "warning": (90, 150)},
|
| 62 |
-
"hdl": {"optimal": (60, None), "warning": (40, 60)},
|
| 63 |
-
"tsh": {"optimal": (0.5, 2.0), "warning": (2.0, 4.0)},
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
class PDFProcessorError(Exception):
|
| 68 |
-
"""Raised when PDF extraction fails."""
|
| 69 |
-
pass
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
def extract_biomarkers(pdf_bytes: bytes) -> list[dict]:
|
| 73 |
-
"""
|
| 74 |
-
Extract biomarkers dari PDF bytes menggunakan Gemini.
|
| 75 |
-
|
| 76 |
-
Args:
|
| 77 |
-
pdf_bytes: raw PDF file bytes
|
| 78 |
-
|
| 79 |
-
Returns:
|
| 80 |
-
list of biomarker dicts dengan keys:
|
| 81 |
-
- name, value, unit, status, reference (Human+ optimal range)
|
| 82 |
-
- raw_value, lab_flag, reference_range (dari lab asli)
|
| 83 |
-
|
| 84 |
-
Raises:
|
| 85 |
-
PDFProcessorError: jika extraction gagal
|
| 86 |
-
"""
|
| 87 |
-
# 1. Call Gemini dengan PDF
|
| 88 |
-
raw_json = _call_gemini_extraction(pdf_bytes)
|
| 89 |
-
|
| 90 |
-
# 2. Parse JSON response
|
| 91 |
-
raw_biomarkers = _parse_json_response(raw_json)
|
| 92 |
-
|
| 93 |
-
# 3. Map ke Human+ status (optimal/warning/danger)
|
| 94 |
-
enriched = [_enrich_biomarker(b) for b in raw_biomarkers]
|
| 95 |
-
|
| 96 |
-
# 4. Filter yang invalid, sort by status priority
|
| 97 |
-
valid = [b for b in enriched if b is not None]
|
| 98 |
-
return _sort_by_priority(valid)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
def get_raw_extraction_text(pdf_bytes: bytes) -> str:
|
| 102 |
-
"""
|
| 103 |
-
Return raw Gemini extraction text (untuk ditampilkan di expander debug).
|
| 104 |
-
Useful untuk troubleshooting jika parsing gagal.
|
| 105 |
-
"""
|
| 106 |
-
return _call_gemini_extraction(pdf_bytes)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
# ── Private helpers ──────────────────────────────────────────────────
|
| 110 |
-
|
| 111 |
-
def _call_gemini_extraction(pdf_bytes: bytes) -> str:
|
| 112 |
-
"""Send PDF to Gemini and return raw text response."""
|
| 113 |
-
try:
|
| 114 |
-
pdf_part = pdf_bytes_to_part(pdf_bytes)
|
| 115 |
-
response = call_gemini(
|
| 116 |
-
contents=[_EXTRACTION_PROMPT, pdf_part],
|
| 117 |
-
temperature=TEMP_EXTRACTION,
|
| 118 |
-
)
|
| 119 |
-
return response
|
| 120 |
-
except Exception as e:
|
| 121 |
-
raise PDFProcessorError(f"Gemini extraction gagal: {e}") from e
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
def _parse_json_response(raw: str) -> list[dict]:
|
| 125 |
-
"""
|
| 126 |
-
Parse JSON dari Gemini response.
|
| 127 |
-
Gemini kadang return ```json ... ``` atau teks tambahan — kita strip dulu.
|
| 128 |
-
"""
|
| 129 |
-
# Strip markdown code fences jika ada
|
| 130 |
-
cleaned = re.sub(r"```(?:json)?", "", raw).strip().rstrip("```").strip()
|
| 131 |
-
|
| 132 |
-
# Coba parse langsung
|
| 133 |
-
try:
|
| 134 |
-
data = json.loads(cleaned)
|
| 135 |
-
if isinstance(data, list):
|
| 136 |
-
return data
|
| 137 |
-
# Kadang Gemini wrap dalam object
|
| 138 |
-
if isinstance(data, dict):
|
| 139 |
-
for key in ["biomarkers", "results", "data", "markers"]:
|
| 140 |
-
if key in data and isinstance(data[key], list):
|
| 141 |
-
return data[key]
|
| 142 |
-
except json.JSONDecodeError:
|
| 143 |
-
pass
|
| 144 |
-
|
| 145 |
-
# Fallback: cari array JSON di dalam teks
|
| 146 |
-
match = re.search(r"\[[\s\S]*\]", cleaned)
|
| 147 |
-
if match:
|
| 148 |
-
try:
|
| 149 |
-
return json.loads(match.group())
|
| 150 |
-
except json.JSONDecodeError:
|
| 151 |
-
pass
|
| 152 |
-
|
| 153 |
-
raise PDFProcessorError(
|
| 154 |
-
"Gagal parse JSON dari Gemini. "
|
| 155 |
-
"PDF mungkin tidak terbaca dengan baik atau format tidak standard."
|
| 156 |
-
)
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
def _enrich_biomarker(raw: dict) -> Optional[dict]:
|
| 160 |
-
"""
|
| 161 |
-
Enrich raw biomarker dict dengan Human+ status mapping.
|
| 162 |
-
|
| 163 |
-
Input keys: name, value, unit, reference_range, lab_flag
|
| 164 |
-
Output adds: status ('optimal'|'warning'|'danger'|'neutral'), reference (Human+)
|
| 165 |
-
"""
|
| 166 |
-
try:
|
| 167 |
-
name = str(raw.get("name", "")).strip()
|
| 168 |
-
value = str(raw.get("value", "")).strip()
|
| 169 |
-
unit = str(raw.get("unit", "")).strip()
|
| 170 |
-
|
| 171 |
-
if not name or not value:
|
| 172 |
-
return None
|
| 173 |
-
|
| 174 |
-
# Parse numeric value
|
| 175 |
-
try:
|
| 176 |
-
numeric_value = float(re.sub(r"[^\d.\-]", "", value))
|
| 177 |
-
except (ValueError, TypeError):
|
| 178 |
-
numeric_value = None
|
| 179 |
-
|
| 180 |
-
# Map status
|
| 181 |
-
status, human_plus_ref = _map_to_human_plus_status(
|
| 182 |
-
name, numeric_value, raw.get("lab_flag", "normal")
|
| 183 |
-
)
|
| 184 |
-
|
| 185 |
-
# Gunakan Human+ target jika tersedia, fallback ke reference_range dari lab
|
| 186 |
-
display_reference = human_plus_ref if human_plus_ref else raw.get("reference_range", "")
|
| 187 |
-
|
| 188 |
-
return {
|
| 189 |
-
# For UI rendering
|
| 190 |
-
"name": name,
|
| 191 |
-
"value": value,
|
| 192 |
-
"unit": unit,
|
| 193 |
-
"status": status,
|
| 194 |
-
"reference": display_reference,
|
| 195 |
-
# Raw lab data (untuk expander / debug)
|
| 196 |
-
"raw_value": numeric_value,
|
| 197 |
-
"lab_flag": raw.get("lab_flag", "normal"),
|
| 198 |
-
"reference_range": raw.get("reference_range", ""),
|
| 199 |
-
"human_plus_ref": human_plus_ref, # Human+ target khusus (bisa berbeda)
|
| 200 |
-
}
|
| 201 |
-
except Exception:
|
| 202 |
-
return None
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
def _map_to_human_plus_status(
|
| 206 |
-
name: str,
|
| 207 |
-
value: Optional[float],
|
| 208 |
-
lab_flag: str,
|
| 209 |
-
) -> tuple[str, str]:
|
| 210 |
-
"""
|
| 211 |
-
Map biomarker to Human+ optimal status.
|
| 212 |
-
|
| 213 |
-
Returns:
|
| 214 |
-
(status, human_plus_reference_string)
|
| 215 |
-
status: 'optimal' | 'warning' | 'danger' | 'neutral'
|
| 216 |
-
"""
|
| 217 |
-
# Normalisasi: hapus teks dalam kurung, lowercase
|
| 218 |
-
# contoh: "hs-CRP (High-sensitivity C-Reactive Protein)" → "hs-crp"
|
| 219 |
-
name_lower = re.sub(r"\s*\(.*?\)", "", name.lower()).strip()
|
| 220 |
-
|
| 221 |
-
# Find matching range config
|
| 222 |
-
range_cfg = None
|
| 223 |
-
for key, cfg in _OPTIMAL_RANGES.items():
|
| 224 |
-
if key == name_lower or key in name_lower or name_lower in key:
|
| 225 |
-
range_cfg = cfg
|
| 226 |
-
break
|
| 227 |
-
|
| 228 |
-
# No config found — fallback ke lab_flag
|
| 229 |
-
if range_cfg is None or value is None:
|
| 230 |
-
if lab_flag in ("normal",):
|
| 231 |
-
return "neutral", ""
|
| 232 |
-
elif lab_flag in ("high", "low"):
|
| 233 |
-
return "warning", ""
|
| 234 |
-
elif lab_flag in ("critical_high", "critical_low"):
|
| 235 |
-
return "danger", ""
|
| 236 |
-
return "neutral", ""
|
| 237 |
-
|
| 238 |
-
# Check optimal range
|
| 239 |
-
optimal = range_cfg.get("optimal")
|
| 240 |
-
warning = range_cfg.get("warning")
|
| 241 |
-
|
| 242 |
-
if optimal:
|
| 243 |
-
lo, hi = optimal
|
| 244 |
-
in_optimal = (
|
| 245 |
-
(lo is None or value >= lo) and
|
| 246 |
-
(hi is None or value <= hi)
|
| 247 |
-
)
|
| 248 |
-
if in_optimal:
|
| 249 |
-
ref_str = _format_range_str(lo, hi)
|
| 250 |
-
return "optimal", ref_str
|
| 251 |
-
|
| 252 |
-
if warning:
|
| 253 |
-
lo, hi = warning
|
| 254 |
-
in_warning = (
|
| 255 |
-
(lo is None or value >= lo) and
|
| 256 |
-
(hi is None or value <= hi)
|
| 257 |
-
)
|
| 258 |
-
if in_warning:
|
| 259 |
-
opt_lo, opt_hi = (optimal or (None, None))
|
| 260 |
-
ref_str = _format_range_str(opt_lo, opt_hi)
|
| 261 |
-
return "warning", ref_str
|
| 262 |
-
|
| 263 |
-
# Below all ranges or above all ranges → danger
|
| 264 |
-
opt_lo, opt_hi = (optimal or (None, None))
|
| 265 |
-
ref_str = _format_range_str(opt_lo, opt_hi)
|
| 266 |
-
return "danger", ref_str
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
def _format_range_str(lo: Optional[float], hi: Optional[float]) -> str:
|
| 270 |
-
"""Format optimal range sebagai human-readable string."""
|
| 271 |
-
if lo is not None and hi is not None:
|
| 272 |
-
return f"{lo}–{hi}"
|
| 273 |
-
elif lo is not None:
|
| 274 |
-
return f"> {lo}"
|
| 275 |
-
elif hi is not None:
|
| 276 |
-
return f"< {hi}"
|
| 277 |
-
return ""
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
def _sort_by_priority(biomarkers: list[dict]) -> list[dict]:
|
| 281 |
-
"""Sort: danger first, then warning, then optimal, then neutral."""
|
| 282 |
-
priority = {"danger": 0, "warning": 1, "optimal": 2, "neutral": 3}
|
| 283 |
-
return sorted(biomarkers, key=lambda b: priority.get(b["status"], 3))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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