ml_service / Dockerfile
bldeaw's picture
Deploy to Hugging Face Spaces: Add application files and dependencies
6d70a1d
Raw
History Blame Contribute Delete
1.47 kB
FROM python:3.9-slim
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user for Hugging Face Spaces
RUN useradd -m -u 1000 user
USER user
ENV PATH="/home/user/.local/bin:$PATH"
WORKDIR /app
# Copy requirements first for better caching
COPY --chown=user ./requirements.txt requirements.txt
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
# Copy application code and models
COPY --chown=user . /app
# Verify critical files exist (helps debug if files are missing)
RUN ls -la /app/*.py && \
test -f /app/app.py || (echo "ERROR: app.py not found!" && exit 1) && \
test -f /app/preprocessing.py || (echo "ERROR: preprocessing.py not found!" && exit 1) && \
test -f /app/model_utils.py || (echo "ERROR: model_utils.py not found!" && exit 1) && \
echo "All required Python files found!"
# Ensure models directory exists (models should be committed to repo)
RUN mkdir -p /app/models /app/data
# Health check (port 7860 for Hugging Face Spaces)
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
CMD python -c "import requests; requests.get('http://localhost:7860/health', timeout=5)" || exit 1
# Expose port 7860 (Hugging Face Spaces default)
EXPOSE 7860
# Run FastAPI application
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]