Spaces:
Sleeping
Sleeping
FROM python:3.11-slim | |
# Set environment variables | |
ENV DEBIAN_FRONTEND=noninteractive | |
ENV PYTHONUNBUFFERED=1 | |
ENV PYTHONDONTWRITEBYTECODE=1 | |
# Install system dependencies | |
RUN apt-get update && apt-get install -y \ | |
git \ | |
wget \ | |
curl \ | |
build-essential \ | |
nodejs \ | |
npm \ | |
&& rm -rf /var/lib/apt/lists/* | |
# Set working directory | |
WORKDIR /app | |
# Upgrade pip | |
RUN python -m pip install --upgrade pip setuptools wheel | |
# Install PyTorch with CUDA support first | |
RUN pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 | |
# Copy requirements and install Python dependencies | |
COPY requirements.txt . | |
RUN pip install -r requirements.txt | |
# Copy application files | |
COPY . . | |
# Create cache directory for HuggingFace models | |
RUN mkdir -p /app/model_cache | |
ENV TRANSFORMERS_CACHE=/app/model_cache | |
ENV HF_HOME=/app/model_cache | |
# Create non-root user for security | |
RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app | |
# Run type checking with pyright BEFORE switching to non-root user | |
# This ensures that if there are any type errors, the build fails early | |
RUN echo "Running pyright type checking..." && \ | |
pyright --version && \ | |
pyright . && \ | |
echo "Type checking passed successfully!" | |
USER appuser | |
# Set environment variables for the application | |
ENV PYTHONPATH=/app | |
ENV HF_TOKEN=${HF_TOKEN} | |
ENV API_HOST=0.0.0.0 | |
ENV API_PORT=7860 | |
# Expose port | |
EXPOSE 7860 | |
# Health check | |
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ | |
CMD curl -f http://localhost:7860/health || exit 1 | |
# Run the application | |
CMD ["python", "main.py"] |