# luongxuannhat/nghitts_diagram architecture

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

- Interactive diagram: https://gitdiagram.com/luongxuannhat/nghitts_diagram
- Repository: https://github.com/luongxuannhat/nghitts_diagram

## Overview

1. **Repo shape: two major deployments in one monorepo**
   - The runtime product is a **Vue 3 + Vite browser app** deployed to **Cloudflare Pages**.
   - A separate `train/` subtree is a **Python/Piper training and export toolchain**; it is not part of the browser runtime, but it produces the TTS models the app consumes.

2. **Main user-facing surfaces**
   - The frontend has four routes: Vietnamese TTS (`/`), English TTS (`/en`), Indonesian TTS (`/id`), and ASR (`/asr`).
   - `src/App.vue` provides the shell: language/feature tab bar, share action, and access to the TTS history panel.

3. **Frontend subsystem boundaries**
   - `src/views/` holds page-level orchestration:
     - `VietnameseView.vue` for the Vietnamese-specific TTS pipeline.
     - `LanguageView.vue` for the reusable non-Vietnamese TTS page.
     - `ASRView.vue` for microphone/file transcription workflows.
   - `src/components/` contains UI controls and result panels such as model selection, voice selection, speed, statistics, demo table, audio chunks, and history.

4. **TTS runtime architecture**
   - Speech synthesis runs **entirely in the browser**.
   - The app loads Piper ONNX models into a **Web Worker** (`src/workers/tts-worker.js` for Vietnamese, `tts-worker-i18n.js` for English/Indonesian) and uses **ONNX Runtime Web** from `public/onnx-runtime/`.
   - The worker streams generated audio chunks back to the UI, which can play them immediately and later merge them into a final WAV result.

5. **Vietnamese vs. other-language TTS pipeline**
   - Vietnamese TTS uses a heavier text-normalization pipeline in `src/utils/vietnamese-processor.js`, `text-cleaner.js`, `transliterator.js`, and related detectors.
   - That pipeline handles numeric/date/currency/time/roman-numeral conversions before phonemization and synthesis.
   - English and Indonesian use the simpler `src/lib/piper-tts-i18n.js` path, with less language-specific preprocessing.

6. **Model discovery and model delivery boundary**
   - The browser never hardcodes model binaries; it discovers them through Cloudflare Pages Functions under `functions/api/`.
   - TTS model listing and serving is split by language:
     - Vietnamese: `/api/models` and `/api/model/[name].ts`
     - English/Indonesian: `/api/piper/{lang}/models` and `/api/model/piper/{lang}/{name}`
   - ASR model listing/serving is separate:
     - `/api/asr/models`
     - `/api/model/asr/{model}/{name}`
   - This creates a clear boundary between the static browser app and server-side model catalog/asset delivery.

7. **Production storage and local-dev parity**
   - In production, model files live in **Cloudflare R2** and are exposed by the Pages Functions.
   - In local development, the same logical model structure is mirrored under `public/tts-model/...` and `public/asr-model/`.
   - Other static runtime assets are shipped from `public/`, including the Sherpa-ONNX WASM bundles, Silero VAD ONNX model, demo audio/text pairs, and normalization CSVs.

8. **ASR runtime architecture**
   - ASR is also browser-local and uses **Sherpa-ONNX WASM** plus **Silero VAD**.
   - `public/code/asr-wasm/` contains the ASR/VAD WASM loaders, while `public/vad-model/silero-vad/silero_vad.onnx` provides voice activity detection.
   - The ASR page supports two input flows:
     - microphone capture with real-time VAD segmentation
     - file upload with decoding, segmentation, and timestamped transcription/SRT export

9. **State and persistence boundaries**
   - TTS generation history is persisted in **IndexedDB** via `src/utils/history-store.js`, letting the user replay, download, or copy prior outputs.
   - ASR model choice is persisted in **localStorage**.
   - Model caching/detection helpers in `src/utils/model-cache.js` and `model-detector.js` keep the browser from repeatedly reloading the same assets.

