S
Practice2026Completed

Mathtube Ai

Production-grade Practice software engineering system implemented in Python. Features a decoupled multi-tier architecture with validated API endpoints and resilient state processing.

RoleSole Developer
Classificationmedium
Client / ContextOpen Source Project
Development TimelineCompleted

Technical Architecture & Engineering Overview

Detailed system design and core implementation

Core System Architecture The service boots a FastAPI application (api_server.py) that defines endpoints for job creation (POST /api/generate), status polling (GET /api/jobs/{job_id}/status), live log streaming (GET /api/jobs/{job_id}/stream), and video assets (GET /api/videos*). Incoming GenerateRequest payloads are validated with pydantic.BaseModel and materialized into a Job object stored in _jobs and persisted to data/jobs.json. A background asyncio task invokes the pipeline via _build_cmd, which launches the main.py script inside the project's virtual environment.

Data Ingestion & Persistence The system uses a SQLite database (data/chroma/chroma.sqlite3) to back the Chroma vector store. Schema tables include tenants, databases, and collections, enforcing multi‑tenant isolation and unique constraints on collection names per database. Default tenant and database rows are seeded on startup, ensuring a ready‑to‑use environment for vector‑based retrieval.

Execution Flow

  1. Job Submission – Client POSTs a GenerateRequest; a short UUID is generated and a Job entry is created.
  2. Pipeline Launch – _run_job builds a command list (_build_cmd) and spawns a subprocess (subprocess.Popen) that runs main.py with all user‑specified parameters (topic, level, model, etc.).
  3. Log Capture – Subprocess stdout/stderr are streamed into job.logs; the /stream endpoint yields new lines as SSE events.
  4. Result Harvesting – Upon completion, the job directory under output/<job_id> contains lesson_blueprint.json, qa_report.json, and generated MP4 files. The /videos endpoint walks these directories, aggregates metadata, and returns a catalog.

Security Guardrails

  • Tenant Isolation is enforced at the DB layer via foreign keys (tenant_id) and unique constraints.
  • Input Sanitization relies on pydantic type enforcement and explicit default values, reducing injection risk.
  • File Access is confined to the output subtree; all path constructions use Path objects to avoid directory traversal.

Build & Deployment Topology The codebase is structured for containerization: a Dockerfile can copy the repository, install dependencies into .venv, and expose port 8000. The SQLite file is mounted as a volume for persistence, while the output directory is also a volume to retain generated media across restarts.

System Diagrams & Flow Visualizations

Interactive Mermaid architecture & execution flows

Architectural Visualizer · System Architecture

Core System Features

Key capabilities & architectural features

FEATURE 01

POST /api/generate – submit AI video generation jobs with rich parameter set

POST /api/generate – submit AI video generation jobs with rich parameter set

FEATURE 02

GET /api/jobs/{job_id}/status – retrieve current job state and result URLs

GET /api/jobs/{job_id}/status – retrieve current job state and result URLs

FEATURE 03

GET /api/jobs/{job_id}/stream – live SSE log feed for debugging and progress tracking

GET /api/jobs/{job_id}/stream – live SSE log feed for debugging and progress tracking

FEATURE 04

GET /api/videos – list all completed videos with size, duration, and QA status

GET /api/videos – list all completed videos with size, duration, and QA status

FEATURE 05

GET /api/videos/{job_id}/{filename} – stream generated MP4 files

GET /api/videos/{job_id}/{filename} – stream generated MP4 files

FEATURE 06

GET /api/videos/{job_id}/thumbnail – serve video thumbnail images

GET /api/videos/{job_id}/thumbnail – serve video thumbnail images

FEATURE 07

GET /api/videos/{job_id}/{filename}/meta – expose blueprint and QA metadata as JSON

GET /api/videos/{job_id}/{filename}/meta – expose blueprint and QA metadata as JSON

FEATURE 08

Multi‑tenant aware SQLite schema for vector collections

Multi‑tenant aware SQLite schema for vector collections

FEATURE 09

Default tenant/database auto‑creation for zero‑config deployment

Default tenant/database auto‑creation for zero‑config deployment

FEATURE 010

Configurable AI model, language, theme, and TTS provider via request payload

