Implement FastAPI backend and /api/clean endpoint in app.py #3

Closed
opened 2026-09-10 23:01:09 +02:00 by tilltheend · 1 comment
Owner

Parent

Parent Map: #1 (Garment Photo Cleaner Web App [Map])

Blocked by

Blocked by: "Decouple core photo cleaning logic for CLI and API reuse"

Question

What is the exact interface and behavior of the POST /api/clean endpoint in app.py (supporting multipart image upload, prompt, optional model, optional API key override), and how does it serve static assets and stream/return base64 or binary cleaned image data with cost/resolution metadata?

## Parent Parent Map: #1 (Garment Photo Cleaner Web App [Map]) ## Blocked by Blocked by: "Decouple core photo cleaning logic for CLI and API reuse" ## Question What is the exact interface and behavior of the `POST /api/clean` endpoint in `app.py` (supporting multipart image upload, prompt, optional model, optional API key override), and how does it serve static assets and stream/return base64 or binary cleaned image data with cost/resolution metadata?
tilltheend added the
wayfinder:task
label 2026-09-10 23:01:10 +02:00
tilltheend self-assigned this 2026-09-10 23:21:23 +02:00
Author
Owner

Resolution

Implemented the FastAPI backend and /api/clean endpoint in app.py.

Architectural decisions and API interface

  1. Endpoint signature: POST /api/clean accepts multipart/form-data with:
    • file: UploadFile (required): uploaded garment image.
    • prompt: str (optional): custom prompt or preset description. Defaults to beige preset.
    • model: str (optional): OpenRouter model slug, defaults to meta/muse-image.
    • api_key: str | None (optional): API key override, falling back to OPENROUTER_API_KEY in environment.
    • restore_res: bool (optional, default True): toggles Lanczos upscaling back to source photo dimensions.
  2. Response format: JSON response containing:
    • image: Base64 data URI (data:<mime>;base64,...) for instant rendering and download without extra roundtrips.
    • cost: Billed OpenRouter generation cost (float or null).
    • width and height: Final dimensions of the delivered image.
    • original_dimensions and model_dimensions: Tuple arrays [w, h] for diagnostics and UI comparison.
    • was_rescaled: Boolean flag indicating whether Lanczos restoration ran.
    • media_type: MIME type of the returned image.
  3. Static file serving: Serves static/index.html at root / with fallback status payload when static files are not yet created, and mounts /static for assets.
  4. Error handling:
    • 400 Bad Request when API key is missing (neither provided nor in env).
    • 400 Bad Request when uploaded file is empty or cannot be decoded as an image.
    • 502 Bad Gateway when upstream OpenRouter API fails, extracting upstream error message.
  5. Direct execution: python3 app.py launches Uvicorn on http://127.0.0.1:8000.
  6. Dependencies: fastapi, uvicorn, and python-multipart added to requirements.txt.
## Resolution Implemented the FastAPI backend and `/api/clean` endpoint in `app.py`. ### Architectural decisions and API interface 1. **Endpoint signature**: `POST /api/clean` accepts `multipart/form-data` with: - `file: UploadFile` (required): uploaded garment image. - `prompt: str` (optional): custom prompt or preset description. Defaults to beige preset. - `model: str` (optional): OpenRouter model slug, defaults to `meta/muse-image`. - `api_key: str | None` (optional): API key override, falling back to `OPENROUTER_API_KEY` in environment. - `restore_res: bool` (optional, default True): toggles Lanczos upscaling back to source photo dimensions. 2. **Response format**: JSON response containing: - `image`: Base64 data URI (`data:<mime>;base64,...`) for instant rendering and download without extra roundtrips. - `cost`: Billed OpenRouter generation cost (float or null). - `width` and `height`: Final dimensions of the delivered image. - `original_dimensions` and `model_dimensions`: Tuple arrays `[w, h]` for diagnostics and UI comparison. - `was_rescaled`: Boolean flag indicating whether Lanczos restoration ran. - `media_type`: MIME type of the returned image. 3. **Static file serving**: Serves `static/index.html` at root `/` with fallback status payload when static files are not yet created, and mounts `/static` for assets. 4. **Error handling**: - 400 Bad Request when API key is missing (neither provided nor in env). - 400 Bad Request when uploaded file is empty or cannot be decoded as an image. - 502 Bad Gateway when upstream OpenRouter API fails, extracting upstream error message. 5. **Direct execution**: `python3 app.py` launches Uvicorn on `http://127.0.0.1:8000`. 6. **Dependencies**: `fastapi`, `uvicorn`, and `python-multipart` added to `requirements.txt`.
Sign in to join this conversation.
No description provided.