# 1. Use Python 3.11 (Matches your Mac version to prevent dependency errors) FROM python:3.11-slim # 2. Force Python to print logs immediately (Helps us debug if it crashes) ENV PYTHONUNBUFFERED=1 # 2b. MEMORY FIX: Limit PyTorch threads to prevent memory bloat ENV OMP_NUM_THREADS=1 ENV MKL_NUM_THREADS=1 ENV TORCH_NUM_THREADS=1 # 3. Set the working directory inside the container WORKDIR /app # 4. Copy requirements file first COPY requirements.txt . # 5. Install dependencies # Added default-timeout to prevent "ReadTimeoutError" on slow connections RUN pip install --default-timeout=1000 --no-cache-dir -r requirements.txt # 5b. HF SPACES: Create a non-root user RUN useradd -m -u 1000 user RUN chown -R user:user /app # 6. Copy the rest of your code COPY --chown=user . . # 7. Run the seed script to "bake" the AI models into the image # We run as root first to ensure permissions, or we can run as user if pip installed locally. # However, usually pip installs are global here. Let's switch to user. USER user # Pre-load/Seed models (as user 1000 to avoid permission issues) RUN python seed_references.py # 8. Start the app (The Critical Fix) # We use "sh -c" so we can read the ${PORT} variable from Render/HF. # Spaces uses 7860 by default. ENV PORT=7860 CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]