How dhananjaylab/memegpt works
MemeGPT is split into two primary applications plus background processing: a FastAPI backend, a React/Vite frontend, and an ARQ worker. The backend is the system boundary that owns auth, generation, persistence, storage, rate limiting, and integrations; the frontend is a thin SPA that drives user workflows and renders previews/editors.
The frontend is a TypeScript React app organized by page and component boundaries. Pages such as Home, Gallery, Dashboard, and Synthesize compose reusable UI pieces like the meme editor, template selector, preview, trending topics, share menu, and protected routes. The frontend talks to the backend through a single API client layer in src/lib/api.ts, and uses an auth context to manage logged-in state and gated routes.
The backend entrypoint is main.py, with shared setup in core/ for config, CORS, and middleware. Domain functionality is split into routers: auth, users, memes, ai, jobs, storage, trending, stripe, and health. This makes the backend’s external API surface map closely to product features rather than infrastructure concerns.
Data persistence is PostgreSQL-backed through SQLAlchemy models and Alembic migrations. The migrations show that the schema covers core application entities plus evolving features like API key hashing, trending scores, template support changes, and contract additions. This is the durable source of truth for users, templates, jobs, generated memes, and subscription-related state.
The main generation pipeline is event-driven. A “quick generate” request first checks caches, then either returns immediately on a hit or offloads the expensive work to the ARQ worker on a miss. The API responds with a job identifier, and the client listens to a Server-Sent Events job stream for progress and completion. This is the key throughput boundary between request/response traffic and heavier AI/image work.
The worker process in backend/workers/meme_worker.py is the async execution boundary. It handles the expensive parts of generation such as AI captioning, image composition, and storage upload. Supporting services like meme_ai, compositor, worker, and storage keep that pipeline isolated from the web request path.
Redis is used for two distinct concerns: caching/queueing and rate limiting. The README describes a cache-first flow for fast responses and a sliding-window limiter with tiered quotas. That means Redis sits on the critical path for both performance and abuse prevention, not just background jobs.
Storage is abstracted behind a service layer with Cloudflare R2 as the primary object store and the local filesystem as a fallback. The backend/public/frames and backend/public/fonts assets support local template rendering, while public/meme_data.json and the template loader/seed scripts suggest a hybrid of static template definitions and database-backed catalog data. Cleanup scripts exist for maintaining fallback storage and migrating content to R2.
Authentication and API key handling are security-sensitive subsystems. The backend includes dedicated auth and API key services, and the README calls out a “store-hash, show-once” policy for API keys. Stripe integration sits alongside this to support subscription tiers that influence access and rate limits.
Template management is its own domain. The load_templates.py and seed_templates.py scripts, together with the template_catalog service and meme routers, indicate a pipeline for loading curated meme templates into the database, serving them to the UI, and keeping local fallback assets available when needed. Trending is also a separate concern, exposed through its own router and service.
Operationally, the repo is container-first. There is a backend Dockerfile, environment variants for local/staging/production, and scripts for deployment and storage cleanup. The architecture assumes multiple cooperating services in deployment: frontend, FastAPI backend, Redis, PostgreSQL, and the ARQ worker, with external dependencies such as Google Gemini, Cloudflare R2, and Stripe plugged in through service adapters.
Main entry points: routers API routers · services Services · api.ts API client · App.tsx SPA
Components 27
Frontend
React/Vite SPA for meme creation, browsing, and authenticated user flows.
- SPA · Single-page application entry and routing shell.
frontend/src/App.tsx - Pages · Home, Gallery, Dashboard, and Synthesize screens.
frontend/src/pages - UI pieces · Reusable editors, previews, selectors, sharing, and guarded UI.
frontend/src/components - Auth state · Logged-in state and route gating.
frontend/src/context/AuthContext.tsx - API client · Single backend API access layer used by the SPA.
frontend/src/lib/api.ts - Canvas · Client-side meme rendering helpers.
frontend/src/lib/canvas.ts
Backend
FastAPI application plus data, services, and worker execution.
- FastAPI app · Backend startup, routing, and application wiring.
backend/main.py - Core setup · Shared configuration, CORS, and middleware.
backend/core - API routers · Feature-oriented REST/API endpoints.
backend/routers - Services · Auth, caching, rate limiting, storage, generation, and trends.
backend/services - Models · Persistent application entities and schemas.
backend/models/models.py - Migrations · Schema evolution for app entities and feature changes.
backend/db/migrations/versions - DB session · Database connection/session setup.
backend/db/session.py - ARQ worker · Background execution boundary for expensive generation jobs.
backend/workers/meme_worker.py - Templates · Template loading, seeding, and catalog management.
backend/services/template_catalog.py - Storage layer · Object storage abstraction, R2 config, local fallback, and cleanup.
backend/services/storage.py - Redis uses · Caching, job coordination, and sliding-window quota enforcement.
backend/services/cache.py - Trending · Trending score calculation and surfacing.
backend/services/trending.py - Auth & keys · User auth and store-hash API key handling.
backend/services/auth.py - Integrations · Stripe, AI model access, and CDN/R2 configuration adapters.
backend/services/meme_ai.py - Public assets · Fonts, frames, and meme template seed data used by template rendering.
backend/public/meme_data.json
External Systems
Third-party services and infrastructure the app integrates with.
- PostgreSQL · Primary relational persistence.
- Redis · Shared cache, queue, and rate-limit store.
- Cloudflare R2 · Primary object store for generated media.
- Gemini · Captioning/generation model integration.
- Stripe · Subscription and entitlement integration.
Connections 30
- SPA → Pages: composes · App routes render page views.
- SPA → UI pieces: uses · Pages assemble reusable UI components.
- SPA → Auth state: reads auth · App gates protected flows on auth state.
- Pages → API client: calls · Pages fetch data and trigger actions through the client.
- UI pieces → API client: calls · Interactive controls submit backend requests.
- UI pieces → Canvas: renders · Client-side preview/editor rendering helpers.
- Auth state → API client: authenticates · Auth context uses API-backed session state.
- API client → FastAPI app: HTTP · SPA talks to the backend API surface.
- FastAPI app → Core setup: boots · Application setup is centralized in core modules.
- FastAPI app → API routers: mounts · Main app exposes feature routers.
- API routers → Auth & keys: auth · Authentication and API key endpoints rely on auth services.
- API routers → Templates: templates · Template browsing and loading flows.
- API routers → Trending: trending · Trending endpoints expose ranking data.
- API routers → Storage layer: storage · Upload and asset-serving routes use storage services.
- API routers → Integrations: billing/ai · AI and Stripe-facing endpoints.
- API routers → Redis uses: jobs/rate · Job status and request throttling rely on Redis.
- Services → Redis: uses · Cache, queue, and rate limiting are Redis-backed.
- Services → Cloudflare R2: uploads · Primary object storage target.
- Services → Gemini: calls · AI captioning and generation provider.
- Services → Stripe: calls · Subscription and billing integration.
- Services → ARQ worker: queues jobs · Expensive generation work is offloaded to the worker.
- ARQ worker → Services: uses · Worker consumes shared generation, storage, and composition services.
- ARQ worker → Redis uses: pulls jobs · Worker coordination and job state use Redis.
- Models → PostgreSQL: persists · SQLAlchemy models map to PostgreSQL tables.
- DB session → PostgreSQL: connects · Database session binds to PostgreSQL.
- Migrations → PostgreSQL: evolves · Alembic migrations update the PostgreSQL schema.
- Templates → Public assets: loads · Template pipeline reads static assets and seed data.
- Templates → Models: stores · Curated templates are persisted in the database.
- Storage layer → Public assets: falls back · Local frames/fonts support offline or fallback rendering.
- Scripts → Storage layer: maintains · Cleanup and migration scripts operate on storage assets.