How ai-maker-space/fastapi-llm-model-serving works
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