# rainyaya/bikuri-nihongolens architecture

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

- Interactive diagram: https://gitdiagram.com/rainyaya/bikuri-nihongolens
- Repository: https://github.com/rainyaya/bikuri-nihongolens

## Overview

#### Purpose and Entry Points
Kotoba Studio is a local-first, single-user Japanese video study application. It imports or downloads video, transcribes it with word-level Japanese timing, and presents a synchronized study workspace. Runtime entry points are FastAPI in `backend/app/main.py` and the React/Vite client in `frontend/src/main.tsx`, with routing in `frontend/src/router.tsx`.

#### Local Application Boundary
The browser communicates with a localhost FastAPI API; Vite provides the development `/api` proxy. No remote application backend stores user data. The system depends on local disk, SQLite, GPU-capable WhisperX, ffmpeg/ffprobe, and optionally yt-dlp. Core architecture decisions are recorded in `docs/adr/0001-local-web-app-architecture.md` and `docs/adr/0003-persistence-sqlite-plus-library.md`.

#### API, Domain, and Persistence Layer
FastAPI routers separate projects, folders, study state, settings, and asynchronous tasks:
- `backend/app/routers/projects.py`
- `backend/app/routers/study.py`
- `backend/app/routers/tasks.py`

SQLite holds durable metadata such as projects, notes, saved vocabulary, viewing progress, and folders through `backend/app/db.py` and `backend/app/models.py`. The filesystem library manages per-project media and generated artifacts via `backend/app/library.py` and `backend/app/media.py`; the database references this local library rather than replacing it.

#### Import and Background Work Orchestration
Project creation accepts either a local media import or a remote URL. URL acquisition uses yt-dlp integration in `backend/app/ytdlp.py`; media then enters the task manager in `backend/app/tasks/manager.py`. The task system supports download concurrency, serialized transcription, cancellation, persisted task state, and SSE progress delivery. Work implementations are split between `backend/app/tasks/download.py` and `backend/app/tasks/transcribe.py`.

#### Transcription and Artifact Pipeline
The transcription worker invokes the vendored pipeline: ASR/alignment produces canonical segment-and-word JSON, while SRT is a derived export. Pipeline responsibilities are concentrated in:
- `backend/pipeline/asr.py`
- `backend/pipeline/tokenize.py`
- `backend/pipeline/subtitle.py`

Video processing such as duration inspection, thumbnail extraction, and waveform peak generation is in `backend/pipeline/video.py`. WhisperX uses local HuggingFace model caches and CUDA fp16 where available; ffmpeg/ffprobe are required external executables.

#### Study API and Playback Model
Study endpoints expose transcript/token data, saved words, notes, and progress from `backend/app/routers/study.py`. Playback time is the single client-side synchronization source: active word highlighting, transcript/focus selection, auto-scroll, subtitle display, and timeline playhead derive from the same player clock rather than independently maintained state. This boundary is formalized in `docs/adr/0004-playback-as-single-source-of-truth.md`.

#### Frontend Experience Structure
The application shell and page routing are rooted in `frontend/src/layouts/AppLayout.tsx` and `frontend/src/pages/StudyPage.tsx`. Project library, task monitoring, and settings are dedicated pages:
- `frontend/src/pages/ProjectsPage.tsx`
- `frontend/src/pages/TasksPage.tsx`
- `frontend/src/pages/SettingsPage.tsx`

The study workspace composes the video stage, timed subtitle/transcript views, vocabulary/notes panels, and timeline from `frontend/src/components/study/VideoStage.tsx`, `frontend/src/components/study/TranscriptPane.tsx`, and `frontend/src/components/study/Timeline.tsx`. `frontend/src/components/study/usePlayerClock.ts` is the key synchronization mechanism. Client API calls are centralized in `frontend/src/lib/api.ts`.

Main entry points: [`backend/app/library.py`](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/library.py) (Project media library), [`frontend/src/pages/StudyPage.tsx`](https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/pages/StudyPage.tsx) (Study workspace), [`backend/app/routers/projects.py`](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/routers/projects.py) (Projects, study & task APIs), [`backend/app/tasks/manager.py`](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/tasks/manager.py) (Task manager)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React client"]
  node_client_entry["Vite client entry<br/>React entry<br/>[main.tsx]"]
  node_client_router["Client routing<br/>React router<br/>[router.tsx]"]
  node_app_shell["Application shell<br/>layout<br/>[AppLayout.tsx]"]
  node_library_pages["Library, tasks &amp; settings<br/>pages<br/>[ProjectsPage.tsx]"]
  node_study_page["Study workspace<br/>study page<br/>[StudyPage.tsx]"]
  node_player_clock(("Playback clock<br/>sync state<br/>[usePlayerClock.ts]"))
  node_study_views["Video, transcript &amp; timeline<br/>study components<br/>[VideoStage.tsx]"]
  node_api_client["API client<br/>HTTP client<br/>[api.ts]"]
