rmbg2 / CODEBASE_CONTEXT.md
Esmaill1
feat: Initialize Flask web application for AI background removal with UI, backend logic, Clerk authentication, and comprehensive project documentation.
680424a

RMBG-2 Studio - Codebase Context

1. Project Overview

RMBG-2 Studio is a web-based application for batch background removal using the BRIA-RMBG-2.0 AI model. It is designed for high performance, supporting GPU acceleration and easy deployment on platforms like Hugging Face Spaces.

2. Technology Stack

  • Backend: Python 3.10+, Flask
  • AI/ML: PyTorch, Hugging Face Transformers, birefnet (Model: cocktailpeanut/rm)
  • Frontend: HTML5, Vanilla JavaScript, CSS3
  • Authentication: Clerk (JWT Verification)
  • Deployment: Docker, Gunicorn

3. Directory Structure

/
├── app/
│   ├── flask_app.py       # MAIN ENTRY POINT: Flask server, ML logic, API routes.
│   ├── requirements.txt   # Python dependencies (pinned versions).
│   ├── static/            # Frontend assets
│   │   ├── app.js         # Client-side logic (upload queue, API calls).
│   │   └── style.css      # Application styling.
│   └── templates/
│       └── index.html     # Main UI template (Jinja2).
├── Dockerfile             # Deployment configuration (User setup, dependencies).
├── DEPLOYMENT.md          # Instructions for Hugging Face Spaces.
├── STANDALONE.md          # Instructions for local development.
└── output_images/         # (Generated) Stores processed images temporarily.

4. Key Files & Components

app/flask_app.py

The heart of the application.

  • Initialization: Loads the ML model (cocktailpeanut/rm) on startup, optimizing for CUDA (NVIDIA), MPS (Mac), or CPU.
  • Routing: Defines API endpoints for image processing and file management.
  • Security: Implements the @require_admin decorator to protect resource-intensive endpoints using Clerk JWTs.
  • Maintenance: Runs a background thread (cleanup_job) to delete processed images older than 3 hours.

Dockerfile

  • Optimized for Hugging Face Spaces.
  • Creates a non-root user user (UID 1000).
  • Installs system libs like libgl1 (required for OpenCV/Pillow operations if needed).
  • Exposes port 7860.

5. Configuration (Environment Variables)

The application relies on .env or system environment variables:

Variable Description Required
CLERK_PUBLISHABLE_KEY Public key for Clerk Frontend SDK. Yes
CLERK_SECRET_KEY Secret key (optional depending on verification depth). No
ADMIN_USER_IDS Comma-separated list of Clerk User IDs allowed to use the app. Yes
PORT Port to run the server on (default: 7860). No