# Use a slim Python base FROM python:3.12-slim # Set working directory WORKDIR /app # Install system dependencies RUN apt-get update \ && apt-get install -y --no-install-recommends \ build-essential \ curl \ procps \ && rm -rf /var/lib/apt/lists/* # Install Python dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Copy your code COPY . . # Create startup script with better process management RUN echo '#!/bin/bash\n\ set -e\n\ \n\ # Function to cleanup background processes\n\ cleanup() {\n\ echo "Cleaning up..."\n\ kill $(jobs -p) 2>/dev/null || true\n\ exit\n\ }\n\ trap cleanup SIGTERM SIGINT\n\ \n\ # Download files if needed\n\ echo "Downloading files..."\n\ python3 main.py download-files || echo "Download failed, continuing..."\n\ \n\ # Start FastAPI server in background first\n\ echo "Starting FastAPI server..."\n\ ENV="production" END_OF_TURN_TIMEOUT="15" uvicorn app:app --host 0.0.0.0 --port 7860 &\n\ API_PID=$!\n\ \n\ # Wait for API to be ready\n\ echo "Waiting for API to be ready..."\n\ for i in {1..30}; do\n\ if curl -f http://localhost:7860/health > /dev/null 2>&1; then\n\ echo "API is ready!"\n\ break\n\ fi\n\ if [ $i -eq 30 ]; then\n\ echo "API failed to start"\n\ exit 1\n\ fi\n\ sleep 2\n\ done\n\ \n\ # Start agent\n\ echo "Starting agent..."\n\ API_URL="http://localhost:7860" END_OF_TURN_TIMEOUT="15" python3 main.py dev &\n\ AGENT_PID=$!\n\ \n\ # Monitor both processes\n\ while true; do\n\ if ! kill -0 $API_PID 2>/dev/null; then\n\ echo "API process died, restarting..."\n\ ENV="production" END_OF_TURN_TIMEOUT="15" uvicorn app:app --host 0.0.0.0 --port 7860 &\n\ API_PID=$!\n\ fi\n\ \n\ if ! kill -0 $AGENT_PID 2>/dev/null; then\n\ echo "Agent process died, restarting..."\n\ API_URL="http://localhost:7860" END_OF_TURN_TIMEOUT="15" python3 main.py dev &\n\ AGENT_PID=$!\n\ fi\n\ \n\ sleep 10\n\ done' > /app/start.sh && chmod +x /app/start.sh # Create log directory RUN mkdir -p /var/log # Hugging Face Spaces expects apps to run on port 7860 EXPOSE 7860 # Health check for HF Spaces RUN echo '#!/bin/bash\n\ curl -f http://localhost:7860/health > /dev/null 2>&1 || exit 1' > /app/healthcheck.sh && chmod +x /app/healthcheck.sh # Start with the startup script CMD ["/app/start.sh"]