Spaces:
Sleeping
Sleeping
Download app.py from vb86/fraudflow-india: direct link, hf CLI and curl.
- Browser
- Download file 4.27 kB
-
https://huggingface.co/spaces/vb86/fraudflow-india/resolve/main/app.py
- Command line
-
hf download hf://spaces/vb86/fraudflow-india/app.py
-
curl -L -o app.py https://huggingface.co/spaces/vb86/fraudflow-india/resolve/main/app.py
4.27 kB
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import plotly.express as px | |
| import plotly.graph_objects as go | |
| import streamlit.components.v1 as components | |
| import xgboost as xgb | |
| from sklearn.model_selection import train_test_split | |
| st.set_page_config(layout="wide", page_title="FraudFlow India") | |
| st.title("FraudFlow India: UPI Fraud Evolution Map") | |
| st.markdown("**Real RBI-style data (2018-2026) + XGBoost risk scoring + Gemini 2.5 Flash predictions.**") | |
| # ββ Load Data ββ | |
| def load_district(): | |
| df = pd.read_csv('fraudflow_districts.csv') | |
| df['year'] = df['year'].astype(int) | |
| return df | |
| def load_base(): | |
| df = pd.read_csv('fraudflow_base.csv') | |
| df['year'] = df['year'].astype(int) | |
| return df | |
| def load_predictions(): | |
| try: | |
| df = pd.read_csv('fraudflow_2026_predictions.csv') | |
| return df | |
| except FileNotFoundError: | |
| return None | |
| df = load_district() | |
| df_base = load_base() | |
| df_pred = load_predictions() | |
| # ββ Animated Bar Chart ββ | |
| st.subheader("Fraud Cases by District & Year (Animated 2018-2025)") | |
| try: | |
| with open("fraudflow_bar.html", "r", encoding="utf-8") as f: | |
| bar_html = f.read() | |
| components.html(bar_html, height=550, scrolling=False) | |
| except FileNotFoundError: | |
| st.error("fraudflow_bar.html not found.") | |
| # ββ National Trend ββ | |
| st.subheader("National Fraud Trend (RBI Data 2018-2025)") | |
| try: | |
| with open("fraudflow_trend.html", "r", encoding="utf-8") as f: | |
| trend_html = f.read() | |
| components.html(trend_html, height=500, scrolling=False) | |
| except FileNotFoundError: | |
| st.error("fraudflow_trend.html not found.") | |
| # ββ XGBoost 2026 Predictions ββ | |
| st.subheader("XGBoost 2026 Fraud Risk Predictions") | |
| st.markdown("*Trained on 2018-2025 district fraud trajectory. Predicts 2026 fraud index per district.*") | |
| # Load pre-built HTML chart | |
| try: | |
| with open("fraudflow_xgb.html", "r", encoding="utf-8") as f: | |
| xgb_html = f.read() | |
| components.html(xgb_html, height=560, scrolling=False) | |
| except FileNotFoundError: | |
| st.error("fraudflow_xgb.html not found. Please upload it.") | |
| if df_pred is not None: | |
| # Predicted vs actual table | |
| st.markdown("#### 2025 Actual vs 2026 XGBoost Predicted Fraud Index") | |
| display_df = df_pred[['district', 'cases_local', 'predicted_cases_2026', 'xgb_risk_pct']].copy() | |
| display_df.columns = ['District', '2025 Fraud Index', '2026 Predicted Index', 'Risk Score (%)'] | |
| display_df = display_df.sort_values('Risk Score (%)', ascending=False).reset_index(drop=True) | |
| display_df.index += 1 | |
| st.dataframe(display_df, use_container_width=True) | |
| # Top 3 metric cards | |
| st.markdown("#### Top 3 High Risk Districts for 2026") | |
| top3 = display_df.head(3) | |
| col1, col2, col3 = st.columns(3) | |
| with col1: | |
| st.metric( | |
| label=top3.iloc[0]['District'], | |
| value=f"{top3.iloc[0]['Risk Score (%)']:.1f}%", | |
| delta=f"Predicted index: {top3.iloc[0]['2026 Predicted Index']:.1f}" | |
| ) | |
| with col2: | |
| st.metric( | |
| label=top3.iloc[1]['District'], | |
| value=f"{top3.iloc[1]['Risk Score (%)']:.1f}%", | |
| delta=f"Predicted index: {top3.iloc[1]['2026 Predicted Index']:.1f}" | |
| ) | |
| with col3: | |
| st.metric( | |
| label=top3.iloc[2]['District'], | |
| value=f"{top3.iloc[2]['Risk Score (%)']:.1f}%", | |
| delta=f"Predicted index: {top3.iloc[2]['2026 Predicted Index']:.1f}" | |
| ) | |
| # ββ Top 5 Hotspots 2025 ββ | |
| st.subheader("Top 5 Fraud Hotspot Districts (2025 Actual)") | |
| top5 = ( | |
| df[df['year'] == 2025] | |
| .sort_values('cases_local', ascending=False) | |
| .head(5)[['district', 'cases_local']] | |
| .reset_index(drop=True) | |
| ) | |
| top5.columns = ['District', 'Fraud Index (per 10K transactions)'] | |
| top5.index += 1 | |
| st.table(top5) | |
| # ββ Gemini Prediction ββ | |
| st.subheader("Gemini 2.5 Flash Predictions for 2026") | |
| try: | |
| with open('gemini_prediction.txt', 'r', encoding='utf-8') as f: | |
| prediction = f.read() | |
| st.info(prediction) | |
| except FileNotFoundError: | |
| st.warning("gemini_prediction.txt not found.") | |
| st.markdown("---") | |
| st.caption("Built by Vipul | Data: RBI DBIE/NCRB | ML: XGBoost | AI: Gemini 2.5 Flash | #FintechAI #UPI #RBI") |