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|
|
|
|
|
|
| 1 |
+
import fitz
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
MODEL_NAME = "meta-llama/Llama-3.2-3B-Instruct"
|
| 8 |
+
|
| 9 |
+
print("Loading model...")
|
| 10 |
+
|
| 11 |
+
pipe = pipeline(
|
| 12 |
+
"text-generation",
|
| 13 |
+
model=MODEL_NAME,
|
| 14 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 15 |
+
device_map="auto"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
print("Model loaded.")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def extract_text(pdf_file):
|
| 22 |
+
|
| 23 |
+
text = ""
|
| 24 |
+
|
| 25 |
+
doc = fitz.open(pdf_file.name)
|
| 26 |
+
|
| 27 |
+
for page in doc:
|
| 28 |
+
text += page.get_text()
|
| 29 |
+
|
| 30 |
+
doc.close()
|
| 31 |
+
|
| 32 |
+
return text[:20000]
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def build_prompt(policy_text):
|
| 36 |
+
|
| 37 |
+
return f"""
|
| 38 |
+
You are a senior insurance consultant.
|
| 39 |
+
|
| 40 |
+
Analyze the insurance policy and produce a customer-friendly report.
|
| 41 |
+
|
| 42 |
+
Return your answer in markdown.
|
| 43 |
+
|
| 44 |
+
# Executive Summary
|
| 45 |
+
|
| 46 |
+
Summarize the policy in simple English.
|
| 47 |
+
|
| 48 |
+
# What Is Covered
|
| 49 |
+
|
| 50 |
+
Provide bullet points.
|
| 51 |
+
|
| 52 |
+
# Major Exclusions
|
| 53 |
+
|
| 54 |
+
Provide bullet points.
|
| 55 |
+
|
| 56 |
+
# Waiting Periods
|
| 57 |
+
|
| 58 |
+
Provide bullet points.
|
| 59 |
+
|
| 60 |
+
# Claim Process
|
| 61 |
+
|
| 62 |
+
Step-by-step instructions.
|
| 63 |
+
|
| 64 |
+
# Coverage Gaps
|
| 65 |
+
|
| 66 |
+
Identify situations where customers may incorrectly assume coverage.
|
| 67 |
+
|
| 68 |
+
# Red Flags
|
| 69 |
+
|
| 70 |
+
Anything customers commonly overlook.
|
| 71 |
+
|
| 72 |
+
# Questions To Ask Your Insurer
|
| 73 |
+
|
| 74 |
+
Provide 5 questions.
|
| 75 |
+
|
| 76 |
+
# Explain Like I'm 15
|
| 77 |
+
|
| 78 |
+
Explain the policy in simple language.
|
| 79 |
+
|
| 80 |
+
Policy Text:
|
| 81 |
+
|
| 82 |
+
{policy_text}
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def analyze_policy(pdf_file):
|
| 87 |
+
|
| 88 |
+
if pdf_file is None:
|
| 89 |
+
return "Please upload a policy PDF."
|
| 90 |
+
|
| 91 |
+
policy_text = extract_text(pdf_file)
|
| 92 |
+
|
| 93 |
+
prompt = build_prompt(policy_text)
|
| 94 |
+
|
| 95 |
+
messages = [
|
| 96 |
+
{
|
| 97 |
+
"role": "system",
|
| 98 |
+
"content": "You are an expert insurance policy analyst."
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"role": "user",
|
| 102 |
+
"content": prompt
|
| 103 |
+
}
|
| 104 |
+
]
|
| 105 |
+
|
| 106 |
+
result = pipe(
|
| 107 |
+
messages,
|
| 108 |
+
max_new_tokens=1200,
|
| 109 |
+
temperature=0.2,
|
| 110 |
+
do_sample=True
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
return result[0]["generated_text"][-1]["content"]
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
with gr.Blocks(
|
| 117 |
+
title="Insurance Policy Decoder",
|
| 118 |
+
theme=gr.themes.Soft()
|
| 119 |
+
) as demo:
|
| 120 |
+
|
| 121 |
+
gr.Markdown(
|
| 122 |
+
"""
|
| 123 |
+
# π‘οΈ Insurance Policy Decoder
|
| 124 |
+
|
| 125 |
+
Understand your insurance policy in less than a minute.
|
| 126 |
+
|
| 127 |
+
Upload a policy PDF to get:
|
| 128 |
+
|
| 129 |
+
β
Simple Summary
|
| 130 |
+
|
| 131 |
+
β
Coverage Details
|
| 132 |
+
|
| 133 |
+
β
Major Exclusions
|
| 134 |
+
|
| 135 |
+
β
Waiting Periods
|
| 136 |
+
|
| 137 |
+
β
Claim Checklist
|
| 138 |
+
|
| 139 |
+
β
Coverage Gaps
|
| 140 |
+
|
| 141 |
+
β
Questions To Ask Your Insurer
|
| 142 |
+
"""
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
with gr.Row():
|
| 146 |
+
|
| 147 |
+
pdf_input = gr.File(
|
| 148 |
+
label="Upload Insurance Policy",
|
| 149 |
+
file_types=[".pdf"]
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
analyze_btn = gr.Button(
|
| 153 |
+
"Decode Policy",
|
| 154 |
+
variant="primary"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
output = gr.Markdown()
|
| 158 |
+
|
| 159 |
+
analyze_btn.click(
|
| 160 |
+
fn=analyze_policy,
|
| 161 |
+
inputs=pdf_input,
|
| 162 |
+
outputs=output
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
demo.launch()
|