Spaces:
Sleeping
Sleeping
| FROM python:3.11-slim-bullseye | |
| # Install system dependencies with clean up in single RUN layer | |
| RUN sed -i 's/main/main contrib non-free/' /etc/apt/sources.list && \ | |
| apt-get update && \ | |
| apt-get install -y --no-install-recommends \ | |
| unrar \ | |
| libgl1 \ | |
| libglib2.0-0 \ | |
| # ffmpeg for video processing | |
| ffmpeg \ | |
| # git for transformers dependencies | |
| git \ | |
| && rm -rf /var/lib/apt/lists/* | |
| WORKDIR /app | |
| # Upgrade pip and install core dependencies first | |
| RUN pip install --no-cache-dir --upgrade pip setuptools wheel packaging | |
| # Install optimized CPU-only PyTorch stack | |
| RUN pip install --no-cache-dir \ | |
| "numpy<2" \ | |
| torch==2.1.2+cpu \ | |
| torchvision==0.16.2+cpu \ | |
| torchaudio==2.1.2+cpu \ | |
| --index-url https://download.pytorch.org/whl/cpu | |
| # Copy requirements and install with special handling | |
| COPY requirements.txt . | |
| RUN pip install --no-cache-dir \ | |
| -r requirements.txt \ | |
| --find-links https://download.pytorch.org/whl/cpu \ | |
| --extra-index-url https://pypi.org/simple | |
| # Install transformers with specific version that works with BLIP-2 | |
| RUN pip install --no-cache-dir \ | |
| accelerate==0.27.2 \ | |
| transformers==4.36.2 \ | |
| timm==0.9.12 \ | |
| einops==0.7.0 \ | |
| bitsandbytes==0.42.0 # Helps with memory optimization | |
| # For BLIP-2 specifically, we need these | |
| RUN pip install --no-cache-dir \ | |
| protobuf==3.20.3 \ | |
| sentencepiece==0.2.0 | |
| # Copy application code with proper permissions | |
| COPY --chown=1000:1000 . . | |
| # Create non-root user and set permissions | |
| RUN useradd -m -u 1000 user && \ | |
| chown -R user:user /app && \ | |
| mkdir -p /app/downloads /app/extracted /app/extracted_frames /app/analysis_results && \ | |
| chown -R user:user /app/* | |
| USER user | |
| # Environment variables for better performance and less logging | |
| ENV HF_HUB_DISABLE_PROGRESS=1 | |
| ENV TF_CPP_MIN_LOG_LEVEL=3 | |
| ENV PYTORCH_NO_CUDA_MEMORY_CACHING=1 | |
| ENV TOKENIZERS_PARALLELISM=false | |
| ENV PYTHONUNBUFFERED=1 | |
| # Set this if you're only using CPU | |
| ENV CUDA_VISIBLE_DEVICES="" | |
| # Pre-download the BLIP-2 model during build (optional) | |
| # RUN python -c "from transformers import AutoModel; AutoModel.from_pretrained('Salesforce/blip2-opt-2.7b', trust_remote_code=True)" | |
| CMD ["uvicorn", "download_api:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"] |