end

subgraph group_api["Local API"]
  node_fastapi["FastAPI application<br/>localhost API<br/>[main.py]"]
  node_api_routers["Projects, study &amp; task APIs<br/>routers<br/>[projects.py]"]
end

subgraph group_work["Background pipeline"]
  node_task_manager{{"Task manager<br/>orchestrator<br/>[manager.py]"}}
  node_download_worker["Download worker<br/>task worker<br/>[download.py]"]
  node_transcription_worker["Transcription worker<br/>task worker<br/>[transcribe.py]"]
  node_ytdlp["yt-dlp acquisition<br/>external integration<br/>[ytdlp.py]"]
  node_asr_pipeline["ASR &amp; alignment<br/>WhisperX pipeline<br/>[asr.py]"]
  node_subtitle_pipeline["Tokenization &amp; SRT export<br/>artifact pipeline<br/>[subtitle.py]"]
  node_video_pipeline["Media analysis<br/>video pipeline<br/>[video.py]"]
  node_local_tools["Local ML &amp; media tools<br/>runtime dependencies"]
end

subgraph group_storage["Local persistence"]
  node_sqlite[("SQLite metadata<br/>database<br/>[db.py]")]
  node_media_library["Project media library<br/>filesystem store<br/>[library.py]"]
end

node_client_entry --> node_client_router
node_client_router --> node_app_shell
node_app_shell --> node_library_pages
node_app_shell --> node_study_page
node_study_page --> node_study_views
node_study_page -->|"drives UI state"| node_player_clock
node_player_clock -->|"synchronizes"| node_study_views
node_library_pages --> node_api_client
node_study_page --> node_api_client
node_api_client -->|"localhost /api"| node_fastapi
node_fastapi --> node_api_routers
node_api_routers -->|"metadata &amp; study state"| node_sqlite
node_api_routers -->|"media &amp; artifacts"| node_media_library
node_api_routers -->|"enqueue and stream progress"| node_task_manager
node_task_manager -->|"concurrent downloads"| node_download_worker
node_task_manager -->|"serialized transcription"| node_transcription_worker
node_task_manager -->|"persist task state"| node_sqlite
node_download_worker -->|"remote URL"| node_ytdlp
node_download_worker -->|"store media"| node_media_library
node_transcription_worker --> node_asr_pipeline
node_transcription_worker --> node_video_pipeline
node_asr_pipeline -->|"canonical transcript"| node_subtitle_pipeline
node_asr_pipeline -->|"timed transcript JSON"| node_media_library
node_subtitle_pipeline -->|"derived SRT"| node_media_library
node_video_pipeline -->|"thumbnails &amp; waveform"| node_media_library
node_asr_pipeline -->|"WhisperX/CUDA"| node_local_tools
node_video_pipeline -->|"ffmpeg/ffprobe"| node_local_tools

