Spaces:
Running
Running
import streamlit as st | |
import io | |
from utils import mock_api_call, parse_analysis_response, validate_pdf, displayPDF | |
from styles import apply_custom_styles, show_factor_section, show_detailed_factors, show_factor_summary | |
from policy_analyser.analyse import analyse | |
def main(): | |
# Apply custom styles | |
apply_custom_styles() | |
# Header | |
st.markdown(""" | |
<div class="header-container"> | |
<img src="https://acko-brand.ackoassets.com/brand/vector-svg/gradient/horizontal-reverse.svg" height=50 width=100> | |
<h1>Insurance Policy Analyzer</h1> | |
<p>Upload and compare insurance policies</p> | |
</div> | |
""", unsafe_allow_html=True) | |
# File upload section | |
st.markdown('<div class="upload-container">', unsafe_allow_html=True) | |
uploaded_files = st.file_uploader("Choose policy PDF files", type="pdf", accept_multiple_files=True) | |
st.markdown('</div>', unsafe_allow_html=True) | |
if uploaded_files: | |
# Create tabs for different views | |
tab1, tab2 = st.tabs(["Summary View", "Detailed Comparison"]) | |
# Store analysis results | |
all_analyses = [] | |
# Process each uploaded file | |
for uploaded_file in uploaded_files: | |
# Read PDF content | |
pdf_bytes = uploaded_file.read() | |
# displayPDF(pdf_bytes) | |
# Validate PDF | |
if not validate_pdf(pdf_bytes): | |
st.error(f"Invalid PDF file: {uploaded_file.name}") | |
continue | |
# Show loading state | |
with st.spinner(f"Analyzing {uploaded_file.name}..."): | |
try: | |
# Make API call | |
response = analyse(pdf_bytes) | |
print(response) | |
summary = next( | |
(item for item in response if item.get("stage") == "ANALYSIS_SUMMARY"), None | |
)['response'] | |
# Parse response | |
good_factors, average_factors, bad_factors = parse_analysis_response(response) | |
# Store results | |
all_analyses.append({ | |
'name': uploaded_file.name, | |
'good_factors': good_factors, | |
'average_factors': average_factors, | |
'bad_factors': bad_factors | |
}) | |
except Exception as e: | |
st.error(f"Error analyzing {uploaded_file.name}: {str(e)}") | |
# Summary View Tab | |
with tab1: | |
for idx, analysis in enumerate(all_analyses): | |
with st.expander(f"### Policy {idx + 1}: {analysis['name']}"): | |
with st.container(): | |
cols = st.columns(2) | |
displayed = 0 | |
for verdict in ['Good', 'Average', 'Bad']: | |
lst = [f.split(':')[0] for f in analysis[f'{verdict.lower()}_factors']] | |
if len(lst) > 0: | |
title = f'{verdict} Factors' | |
if verdict == 'Good': | |
title += 'β ' | |
sentiment = 'Yay!' | |
elif verdict == 'Average': | |
title += 'β οΈ' | |
sentiment = 'Hmmmm' | |
else: | |
title += 'β' | |
sentiment = 'Meh' | |
cols = st.columns(2) | |
with st.container(): | |
with cols[0]: | |
if displayed % 3 == 0: | |
show_factor_section(title, lst, verdict.lower()) | |
else: | |
show_factor_summary(summary[verdict], verdict.lower(), sentiment) | |
with cols[1]: | |
if displayed % 3 == 0: | |
show_factor_summary(summary[verdict], verdict.lower(), sentiment) | |
else: | |
show_factor_section(title, lst, verdict.lower()) | |
displayed += 1 | |
st.markdown('-----') | |
# with st.container(): | |
# suggestion = next((item for item in response if item.get("stage") == "SUGGEST"), None)['response'] | |
# show_factor_summary(suggestion, 'Good', 'What we have βοΈ') | |
# Detailed Comparison Tab | |
with tab2: | |
if len(all_analyses) > 1: | |
# Create comparison matrix | |
factors_to_compare = set() | |
for analysis in all_analyses: | |
factors_to_compare.update( | |
[f.split(':')[0] for f in analysis['good_factors'] + | |
analysis['average_factors'] + analysis['bad_factors']] | |
) | |
# Create comparison table | |
st.markdown("### Policy Comparison Matrix") | |
comparison_data = [] | |
for factor in sorted(factors_to_compare): | |
row = {'Factor': factor} | |
for idx, analysis in enumerate(all_analyses): | |
policy_name = f"Policy {idx + 1}" | |
verdict = 'Not Found' | |
for category in ['good_factors', 'average_factors', 'bad_factors']: | |
for item in analysis[category]: | |
if item.split(':')[0] == factor: | |
verdict = category.split('_')[0].title() | |
break | |
row[policy_name] = verdict | |
comparison_data.append(row) | |
# Display comparison table | |
st.table(comparison_data) | |
else: | |
st.info("Upload multiple policies to see comparison") | |
# Footer | |
st.markdown(""" | |
<div style="margin-top: 50px; text-align: center; color: #666;"> | |
<p>Upload one or more insurance policy PDFs to get detailed analysis and comparison.</p> | |
<p>We support all major insurance providers.</p> | |
</div> | |
""", unsafe_allow_html=True) | |
if __name__ == "__main__": | |
st.set_page_config( | |
page_title="Insurance Policy Analyzer", | |
page_icon="π", | |
layout="wide" | |
) | |
main() |