# mateolafalce/topic2manim architecture

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

- Interactive diagram: https://gitdiagram.com/mateolafalce/topic2manim
- Repository: https://github.com/mateolafalce/topic2manim

## Overview

#### Purpose and Entry Points
Topic2Manim is a Flask-hosted pipeline that turns a user-entered educational topic into an approximately 60-second narrated Manim video. The HTTP application and orchestration entry point is `src/main.py`; the browser UI is served from `src/frontend/index.html` with behavior in `src/frontend/app.js`. It can run locally or in Docker via `Dockerfile` and `docker-compose.yml`.

#### Request and Orchestration Boundary
The frontend submits a topic to the Flask backend, which coordinates generation stages and returns/serves the resulting video. `src/main.py` is the system boundary between the web request lifecycle and long-running AI/media work. Environment-based API credentials and provider configuration are expected through `.env.example`.

#### LLM Script Planning
`src/animations.py` produces structured video-script JSON from the topic. It selects or configures LLM access with provider priority/fallback between Anthropic Claude and OpenAI GPT. The resulting script is the shared contract consumed independently by narration generation and Manim-code generation.

#### Narration and Timing
`src/tts_generator.py` converts script fragments into speech, concatenates them into a complete audio track, and exposes audio durations. Timing is a key cross-stage dependency: narration durations inform animation generation so visual scenes can fit their spoken sections.

#### Manim Scene Generation
`src/manim_generator.py` turns the script plus audio timing into generated Python/Manim scene files. This is the boundary from AI-generated textual plans/code to the local Manim Community Edition rendering runtime. Generated scenes are expected to contain mathematical and educational visualizations.

#### Video Rendering and Assembly
`src/concat_video.py` compiles generated Manim scene code into MP4 fragments, concatenates silent video fragments, then merges the assembled video with the final MP3 narration. Its output is the final playable video artifact; intermediate script, audio, source, and rendered-fragment artifacts connect the pipeline stages.

#### Presentation and Runtime Infrastructure
The static single-page interface resides in `src/frontend/index.html`, `src/frontend/app.js`, and `src/frontend/style.css`; example/output media is under `public/`. Python dependencies and runtime metadata are defined in `pyproject.toml`. Container execution is supported by `Dockerfile` and `docker-compose.yml`, while external dependencies are LLM APIs, a text-to-speech provider/runtime, and the local Manim rendering/media toolchain.

