# ai-maker-space/fastapi-llm-model-serving architecture

> Architecture diagram of the GitHub repository ai-maker-space/fastapi-llm-model-serving, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ai-maker-space/fastapi-llm-model-serving
- Repository: https://github.com/ai-maker-space/fastapi-llm-model-serving

## Overview

##### Project type and purpose
- This is a GPU-backed inference service exposing HTTP endpoints via FastAPI (`app.py`), offloading work to Celery workers (`celery_worker.py`) using Redis as broker (and optionally result backend). It’s containerized with Docker & orchestrated via Docker Compose, leveraging NVIDIA’s container toolkit for GPU access.

##### File structure
- Top level:
- Dockerfile / docker-compose.yml → container definitions for API, worker, Redis
- app.py → FastAPI application, defines REST endpoints & enqueues tasks
- celery_worker.py → Celery worker entrypoint, pulls tasks from Redis
- model_loader.py → logic to load ML model onto GPU
- redis_server.py → helper to launch or configure Redis (for local/dev)
- utils.py → shared utilities (serialization, logging)
- run.py → script to start the API (invokes Uvicorn, sets up GPU context)
- requirements.txt / .env.sample → dependencies and env vars

##### README
- Confirms need for GPU ≥16 GB, Docker Compose plugin (not legacy), NVIDIA container toolkit.

##### Main components & their interactions
a. External Client / API Consumer
- Users or other services send HTTP requests (e.g., POST /infer)
b. FastAPI Service (app.py + run.py)
- Listens on port 80/8000, validates input, enqueues inference tasks via Celery
c. Redis Server
- Serves as Celery broker (and optionally result backend)
d. Celery Worker(s) (celery_worker.py)
- Subscribes to Redis, pulls tasks, calls model_loader and ML inference
e. Model Loader & GPU
- model_loader.py initializes model on GPU memory; each worker performs inference
f. Result Propagation
- Worker pushes result back to Redis; API either polls or uses Celery’s result backend to retrieve and return to client
g. Docker & Docker Compose
- Defines and links containers, injects NVIDIA runtime, networks them

##### Architectural patterns & design principles
- Asynchronous task queue (decoupling HTTP request/response from heavy GPU workloads)
- Containerization for environment consistency and GPU isolation
- Separation of concerns: web layer vs. worker layer vs. infrastructure
- Scalability: multiple API replicas behind a load balancer, multiple worker replicas for throughput

## Diagram (Mermaid)

```mermaid
graph TD
    %% External client
    Client["External Client"]:::external

    %% Docker host boundary
    subgraph "Docker Host / NVIDIA Toolkit"
        Host["Docker Host\nNVIDIA Container Toolkit"]:::infra

        subgraph "Docker Compose"
            %% API service
            subgraph "FastAPI Service"
                App["app.py"]:::service
                Run["run.py"]:::service
            end

            %% Redis broker
            Redis["Redis Broker"]:::queue
            RS["redis_server.py"]:::service

            %% Worker service
            subgraph "Celery Worker"
                Worker["celery_worker.py"]:::service
            end

            %% Compute layer
            Model["model_loader.py"]:::compute

            %% Shared utilities
            Utils["utils.py"]:::service
        end
    end

    %% Configuration files
    subgraph "Configuration"
        DockerfileNode["Dockerfile"]:::config
        ComposeNode["docker-compose.yml"]:::config
        EnvNode[".env.sample"]:::config
        ReqNode["requirements.txt"]:::config
        ReadmeNode["README.md"]:::config
    end

    %% Data flows
    Client -->|"JSON over HTTP"| App
    App -->|"AMQP via Redis (enqueue)"| Redis
    Redis -->|"AMQP (deliver task)"| Worker
    Worker -->|"calls inference()"| Model
    Model -->|"returns tensor result"| Worker
    Worker -->|"AMQP via Redis (store result)"| Redis
    App -->|"AMQP via Redis (fetch result)"| Redis
    Redis -->|"AMQP (result delivery)"| App
    App -->|"JSON Response"| Client

    %% Click events
    click App "https://github.com/ai-maker-space/fastapi-llm-model-serving/blob/main/app.py"
    click Run "https://github.com/ai-maker-space/fastapi-llm-model-serving/blob/main/run.py"
    click Worker "https://github.com/ai-maker-space/fastapi-llm-model-serving/blob/main/celery_worker.py"
    click Model "https://github.com/ai-maker-space/fastapi-llm-model-serving/blob/main/model_loader.py"
    click RS "https://github.com/ai-maker-space/fastapi-llm-model-serving/blob/main/redis_server.py"
    click Utils "https://github.com/ai-maker-space/fastapi-llm-model-serving/blob/main/utils.py"
    click DockerfileNode "https://github.com/ai-maker-space/fastapi-llm-model-serving/tree/main/Dockerfile"
    click ComposeNode "https://github.com/ai-maker-space/fastapi-llm-model-serving/blob/main/docker-compose.yml"
    click EnvNode "https://github.com/ai-maker-space/fastapi-llm-model-serving/blob/main/.env.sample"
    click ReqNode "https://github.com/ai-maker-space/fastapi-llm-model-serving/blob/main/requirements.txt"
    click ReadmeNode "https://github.com/ai-maker-space/fastapi-llm-model-serving/blob/main/README.md"

    %% Styles
    classDef external fill:#f2f2f2,stroke:#333,stroke-width:1px
    classDef service fill:#cfe2f3,stroke:#055160,stroke-width:1px
    classDef queue fill:#f9d29d,stroke:#7f6000,stroke-width:1px
    classDef compute fill:#d4edda,stroke:#1e4620,stroke-width:1px
    classDef infra fill:#e0e0e0,stroke:#555,stroke-width:1px
    classDef config fill:#efefef,stroke:#888,stroke-dasharray: 2 2
```
