# francesshenaiclass/tpe-real-estate-ai architecture

> Architecture diagram of the GitHub repository francesshenaiclass/tpe-real-estate-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/francesshenaiclass/tpe-real-estate-ai
- Repository: https://github.com/francesshenaiclass/tpe-real-estate-ai

## Overview

1. **Repository purpose**
   - This is a file-based, end-to-end Taipei real-estate analytics repo: it collects raw housing and geography data, cleans and merges it, trains price models, and serves prediction/recommendation/trend APIs plus a few HTML dashboards.

2. **Main architectural boundary**
   - The clearest “production-like” path is under `project/` and `train/`.
   - Everything else (`kevin/`, `frances/`, `永慶房屋/`, `實價登錄最終版/`, `final/`) is part of the broader data-prep ecosystem: crawlers, cleaning scripts, intermediate CSV/JSON outputs, and earlier pipeline variants.

3. **Data ingestion layer**
   - Raw market data comes from multiple district-based crawler outputs and transaction datasets, stored mostly as CSVs/JSONs.
   - The repo contains many per-district files for Taipei’s 12 districts (`中山區`, `中正區`, `信義區`, etc.), indicating the pipeline is partitioned geographically at ingestion time.
   - Sources include:
     - current listings / asking prices
     - real transaction / “實價登錄” records
     - MRT station coordinates / exits
     - district population, age, and income data

4. **Cleaning and normalization layer**
   - Scripts such as `data_cleaning*.py`, `clean_v1.py`, `district_population.py`, `mrt.py`, `history_merge.py`, `prediction_merge.py`, and various merge scripts normalize raw crawler outputs into consistent tabular features.
   - This layer appears to resolve:
     - district naming and encoding
     - MRT proximity / station clustering
     - coordinate-based features
     - demographic enrichment
     - district-level aggregation and deduplication

5. **Intermediate datasets**
   - The repo is strongly file-driven rather than database-driven.
   - Important merged outputs include datasets such as:
     - `house_prices_taipei2.csv`
     - `house_prediction_data.csv`
     - `final_taipei_real_price.csv`
     - `merged_cleaned.csv`
     - `api_merge_all_data.csv`
     - district-specific cleaned CSVs
   - These files act as the interface between ETL and model training.

6. **Model training subsystem**
   - `train/train_v3.py` is the main training entry point in the consolidated flow.
   - The system trains six LightGBM quantile models:
     - three for **real transaction prices**
     - three for **listing prices**
   - Each family predicts low / mid / high price bands, corresponding to P10 / P50 / P90.
   - Trained model artifacts are stored as `.pkl` files in `train/models_v3_mrt_cluster/` and mirrored in `project/models/`.

7. **Feature and encoding boundary**
   - The model boundary depends on encoded geography and transport features:
     - district encoding
     - MRT cluster encoding
     - exact latitude/longitude
     - distance to MRT
     - coordinate interaction terms
     - building attributes and transaction month
   - Supporting lookup maps live in JSON files such as `district_map.json`, `mrt_cluster_map.json`, and `mrt_stations.json`, so inference depends on these static reference assets.

8. **Serving layer**
   - `project/main.py` is the FastAPI application entry point.
   - The API surface is split into three functional modules:
     - `predict_v3.py` for price prediction
     - `recommender.py` for recommendation logic
     - `market_trends.py` for market trend analytics
   - The API loads trained model files from `project/models/` and reads static geography maps from `project/static/`.

9. **Presentation layer**
   - `project/templates/` contains HTML pages for the main UI:
     - `index.html`
     - `page_predict.html`
     - `page_recommend.html`
     - `page_trends.html`
   - `project/static/` provides the map and station metadata needed by the front end to render districts, MRT coverage, and cluster information.
   - There are also standalone demo/dashboard entry points in `taipeihouseapi/` and `taipei-real-estate-quant-engine/`.

10. **Operational flow**
   - The runtime flow is:
     - crawl raw housing/geographic data
     - clean and merge into modeling tables
     - train LightGBM quantile models
     - store `.pkl` model artifacts plus JSON lookup maps
     - serve predictions and analytics through FastAPI
     - render results in template-based pages

11. **Repo organization insight**
   - The repo looks like a merged workspace from multiple contributors and iterations, not a single tidy app.
   - For architecture diagrams, treat `project/` + `train/` as the core system, and the other top-level folders as supporting ETL/crawler branches and legacy or exploratory implementations.

