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.
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
- Job Submission – Client POSTs a
GenerateRequest; a short UUID is generated and aJobentry is created. - Pipeline Launch –
_run_jobbuilds a command list (_build_cmd) and spawns a subprocess (subprocess.Popen) that runsmain.pywith all user‑specified parameters (topic, level, model, etc.). - Log Capture – Subprocess stdout/stderr are streamed into
job.logs; the/streamendpoint yields new lines as SSE events. - Result Harvesting – Upon completion, the job directory under
output/<job_id>containslesson_blueprint.json,qa_report.json, and generated MP4 files. The/videosendpoint 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
pydantictype enforcement and explicit default values, reducing injection risk. - File Access is confined to the
outputsubtree; all path constructions usePathobjects 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
Core System Features
Key capabilities & architectural features
POST /api/generate – submit AI video generation jobs with rich parameter set
POST /api/generate – submit AI video generation jobs with rich parameter set
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
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
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
GET /api/videos/{job_id}/{filename} – stream generated MP4 files
GET /api/videos/{job_id}/{filename} – stream generated MP4 files
GET /api/videos/{job_id}/thumbnail – serve video thumbnail images
GET /api/videos/{job_id}/thumbnail – serve video thumbnail images
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
Multi‑tenant aware SQLite schema for vector collections
Multi‑tenant aware SQLite schema for vector collections
Default tenant/database auto‑creation for zero‑config deployment
Default tenant/database auto‑creation for zero‑config deployment
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
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
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
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
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
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
| METHOD | ENDPOINT PATH | DESCRIPTION | COPY |
|---|---|---|---|
| POST | /api/generate | Endpoint defined in api_server.py | |
| GET | /api/jobs/{job_id}/stream | Endpoint defined in api_server.py | |
| GET | /api/jobs/{job_id}/status | Endpoint defined in api_server.py | |
| GET | /api/videos | Endpoint defined in api_server.py | |
| GET | /api/videos/{job_id}/thumbnail | Endpoint defined in api_server.py | |
| GET | /api/videos/{job_id}/{filename} | Endpoint defined in api_server.py | |
| GET | /api/videos/{job_id}/{filename}/meta | Endpoint defined in api_server.py |
Key Code Modules & Architecture
Core file entrypoints and service responsibilities
frontendModule component managing frontend source logic
scriptsModule component managing scripts source logic
srcModule component managing src source logic
User Journey Pipeline
- 1User Access & Preset Selection: User opens client interface and configures request parameters or task workflow presets.
- 2Client Payload Dispatch: Front-end components (React) serialize input objects and send HTTP/WebSocket requests to `/api/generate`.
- 3Middleware & Validation Check: API Gateway evaluates CORS headers, authorization tokens, and input validation schemas.
- 4Service & Inference Execution: Service controllers dispatch request frames to model inference engine (Local Engine) or storage layer.
- 5Response Delivery & State Synchronization: Serialized output payload is returned to the client and rendered in the visual workspace.
Execution Code Flow
- 1Entrypoint Invocation: Request enters network interface layer via `POST /api/generate`.
- 2Schema & Guardrail Verification: Middleware sanitizes payload parameters and checks rate-limiting limits.
- 3Controller Dispatch: Router delegates validated object to `Frontend` service controller.
- 4Engine Execution & Storage Operations: Controller executes business logic, queries database models (Persistence), and invokes ML workers.
- 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