forked from Ronnie-Leon76/Automated-Claims-Processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
230 lines (190 loc) · 9.47 KB
/
app.py
File metadata and controls
230 lines (190 loc) · 9.47 KB
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
import streamlit as st
from datetime import datetime
from data_loader import extract_treaty_information_from_documents
from services import process_claims
from models import ClaimsBorderaux, TreatyStatementInformation, Treaty
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import os
import uuid
import hashlib
import redis
import json
import pandas as pd
# Define directory for saving uploaded files
UPLOAD_DIR = "uploaded_files"
REDIS_URL = os.getenv("REDIS_URL")
CACHE_TTL = 432000
redis_client = redis.StrictRedis.from_url(REDIS_URL)
def get_file_hash(file_object):
if file_object is None:
return None
file_object.seek(0)
file_content = file_object.read()
file_object.seek(0)
return hashlib.md5(file_content).hexdigest()
def get_cache_key(pdf_file, excel_file, treaty_file):
pdf_hash = get_file_hash(pdf_file)
excel_hash = get_file_hash(excel_file)
treaty_hash = get_file_hash(treaty_file)
combined_hash = f"{pdf_hash}-{excel_hash}-{treaty_hash}"
return hashlib.md5(combined_hash.encode()).hexdigest()
def cache_result(key, result):
redis_client.setex(key, CACHE_TTL, json.dumps(result))
def get_cached_result(key):
result = redis_client.get(key)
if result:
return json.loads(result.decode())
return None
# Ensure the directory exists
os.makedirs(UPLOAD_DIR, exist_ok=True)
# Streamlit App Implementation
st.title('Claims Processing Application')
# Add information about downloading files
st.info("""
Before proceeding, please download the required files from the following Google Drive link:
[Download Files](https://drive.google.com/drive/folders/1AHOomfMfKPvH6T7Z6LoA5ERI2PNDh_Px?usp=sharing)
You will need to download three files:
1. Contract PDF file
2. Excel file for Borderaux
3. Treaty Slip
The borderaux and treaty slip documents are for the third quarter of the year.
""")
# Step 1: Upload three documents
st.header("Upload Documents")
pdf_directory = st.file_uploader("Upload the contract PDF file", type=["pdf"])
excel_file = st.file_uploader("Upload the Excel file for Borderaux", type=["xlsx"])
treaty_pdf_with_images = st.file_uploader("Upload the Treaty Slip PDF", type=["pdf"])
# Step 2: Select quarter and year
st.header("Select Quarter")
quarter = st.selectbox('Select the quarter:', [1, 2, 3, 4])
# Step 3: Process claims
if st.button("Process Claims"):
if pdf_directory is not None and excel_file is not None and treaty_pdf_with_images is not None:
try:
# Create a progress bar
progress_bar = st.progress(0)
status_text = st.empty()
# Update progress
def update_progress(progress, status):
progress_bar.progress(progress)
status_text.text(status)
# Generate cache keys based on file contents
update_progress(0.1, "Generating cache keys...")
cache_key = get_cache_key(pdf_directory, excel_file, treaty_pdf_with_images)
print(f"Cache key: {cache_key}")
# pdf_key = get_cache_key(pdf_directory.getvalue())
# excel_key = get_cache_key(excel_file.getvalue())
# treaty_key = get_cache_key(treaty_pdf_with_images.getvalue())
# combined_key = get_cache_key(f"{pdf_key}-{excel_key}-{treaty_key}")
# Check if results are already cached
cached_result = get_cached_result(cache_key)
if cached_result:
update_progress(0.9, "Retrieved cached results...")
results = cached_result
else:
# Save uploaded files in a permanent directory
update_progress(0.2, "Saving uploaded files...")
unique_id = str(uuid.uuid4())
pdf_path = os.path.join(UPLOAD_DIR, f"contract_{unique_id}.pdf")
excel_path = os.path.join(UPLOAD_DIR, f"borderaux_{unique_id}.xlsx")
treaty_path = os.path.join(UPLOAD_DIR, f"treaty_{unique_id}.pdf")
# Write the uploaded files to the specified paths
with open(pdf_path, "wb") as f:
f.write(pdf_directory.getvalue())
with open(excel_path, "wb") as f:
f.write(excel_file.getvalue())
with open(treaty_path, "wb") as f:
f.write(treaty_pdf_with_images.getvalue())
# Extract treaty information
update_progress(0.4, "Extracting information from the documents...")
treaty_object, borderaux_data, treaty_statement_information = extract_treaty_information_from_documents(pdf_path, excel_path, treaty_path)
# Process claims
update_progress(0.7, "Processing claims...")
results = process_claims(borderaux_data.claims_borderaux, treaty_statement_information, treaty_object, quarter)
# Cache the results
cache_result(cache_key, results)
# Clean up temporary files
os.remove(pdf_path)
os.remove(excel_path)
os.remove(treaty_path)
# Display results as a report
update_progress(0.9, "Generating report...")
st.success("Claims Processing Complete")
st.header("Claims Processing Report")
# Quarter Info
st.header("Period Information")
st.info(f"**Quarter**: {results['quarter']}")
# Financial Summary
st.header("Financial Summary")
col1, col2, col3 = st.columns(3)
with col1:
st.metric(label="Total Claims that should be paid", value=f"{results['total_claims_paid']:,.2f}")
with col2:
st.metric(label="Claim Limit", value=f"{results['claim_limit']:,.2f}")
with col3:
st.metric(label="Exceeds Limit", value="Yes" if results['exceeds_limit'] else "No",
delta="Exceeds" if results['exceeds_limit'] else "Within Limit",
delta_color="inverse")
# Claims Overview
st.header("Claims Overview")
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])
# Pie chart for claims vs limit
fig.add_trace(go.Pie(labels=['Claims that should be paid', 'Remaining Limit'],
values=[results['total_claims_paid'], max(0, results['claim_limit'] - results['total_claims_paid'])],
name="Claims vs Limit"), 1, 1)
# Gauge chart for limit usage
limit_usage = min(results['total_claims_paid'] / results['claim_limit'] * 100, 100)
fig.add_trace(go.Indicator(
mode = "gauge+number",
value = limit_usage,
title = {'text': "Limit Usage"},
gauge = {'axis': {'range': [None, 100]},
'steps': [
{'range': [0, 60], 'color': "lightgreen"},
{'range': [60, 80], 'color': "yellow"},
{'range': [80, 100], 'color': "red"}],
'threshold': {'line': {'color': "red", 'width': 4}, 'thickness': 0.75, 'value': 100}}), 1, 2)
fig.update_layout(height=400)
st.plotly_chart(fig, use_container_width=True)
# Fraud Checks
st.header("Fraud Detection Results")
fraud_checks = results['fraud_checks']
if any(fraud_checks.values()):
for check, data in fraud_checks.items():
if data:
st.subheader(check.replace('_', ' ').title())
if check == 'multiple_claims_same_day':
for date, claims in data:
st.warning(f"**Date**: {date}")
st.table(pd.DataFrame(claims))
elif check == 'frequent_claimants':
st.table(pd.DataFrame(data, columns=['Member ID', 'Claim Count']))
else:
st.table(pd.DataFrame(data))
else:
st.success("No fraudulent activities detected.")
# Additional Statistics
st.header("Additional Statistics")
col1, col2 = st.columns(2)
with col1:
st.metric(label="Claim Frequency (per day)", value=f"{results['claim_frequency']:.2f}")
with col2:
st.metric(label="Average Claim Amount", value=f"{results['average_claim_amount']:,.2f}")
# Summary of the Report
st.header("Report Summary")
st.markdown(f"""
- **Total Claims Paid**: {results['total_claims_paid']:,.2f}
- **Claim Limit**: {results['claim_limit']:,.2f}
- **Exceeds Limit**: {'Yes' if results['exceeds_limit'] else 'No'}
- **Fraudulent Activities Detected**: {sum(len(v) for v in fraud_checks.values())}
- **Claim Frequency**: {results['claim_frequency']:.2f} claims per day
- **Average Claim Amount**: {results['average_claim_amount']:,.2f}
""")
# Complete the progress bar
update_progress(1.0, "Processing complete!")
except Exception as e:
st.error(f"An error occurred: {e}")
st.write("Please check the server logs for more details.")
else:
st.warning("Please upload all required documents.")