Main entry points: [`project/main.py`](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/main.py) (API entry), [`project/static/district_map.json`](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/static/district_map.json) (Static maps), [`實價登錄最終版/process.py`](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/實價登錄最終版/process.py) (Real-price merge), [`project/api/predict_v3.py`](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/api/predict_v3.py) (Predict API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g1["Data prep"]
  node_n1["Raw crawlers<br/>crawler/ingest<br/>[crawler.py]"]
  node_n2["JSON to CSV<br/>transform<br/>[json_to_csv.py]"]
  node_n3["Clean v1<br/>cleaning<br/>[clean_v1.py]"]
  node_n4["Merge pipeline<br/>[merge.py]"]
  node_n5["API merge<br/>etl<br/>[api_merge_all.py]"]
  node_n6["District cleaning<br/>[data_cleaning.py]"]
  node_n7["Geo enrich<br/>feature-engineering<br/>[mrt.py]"]
  node_n8["Real-price merge<br/>feature-build<br/>[process.py]"]
end

subgraph group_g2["Core app"]
  node_n9["Training entry<br/>[train_v3.py]"]
  node_n10[("Model artifacts<br/>[lgbm_real_mid.pkl]")]
  node_n11["API entry<br/>fastapi<br/>[main.py]"]
  node_n12["Predict API<br/>[predict_v3.py]"]
  node_n13["Recommend API<br/>[recommender.py]"]
  node_n14["Trends API<br/>analytics<br/>[market_trends.py]"]
  node_n15["Static maps<br/>reference-data<br/>[district_map.json]"]
  node_n16["HTML views<br/>ui<br/>[index.html]"]
end

subgraph group_g3["Legacy demos"]
  node_n17["Legacy API<br/>[api.py]"]
  node_n18["Quant dashboard<br/>[main.py]"]
  node_n19["Final price app<br/>[main.py]"]
end

node_n1 -->|"raw output"| node_n2
node_n2 -->|"clean"| node_n3
node_n3 -->|"merge"| node_n4
node_n1 -->|"district feeds"| node_n5
node_n6 -->|"enrich"| node_n7
node_n7 -->|"build table"| node_n8
node_n4 -->|"source data"| node_n8
node_n5 -->|"source data"| node_n8
node_n8 -->|"train on"| node_n9
node_n9 -->|"persist"| node_n10
node_n11 -->|"route"| node_n12
node_n11 -->|"route"| node_n13
node_n11 -->|"route"| node_n14
node_n11 -->|"load refs"| node_n15
node_n12 -->|"infer"| node_n10
node_n12 -->|"encode"| node_n15
node_n16 -->|"calls"| node_n11
node_n17 -->|"uses"| node_n15
node_n18 -->|"uses"| node_n15
node_n19 -->|"uses"| node_n15
node_n19 -.->|"infer"| node_n10

click node_n1 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/kevin/crawler.py"
click node_n2 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/kevin/json_to_csv.py"
click node_n3 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/kevin/clean_v1.py"
click node_n4 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/kevin/merge.py"
click node_n5 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/frances/api/api_merge_all.py"
click node_n6 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/永慶房屋/data_cleaning.py"
click node_n7 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/final/mrt.py"
click node_n8 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/實價登錄最終版/process.py"
click node_n9 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/train/train_v3.py"
click node_n10 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/train/models_v3_mrt_cluster/lgbm_real_mid.pkl"
click node_n11 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/main.py"
click node_n12 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/api/predict_v3.py"
click node_n13 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/api/recommender.py"
click node_n14 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/api/market_trends.py"
click node_n15 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/static/district_map.json"
click node_n16 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/templates/index.html"
click node_n17 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/taipeihouseapi/api.py"
click node_n18 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/taipei-real-estate-quant-engine/main.py"
click node_n19 "https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/永慶房屋/main.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n1,node_n2,node_n3,node_n4,node_n5,node_n6,node_n7,node_n8 toneBlue
class node_n9,node_n10,node_n11,node_n12,node_n13,node_n14,node_n15,node_n16 toneAmber
class node_n17,node_n18,node_n19 toneMint
```

## Components

### Data prep

Crawler, cleaning, and merge pipelines that build modeling tables.

- [Raw crawlers](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/kevin/crawler.py): crawler/ingest · District-based housing and listing collection across multiple contributor branches. · `kevin/crawler.py`
- [JSON to CSV](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/kevin/json_to_csv.py): transform · Converts crawler outputs into tabular district files. · `kevin/json_to_csv.py`
- [Clean v1](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/kevin/clean_v1.py): cleaning · Early normalization and cleanup for housing data. · `kevin/clean_v1.py`
- [Merge pipeline](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/kevin/merge.py): merge · Combines district outputs into merged datasets for downstream use. · `kevin/merge.py`
- [API merge](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/frances/api/api_merge_all.py): etl · Merges district-level API inputs into a consolidated dataset. · `frances/api/api_merge_all.py`
- [District cleaning](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/永慶房屋/data_cleaning.py): cleaning · Cleans and standardizes district and age/population inputs. · `永慶房屋/data_cleaning.py`
- [Geo enrich](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/final/mrt.py): feature-engineering · Adds MRT and demographic features such as station coordinates and population. · `final/mrt.py`
- [Real-price merge](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/實價登錄最終版/process.py): feature-build · Builds the consolidated real-estate training table. · `實價登錄最終版/process.py`

### Core app

Training artifacts and FastAPI serving path used in the main flow.

- [Training entry](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/train/train_v3.py): training · Main consolidated LightGBM training entry point. · `train/train_v3.py`
- [Model artifacts](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/train/models_v3_mrt_cluster/lgbm_real_mid.pkl): artifacts · Stored LightGBM quantile models for real and listing price bands. · `train/models_v3_mrt_cluster/lgbm_real_mid.pkl`
- [API entry](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/main.py): fastapi · Main FastAPI application that wires routes and serves the app. · `project/main.py`
- [Predict API](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/api/predict_v3.py): service · Price prediction endpoints backed by trained models. · `project/api/predict_v3.py`
- [Recommend API](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/api/recommender.py): service · Property recommendation logic and ranking endpoints. · `project/api/recommender.py`
- [Trends API](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/api/market_trends.py): analytics · Market trend analytics and summary endpoints. · `project/api/market_trends.py`
- [Static maps](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/static/district_map.json): reference-data · District, MRT cluster, and station lookup maps used during inference and UI rendering. · `project/static/district_map.json`
- [HTML views](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/project/templates/index.html): ui · Template pages for prediction, recommendation, and trend dashboards. · `project/templates/index.html`

### Legacy demos

Alternative or earlier UI/API implementations and dashboards.

- [Legacy API](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/taipeihouseapi/api.py): api · Earlier API/dashboard implementation for house-price exploration. · `taipeihouseapi/api.py`
- [Quant dashboard](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/taipei-real-estate-quant-engine/main.py): dashboard · Standalone quant-engine demo UI and entry point. · `taipei-real-estate-quant-engine/main.py`
- [Final price app](https://github.com/francesshenaiclass/tpe-real-estate-ai/blob/main/永慶房屋/main.py): app · Alternative real-price dashboard and API implementation. · `永慶房屋/main.py`

## Connections

- Raw crawlers → JSON to CSV: raw output · Crawler outputs are converted into CSV/JSON-friendly tables.
- JSON to CSV → Clean v1: clean · Tabular extraction feeds cleanup and normalization.
- Clean v1 → Merge pipeline: merge · Cleaned district files are merged into consolidated datasets.
- Raw crawlers → API merge: district feeds · Multiple district inputs are merged into API-facing datasets.
- District cleaning → Geo enrich: enrich · Demographic cleaning and MRT processing produce feature-enriched tables.
- Geo enrich → Real-price merge: build table · Geographic and demographic features are folded into the final modeling dataset.
- Merge pipeline → Real-price merge: source data · Merged housing data contributes to the real-price training table.
- API merge → Real-price merge: source data · API merge outputs also feed the consolidated modeling table.
- Real-price merge → Training entry: train on · Training consumes the prepared modeling dataset.
- Training entry → Model artifacts: persist · Training emits LightGBM model artifacts.
- API entry → Predict API: route · FastAPI exposes prediction endpoints.
- API entry → Recommend API: route · FastAPI exposes recommendation endpoints.
- API entry → Trends API: route · FastAPI exposes market trend endpoints.
- API entry → Static maps: load refs · Serving code loads district and MRT reference maps.
- Predict API → Model artifacts: infer · Prediction uses the trained price models.
- Predict API → Static maps: encode · Inference depends on static geography encodings.
- HTML views → API entry: calls · Template pages are rendered by the main API app.
- Legacy API → Static maps: uses · Legacy API/dashboard also depends on static geography data.
- Quant dashboard → Static maps: uses · Quant dashboard reads district and station metadata.
- Final price app → Static maps: uses · Alternative price app uses the same geography reference assets.
- Final price app → Model artifacts: infer · Alternative app can also consume trained price artifacts.
