# fugubiao/medical-insurance-forecast architecture

> Architecture diagram of the GitHub repository fugubiao/medical-insurance-forecast, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fugubiao/medical-insurance-forecast
- Repository: https://github.com/fugubiao/medical-insurance-forecast

## Overview

##### Project type and purpose
- This is a full‑stack, monolithic Flask web application that hosts a pre‐trained medical‑insurance‑fraud detection model.
- Purpose: allow users to upload or view insurance claim data, run a machine‑learning inference pipeline (outlier treatment, feature selection, PCA, SMOTE, random forest or LightGBM), and display fraud‐risk results with optional tree visualization.

##### File structure
- Top‑level files:
– app.py: Flask application entry‐point with HTTP routes for inference and visualization
– rantree.py: code to render or traverse random‐forest decision trees
– show_1.py, test.py: auxiliary scripts for demonstration or testing
– requirements.txt: Python dependencies (Flask, scikit‑learn, pandas, numpy, imbalanced‑learn, etc.)
– [csv file]: raw dataset for offline training/analysis
- models/: stores model artifacts (.pkl, .txt) produced by offline training pipeline
- static/: CSS, JS and Bootstrap assets for the browser UI
- templates/: Jinja2 HTML templates for views

##### Key insights from README
- Offline pipeline steps:
1. 3σ outlier removal
2. Linear regression‑based feature selection
3. PCA dimensionality reduction
4. SMOTE oversampling
5. Cross‑validation
- Models are trained, serialized, and then served by the Flask app.

a. User & Browser UI
b. Frontend assets (static CSS/JS, Bootstrap) + Jinja2 templates
c. Flask Backend
- HTTP Routes / Controllers (in app.py)
- Inference Engine (loads .pkl from models/, calls scikit‑learn predict)
- Tree‑visualization service (rantree.py)
d. Data
- Raw dataset (CSV) – used offline
- Model Artifact Store (models/ folder)
e. Offline ML Pipeline (optional overlay)
- Preprocessing (pandas + custom scripts)
- Training (scikit‑learn, imblearn SMOTE)
- Model serialization

##### Relationships and interactions
- Browser → HTTP(S) request → Flask controller
- Controller loads model artifact → preprocesses input → inference → render result via template or JSON
- For tree view: controller calls rantree module → renders SVG/HTML snippet → returns to browser
- Offline: CSV data → preprocessing scripts → model trainer → output to models/

##### Architectural patterns & design principles
- MVC‑like separation:
- Models = serialized ML objects
- Views = Jinja2 templates + static assets
- Controllers = Flask app.py routes
- Single‑process, file‐based persistence (no external DB)
- ML pipeline decoupling: offline training vs. online inference

##### Technologies, frameworks, libraries
- Python 3.x, Flask, Jinja2
- scikit‑learn, pandas, numpy, imbalanced‑learn (SMOTE), LightGBM (optional)
- Bootstrap 3 & 5 for responsive UI
- Pickle for model serialization

- HTTP request/response
- Prediction requests
- Offline pipeline → model store
- Blue = frontend/UI
- Green = backend app services
- Orange = ML/data pipeline
- Gray = external/filesystem

##### Don’t overthink – capture every logical component
- User interface (templates, JS)
- Web server & controllers (app.py)
- ML inference modules (model loader, predictor, rantree)
- Data artifacts (CSV, .pkl)
- Offline training pipeline

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend Layer"
        Browser["User & Browser UI"]:::frontend
        BS3["Bootstrap 3"]:::frontend
        BS5["Bootstrap 5"]:::frontend
        StyleCSS["style.css"]:::frontend
        ScriptJS["script.js"]:::frontend
        NavJS["nav.js"]:::frontend
        Template["show.html"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend (Flask Service)"
        Flask["Flask App (app.py)"]:::backend
        Infer["Inference Engine"]:::backend
        TreeVis["Tree-Visualization Service (rantree.py)"]:::backend
    end

    %% Model Store
    subgraph "Model Artifact Store"
        RF["random_forest_model.pkl"]:::filesystem
        LGBM["lightgbm_model.txt"]:::filesystem
    end

    %% Raw Dataset
    subgraph "Raw Dataset"
        CSV["东软集团A08】医保特征数据16000（修订版）.csv"]:::filesystem
    end

    %% Offline ML Pipeline
    subgraph "Offline ML Pipeline"
        Scripts["Demo Scripts (show_1.py, test.py)"]:::ml
        Req["requirements.txt"]:::ml
    end

    %% Connections
    Browser -->|"HTTP request"| Flask
    Flask -->|"renders"| Template
    Flask -->|"serves static assets"| StyleCSS
    Flask --> ScriptJS
    Flask --> NavJS
    Flask --> BS3
    Flask --> BS5

    Flask --> Infer
    Infer --> RF
    Infer --> LGBM

    Flask --> TreeVis
    TreeVis --> RF

    CSV --> Scripts
    Scripts --> RF
    Scripts --> LGBM
    Scripts --> Req

    %% Click Events
    click StyleCSS "https://github.com/fugubiao/medical-insurance-forecast/blob/main/static/css/style.css"
    click ScriptJS "https://github.com/fugubiao/medical-insurance-forecast/blob/main/static/js/script.js"
    click NavJS "https://github.com/fugubiao/medical-insurance-forecast/blob/main/static/nav.js"
    click BS3 "https://github.com/fugubiao/medical-insurance-forecast/tree/main/static/bootstrap-3.4.1-dist/"
    click BS5 "https://github.com/fugubiao/medical-insurance-forecast/tree/main/static/bootstrap-5.3.0-alpha1-dist/"
    click Template "https://github.com/fugubiao/medical-insurance-forecast/blob/main/templates/show.html"
    click Flask "https://github.com/fugubiao/medical-insurance-forecast/blob/main/app.py"
    click TreeVis "https://github.com/fugubiao/medical-insurance-forecast/blob/main/rantree.py"
    click RF "https://github.com/fugubiao/medical-insurance-forecast/blob/main/models/random_forest_model.pkl"
    click LGBM "https://github.com/fugubiao/medical-insurance-forecast/blob/main/models/备份/性能好的/lightgbm_model.txt"
    click CSV "https://github.com/fugubiao/medical-insurance-forecast/blob/main/【东软集团A08】医保特征数据16000（修订版）.csv"
    click Scripts "https://github.com/fugubiao/medical-insurance-forecast/blob/main/show_1.py"
    click Scripts "https://github.com/fugubiao/medical-insurance-forecast/blob/main/test.py"
    click Req "https://github.com/fugubiao/medical-insurance-forecast/blob/main/requirements.txt"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#333,stroke-width:1px
    classDef backend fill:#D0FFD0,stroke:#333,stroke-width:1px
    classDef ml fill:#FFE8A0,stroke:#333,stroke-width:1px
    classDef filesystem fill:#E0E0E0,stroke:#333,stroke-width:1px
```
