# diepdo1810/moneyprinterturbo architecture

> Architecture diagram of the GitHub repository diepdo1810/moneyprinterturbo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/diepdo1810/moneyprinterturbo
- Repository: https://github.com/diepdo1810/moneyprinterturbo

## Overview

##### Project Type and Purpose
- MoneyPrinterTurbo is a full-stack, MVC-architected application for end-to-end automatic short-video generation.
- It exposes:
- A RESTful API (FastAPI) for programmatic access.
- A Streamlit-based Web UI for interactive use.
- Core features include AI-powered copywriting (LLM), video and subtitle synthesis, voice-over, background music, and final video assembly.

##### File Structure Highlights
- Top-level:
- Dockerfile & docker-compose.yml → containerized deployment
- main.py → FastAPI app entrypoint
- webui/ → Streamlit front-end
- app/ → MVC layers (controllers, services, models, router, config)
- resource/ → static assets (fonts, songs, HTML)
- test/ → unit tests for services
- app/controllers/manager → pluggable state managers (in-memory, Redis)
- app/services → business logic: llm, video, subtitle, voice, material selection
- config.example.toml → central configuration for keys, ffmpeg/ImageMagick paths, model providers
- .streamlit/config.toml → Streamlit UI settings

##### Key Architectural Components
A. Frontend (Streamlit UI)
- Runs as a separate process (webui/Main.py)
- Interacts with backend API over HTTP
- Serves index.html and static assets from resource/public
B. API Backend
- FastAPI application (main.py → app/asgi.py → app/router.py)
- Controllers (app/controllers/) handle endpoints: ping, v1/llm, v1/video
- Manager layer abstracts state caches (memory or Redis)
- Services implement core logic:
- LLM calls (OpenAI, Moonshot, DeepSeek, etc.)
- Video/material selection (Pexels integration or local assets)
- Subtitle generation (edge or whisper)
- Voice synthesis (Azure, gpt4free, etc.)
- Video composition using ffmpeg and ImageMagick
C. Resource Store
- Fonts for subtitles (resource/fonts)
- Music files (resource/songs)
- Public HTML template (resource/public/index.html)
D. External Dependencies
- LLM API providers
- Pexels API for video clips
- ImageMagick & ffmpeg binaries
- Optional Redis instance for distributed state

##### Relationships & Data Flow
1. User interacts with Streamlit UI → submits topic/keywords
2. UI issues HTTP request to FastAPI endpoint (e.g., /v1/video)
3. Controller validates input, selects manager (memory/Redis)
4. Service layer:
a. Generates script via LLM service
b. Fetches video clips (Pexels or local)
c. Synthesizes voiceover
d. Generates subtitles
e. Composes video segments with ffmpeg/ImageMagick
5. Final video stored locally and URL returned to UI or client
6. Optionally, state (progress, caching) persisted in Redis