click node_client_entry "https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/main.tsx"
click node_client_router "https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/router.tsx"
click node_app_shell "https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/layouts/AppLayout.tsx"
click node_library_pages "https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/pages/ProjectsPage.tsx"
click node_study_page "https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/pages/StudyPage.tsx"
click node_player_clock "https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/components/study/usePlayerClock.ts"
click node_study_views "https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/components/study/VideoStage.tsx"
click node_api_client "https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/lib/api.ts"
click node_fastapi "https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/main.py"
click node_api_routers "https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/routers/projects.py"
click node_task_manager "https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/tasks/manager.py"
click node_download_worker "https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/tasks/download.py"
click node_transcription_worker "https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/tasks/transcribe.py"
click node_ytdlp "https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/ytdlp.py"
click node_asr_pipeline "https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/pipeline/asr.py"
click node_subtitle_pipeline "https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/pipeline/subtitle.py"
click node_video_pipeline "https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/pipeline/video.py"
click node_sqlite "https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/db.py"
click node_media_library "https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/library.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_client_entry,node_client_router,node_app_shell,node_library_pages,node_study_page,node_player_clock,node_study_views,node_api_client toneBlue
class node_fastapi,node_api_routers toneAmber
class node_task_manager,node_download_worker,node_transcription_worker,node_ytdlp,node_asr_pipeline,node_subtitle_pipeline,node_video_pipeline,node_local_tools toneMint
class node_sqlite,node_media_library toneRose
```

## Components

### React client

- [Vite client entry](https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/main.tsx): React entry · `frontend/src/main.tsx`
- [Client routing](https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/router.tsx): React router · `frontend/src/router.tsx`
- [Application shell](https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/layouts/AppLayout.tsx): layout · `frontend/src/layouts/AppLayout.tsx`
- [Library, tasks & settings](https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/pages/ProjectsPage.tsx): pages · `frontend/src/pages/ProjectsPage.tsx`
- [Study workspace](https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/pages/StudyPage.tsx): study page · `frontend/src/pages/StudyPage.tsx`
- [Playback clock](https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/components/study/usePlayerClock.ts): sync state · Single source for study-view synchronization. · `frontend/src/components/study/usePlayerClock.ts`
- [Video, transcript & timeline](https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/components/study/VideoStage.tsx): study components · `frontend/src/components/study/VideoStage.tsx`
- [API client](https://github.com/rainyaya/bikuri-nihongolens/blob/main/frontend/src/lib/api.ts): HTTP client · `frontend/src/lib/api.ts`

### Local API

- [FastAPI application](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/main.py): localhost API · `backend/app/main.py`
- [Projects, study & task APIs](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/routers/projects.py): routers · `backend/app/routers/projects.py`

### Background pipeline

- [Task manager](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/tasks/manager.py): orchestrator · Coordinates concurrency, cancellation, persisted state, and SSE progress. · `backend/app/tasks/manager.py`
- [Download worker](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/tasks/download.py): task worker · `backend/app/tasks/download.py`
- [Transcription worker](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/tasks/transcribe.py): task worker · `backend/app/tasks/transcribe.py`
- [yt-dlp acquisition](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/ytdlp.py): external integration · Optional remote URL download. · `backend/app/ytdlp.py`
- [ASR & alignment](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/pipeline/asr.py): WhisperX pipeline · Produces canonical timed segment-and-word JSON. · `backend/pipeline/asr.py`
- [Tokenization & SRT export](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/pipeline/subtitle.py): artifact pipeline · SRT is derived from canonical transcript data. · `backend/pipeline/subtitle.py`
- [Media analysis](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/pipeline/video.py): video pipeline · Inspects duration and generates thumbnails and waveform peaks. · `backend/pipeline/video.py`
- **Local ML & media tools**: runtime dependencies · WhisperX model cache, CUDA, ffmpeg, and ffprobe.

### Local persistence

- [SQLite metadata](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/db.py): database · Stores projects, folders, notes, vocabulary, progress, and task state. · `backend/app/db.py`
- [Project media library](https://github.com/rainyaya/bikuri-nihongolens/blob/main/backend/app/library.py): filesystem store · Per-project source media and generated artifacts. · `backend/app/library.py`

## Connections

- Vite client entry → Client routing
- Client routing → Application shell
- Application shell → Library, tasks & settings
- Application shell → Study workspace
- Study workspace → Video, transcript & timeline
- Study workspace → Playback clock: drives UI state
- Playback clock → Video, transcript & timeline: synchronizes
- Library, tasks & settings → API client
- Study workspace → API client
- API client → FastAPI application: localhost /api · Vite proxies API calls during development.
- FastAPI application → Projects, study & task APIs
- Projects, study & task APIs → SQLite metadata: metadata & study state
- Projects, study & task APIs → Project media library: media & artifacts
- Projects, study & task APIs → Task manager: enqueue and stream progress
- Task manager → Download worker: concurrent downloads
- Task manager → Transcription worker: serialized transcription
- Task manager → SQLite metadata: persist task state
- Download worker → yt-dlp acquisition: remote URL
- Download worker → Project media library: store media
- Transcription worker → ASR & alignment
- Transcription worker → Media analysis
- ASR & alignment → Tokenization & SRT export: canonical transcript
- ASR & alignment → Project media library: timed transcript JSON
- Tokenization & SRT export → Project media library: derived SRT
- Media analysis → Project media library: thumbnails & waveform
- ASR & alignment → Local ML & media tools: WhisperX/CUDA
- Media analysis → Local ML & media tools: ffmpeg/ffprobe
