# varunvekaria/languageai architecture

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

- Interactive diagram: https://gitdiagram.com/varunvekaria/languageai
- Repository: https://github.com/varunvekaria/languageai

## Overview

1. **High-level shape**
   - This repo is a split full-stack app: a **Next.js frontend** in `src/` and a **FastAPI backend** in `backend/app/`.
   - The backend is the main orchestration layer for user onboarding, voice cloning, lesson retrieval, and audio generation.
   - **Supabase** provides auth, persistence, and storage; **ElevenLabs** provides voice cloning and text-to-speech.

2. **Frontend boundary**
   - The Next.js app uses the App Router with pages for landing, auth, dashboard, and lesson playback:
     - `src/app/page.tsx`
     - `src/app/auth/*`
     - `src/app/dashboard/page.tsx`
     - `src/app/lesson/[lessonId]/page.tsx`
   - `src/components/LessonWorkspace.tsx` is the main interactive lesson UI surface.
   - `src/app/layout.tsx` and `src/app/globals.css` provide the shared shell and styling.

3. **Frontend integrations**
   - `src/lib/supabase/client.ts` is the frontend Supabase client, used for sign-in/session handling and token acquisition.
   - `src/lib/api.ts` is the frontend wrapper for calling the FastAPI backend.
   - `src/types/api.ts` suggests the frontend consumes typed backend responses rather than ad hoc JSON shapes.

4. **Authentication flow**
   - Authentication is delegated to **Supabase Auth** on the frontend.
   - The backend does not own login; instead, protected endpoints require `Authorization: Bearer <supabase_access_token>`.
   - `backend/app/deps/auth.py` is the auth boundary that validates the Supabase token and resolves the current user.

5. **Backend API structure**
   - `backend/app/main.py` is the FastAPI entrypoint.
   - The backend is organized by router:
     - `routers/health.py` for liveness checks
     - `routers/users.py` for onboarding/profile operations
     - `routers/voices.py` for cloning and listing voices
     - `routers/lessons.py` for lesson library and audio rendering
   - The API is versioned under `/api/v1`, matching the README contract.

6. **Lesson content subsystem**
   - `backend/app/data/lessons.py` holds the prewritten lesson library.
   - This means lesson content is currently **application-owned/static**, not user-generated or fetched from an external CMS.
   - `GET /api/v1/lessons/library` exposes that content, and `GET /api/v1/lessons/{lesson_id}` returns a specific lesson.

7. **User profile onboarding**
   - `POST /api/v1/users/onboard` is the profile bootstrap step after auth.
   - The backend verifies the Supabase token, then upserts a profile row into Supabase, typically in `user_profiles`.
   - This creates the link between the authenticated auth user and app-specific preferences like `target_language`.

8. **Voice cloning subsystem**
   - `POST /api/v1/voices/clone` accepts multipart uploads of voice samples plus a `voice_name`.
   - `backend/app/services/elevenlabs.py` handles the ElevenLabs VoiceLab clone API call.
   - The resulting voice metadata is persisted in Supabase, usually in `user_voices`.
   - The backend also cleans up the temporary cloned voice after generating the needed lesson audio, so cloning is treated as an intermediate step.

9. **Lesson audio generation pipeline**
   - `POST /api/v1/lessons/render-audio` is the main media pipeline.
   - `backend/app/services/lesson_audio.py` orchestrates per-line TTS generation rather than producing one long file.
   - Audio is stored in a **private Supabase Storage bucket** (`lesson-audio`), and metadata is written to `user_lesson_audio`.
   - The API returns signed playback URLs, so the client can stream audio without making the bucket public.

10. **Persistence and service boundaries**
   - `backend/app/services/supabase.py` centralizes Supabase client access and likely switches between anon/service-role credentials.
   - The backend uses the **service role key** for server-side writes to profiles, voice metadata, and lesson-audio metadata/storage.
   - The frontend uses only the public Supabase anon key and never writes directly to the sensitive tables.

11. **External service dependencies**
   - **Supabase** is the system of record for identity, user data, and generated audio metadata.
   - **ElevenLabs** is the external media-generation provider for both cloned voices and TTS.
   - These two services define the main runtime boundary: the app coordinates them, but does not implement its own auth or speech synthesis.