##### Architectural Patterns & Design Principles
- MVC Separation: controllers (API layer), services (business logic), models (data schemas/exceptions)
- Manager Pattern for caching/state (memory or Redis swap-in)
- Configuration-driven design (config.toml)
- Modular, pluggable LLM and subtitle providers
- Containerized deployment via Docker Compose

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User and Deployment
    User["User / Browser"]:::external
    subgraph "Deployment (Docker Compose)"
        direction TB
        UIContainer["Streamlit UI Container\n(Port 8501)"]:::frontend
        APIContainer["FastAPI Container\n(Port 8080)"]:::backend
        RedisContainer[(Redis Cache)]:::datastore
    end

    %% Frontend Zone
    subgraph "Frontend Zone"
        direction TB
        UIProcess["Streamlit UI Process"]:::frontend
        UIProcess -->|"HTTP 8501"| APIContainer
        UIConfig["Streamlit Config"]:::frontend
        UII18n["i18n Resources"]:::frontend
        UIPublic["Public HTML\nTemplate"]:::frontend
    end

    %% Backend Zone
    subgraph "Backend Zone"
        direction TB
        subgraph "API Gateway"
            direction TB
            APIMain["main.py"]:::backend
            ASGI["app/asgi.py"]:::backend
            Router["app/router.py"]:::backend
            APIMain --> ASGI --> Router
        end

        subgraph "Controller Layer"
            direction TB
            PingCtl["ping.py"]:::backend
            LLMCtl["v1/llm.py"]:::backend
            VideoCtl["v1/video.py"]:::backend
            Router --> PingCtl
            Router --> LLMCtl
            Router --> VideoCtl
        end

        subgraph "Manager Layer"
            direction TB
            BaseMgr["base_manager.py"]:::backend
            MemMgr["memory_manager.py"]:::backend
            RedisMgr["redis_manager.py"]:::backend
            PingCtl --> BaseMgr
            LLMCtl --> BaseMgr
            VideoCtl --> BaseMgr
            BaseMgr --> MemMgr
            BaseMgr --> RedisMgr
        end

        subgraph "Service Layer"
            direction TB
            LLMService["llm.py"]:::backend
            MaterialService["material.py"]:::backend
            SubtitleService["subtitle.py"]:::backend
            VoiceService["voice.py"]:::backend
            VideoService["video.py"]:::backend
            StateService["state.py"]:::backend
            TaskService["task.py"]:::backend
            EffectsUtil["video_effects.py"]:::backend
            LLMCtl --> LLMService
            VideoCtl --> MaterialService
            VideoCtl --> SubtitleService
            VideoCtl --> VoiceService
            VideoCtl --> VideoService
            VideoCtl --> TaskService
            Any --> EffectsUtil
        end

        subgraph "Models & Utils"
            direction TB
            Schema["schema.py"]:::backend
            Consts["const.py"]:::backend
            Exceptions["exception.py"]:::backend
            Utils["utils.py"]:::backend
        end

        subgraph "Config & Deployment Files"
            direction TB
            AppConfig["app/config/config.py"]:::backend
            ExampleConfig["config.example.toml"]:::backend
            Dockerfile["Dockerfile"]:::backend
            ComposeFile["docker-compose.yml"]:::backend
        end

        subgraph "Resource Store"
            direction TB
            Fonts[(Fonts)]:::datastore
            Songs[(Music Files)]:::datastore
            PublicHTML[(Public HTML)]:::datastore
        end

        %% Internal Flows
        Router --> BaseMgr
        BaseMgr --> StateService
        StateService --> RedisContainer
        MaterialService -->|"Pexels API"| Pexels[("Pexels API")]:::external
        LLMService -->|"LLM APIs"| LLMAPIs[("OpenAI/Moonshot/...")]:::external
        VideoService -->|"ffmpeg & ImageMagick"| FFMPEG[("FFmpeg/ImageMagick")]:::external
        PublicHTML --> UIContainer
    end

    %% Flow of Operations
    User --> UIContainer
    UIContainer --> UIProcess
    UIProcess -->|"Submit Topic"| UIContainer
    UIContainer -->|"HTTP Request"| APIMain
    APIMain --> Router
    Router --> VideoCtl
    VideoCtl --> LLMService
    VideoCtl --> MaterialService
    VideoCtl --> SubtitleService
    VideoCtl --> VoiceService
    VideoCtl --> VideoService
    VideoService --> PublicHTML

    %% Click Events
    click UIProcess "https://github.com/diepdo1810/moneyprinterturbo/blob/main/webui/Main.py"
    click UIConfig "https://github.com/diepdo1810/moneyprinterturbo/blob/main/webui/.streamlit/config.toml"
    click UII18n "https://github.com/diepdo1810/moneyprinterturbo/tree/main/webui/i18n/"
    click UIPublic "https://github.com/diepdo1810/moneyprinterturbo/blob/main/resource/public/index.html"
    click APIMain "https://github.com/diepdo1810/moneyprinterturbo/blob/main/main.py"
    click ASGI "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/asgi.py"
    click Router "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/router.py"
    click PingCtl "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/controllers/ping.py"
    click LLMCtl "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/controllers/v1/llm.py"
    click VideoCtl "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/controllers/v1/video.py"
    click BaseMgr "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/controllers/manager/base_manager.py"
    click MemMgr "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/controllers/manager/memory_manager.py"
    click RedisMgr "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/controllers/manager/redis_manager.py"
    click LLMService "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/services/llm.py"
    click MaterialService "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/services/material.py"
    click SubtitleService "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/services/subtitle.py"
    click VoiceService "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/services/voice.py"
    click VideoService "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/services/video.py"
    click StateService "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/services/state.py"
    click TaskService "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/services/task.py"
    click EffectsUtil "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/services/utils/video_effects.py"
    click Schema "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/models/schema.py"
    click Consts "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/models/const.py"
    click Exceptions "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/models/exception.py"
    click Utils "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/utils/utils.py"
    click AppConfig "https://github.com/diepdo1810/moneyprinterturbo/blob/main/app/config/config.py"
    click ExampleConfig "https://github.com/diepdo1810/moneyprinterturbo/blob/main/config.example.toml"
    click Dockerfile "https://github.com/diepdo1810/moneyprinterturbo/tree/main/Dockerfile"
    click ComposeFile "https://github.com/diepdo1810/moneyprinterturbo/blob/main/docker-compose.yml"
    click Fonts "https://github.com/diepdo1810/moneyprinterturbo/tree/main/resource/fonts/"
    click Songs "https://github.com/diepdo1810/moneyprinterturbo/tree/main/resource/songs/"
    click PublicHTML "https://github.com/diepdo1810/moneyprinterturbo/blob/main/resource/public/index.html"
    click TestTask "https://github.com/diepdo1810/moneyprinterturbo/blob/main/test/services/test_task.py"
    click TestVideo "https://github.com/diepdo1810/moneyprinterturbo/blob/main/test/services/test_video.py"
    click TestVoice "https://github.com/diepdo1810/moneyprinterturbo/blob/main/test/services/test_voice.py"

    %% Styles
    classDef frontend fill:#D6EAF8,stroke:#1F618D,color:#1B4F72;
    classDef backend fill:#D5F5E3,stroke:#239B56,color:#1D8348;
    classDef external fill:#F2F3F4,stroke:#566573,color:#424949,shape:cloud;
    classDef datastore fill:#FDEBD0,stroke:#D35400,color:#BA4A00,shape:cylinder;
```
