File size: 4,256 Bytes
900dfe3 a38d4ad 35872c0 a38d4ad c40d86c a38d4ad 35872c0 a38d4ad 35872c0 f0d876d 35872c0 a38d4ad 35872c0 a38d4ad 900dfe3 a38d4ad 35872c0 a38d4ad 35872c0 fbc4dd2 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 f0d876d efe28a5 3defa5a f0d876d 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad 35872c0 a38d4ad | 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | import pdfplumber
import gradio as gr
import torch
from transformers import (
AutoTokenizer,
AutoModelForCausalLM
)
MODEL_NAME = "microsoft/Phi-3.5-mini-instruct"
print("Loading model...")
tokenizer = AutoTokenizer.from_pretrained(
MODEL_NAME,
trust_remote_code=True
)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
torch_dtype=torch.float32,
trust_remote_code=True,
low_cpu_mem_usage=True
)
print("Model loaded successfully.")
def extract_text(pdf_file):
text = ""
try:
with pdfplumber.open(pdf_file.name) as pdf:
for page in pdf.pages:
page_text = page.extract_text()
if page_text:
text += page_text + "\n"
except Exception as e:
return f"PDF Extraction Error: {str(e)}"
print(f"Extracted {len(text)} characters")
# Keep small for free-tier inference
return text[:1000]
def build_prompt(policy_text):
return f"""
You are a senior insurance consultant.
Analyze the insurance policy and create a customer-friendly report.
Return markdown.
# Executive Summary
Summarize the policy in plain English.
# Customer Risk Score
Rate 1-10 and explain why.
# Policy Complexity Score
Rate 1-10 and explain why.
# Claim Difficulty Score
Rate 1-10 and explain why.
# What Is Covered
Provide bullet points.
# Major Exclusions
Provide bullet points.
# Waiting Periods
Provide bullet points.
# Coverage Gaps
Identify situations where customers may wrongly assume they are covered.
# Claim Checklist
Provide step-by-step instructions.
# Questions To Ask The Insurer
Provide 5 questions.
# Explain Like I'm 15
Explain the policy simply.
POLICY DOCUMENT:
{policy_text}
"""
def generate_response(prompt):
messages = [
{
"role": "system",
"content": "You are an expert insurance policy analyst."
},
{
"role": "user",
"content": prompt
}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
inputs = tokenizer(
text,
return_tensors="pt",
truncation=True,
max_length=4096
)
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
inputs = {k: v.to(device) for k, v in inputs.items()}
outputs = model.generate(
**inputs,
max_new_tokens=800,
temperature=0.2,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
generated_tokens = outputs[0][inputs["input_ids"].shape[1]:]
response = tokenizer.decode(
generated_tokens,
skip_special_tokens=True
)
return response
def analyze_policy(pdf_file):
try:
if pdf_file is None:
return "Please upload a policy PDF."
policy_text = extract_text(pdf_file)
if len(policy_text.strip()) == 0:
return "No text could be extracted from this PDF."
prompt = build_prompt(policy_text)
response = generate_response(prompt)
return response
except Exception as e:
print("ERROR:", e)
return f"""
# Error
{str(e)}
"""
CUSTOM_CSS = """
footer {
display:none;
}
.gradio-container {
max-width: 1200px !important;
}
"""
with gr.Blocks(
title="Insurance Policy Decoder",
theme=gr.themes.Soft(),
css=CUSTOM_CSS
) as demo:
gr.Markdown(
"""
# π‘οΈ Insurance Policy Decoder
Understand your insurance policy in less than a minute.
Upload a policy PDF and receive:
β
Executive Summary
β
Coverage Details
β
Exclusions
β
Waiting Periods
β
Coverage Gaps
β
Risk Scores
β
Claim Checklist
β
Questions To Ask Your Insurer
"""
)
pdf_input = gr.File(
label="Upload Insurance Policy PDF",
file_types=[".pdf"]
)
analyze_btn = gr.Button(
"Decode Policy",
variant="primary"
)
output = gr.Markdown(
value="Upload a policy document and click **Decode Policy**."
)
analyze_btn.click(
fn=analyze_policy,
inputs=pdf_input,
outputs=output,
show_progress="full"
)
demo.launch() |