import streamlit as st import pandas as pd import plotly.express as px import plotly.graph_objects as go import numpy as np from datetime import datetime, timedelta # 1. PAGE CONFIGURATION st.set_page_config( page_title="S.T.A.R.K AI | UIDAI Fraud Detection", page_icon="π‘οΈ", layout="wide", initial_sidebar_state="expanded" ) # 2. ENHANCED PROFESSIONAL STYLING (Optimized) st.markdown(""" """, unsafe_allow_html=True) # 3. ENHANCED DATA LOADING @st.cache_data(ttl=300) def load_data(): # Strictly load data from CSV df = pd.read_csv('analyzed_aadhaar_data.csv') # Removed st.toast from inside cached function to prevent CacheReplayClosureError if 'date' in df.columns: df['date'] = pd.to_datetime(df['date']) # Precise Geometric Centers state_centers = { 'Andaman and Nicobar Islands': (11.7401, 92.6586), 'Andhra Pradesh': (15.9129, 79.7400), 'Arunachal Pradesh': (28.2180, 94.7278), 'Assam': (26.2006, 92.9376), 'Bihar': (25.0961, 85.3131), 'Chandigarh': (30.7333, 76.7794), 'Chhattisgarh': (21.2787, 81.8661), 'Delhi': (28.7041, 77.1025), 'Goa': (15.2993, 74.1240), 'Gujarat': (22.2587, 71.1924), 'Haryana': (29.0588, 76.0856), 'Himachal Pradesh': (31.9579, 77.1095), 'Jammu and Kashmir': (33.7782, 76.5762), 'Jharkhand': (23.6102, 85.2799), 'Karnataka': (15.3173, 75.7139), 'Kerala': (10.8505, 76.2711), 'Ladakh': (34.1526, 77.5770), 'Madhya Pradesh': (22.9734, 78.6569), 'Maharashtra': (19.7515, 75.7139), 'Manipur': (24.6637, 93.9063), 'Meghalaya': (25.4670, 91.3662), 'Mizoram': (23.1645, 92.9376), 'Nagaland': (26.1584, 94.5624), 'Odisha': (20.9517, 85.0985), 'Puducherry': (11.9416, 79.8083), 'Punjab': (31.1471, 75.3412), 'Rajasthan': (27.0238, 74.2179), 'Sikkim': (27.5330, 88.5122), 'Tamil Nadu': (11.1271, 78.6569), 'Telangana': (18.1124, 79.0193), 'Tripura': (23.9408, 91.9882), 'Uttar Pradesh': (26.8467, 80.9462), 'Uttarakhand': (30.0668, 79.0193), 'West Bengal': (22.9868, 87.8550) } # EXPANDED Aspect Ratio Definitions (Lat spread, Lon spread) state_spreads = { 'Kerala': (1.2, 0.25), 'West Bengal': (1.4, 0.4), 'Assam': (0.4, 1.8), 'Maharashtra': (1.8, 2.2), 'Uttar Pradesh': (1.2, 2.5), 'Bihar': (0.8, 1.5), 'Delhi': (0.1, 0.12), 'Goa': (0.15, 0.15), 'Chandigarh': (0.04, 0.04), 'Gujarat': (1.5, 1.8), 'Rajasthan': (2.0, 2.0), 'Madhya Pradesh': (1.8, 2.5), 'Himachal Pradesh': (0.6, 0.8), 'Punjab': (0.8, 0.9), 'Haryana': (0.9, 0.8), 'Tamil Nadu': (1.2, 1.0), 'Karnataka': (1.5, 1.2), 'Telangana': (1.0, 1.0), 'Andhra Pradesh': (1.5, 1.5), 'Odisha': (1.2, 1.2), 'Chhattisgarh': (1.5, 0.9), 'Jharkhand': (0.8, 1.0), 'Jammu and Kashmir': (1.0, 1.5), 'Ladakh': (1.0, 1.5), 'Uttarakhand': (0.7, 0.8) } def get_coords(row): state = row.get('state', 'Delhi') district = str(row.get('district', 'Unknown')).lower() base_lat, base_lon = state_centers.get(state, (20.5937, 78.9629)) # Safer Default if state not found lat_scale, lon_scale = state_spreads.get(state, (0.7, 0.7)) lat_bias, lon_bias = 0, 0 bias = 0.6 if 'north' in district: lat_bias += lat_scale * bias if 'south' in district: lat_bias -= lat_scale * bias if 'east' in district: lon_bias += lon_scale * bias if 'west' in district: lon_bias -= lon_scale * bias np.random.seed(hash(state + district) % 2**32) rf = 0.5 if (lat_bias or lon_bias) else 1.0 return pd.Series({ 'lat': base_lat + lat_bias + np.random.uniform(-lat_scale*rf, lat_scale*rf) + np.random.normal(0, 0.04), 'lon': base_lon + lon_bias + np.random.uniform(-lon_scale*rf, lon_scale*rf) + np.random.normal(0, 0.04) }) coords = df.apply(get_coords, axis=1) df['lat'], df['lon'] = coords['lat'], coords['lon'] df['risk_category'] = pd.cut(df['RISK_SCORE'], bins=[-1, 50, 75, 85, 100], labels=['Low', 'Medium', 'High', 'Critical']) return df with st.spinner('Loading S.T.A.R.K AI System...'): df = load_data() # Toast moved outside cached function # st.toast("β Data loaded successfully", icon="β ") # 4. SIDEBAR & FILTERS with st.sidebar: st.markdown("### π‘οΈ S.T.A.R.K AI Control") st.markdown("---") if 'date' in df.columns: min_d, max_d = df['date'].min().date(), df['date'].max().date() dr = st.date_input("Date Range", value=(min_d, max_d), min_value=min_d, max_value=max_d) if len(dr) == 2: df = df[(df['date'].dt.date >= dr[0]) & (df['date'].dt.date <= dr[1])] state_list = ['All'] + sorted(df['state'].unique().tolist()) sel_state = st.selectbox("State", state_list) filtered_df = df[df['state'] == sel_state] if sel_state != 'All' else df.copy() dist_list = ['All'] + sorted(filtered_df['district'].unique().tolist()) sel_dist = st.selectbox("District", dist_list) if sel_dist != 'All': filtered_df = filtered_df[filtered_df['district'] == sel_dist] st.markdown("---") risk_filter = st.multiselect("Risk Level", ['Low', 'Medium', 'High', 'Critical'], default=['High', 'Critical']) if risk_filter: filtered_df = filtered_df[filtered_df['risk_category'].isin(risk_filter)] st.markdown("---") st.link_button("π Open Analysis Notebook", "https://colab.research.google.com/drive/1YAQ4nfxltvG_cts3fmGc_zi2JQc4oPOT?usp=sharing", use_container_width=True) st.info(f"**User:** UIDAI_Officer\n\n**Team:** UIDAI_4571\n\n**Update:** {datetime.now().strftime('%H:%M:%S')}") # 5. HEADER & METRICS col1, col2 = st.columns([3, 1]) with col1: st.title("π‘οΈ S.T.A.R.K AI Dashboard") st.markdown("**Context-Aware Fraud Detection & Prevention System**") with col2: st.markdown(f"""