Spaces:
Running
Running
import os | |
import streamlit as st | |
from utils import validate_pdf | |
from styles import apply_custom_styles | |
from policy_analyser.analyse import analyse | |
if 'GPT_KEY' not in os.environ or os.environ.get('GPT_KEY') in [None, '']: | |
os.environ['GPT_KEY'] = st.secrets['GPT_KEY'] | |
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) | |
print(uploaded_files) | |
st.markdown('</div>', unsafe_allow_html=True) | |
if uploaded_files and st.button('Analyse'): | |
# 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, True) | |
analysis = next( | |
(item for item in response if item.get("stage") == "ANALYSE"), None | |
)['response'] | |
analysis = analysis.split('[CUSTOMER_RESPONSE]')[-1].split('[/CUSTOMER_RESPONSE]')[0] | |
suggestion = next( | |
(item for item in response if item.get("stage") == "SUGGEST"), None | |
)['response'] | |
suggestion = suggestion.split('[POLICY_PITCH]')[-1].split('[/POLICY_PITCH]')[0] | |
# Store results | |
all_analyses.append({ | |
'name': uploaded_file.name, | |
'analysis' : analysis, | |
'suggestion' : suggestion | |
}) | |
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(): | |
st.markdown(analysis['analysis']) | |
with st.container(): | |
st.markdown('# Why Acko? π') | |
st.markdown(analysis['suggestion']) | |
# 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() |