Spaces:
Sleeping
Sleeping
| # Use Python 3.10 slim image for smaller size | |
| FROM python:3.10-slim | |
| # Set working directory | |
| WORKDIR /app | |
| # Install system dependencies required for OpenCV and other packages | |
| RUN apt-get update && apt-get install -y \ | |
| libgl1 \ | |
| libglib2.0-0 \ | |
| libsm6 \ | |
| libxext6 \ | |
| libxrender-dev \ | |
| libgomp1 \ | |
| wget \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Copy requirements first for better caching | |
| COPY requirements.txt . | |
| # Install Python dependencies | |
| # Install PyTorch CPU version to reduce image size (GPU not available on HF free tier) | |
| RUN pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cpu && \ | |
| pip install --no-cache-dir -r requirements.txt | |
| # Copy application code | |
| COPY main.py . | |
| # Download SAM model (using smaller vit_b model for HF) | |
| # You can change this to vit_h or vit_l if needed, but they're larger | |
| RUN wget -q https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth || \ | |
| echo "Warning: Could not download SAM model. App will run with fallback methods." | |
| # Update main.py to use the vit_b model if downloaded | |
| RUN if [ -f "sam_vit_b_01ec64.pth" ]; then \ | |
| sed -i 's/sam_vit_h_4b8939.pth/sam_vit_b_01ec64.pth/g' main.py && \ | |
| sed -i 's/vit_h/vit_b/g' main.py; \ | |
| fi | |
| # Create a non-root user for Hugging Face | |
| RUN useradd -m -u 1000 user | |
| USER user | |
| # Set environment variables | |
| ENV HOME=/home/user \ | |
| PATH=/home/user/.local/bin:$PATH \ | |
| PYTHONUNBUFFERED=1 | |
| # Hugging Face Spaces uses port 7860 by default | |
| EXPOSE 7860 | |
| # Health check | |
| HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ | |
| CMD python -c "import requests; requests.get('http://localhost:7860/health')" || exit 1 | |
| # Run the application on port 7860 (Hugging Face default) | |
| CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"] | |