Spaces:
Running
Running
import base64 | |
import json | |
import requests | |
from typing import Dict, List, Tuple | |
import streamlit as st | |
def mock_api_call(pdf_bytes: bytes) -> List: | |
""" | |
Simulates an API call for policy analysis. | |
In production, this would make a real API call. | |
""" | |
# Mock response based on the provided sample | |
return [{ | |
"stage": "ANALYSE", | |
"response": [ | |
{ | |
"factor": "Room rent limit", | |
"verdict": "Bad", | |
"reason": "There is cap of 100 on room rent" | |
}, | |
{ | |
"factor": "Deductible", | |
"verdict": "Good", | |
"reason": "No deductible" | |
}, | |
{ | |
"factor": "Copay", | |
"verdict": "Good", | |
"reason": "Copayment (0.0) < 5%" | |
} | |
] | |
}, | |
{ | |
"stage" : "ANALYSIS_SUMMARY", | |
"response" : { | |
"Good" : "This was great!", | |
# "Average" : "This was okay", | |
"Bad" : "This was meh :/" | |
} | |
} | |
] | |
def parse_analysis_response(response: List) -> Tuple[List[str], List[str], List[str]]: | |
""" | |
Parses the API response and extracts categorized factors with their reasons. | |
Handles the verdict-based factor format. | |
""" | |
try: | |
# Find the analysis stage in the response list | |
analysis_item = next( | |
(item for item in response if item.get("stage") == "ANALYSE"), | |
None | |
) | |
if not analysis_item: | |
st.error("No analysis data found in the response") | |
return [], [], [] | |
# Get the response list from analysis item | |
analysis_list = analysis_item.get("response", []) | |
if not analysis_list or not isinstance(analysis_list, list): | |
st.error("Invalid analysis response format") | |
return [], [], [] | |
# Initialize categorized factors | |
good_factors = [] | |
average_factors = [] | |
bad_factors = [] | |
# Categorize factors based on verdict | |
for item in analysis_list: | |
factor_text = f"{item.get('factor')}: {item.get('reason')}" | |
verdict = item.get('verdict', '').lower() | |
if verdict == 'good': | |
good_factors.append(factor_text) | |
elif verdict == 'average': | |
average_factors.append(factor_text) | |
elif verdict == 'bad': | |
bad_factors.append(factor_text) | |
return good_factors, average_factors, bad_factors | |
except Exception as e: | |
st.error(f"Error parsing analysis response: {str(e)}") | |
return [], [], [] | |
def validate_pdf(pdf_bytes: bytes) -> bool: | |
""" | |
Validates the uploaded PDF file. | |
""" | |
if not pdf_bytes: | |
return False | |
# Check file signature for PDF (%PDF-) | |
return pdf_bytes.startswith(b'%PDF-') | |
def displayPDF(file): | |
# Opening file from file path | |
if isinstance(file, str): | |
file_bytes = open(file, 'rb').read() | |
else: | |
file_bytes = file | |
# with open(file, "rb") as f: | |
base64_pdf = base64.b64encode(file_bytes).decode('utf-8') | |
# Embedding PDF in HTML | |
pdf_display = F'<embed src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf">' | |
# Displaying File | |
st.markdown(pdf_display, unsafe_allow_html=True) |