Skip to content

Commit b37ac4f

Browse files
committed
insurance fix
1 parent f9fcfe5 commit b37ac4f

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

app.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,10 @@ def extract_insurance_hybrid(text):
796796
is_health_insurance = bool(re.search(r"(?i)(health\s+insurance|mediclaim|medical\s+insurance|hospitali[sz]ation|health\s*cover|in-?patient\s+treatment|annual\s+sum\s+insured|sum\s+insured|health\s+advantedge|health\s+plan|health\s+policy)", text))
797797
is_general_insurance = bool(re.search(r"(?i)(motor\s+insurance|vehicle\s+insurance|property\s+insurance|home\s+insurance|fire\s+insurance)", text))
798798

799+
# Debug logging
800+
print(f"[Insurance Extract] is_life_insurance: {is_life_insurance}, is_health_insurance: {is_health_insurance}, is_general_insurance: {is_general_insurance}")
801+
print(f"[Insurance Extract] Text snippet (first 500 chars): {text[:500] if text else 'None'}")
802+
799803
if is_life_insurance:
800804
data["insurance_type"] = "Life Insurance"
801805
elif is_health_insurance:
@@ -804,6 +808,8 @@ def extract_insurance_hybrid(text):
804808
data["insurance_type"] = "General Insurance"
805809
else:
806810
data["insurance_type"] = "Unknown"
811+
812+
print(f"[Insurance Extract] Final insurance_type: {data['insurance_type']}")
807813

808814
patterns = {
809815
"policy_number": [
@@ -857,15 +863,17 @@ def _parse_indian_amount(num_str, suffix_str=""):
857863
"""Convert amount with optional Lakhs/Crore suffix to actual number."""
858864
try:
859865
base_val = clean_and_convert_to_float(num_str)
860-
if base_val is None:
866+
# clean_and_convert_to_float returns "N/A" string on failure, not None
867+
if base_val is None or base_val == "N/A" or not isinstance(base_val, (int, float)):
861868
return None
862869
suffix_lower = suffix_str.lower().strip() if suffix_str else ""
863870
if suffix_lower in ("lakh", "lakhs", "lac", "lacs"):
864871
return base_val * 100000
865872
elif suffix_lower in ("crore", "crores", "cr"):
866873
return base_val * 10000000
867874
return base_val
868-
except Exception:
875+
except Exception as e:
876+
print(f"[Insurance Extract] Error in _parse_indian_amount: {e}")
869877
return None
870878

871879
# Patterns that capture amount AND optional Lakhs/Crore suffix
@@ -900,12 +908,15 @@ def _parse_indian_amount(num_str, suffix_str=""):
900908
best_val = parsed
901909
if best_val is not None:
902910
sum_value = best_val
911+
print(f"[Insurance Extract] Pattern matched: {pattern}, best_val: {best_val}")
903912
break
904913

905914
if sum_value is not None:
906915
data["sum_assured_or_insured"] = sum_value
907916
else:
908917
data["sum_assured_or_insured"] = "N/A"
918+
919+
print(f"[Insurance Extract] Final sum_assured_or_insured: {data['sum_assured_or_insured']}")
909920

910921
premium_patterns = [
911922
r"(?i)(?:Annual\s*)?Premium(?:\s*Amount)?[\s:\-]*(?:Rs\.?|₹)?\s*([\d,]+)",
@@ -2119,27 +2130,34 @@ def build_prefill_from_insights(qid: int) -> dict:
21192130
doc_type = (upload["doc_type"] or "").lower()
21202131
if "insurance" in doc_type:
21212132
metadata_json = upload["metadata_json"]
2133+
print(f"[Prefill] Found insurance upload, metadata_json: {metadata_json[:500] if metadata_json else 'None'}")
21222134
if metadata_json:
21232135
try:
21242136
metadata = json.loads(metadata_json)
21252137
ins_type = str(metadata.get("insurance_type") or "").lower()
21262138
sum_val = metadata.get("sum_assured_or_insured")
21272139

2140+
print(f"[Prefill] Parsed insurance_type: '{ins_type}', sum_assured_or_insured: {sum_val}")
2141+
21282142
# Also check aggregated insights if not in metadata
21292143
if sum_val is None or sum_val == "N/A":
21302144
sum_val = ins.get("sum_assured_or_insured")
2145+
print(f"[Prefill] Fallback to aggregated insights: {sum_val}")
21312146

21322147
if isinstance(sum_val, (int, float)) and sum_val > 0:
21332148
if "health" in ins_type or "mediclaim" in ins_type:
21342149
# Add to health cover (may have multiple health policies)
21352150
existing_health = insurance_prefill.get("health_cover", 0.0)
21362151
insurance_prefill["health_cover"] = existing_health + float(sum_val)
2152+
print(f"[Prefill] Added to health_cover: {sum_val}")
21372153
elif "life" in ins_type or "term" in ins_type or "ulip" in ins_type:
21382154
# Add to life cover
21392155
existing_life = insurance_prefill.get("life_cover", 0.0)
21402156
insurance_prefill["life_cover"] = existing_life + float(sum_val)
2157+
print(f"[Prefill] Added to life_cover: {sum_val}")
21412158
else:
21422159
# Unknown type: default to life_cover
2160+
print(f"[Prefill] Unknown insurance type '{ins_type}', defaulting to life_cover")
21432161
existing_life = insurance_prefill.get("life_cover", 0.0)
21442162
insurance_prefill["life_cover"] = existing_life + float(sum_val)
21452163
except Exception:
@@ -2415,6 +2433,7 @@ def upload_document():
24152433
other_data["provenance"] = summaries["provenance"]
24162434

24172435
# Update document metadata if linked to questionnaire
2436+
print(f"[Upload] doc_type={doc_type}, idx={idx}, upload_link_ids={upload_link_ids}, idx in upload_link_ids: {idx in upload_link_ids}")
24182437
if idx in upload_link_ids:
24192438
try:
24202439
metadata_update = {"size_bytes": len(file_bytes)}
@@ -2442,8 +2461,10 @@ def upload_document():
24422461
metadata_update["insurance_type"] = other_data.get("insurance_type")
24432462
metadata_update["sum_assured_or_insured"] = other_data.get("sum_assured_or_insured")
24442463
metadata_update["date_of_birth"] = other_data.get("date_of_birth")
2464+
print(f"[Upload] Insurance metadata to save: insurance_type={metadata_update.get('insurance_type')}, sum={metadata_update.get('sum_assured_or_insured')}")
24452465

24462466
update_questionnaire_upload_metadata(upload_link_ids[idx], metadata_update)
2467+
print(f"[Upload] Metadata updated for doc_type={doc_type}, upload_id={upload_link_ids[idx]}")
24472468
except Exception as e:
24482469
print(f"Error updating {doc_type} metadata: {e}")
24492470

output/index.db

4 KB
Binary file not shown.

0 commit comments

Comments
 (0)