Main entry points: [`src/concat_video.py`](https://github.com/mateolafalce/topic2manim/blob/main/src/concat_video.py) (Render and assemble), [`src/tts_generator.py`](https://github.com/mateolafalce/topic2manim/blob/main/src/tts_generator.py) (Narration generator), [`src/main.py`](https://github.com/mateolafalce/topic2manim/blob/main/src/main.py) (Flask orchestrator), [`src/animations.py`](https://github.com/mateolafalce/topic2manim/blob/main/src/animations.py) (Script planner)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web interface"]
  node_browser["Browser UI<br/>single-page app<br/>[index.html]"]
  node_frontend_logic["Topic submission<br/>browser logic<br/>[app.js]"]
  node_flask_app{{"Flask orchestrator<br/>HTTP application<br/>[main.py]"}}
end

subgraph group_pipeline["Generation pipeline"]
  node_script_planner["Script planner<br/>LLM generation<br/>[animations.py]"]
  node_llm_apis(("Claude / GPT<br/>LLM providers"))
  node_script_json["Video script<br/>JSON contract"]
  node_tts["Narration generator<br/>text-to-speech<br/>[tts_generator.py]"]
  node_tts_provider(("TTS provider<br/>speech runtime"))
  node_narration_audio["Narration MP3<br/>audio artifact"]
  node_audio_timing["Audio timings<br/>timing metadata"]
  node_scene_generator["Scene generator<br/>Manim code generation<br/>[manim_generator.py]"]
  node_generated_scenes["Generated scenes<br/>Python scene files"]
end

subgraph group_runtime["Rendering runtime"]
  node_video_assembly["Render and assemble<br/>media pipeline<br/>[concat_video.py]"]
  node_manim_toolchain{{"Manim media toolchain<br/>local renderer"}}
  node_video_fragments["Rendered fragments<br/>MP4 artifacts"]
  node_final_video["Final video<br/>playable MP4"]
  node_container_runtime["Container runtime<br/>Docker deployment"]
end

node_browser -->|"runs"| node_frontend_logic
node_frontend_logic -->|"submits topic"| node_flask_app
node_flask_app -->|"orchestrates"| node_script_planner
node_script_planner -->|"plans with"| node_llm_apis
node_script_planner -->|"produces"| node_script_json
node_script_json -->|"narration text"| node_tts
node_tts -->|"synthesizes via"| node_tts_provider
node_tts -->|"concatenates"| node_narration_audio
node_tts -->|"measures"| node_audio_timing
node_script_json -->|"scene plan"| node_scene_generator
node_audio_timing -->|"fits timing"| node_scene_generator
node_scene_generator -->|"writes"| node_generated_scenes
node_generated_scenes -->|"renders"| node_video_assembly
node_video_assembly -->|"invokes"| node_manim_toolchain
node_manim_toolchain -->|"compiles"| node_video_fragments
node_video_fragments -->|"concatenates"| node_video_assembly
node_narration_audio -->|"merges"| node_video_assembly
node_video_assembly -->|"outputs"| node_final_video
node_flask_app -->|"serves"| node_final_video
node_container_runtime -.->|"runs"| node_flask_app

click node_browser "https://github.com/mateolafalce/topic2manim/blob/main/src/frontend/index.html"
click node_frontend_logic "https://github.com/mateolafalce/topic2manim/blob/main/src/frontend/app.js"
click node_flask_app "https://github.com/mateolafalce/topic2manim/blob/main/src/main.py"
click node_script_planner "https://github.com/mateolafalce/topic2manim/blob/main/src/animations.py"
click node_tts "https://github.com/mateolafalce/topic2manim/blob/main/src/tts_generator.py"
click node_scene_generator "https://github.com/mateolafalce/topic2manim/blob/main/src/manim_generator.py"
click node_video_assembly "https://github.com/mateolafalce/topic2manim/blob/main/src/concat_video.py"
click node_final_video "https://github.com/mateolafalce/topic2manim/tree/main/public"
click node_container_runtime "https://github.com/mateolafalce/topic2manim/blob/main/Dockerfile"

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_browser,node_frontend_logic,node_flask_app toneBlue
class node_script_planner,node_llm_apis,node_script_json,node_tts,node_tts_provider,node_narration_audio,node_audio_timing,node_scene_generator,node_generated_scenes toneAmber
class node_video_assembly,node_manim_toolchain,node_video_fragments,node_final_video,node_container_runtime toneMint
```

## Components

### Web interface

- [Browser UI](https://github.com/mateolafalce/topic2manim/blob/main/src/frontend/index.html): single-page app · `src/frontend/index.html`
- [Topic submission](https://github.com/mateolafalce/topic2manim/blob/main/src/frontend/app.js): browser logic · `src/frontend/app.js`
- [Flask orchestrator](https://github.com/mateolafalce/topic2manim/blob/main/src/main.py): HTTP application · Coordinates request-scoped generation and serves the resulting video. · `src/main.py`

### Generation pipeline

- [Script planner](https://github.com/mateolafalce/topic2manim/blob/main/src/animations.py): LLM generation · `src/animations.py`
- **Claude / GPT**: LLM providers · Provider selection supports priority and fallback.
- **Video script**: JSON contract · Shared input for narration and scene generation.
- [Narration generator](https://github.com/mateolafalce/topic2manim/blob/main/src/tts_generator.py): text-to-speech · Builds complete narration and exposes section durations. · `src/tts_generator.py`
- **TTS provider**: speech runtime
- **Narration MP3**: audio artifact
- **Audio timings**: timing metadata
- [Scene generator](https://github.com/mateolafalce/topic2manim/blob/main/src/manim_generator.py): Manim code generation · Produces Python scenes fitted to spoken sections. · `src/manim_generator.py`
- **Generated scenes**: Python scene files

### Rendering runtime

- [Render and assemble](https://github.com/mateolafalce/topic2manim/blob/main/src/concat_video.py): media pipeline · Renders fragments, concatenates video, and merges narration. · `src/concat_video.py`
- **Manim media toolchain**: local renderer · Manim Community Edition and media tooling compile scene code.
- **Rendered fragments**: MP4 artifacts
- [Final video](https://github.com/mateolafalce/topic2manim/tree/main/public): playable MP4 · `public`
- [Container runtime](https://github.com/mateolafalce/topic2manim/blob/main/Dockerfile): Docker deployment · `Dockerfile`

## Connections

- Browser UI → Topic submission: runs
- Topic submission → Flask orchestrator: submits topic
- Flask orchestrator → Script planner: orchestrates
- Script planner → Claude / GPT: plans with
- Script planner → Video script: produces
- Video script → Narration generator: narration text
- Narration generator → TTS provider: synthesizes via
- Narration generator → Narration MP3: concatenates
- Narration generator → Audio timings: measures
- Video script → Scene generator: scene plan
- Audio timings → Scene generator: fits timing
- Scene generator → Generated scenes: writes
- Generated scenes → Render and assemble: renders
- Render and assemble → Manim media toolchain: invokes
- Manim media toolchain → Rendered fragments: compiles
- Rendered fragments → Render and assemble: concatenates
- Narration MP3 → Render and assemble: merges
- Render and assemble → Final video: outputs
- Flask orchestrator → Final video: serves
- Container runtime → Flask orchestrator: runs
