#!/bin/bash
# Launch the FastAPI edit service (talks to the local ComfyUI on :8188).
set -e
# env.sh resolves API_DIR/VENV (and keeps the venv off NTFS).
source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/env.sh"
source "$VENV/bin/activate"
cd "$API_DIR"

# Add all nvidia CUDA library paths bundled with the venv (needed by onnxruntime-gpu / insightface)
_NV_BASE="$VENV/lib/python3.13/site-packages/nvidia"
_NV_LIBPATH="$_NV_BASE/cuda_runtime/lib:$_NV_BASE/cublas/lib:$_NV_BASE/cudnn/lib:$_NV_BASE/curand/lib:$_NV_BASE/cufft/lib:$_NV_BASE/cusolver/lib:$_NV_BASE/cusparse/lib:$_NV_BASE/nvjitlink/lib:$_NV_BASE/cuda_nvrtc/lib"
export LD_LIBRARY_PATH="${_NV_LIBPATH}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"

export COMFY_URL="http://127.0.0.1:8188"
export HOST="0.0.0.0"
export PORT="8500"
# Output pixel budget. MI50 is compute-bound on this 20B model:
#   ~0.59MP -> ~110s   ~0.79MP -> ~140s   ~1.0MP -> ~180s  (4 steps)
# 0.79MP is a sane speed/quality default; raise for bigger output.
# Lowered to 0.65MP to help prevent GPU OOM on MI50.
export MAX_AREA="${MAX_AREA:-655360}"

exec python edit_api.py
