# emmaarenas/tfm_fraudeseguros architecture

> Architecture diagram of the GitHub repository emmaarenas/tfm_fraudeseguros, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/emmaarenas/tfm_fraudeseguros
- Repository: https://github.com/emmaarenas/tfm_fraudeseguros

## Overview

##### Project type and purpose
- This is an end-to-end machine-learning web application built with Python and Streamlit.
- Purpose: ingest raw insurance‐claims data, preprocess it, train multiple models (logistic regression, random forest, XGBoost, LightGBM), ensemble them, and expose an interactive UI for inference.

##### File structure
- Top-level directories:
- Artifacts – serialized model files (.pkl) and metadata (feature columns, I/O schema, ensemble config)
- Data – raw and processed CSV datasets
- Docs – EDA, preprocessing, modeling reports (PDFs)
- Notebooks – Jupyter workflows for each pipeline stage
- Service – the production code: Streamlit UI (app.py), backend API (api_backend.py), config
- Other files: requirements.txt, LICENSE, .gitignore

##### README
- The README link points to the deployed Streamlit app (carclaim.streamlit.app)
- Confirms the app’s public URL and high-level UI purpose

##### Main components and their roles
a. Data Layer
- Raw data (Data/data_raw.csv)
- Processed data (Data/data_processed.csv)
b. Feature Engineering & Training
- Jupyter notebooks for EDA, preprocessing, modeling
- Outputs: feature_cols.pkl, explainer.pkl, model_*.pkl, ensemble_config.json
c. Artifact Store
- Central folder (Artifacts/) that holds all pickled artifacts and schema definitions
d. Backend Service
- api_backend.py: loads artifacts and exposes inference functions
- histo­rial.json: persisted request/response log
e. Frontend UI
- app.py: Streamlit application code using Service/.streamlit/config.toml
- Assets/: static assets (images, CSS, JS if any)
f. External Libraries & Infra
- Python, Streamlit, scikit-learn, XGBoost, LightGBM, pandas, numpy

##### Relationships & data flow
1. Data Ingestion → preprocessing (notebook) → processed data
2. Processed data → model training (notebook) → serialized artifacts
3. Artifacts → loaded by api_backend.py at startup
4. Streamlit UI (app.py) ←→ Backend (in-process function calls)
5. User inputs on UI → backend inference → ensemble predictions → UI output
6. Requests logged to historial.json

##### Architectural patterns & design principles
- Separation of concerns: distinct layers for data, training, storage, backend, UI
- Artifact versioning: models and schemas in a single folder
- Configuration as code: streamlit config, ensemble config JSON
- Reproducibility: notebooks + serialized artifacts

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Layer
    subgraph "Data Layer"
        direction TB
        RawData["Raw Data\nData/data_raw.csv"]:::data
        ProcessedData["Processed Data\nData/data_processed.csv"]:::data
    end

    %% Notebook Pipeline
    subgraph "Notebook Pipeline"
        direction TB
        EDA["EDA Notebook\nNotebooks/EDA.ipynb"]:::compute
        Preprocessing["Preprocessing Notebook\nNotebooks/Preprocesado.ipynb"]:::compute
        Modeling["Modeling Notebook\nNotebooks/Modelado.ipynb"]:::compute
    end

    %% Artifact Store
    subgraph "Artifact Store"
        direction TB
        EnsembleConfig["ensemble_config.json"]:::storage
        FeatureCols["feature_cols.pkl"]:::storage
        IOSchema["io_schema.json"]:::storage
        LRModel["model_logreg.pkl"]:::storage
        RFModel["model_rf.pkl"]:::storage
        XGBModel["model_xgb.pkl"]:::storage
        LGBMModel["model_lgbm.pkl"]:::storage
        Explainer["explainer.pkl"]:::storage
    end

    %% Backend Service
    subgraph "Backend Service"
        direction TB
        API["api_backend.py"]:::service
        HistLog["historial.json"]:::service
    end

    %% Frontend UI
    subgraph "Frontend UI"
        direction TB
        StreamlitApp["app.py"]:::service
        Config["config.toml"]:::service
        Assets["Assets/"]:::service
    end

    %% External Libraries & Environment
    subgraph "External Libraries & Env"
        direction TB
        ReqTxt["requirements.txt"]:::external
    end

    %% Connections
    RawData -->|"analysis"| EDA
    RawData -->|"preprocess"| Preprocessing
    Preprocessing -->|"produce"| ProcessedData
    ProcessedData -->|"train"| Modeling
    EDA -->|"insights"| Modeling
    Modeling -->|"export artifacts"| EnsembleConfig
    Modeling --> FeatureCols
    Modeling --> IOSchema
    Modeling --> LRModel
    Modeling --> RFModel
    Modeling --> XGBModel
    Modeling --> LGBMModel
    Modeling --> Explainer

    EnsembleConfig -->|"load"| API
    FeatureCols --> API
    IOSchema --> API
    LRModel --> API
    RFModel --> API
    XGBModel --> API
    LGBMModel --> API
    Explainer --> API

    StreamlitApp <-->|"in-process calls"| API
    API -->|"log"| HistLog
    StreamlitApp --> Config
    StreamlitApp --> Assets

    ReqTxt

    %% Click Events
    click RawData "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Data/data_raw.csv"
    click ProcessedData "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Data/data_processed.csv"
    click EDA "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Notebooks/EDA.ipynb"
    click Preprocessing "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Notebooks/Preprocesado.ipynb"
    click Modeling "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Notebooks/Modelado.ipynb"
    click EnsembleConfig "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Artifacts/ensemble_config.json"
    click FeatureCols "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Artifacts/feature_cols.pkl"
    click IOSchema "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Artifacts/io_schema.json"
    click LRModel "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Artifacts/model_logreg.pkl"
    click RFModel "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Artifacts/model_rf.pkl"
    click XGBModel "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Artifacts/model_xgb.pkl"
    click LGBMModel "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Artifacts/model_lgbm.pkl"
    click Explainer "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Artifacts/explainer.pkl"
    click API "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Service/api_backend.py"
    click HistLog "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Service/historial.json"
    click StreamlitApp "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Service/app.py"
    click Config "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/Service/.streamlit/config.toml"
    click Assets "https://github.com/emmaarenas/tfm_fraudeseguros/tree/master/Assets/"
    click ReqTxt "https://github.com/emmaarenas/tfm_fraudeseguros/blob/master/requirements.txt"

    %% Styles
    classDef data fill:#E3F2FD,stroke:#2196F3,stroke-width:2px
    classDef compute fill:#E8F5E9,stroke:#388E3C,stroke-width:2px
    classDef storage fill:#FFF9C4,stroke:#FBC02D,stroke-width:2px
    classDef service fill:#FFE0B2,stroke:#F57C00,stroke-width:2px
    classDef external fill:#E1BEE7,stroke:#8E24AA,stroke-width:2px
```