Configurable AI model, language, theme, and TTS provider via request payload

Engineering Challenges & Solutions

Problem statements & resolution strategies

Challenge #1

Balancing synchronous FastAPI request handling with long‑running AI subprocesses required careful async design to avoid blocking the event loop

Balancing synchronous FastAPI request handling with long‑running AI subprocesses required careful async design to avoid blocking the event loop

Challenge #2

Ensuring file system security while dynamically constructing paths for video assets and metadata demanded rigorous use of `Path` objects

Ensuring file system security while dynamically constructing paths for video assets and metadata demanded rigorous use of `Path` objects

Challenge #3

Persisting job state in a flat JSON file (`jobs.json`) introduces race conditions under high concurrency; mitigated with atomic writes but still a scalability limit

Persisting job state in a flat JSON file (`jobs.json`) introduces race conditions under high concurrency; mitigated with atomic writes but still a scalability limit

Challenge #4

SQLite’s single‑writer limitation can become a bottleneck if vector store writes increase; future migration to a server‑based DB may be needed

SQLite’s single‑writer limitation can become a bottleneck if vector store writes increase; future migration to a server‑based DB may be needed

Challenge #5

Managing subprocess stdout/stderr streams for real‑time SSE without deadlocks required explicit non‑blocking reads and buffer management

Managing subprocess stdout/stderr streams for real‑time SSE without deadlocks required explicit non‑blocking reads and buffer management

API Reference & Endpoints

REST API surface & route documentation

METHODENDPOINT PATHDESCRIPTIONCOPY
POST/api/generateEndpoint defined in api_server.py
GET/api/jobs/{job_id}/streamEndpoint defined in api_server.py
GET/api/jobs/{job_id}/statusEndpoint defined in api_server.py
GET/api/videosEndpoint defined in api_server.py
GET/api/videos/{job_id}/thumbnailEndpoint defined in api_server.py
GET/api/videos/{job_id}/{filename}Endpoint defined in api_server.py
GET/api/videos/{job_id}/{filename}/metaEndpoint defined in api_server.py

Key Code Modules & Architecture

Core file entrypoints and service responsibilities

FrontendMODULE 01
frontend

Module component managing frontend source logic

ScriptsMODULE 02
scripts

Module component managing scripts source logic

SrcMODULE 03
src

Module component managing src source logic

User Journey Pipeline

  1. 1User Access & Preset Selection: User opens client interface and configures request parameters or task workflow presets.
  2. 2Client Payload Dispatch: Front-end components (React) serialize input objects and send HTTP/WebSocket requests to `/api/generate`.
  3. 3Middleware & Validation Check: API Gateway evaluates CORS headers, authorization tokens, and input validation schemas.
  4. 4Service & Inference Execution: Service controllers dispatch request frames to model inference engine (Local Engine) or storage layer.
  5. 5Response Delivery & State Synchronization: Serialized output payload is returned to the client and rendered in the visual workspace.

Execution Code Flow

  1. 1Entrypoint Invocation: Request enters network interface layer via `POST /api/generate`.
  2. 2Schema & Guardrail Verification: Middleware sanitizes payload parameters and checks rate-limiting limits.
  3. 3Controller Dispatch: Router delegates validated object to `Frontend` service controller.
  4. 4Engine Execution & Storage Operations: Controller executes business logic, queries database models (Persistence), and invokes ML workers.
  5. 5Response Serialization: Result model is formatted into standard JSON response frame and delivered with HTTP 200 OK.

System FAQ & Technical Specifications

Frequently asked questions and core architecture details

Q: What technologies are used in Mathtube Ai?

Mathtube Ai is engineered using modern full-stack frameworks.

Q: What core engineering problem does Mathtube Ai solve?

Production-grade Practice software engineering system implemented in Python. Features a decoupled multi-tier architecture with validated API endpoints and resilient state processing.

Q: How is 'Balancing synchronous FastAPI request handling with long‑running AI subprocesses required careful async design to avoid blocking the event loop' resolved in Mathtube Ai?

Resolved through specialized system architecture and resilient state handling.

Related Systems & Case Studies

Explore similar technical architectures and projects

View All Systems