File size: 4,891 Bytes
76d67c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import base64
from io import BytesIO
from PIL import Image
from markitdown import MarkItDown

def encode_image_to_base64(image):
    """Encodes a PIL Image to a base64 string."""
    if isinstance(image, str):
        # If it's a file path
        with open(image, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')
    elif isinstance(image, Image.Image):
        buffered = BytesIO()
        image.save(buffered, format="JPEG")
        return base64.b64encode(buffered.getvalue()).decode('utf-8')
    return None

def convert_doc_to_markdown(file_path):
    """Converts a document (PDF, Docx, etc.) to markdown."""
    if not file_path:
        return ""
    try:
        md = MarkItDown()
        result = md.convert(file_path)
        return result.text_content
    except Exception as e:
        return f"Error converting document: {str(e)}"

def get_mock_claims():
    """Returns a list of mock claims for the dashboard."""
    return [
        {
            "id": "CLM-1001",
            "submitter": "John Doe",
            "date": "2023-10-25",
            "vehicle": "2018 Toyota Camry",
            "status": "New",
            "ai_analysis": {
                "damage_estimate": "£820",
                "fraud_risk": "Low",
                "adjuster_classification": "Junior Adjuster",
                "recommendation": "Auto-approve payment",
                "summary": "Minor rear bumper damage. Policy active. No suspicious indicators."
            }
        },
        {
            "id": "CLM-1002",
            "submitter": "Jane Smith",
            "date": "2023-10-24",
            "vehicle": "2022 Tesla Model 3",
            "status": "Under Review",
            "ai_analysis": {
                "damage_estimate": "£15,000",
                "fraud_risk": "High",
                "adjuster_classification": "Senior Adjuster",
                "recommendation": "Escalate to SIU",
                "summary": "Severe front-end collision. Multiple vehicles involved. Discrepancy in accident location report."
            }
        },
        {
            "id": "CLM-1003",
            "submitter": "Robert Brown",
            "date": "2023-10-26",
            "vehicle": "2015 Ford Focus",
            "status": "New",
            "ai_analysis": {
                "damage_estimate": "£1,200",
                "fraud_risk": "Low",
                "adjuster_classification": "Junior Adjuster",
                "recommendation": "Review Further",
                "summary": "Side panel scratch and dent. consistent with description. Higher than average repair cost for model."
            }
        }
    ]

def extract_claim_data(text):
    """
    Extracts structured data from the AI's markdown response.
    Returns a dictionary with keys: vehicle, damage_estimate, fraud_risk, adjuster_classification, summary, recommendation.
    """
    import re
    data = {
        "submitter": "Anonymous",
        "vehicle": "Unknown",
        "damage_estimate": "N/A",
        "fraud_risk": "Unknown",
        "adjuster_classification": "Junior Adjuster",
        "recommendation": "Review",
        "summary": "Auto-generated summary from intake."
    }
    
    # Simple regex extraction based on the prompt format
    # *   **Submitter Name:** [Name]
    name_match = re.search(r"\*\*Submitter Name:\*\*\s*(.*)", text)
    if name_match:
        data["submitter"] = name_match.group(1).strip()

    # *   **Vehicle:** [Vehicle Make/Model/Year if identifiable, else 'Unknown']
    vehicle_match = re.search(r"\*\*Vehicle:\*\*\s*(.*)", text)
    if vehicle_match:
        data["vehicle"] = vehicle_match.group(1).strip()

    estimate_match = re.search(r"\*\*Estimated Repair Cost:\*\*\s*(.*)", text)
    if estimate_match:
        data["damage_estimate"] = estimate_match.group(1).strip()

    fraud_match = re.search(r"\*\*Fraud Risk:\*\*\s*(.*)", text)
    if fraud_match:
        data["fraud_risk"] = fraud_match.group(1).strip()

    class_match = re.search(r"\*\*Adjuster Classification:\*\*\s*(.*)", text)
    if class_match:
        data["adjuster_classification"] = class_match.group(1).strip()
        
    summary_match = re.search(r"\*\*Summary:\*\*\s*(.*)", text)
    if summary_match:
        data["summary"] = summary_match.group(1).strip()
    else:
        # Fallback: use the first few lines or the whole text if summary not found explicitly
        data["summary"] = text[:200] + "..."

    # Infer recommendation based on classification/risk if not explicitly parsed (or add prompt for it)
    # For now, let's look for "Next Steps" or just default based on risk
    if "High" in data["fraud_risk"]:
        data["recommendation"] = "Escalate to SIU"
    elif "Low" in data["fraud_risk"] and "Junior" in data["adjuster_classification"]:
        data["recommendation"] = "Auto-approve payment"
    
    return data