import streamlit as st import requests import uuid # Page configuration st.set_page_config( page_title="HealthGuard AI: Insurance Cost Predictor", page_icon="đŸĨ", layout="wide" ) # Initialize session storage if "chat_history" not in st.session_state: st.session_state.chat_history = [] if "thread_id" not in st.session_state: st.session_state.thread_id = str(uuid.uuid4()) if "analysis_done" not in st.session_state: st.session_state.analysis_done = False if "show_info" not in st.session_state: st.session_state.show_info = False # ========================= ENHANCED UI STYLING ========================= st.markdown(""" """, unsafe_allow_html=True) # ========================= HEADER ========================= st.markdown("""

đŸĨ HealthGuard AI

AI-Powered Health Insurance Cost Prediction Platform

""", unsafe_allow_html=True) # Alert Banner st.markdown("""
âš ī¸ Note: First request may take up to 20 seconds (API cold start).
""", unsafe_allow_html=True) # Info Toggle if st.button("â„šī¸ How It Works"): st.session_state.show_info = not st.session_state.show_info if st.session_state.show_info: st.markdown("""

📊 About HealthGuard AI

What we analyze:

Our AI provides:

""", unsafe_allow_html=True) # Categorical options categorical_options = { 'Gender': ['Male', 'Female'], 'Marital Status': ['Unmarried', 'Married'], 'BMI Category': ['Normal', 'Obesity', 'Overweight', 'Underweight'], 'Smoking Status': ['No Smoking', 'Regular', 'Occasional'], 'Employment Status': ['Salaried', 'Self-Employed', 'Freelancer'], 'Region': ['Northwest', 'Southeast', 'Northeast', 'Southwest'], 'Medical History': [ 'No Disease', 'Diabetes', 'High blood pressure', 'Diabetes & High blood pressure', 'Thyroid', 'Heart disease', 'High blood pressure & Heart disease', 'Diabetes & Thyroid', 'Diabetes & Heart disease' ], 'Insurance Plan': ['Bronze', 'Silver', 'Gold'] } # ========================= INPUT FORM ========================= col_left, col_right = st.columns([1, 1], gap="large") with col_left: st.markdown('
', unsafe_allow_html=True) st.markdown('
👤 Personal Information
', unsafe_allow_html=True) age = st.number_input('Age', min_value=18, max_value=100, value=30, step=1) gender = st.selectbox('Gender', categorical_options['Gender']) marital_status = st.selectbox('Marital Status', categorical_options['Marital Status']) number_of_dependants = st.number_input('Number of Dependants', min_value=0, max_value=7, value=2, step=1) region = st.selectbox('Region', categorical_options['Region']) st.markdown('
', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('
đŸ’ŧ Financial Details
', unsafe_allow_html=True) income_lakhs = st.number_input('Annual Income (Lakhs)', min_value=1, max_value=200, value=10, step=1) employment_status = st.selectbox('Employment Status', categorical_options['Employment Status']) insurance_plan = st.selectbox('Insurance Plan', categorical_options['Insurance Plan']) st.markdown('
', unsafe_allow_html=True) with col_right: st.markdown('
', unsafe_allow_html=True) st.markdown('
đŸĨ Health Information
', unsafe_allow_html=True) bmi_category = st.selectbox('BMI Category', categorical_options['BMI Category']) smoking_status = st.selectbox('Smoking Status', categorical_options['Smoking Status']) medical_history = st.selectbox('Medical History', categorical_options['Medical History']) genetical_risk = st.number_input('Genetical Risk (1-5)', min_value=1, max_value=5, value=3, step=1) st.markdown('
', unsafe_allow_html=True) # Risk indicator risk_color = "#e74c3c" if genetical_risk >= 4 else "#f39c12" if genetical_risk == 3 else "#27ae60" st.markdown(f"""
Genetic Risk Level
{genetical_risk}/5
""", unsafe_allow_html=True) # Prepare input dictionary input_dict = { 'age': age, 'number_of_dependants': number_of_dependants, 'income_lakhs': income_lakhs, 'genetical_risk': genetical_risk, 'insurance_plan': insurance_plan, 'employment_status': employment_status, 'gender': gender.lower(), 'marital_status': marital_status.lower(), 'bmi_category': bmi_category, 'smoking_status': smoking_status, 'region': region, 'medical_history': medical_history } # ========================= PREDICTION BUTTON ========================= st.markdown("
", unsafe_allow_html=True) if st.button("💰 Calculate Insurance Premium", use_container_width=True): API_URL = st.secrets["API_URL"] with st.spinner('🤖 Calculating your premium...'): try: response = requests.post(API_URL, json=input_dict, timeout=30) if response.status_code == 200: result = response.json() yearly = result['yearly'] monthly = result['monthly'] advice = result['advice'] # Display Results st.markdown('
', unsafe_allow_html=True) st.markdown('

📊 Premium Calculation Results

', unsafe_allow_html=True) st.markdown(f"""
Annual Premium
₹ {yearly:,.2f}
Monthly Premium
₹ {monthly:,.2f}
Insurance Plan
{insurance_plan}
""", unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) # Display advice st.markdown(f"""

💡 AI Health Advisor Insights

{advice}

""", unsafe_allow_html=True) # Store results in session state st.session_state.yearly_cost = yearly st.session_state.monthly_cost = monthly st.session_state.ai_summary = advice st.session_state.analysis_done = True st.success("✅ Calculation complete! You can now chat with our AI assistant below.") else: st.error(f"❌ API Error: {response.status_code}") except requests.exceptions.Timeout: st.error("âąī¸ Request timed out. Please try again.") except Exception as e: st.error(f"❌ Connection error: {str(e)}") # ========================= CHATBOT ========================= if st.session_state.analysis_done: st.markdown("

", unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('
đŸ’Ŧ Interactive Health Insurance Assistant
', unsafe_allow_html=True) if st.session_state.chat_history: st.markdown('
', unsafe_allow_html=True) for role, msg in st.session_state.chat_history: bubble = "chat-bubble-user" if role == "user" else "chat-bubble-bot" prefix = "You: " if role == "user" else "🤖 Assistant: " st.markdown(f"
{prefix}{msg}
", unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) user_query = st.text_input("Ask a question about your insurance:", placeholder="e.g., How can I reduce my premium costs?") col_send, col_clear = st.columns([3, 1]) with col_send: send_button = st.button("📤 Send Message", use_container_width=True) with col_clear: if st.button("đŸ—‘ī¸ Clear Chat", use_container_width=True): st.session_state.chat_history = [] st.session_state.thread_id = str(uuid.uuid4()) st.experimental_rerun() if send_button and user_query.strip(): CHAT_URL = st.secrets["CHAT_URL"] payload = { "thread_id": st.session_state.thread_id, "message": user_query, "yearly_cost": st.session_state.yearly_cost, "monthly_cost": st.session_state.monthly_cost, "ai_summary": st.session_state.ai_summary } with st.spinner("🤖 Thinking..."): try: r = requests.post(CHAT_URL, json=payload, timeout=30) if r.status_code == 200: reply = r.json()["response"] st.session_state.chat_history.append(("user", user_query)) st.session_state.chat_history.append(("bot", reply)) st.experimental_rerun() else: st.error(f"❌ Chat server error: {r.status_code}") except Exception as e: st.error(f"❌ Chat failed: {e}") st.markdown('
', unsafe_allow_html=True) # Footer st.markdown("

", unsafe_allow_html=True) st.markdown("""

đŸĨ HealthGuard AI Š 2025 | Powered by Advanced Machine Learning

For demonstration purposes only. Not medical or financial advice.

""", unsafe_allow_html=True)