12. **Operational/runtime view**
   - Frontend runs on `localhost:3000`, backend on `localhost:8000`, with Swagger at `/docs` and health at `/api/v1/health`.
   - Configuration is environment-driven in both layers:
     - frontend: Supabase URL/anon key, API base URL
     - backend: Supabase credentials, ElevenLabs key, storage/bucket/table names, TTS speed
   - This makes the system easy to diagram as a browser app talking to a stateless API, which in turn talks to Supabase and ElevenLabs.

Main entry points: [`backend/app/main.py`](https://github.com/varunvekaria/languageai/blob/main/backend/app/main.py) (API entry), [`backend/app/services/supabase.py`](https://github.com/varunvekaria/languageai/blob/main/backend/app/services/supabase.py) (Supabase adapter), [`backend/app/routers/voices.py`](https://github.com/varunvekaria/languageai/blob/main/backend/app/routers/voices.py) (Voices router), [`backend/app/routers/lessons.py`](https://github.com/varunvekaria/languageai/blob/main/backend/app/routers/lessons.py) (Lessons router)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_next_app["Next.js app<br/>web frontend"]
  node_landing_page["Landing<br/>page<br/>[page.tsx]"]
  node_auth_pages["Auth pages"]
  node_dashboard_page["Dashboard<br/>page<br/>[page.tsx]"]
  node_lesson_page["Lesson page<br/>[page.tsx]"]
  node_lesson_workspace["Lesson workspace<br/>ui component"]
  node_frontend_api["API client<br/>client wrapper<br/>[api.ts]"]
  node_supabase_client["Supabase client<br/>auth client<br/>[client.ts]"]
  node_typed_api["API types<br/>[api.ts]"]
end

subgraph group_backend["Backend"]
  node_api_main["API entry<br/>fastapi app<br/>[main.py]"]
  node_auth_dep["Auth guard<br/>dependency<br/>[auth.py]"]
  node_users_router["Users router<br/>api router<br/>[users.py]"]
  node_voices_router["Voices router<br/>api router<br/>[voices.py]"]
  node_lessons_router["Lessons router<br/>api router<br/>[lessons.py]"]
  node_health_router["Health router<br/>api router<br/>[health.py]"]
  node_lesson_catalog["Lesson catalog<br/>content store<br/>[lessons.py]"]
  node_lesson_audio_service["Audio pipeline<br/>[lesson_audio.py]"]
  node_elevenlabs_service["ElevenLabs adapter<br/>[elevenlabs.py]"]
  node_supabase_service["Supabase adapter<br/>[supabase.py]"]
end

subgraph group_external["External services"]
  node_supabase[("Supabase<br/>identity+data platform")]
  node_elevenlabs{{"ElevenLabs<br/>speech service"}}
end

node_landing_page -->|"sign in"| node_supabase_client
node_auth_pages -->|"auth callback"| node_supabase_client
node_dashboard_page -->|"uses"| node_lesson_workspace
node_lesson_page -->|"renders"| node_lesson_workspace
node_lesson_workspace -->|"fetches data"| node_frontend_api
node_frontend_api -->|"requests"| node_api_main
node_supabase_client -->|"auth/session"| node_supabase
node_api_main -->|"protects"| node_auth_dep
node_auth_dep -->|"verifies token"| node_supabase
node_api_main -->|"mounts"| node_users_router
node_api_main -->|"mounts"| node_voices_router
node_api_main -->|"mounts"| node_lessons_router
node_api_main -->|"mounts"| node_health_router
node_users_router -->|"writes profiles"| node_supabase_service
node_voices_router -->|"clones voice"| node_elevenlabs_service
node_voices_router -->|"stores voice meta"| node_supabase_service
node_lessons_router -->|"reads lessons"| node_lesson_catalog
node_lessons_router -->|"renders audio"| node_lesson_audio_service
node_lesson_audio_service -->|"tts calls"| node_elevenlabs_service
node_lesson_audio_service -->|"stores metadata"| node_supabase_service
node_supabase_service -->|"data+storage"| node_supabase
node_elevenlabs_service -->|"api calls"| node_elevenlabs

click node_next_app "https://github.com/varunvekaria/languageai/tree/main/src/app"
click node_landing_page "https://github.com/varunvekaria/languageai/blob/main/src/app/page.tsx"
click node_auth_pages "https://github.com/varunvekaria/languageai/tree/main/src/app/auth"
click node_dashboard_page "https://github.com/varunvekaria/languageai/blob/main/src/app/dashboard/page.tsx"
click node_lesson_page "https://github.com/varunvekaria/languageai/blob/main/src/app/lesson/[lessonId]/page.tsx"
click node_lesson_workspace "https://github.com/varunvekaria/languageai/blob/main/src/components/LessonWorkspace.tsx"
click node_frontend_api "https://github.com/varunvekaria/languageai/blob/main/src/lib/api.ts"
click node_supabase_client "https://github.com/varunvekaria/languageai/blob/main/src/lib/supabase/client.ts"
click node_typed_api "https://github.com/varunvekaria/languageai/blob/main/src/types/api.ts"
click node_api_main "https://github.com/varunvekaria/languageai/blob/main/backend/app/main.py"
click node_auth_dep "https://github.com/varunvekaria/languageai/blob/main/backend/app/deps/auth.py"
click node_users_router "https://github.com/varunvekaria/languageai/blob/main/backend/app/routers/users.py"
click node_voices_router "https://github.com/varunvekaria/languageai/blob/main/backend/app/routers/voices.py"
click node_lessons_router "https://github.com/varunvekaria/languageai/blob/main/backend/app/routers/lessons.py"
click node_health_router "https://github.com/varunvekaria/languageai/blob/main/backend/app/routers/health.py"
click node_lesson_catalog "https://github.com/varunvekaria/languageai/blob/main/backend/app/data/lessons.py"
click node_lesson_audio_service "https://github.com/varunvekaria/languageai/blob/main/backend/app/services/lesson_audio.py"
click node_elevenlabs_service "https://github.com/varunvekaria/languageai/blob/main/backend/app/services/elevenlabs.py"
click node_supabase_service "https://github.com/varunvekaria/languageai/blob/main/backend/app/services/supabase.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_next_app,node_landing_page,node_auth_pages,node_dashboard_page,node_lesson_page,node_lesson_workspace,node_frontend_api,node_supabase_client,node_typed_api toneBlue
class node_api_main,node_auth_dep,node_users_router,node_voices_router,node_lessons_router,node_health_router,node_lesson_catalog,node_lesson_audio_service,node_elevenlabs_service,node_supabase_service toneAmber
class node_supabase,node_elevenlabs toneMint
```

## Components

### Frontend

Next.js app, client-side auth, and lesson UI

- [Next.js app](https://github.com/varunvekaria/languageai/tree/main/src/app): web frontend · App Router shell and pages for the browser experience · `src/app`
- [Landing](https://github.com/varunvekaria/languageai/blob/main/src/app/page.tsx): page · Public entry page · `src/app/page.tsx`
- [Auth pages](https://github.com/varunvekaria/languageai/tree/main/src/app/auth): pages · Login, callback, and reset-password routes · `src/app/auth`
- [Dashboard](https://github.com/varunvekaria/languageai/blob/main/src/app/dashboard/page.tsx): page · Post-login workspace entry · `src/app/dashboard/page.tsx`
- [Lesson page](https://github.com/varunvekaria/languageai/blob/main/src/app/lesson/[lessonId]/page.tsx): page · Lesson playback route · `src/app/lesson/[lessonId]/page.tsx`
- [Lesson workspace](https://github.com/varunvekaria/languageai/blob/main/src/components/LessonWorkspace.tsx): ui component · Main interactive lesson interface · `src/components/LessonWorkspace.tsx`
- [API client](https://github.com/varunvekaria/languageai/blob/main/src/lib/api.ts): client wrapper · Frontend wrapper for backend requests · `src/lib/api.ts`
- [Supabase client](https://github.com/varunvekaria/languageai/blob/main/src/lib/supabase/client.ts): auth client · Browser-side Supabase session and token access · `src/lib/supabase/client.ts`
- [API types](https://github.com/varunvekaria/languageai/blob/main/src/types/api.ts): types · Shared response types for backend calls · `src/types/api.ts`

### Backend

FastAPI API, orchestration, and persistence

- [API entry](https://github.com/varunvekaria/languageai/blob/main/backend/app/main.py): fastapi app · FastAPI application and route mounting · `backend/app/main.py`
- [Auth guard](https://github.com/varunvekaria/languageai/blob/main/backend/app/deps/auth.py): dependency · Validates Supabase bearer tokens and resolves user context · `backend/app/deps/auth.py`
- [Users router](https://github.com/varunvekaria/languageai/blob/main/backend/app/routers/users.py): api router · Onboarding and profile operations · `backend/app/routers/users.py`
- [Voices router](https://github.com/varunvekaria/languageai/blob/main/backend/app/routers/voices.py): api router · Voice cloning and voice listing endpoints · `backend/app/routers/voices.py`
- [Lessons router](https://github.com/varunvekaria/languageai/blob/main/backend/app/routers/lessons.py): api router · Lesson library and audio rendering endpoints · `backend/app/routers/lessons.py`
- [Health router](https://github.com/varunvekaria/languageai/blob/main/backend/app/routers/health.py): api router · Liveness endpoint · `backend/app/routers/health.py`
- [Lesson catalog](https://github.com/varunvekaria/languageai/blob/main/backend/app/data/lessons.py): content store · Static lesson library bundled with the app · `backend/app/data/lessons.py`
- [Audio pipeline](https://github.com/varunvekaria/languageai/blob/main/backend/app/services/lesson_audio.py): service · Orchestrates per-line TTS generation and playback asset creation · `backend/app/services/lesson_audio.py`
- [ElevenLabs adapter](https://github.com/varunvekaria/languageai/blob/main/backend/app/services/elevenlabs.py): service · Voice clone and text-to-speech integration · `backend/app/services/elevenlabs.py`
- [Supabase adapter](https://github.com/varunvekaria/languageai/blob/main/backend/app/services/supabase.py): service · Server-side Supabase client and data access boundary · `backend/app/services/supabase.py`

### External services

Third-party systems used for identity, storage, and speech

- **Supabase**: identity+data platform · Auth, database, and storage system of record
- **ElevenLabs**: speech service · Voice cloning and TTS provider

## Connections

- Landing → Supabase client: sign in · Starts auth/session flow
- Auth pages → Supabase client: auth callback · Completes login and token handling
- Dashboard → Lesson workspace: uses · Loads the main lesson UI
- Lesson page → Lesson workspace: renders · Embeds lesson playback workspace
- Lesson workspace → API client: fetches data · Loads lesson and audio state from backend
- API client → API entry: requests · Calls the FastAPI backend
- Supabase client → Supabase: auth/session · Uses public Supabase auth and token acquisition
- API entry → Auth guard: protects · Applies bearer-token auth to protected routes
- Auth guard → Supabase: verifies token · Validates Supabase access tokens
- API entry → Users router: mounts · Exposes onboarding/profile routes
- API entry → Voices router: mounts · Exposes voice routes
- API entry → Lessons router: mounts · Exposes lesson routes
- API entry → Health router: mounts · Exposes liveness checks
- Users router → Supabase adapter: writes profiles · Persists user onboarding state
- Voices router → ElevenLabs adapter: clones voice · Creates cloned voices through ElevenLabs
- Voices router → Supabase adapter: stores voice meta · Persists voice records in Supabase
- Lessons router → Lesson catalog: reads lessons · Serves the static lesson library
- Lessons router → Audio pipeline: renders audio · Generates lesson playback assets
- Audio pipeline → ElevenLabs adapter: tts calls · Generates per-line speech audio
- Audio pipeline → Supabase adapter: stores metadata · Writes generated audio records and signed-access state
- Supabase adapter → Supabase: data+storage · Uses service-role access for persistence and storage
- ElevenLabs adapter → ElevenLabs: api calls · Invokes cloning and TTS APIs