10. **Configuration and routing**
   - `src/router/index.js` maps the four main feature routes.
   - `src/config.js` and `src/config.json` control runtime behavior such as debug mode and roman numeral handling.
   - `public/_routes.json` and `wrangler.toml` tie the app to Cloudflare Pages deployment behavior and the R2 binding.

11. **Training/export subsystem**
   - `train/` is a standalone Piper ecosystem for dataset handling, phonemization, model training, ONNX export, and voice packaging.
   - It includes Python modules, C/C++ bindings, tests, and scripts for training/export/server tasks.
   - Conceptually, this is the upstream model-production boundary: it generates the `.onnx` and `.onnx.json` assets that the browser app later discovers and runs.

12. **End-to-end data flow summary**
   - User input enters the Vue UI → text/audio is prepared in the browser → model metadata is fetched from Pages Functions → model files are streamed from local `public/` or R2 → inference runs in Web Workers/WASM → results return to the UI for playback/transcription/download → optional persistence stores history or selected model state.

Main entry points: [`functions/api/model`](https://github.com/luongxuannhat/nghitts_diagram/tree/main/functions/api/model) (Model serving), [`src/router/index.js`](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/router/index.js) (Router), [`src/views/VietnameseView.vue`](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/views/VietnameseView.vue) (Vietnamese TTS), [`src/views/LanguageView.vue`](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/views/LanguageView.vue) (Other TTS)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Browser app"]
  node_src_main["Main app<br/>Vue entry<br/>[main.js]"]
  node_src_app["App shell<br/>Vue shell<br/>[App.vue]"]
  node_src_router["Router<br/>route map<br/>[index.js]"]
  node_view_vn["Vietnamese TTS<br/>page view<br/>[VietnameseView.vue]"]
  node_view_lang["Other TTS<br/>page view<br/>[LanguageView.vue]"]
  node_view_asr["ASR view<br/>page view<br/>[ASRView.vue]"]
  node_ui_components["UI controls<br/>component set"]
  node_vn_processor["VN processor<br/>text pipeline"]
  node_text_cleaner["Text cleanup<br/>normalization<br/>[text-cleaner.js]"]
  node_i18n_tts["I18n TTS core<br/>browser synth<br/>[piper-tts-i18n.js]"]
  node_tts_worker_vn["VN worker<br/>web worker<br/>[tts-worker.js]"]
  node_tts_worker_i18n["I18n worker<br/>web worker<br/>[tts-worker-i18n.js]"]
  node_tts_core["Piper TTS<br/>synthesis core<br/>[piper-tts.js]"]
  node_asr_core["ASR runtime<br/>browser inference<br/>[sherpa-onnx-asr.js]"]
  node_history_store["History store<br/>IndexedDB<br/>[history-store.js]"]
  node_model_cache["Model cache<br/>cache helpers<br/>[model-cache.js]"]
end

subgraph group_serverless["Pages Functions"]
  node_fn_models["Model APIs<br/>Pages functions"]
  node_fn_model_serve["Model serving<br/>asset endpoints"]
end

subgraph group_assets["Public assets"]
  node_public_runtime["Runtime assets<br/>static bundle"]
  node_public_tts_models["TTS models<br/>static mirror"]
  node_public_asr_models["ASR models<br/>static mirror"]
  node_public_vad["VAD model<br/>static asset<br/>[silero_vad.onnx]"]
end

subgraph group_training["Training toolchain"]
  node_train_pkg["Piper train<br/>python package"]
  node_train_vits["VITS trainer<br/>model training"]
  node_train_export["Export tools<br/>export pipeline<br/>[export_onnx.py]"]
  node_train_voice["Voice packaging<br/>voice tooling<br/>[voice.py]"]
end

node_src_main -->|"mounts"| node_src_app
node_src_app -->|"navigates"| node_src_router
node_src_app -->|"composes"| node_ui_components
node_src_router -->|"route /"| node_view_vn
node_src_router -->|"routes /en,/id"| node_view_lang
node_src_router -->|"route /asr"| node_view_asr
node_view_vn -->|"preprocesses"| node_vn_processor
node_view_vn -->|"sends text"| node_tts_worker_vn
node_view_vn -->|"persists results"| node_history_store
node_view_lang -->|"cleans text"| node_text_cleaner
node_view_lang -->|"sends text"| node_tts_worker_i18n
node_view_lang -->|"tracks models"| node_model_cache
node_tts_worker_vn -->|"runs"| node_tts_core
node_tts_worker_i18n -->|"runs"| node_i18n_tts
node_tts_worker_vn -->|"loads runtime"| node_public_runtime
node_tts_worker_i18n -->|"loads runtime"| node_public_runtime
node_view_asr -->|"captures audio"| node_asr_core
node_view_asr -->|"uses VAD"| node_public_vad
node_view_asr -->|"persists choice"| node_model_cache
node_fn_models -->|"feeds"| node_fn_model_serve
node_fn_model_serve -->|"dev mirror"| node_public_tts_models
node_fn_model_serve -->|"dev mirror"| node_public_asr_models
node_fn_model_serve -->|"serves assets"| node_public_runtime
node_train_pkg -->|"contains"| node_train_vits
node_train_pkg -->|"exports"| node_train_export
node_train_pkg -->|"packages"| node_train_voice
node_train_export -.->|"produces assets for"| node_fn_model_serve
node_train_voice -.->|"targets"| node_public_tts_models

click node_src_main "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/main.js"
click node_src_app "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/App.vue"
click node_src_router "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/router/index.js"
click node_view_vn "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/views/VietnameseView.vue"
click node_view_lang "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/views/LanguageView.vue"
click node_view_asr "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/views/ASRView.vue"
click node_ui_components "https://github.com/luongxuannhat/nghitts_diagram/tree/main/src/components"
click node_vn_processor "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/utils/vietnamese-processor.js"
click node_text_cleaner "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/utils/text-cleaner.js"
click node_i18n_tts "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/lib/piper-tts-i18n.js"
click node_tts_worker_vn "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/workers/tts-worker.js"
click node_tts_worker_i18n "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/workers/tts-worker-i18n.js"
click node_tts_core "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/lib/piper-tts.js"
click node_asr_core "https://github.com/luongxuannhat/nghitts_diagram/blob/main/public/code/asr-wasm/sherpa-onnx-asr.js"
click node_history_store "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/utils/history-store.js"
click node_model_cache "https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/utils/model-cache.js"
click node_fn_models "https://github.com/luongxuannhat/nghitts_diagram/tree/main/functions/api"
click node_fn_model_serve "https://github.com/luongxuannhat/nghitts_diagram/tree/main/functions/api/model"
click node_public_runtime "https://github.com/luongxuannhat/nghitts_diagram/tree/main/public"
click node_public_tts_models "https://github.com/luongxuannhat/nghitts_diagram/tree/main/public/tts-model"
click node_public_vad "https://github.com/luongxuannhat/nghitts_diagram/blob/main/public/vad-model/silero-vad/silero_vad.onnx"
click node_train_pkg "https://github.com/luongxuannhat/nghitts_diagram/tree/main/train/src/piper"
click node_train_vits "https://github.com/luongxuannhat/nghitts_diagram/tree/main/train/src/piper/train/vits"
click node_train_export "https://github.com/luongxuannhat/nghitts_diagram/blob/main/train/src/piper/train/export_onnx.py"
click node_train_voice "https://github.com/luongxuannhat/nghitts_diagram/blob/main/train/src/piper/voice.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_src_main,node_src_app,node_src_router,node_view_vn,node_view_lang,node_view_asr,node_ui_components,node_vn_processor,node_text_cleaner,node_i18n_tts,node_tts_worker_vn,node_tts_worker_i18n,node_tts_core,node_asr_core,node_history_store,node_model_cache toneBlue
class node_fn_models,node_fn_model_serve toneAmber
class node_public_runtime,node_public_tts_models,node_public_asr_models,node_public_vad toneMint
class node_train_pkg,node_train_vits,node_train_export,node_train_voice toneRose
```

## Components

### Browser app

Vue 3 + Vite runtime deployed to Cloudflare Pages.

- [Main app](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/main.js): Vue entry · Bootstraps the SPA, global styling, and app composition. · `src/main.js`
- [App shell](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/App.vue): Vue shell · Top-level UI with feature tabs, sharing, and history access. · `src/App.vue`
- [Router](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/router/index.js): route map · Maps the four main feature routes. · `src/router/index.js`
- [Vietnamese TTS](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/views/VietnameseView.vue): page view · Orchestrates Vietnamese synthesis and preprocessing. · `src/views/VietnameseView.vue`
- [Other TTS](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/views/LanguageView.vue): page view · Reusable English and Indonesian synthesis page. · `src/views/LanguageView.vue`
- [ASR view](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/views/ASRView.vue): page view · Microphone and file transcription workflows. · `src/views/ASRView.vue`
- [UI controls](https://github.com/luongxuannhat/nghitts_diagram/tree/main/src/components): component set · Model, voice, speed, stats, demos, audio chunks, and history panels. · `src/components`
- [VN processor](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/utils/vietnamese-processor.js): text pipeline · Normalization, transliteration, and detector chain for Vietnamese TTS. · `src/utils/vietnamese-processor.js`
- [Text cleanup](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/utils/text-cleaner.js): normalization · Shared cleanup path for non-Vietnamese synthesis. · `src/utils/text-cleaner.js`
- [I18n TTS core](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/lib/piper-tts-i18n.js): browser synth · English and Indonesian browser synthesis path with lighter preprocessing. · `src/lib/piper-tts-i18n.js`
- [VN worker](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/workers/tts-worker.js): web worker · Loads Piper ONNX runtime and streams generated audio chunks back to UI. · `src/workers/tts-worker.js`
- [I18n worker](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/workers/tts-worker-i18n.js): web worker · Worker for English and Indonesian synthesis. · `src/workers/tts-worker-i18n.js`
- [Piper TTS](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/lib/piper-tts.js): synthesis core · Shared browser-side synthesis logic used by both workers. · `src/lib/piper-tts.js`
- [ASR runtime](https://github.com/luongxuannhat/nghitts_diagram/blob/main/public/code/asr-wasm/sherpa-onnx-asr.js): browser inference · Sherpa-ONNX WASM plus VAD-driven transcription pipeline. · `public/code/asr-wasm/sherpa-onnx-asr.js`
- [History store](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/utils/history-store.js): IndexedDB · Persists TTS outputs for replay, download, and copy. · `src/utils/history-store.js`
- [Model cache](https://github.com/luongxuannhat/nghitts_diagram/blob/main/src/utils/model-cache.js): cache helpers · Avoids repeated reloads and detects available models. · `src/utils/model-cache.js`

### Pages Functions

Model catalog and asset delivery boundary between app and storage.

- [Model APIs](https://github.com/luongxuannhat/nghitts_diagram/tree/main/functions/api): Pages functions · Lists and serves TTS and ASR models from the serverless boundary. · `functions/api`
- [Model serving](https://github.com/luongxuannhat/nghitts_diagram/tree/main/functions/api/model): asset endpoints · Language-aware model asset routes for TTS and ASR binaries. · `functions/api/model`

### Public assets

Static runtime files shipped with the browser app.

- [Runtime assets](https://github.com/luongxuannhat/nghitts_diagram/tree/main/public): static bundle · ONNX Runtime Web, ASR WASM loaders, demos, and normalization data. · `public`
- [TTS models](https://github.com/luongxuannhat/nghitts_diagram/tree/main/public/tts-model): static mirror · Local-dev mirror for language models and voices metadata. · `public/tts-model`
- **ASR models**: static mirror · Local-dev mirror for ASR models.
- [VAD model](https://github.com/luongxuannhat/nghitts_diagram/blob/main/public/vad-model/silero-vad/silero_vad.onnx): static asset · Silero VAD ONNX model used by ASR segmentation. · `public/vad-model/silero-vad/silero_vad.onnx`

### Training toolchain

Standalone Piper training, export, and packaging subtree.

- [Piper train](https://github.com/luongxuannhat/nghitts_diagram/tree/main/train/src/piper): python package · Standalone training, export, and packaging toolchain. · `train/src/piper`
- [VITS trainer](https://github.com/luongxuannhat/nghitts_diagram/tree/main/train/src/piper/train/vits): model training · Core training stack for datasets, losses, and VITS models. · `train/src/piper/train/vits`
- [Export tools](https://github.com/luongxuannhat/nghitts_diagram/blob/main/train/src/piper/train/export_onnx.py): export pipeline · ONNX export and voice packaging generators. · `train/src/piper/train/export_onnx.py`
- [Voice packaging](https://github.com/luongxuannhat/nghitts_diagram/blob/main/train/src/piper/voice.py): voice tooling · Voice metadata, phonemization, and packaged model helpers. · `train/src/piper/voice.py`

## Connections

- Main app → App shell: mounts · Main entry renders the SPA shell.
- App shell → Router: navigates · Shell delegates feature switching to router.
- App shell → UI controls: composes · Shell uses shared controls and panels.
- Router → Vietnamese TTS: route / · Vietnamese TTS page.
- Router → Other TTS: routes /en,/id · Reusable non-Vietnamese TTS pages.
- Router → ASR view: route /asr · ASR transcription page.
- Vietnamese TTS → VN processor: preprocesses · Vietnamese text normalization before synthesis.
- Vietnamese TTS → VN worker: sends text · Hands off synthesis to worker.
- Vietnamese TTS → History store: persists results · Stores synthesis history in IndexedDB.
- Other TTS → Text cleanup: cleans text · Shared text cleanup for English and Indonesian.
- Other TTS → I18n worker: sends text · Hands off synthesis to i18n worker.
- Other TTS → Model cache: tracks models · Caches and detects model availability.
- VN worker → Piper TTS: runs · Worker executes shared Piper synthesis core.
- I18n worker → I18n TTS core: runs · I18n worker uses lighter browser synthesis path.
- VN worker → Runtime assets: loads runtime · Uses ONNX Runtime Web and related assets.
- I18n worker → Runtime assets: loads runtime · Uses ONNX Runtime Web assets.
- ASR view → ASR runtime: captures audio · Uses WASM ASR with VAD for microphone and file flows.
- ASR view → VAD model: uses VAD · Voice activity detection model for segmentation.
- ASR view → Model cache: persists choice · Stores selected ASR model in localStorage.
- Model APIs → Model serving: feeds · Catalog endpoints connect to model asset routes.
- Model serving → TTS models: dev mirror · Local development TTS model layout.
- Model serving → ASR models: dev mirror · Local development ASR model layout.
- Model serving → Runtime assets: serves assets · Static bundles and runtime files are delivered to the browser.
- Piper train → VITS trainer: contains · Training package includes VITS model stack.
- Piper train → Export tools: exports · Generates ONNX and packaging artifacts.
- Piper train → Voice packaging: packages · Handles voice metadata and packaging helpers.
- Export tools → Model serving: produces assets for · Training/export outputs become browser-consumable model files.
- Voice packaging → TTS models: targets · Packaged voice artifacts match the runtime model layout.
