Spaces:
Sleeping
Sleeping
| 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 | |