Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- Dockerfile +45 -21
- app.py +2 -1
Dockerfile
CHANGED
|
@@ -5,22 +5,29 @@ FROM python:3.11-slim AS builder
|
|
| 5 |
|
| 6 |
WORKDIR /install
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
# (OpenCV, MediaPipe, grpcio, cffi, etc.)
|
| 10 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 11 |
build-essential \
|
|
|
|
| 12 |
libgl1 \
|
| 13 |
libglib2.0-0 \
|
| 14 |
libgomp1 \
|
|
|
|
|
|
|
|
|
|
| 15 |
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
|
| 17 |
COPY requirements.txt .
|
| 18 |
|
| 19 |
-
# Install all Python deps into an isolated prefix
|
| 20 |
-
# only the built packages to the final stage
|
| 21 |
-
# Also install gunicorn as the production WSGI server
|
| 22 |
RUN pip install --no-cache-dir --prefix=/python-deps -r requirements.txt gunicorn
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# ============================================================
|
| 26 |
# Stage 2: Runtime image
|
|
@@ -28,42 +35,59 @@ RUN pip install --no-cache-dir --prefix=/python-deps -r requirements.txt gunicor
|
|
| 28 |
FROM python:3.11-slim
|
| 29 |
|
| 30 |
LABEL maintainer="undertone-classification"
|
| 31 |
-
LABEL description="Skin undertone classification β Flask + TensorFlow + MediaPipe"
|
| 32 |
|
| 33 |
-
# Runtime system libs
|
| 34 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 35 |
libgl1 \
|
| 36 |
libglib2.0-0 \
|
| 37 |
libgomp1 \
|
|
|
|
|
|
|
|
|
|
| 38 |
&& rm -rf /var/lib/apt/lists/*
|
| 39 |
|
| 40 |
# Copy installed Python packages from the builder stage
|
| 41 |
COPY --from=builder /python-deps /usr/local
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
WORKDIR /app
|
| 44 |
|
| 45 |
# Copy application source code
|
| 46 |
COPY app.py .
|
| 47 |
COPY templates/ templates/
|
| 48 |
-
COPY static/
|
| 49 |
-
COPY models/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
RUN mkdir -p static/uploads
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
# Run with Gunicorn for production (single worker keeps model in memory)
|
| 58 |
-
# Falls back gracefully: if gunicorn is unavailable the CMD can be swapped
|
| 59 |
-
# to `python app.py` during development.
|
| 60 |
ENV FLASK_ENV=production \
|
| 61 |
PYTHONUNBUFFERED=1 \
|
| 62 |
-
PYTHONDONTWRITEBYTECODE=1
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
"--workers", "1", \
|
| 68 |
-
"--timeout", "
|
|
|
|
| 69 |
"app:app"]
|
|
|
|
| 5 |
|
| 6 |
WORKDIR /install
|
| 7 |
|
| 8 |
+
# Build-time system deps (compilers, headers needed by grpcio, cffi, etc.)
|
|
|
|
| 9 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 10 |
build-essential \
|
| 11 |
+
wget \
|
| 12 |
libgl1 \
|
| 13 |
libglib2.0-0 \
|
| 14 |
libgomp1 \
|
| 15 |
+
libsm6 \
|
| 16 |
+
libxext6 \
|
| 17 |
+
libxrender1 \
|
| 18 |
&& rm -rf /var/lib/apt/lists/*
|
| 19 |
|
| 20 |
COPY requirements.txt .
|
| 21 |
|
| 22 |
+
# Install all Python deps + gunicorn into an isolated prefix
|
|
|
|
|
|
|
| 23 |
RUN pip install --no-cache-dir --prefix=/python-deps -r requirements.txt gunicorn
|
| 24 |
|
| 25 |
+
# Pre-download the MediaPipe face-detector tflite model at build time
|
| 26 |
+
# so the container never needs to reach out to Google Storage at runtime.
|
| 27 |
+
RUN mkdir -p /models && \
|
| 28 |
+
wget -q -O /models/detector.tflite \
|
| 29 |
+
"https://storage.googleapis.com/mediapipe-models/face_detector/blaze_face_short_range/float16/1/blaze_face_short_range.tflite"
|
| 30 |
+
|
| 31 |
|
| 32 |
# ============================================================
|
| 33 |
# Stage 2: Runtime image
|
|
|
|
| 35 |
FROM python:3.11-slim
|
| 36 |
|
| 37 |
LABEL maintainer="undertone-classification"
|
| 38 |
+
LABEL description="Skin undertone classification β Flask + TensorFlow + MediaPipe (HF Spaces)"
|
| 39 |
|
| 40 |
+
# Runtime system libs required by OpenCV and MediaPipe
|
| 41 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 42 |
libgl1 \
|
| 43 |
libglib2.0-0 \
|
| 44 |
libgomp1 \
|
| 45 |
+
libsm6 \
|
| 46 |
+
libxext6 \
|
| 47 |
+
libxrender1 \
|
| 48 |
&& rm -rf /var/lib/apt/lists/*
|
| 49 |
|
| 50 |
# Copy installed Python packages from the builder stage
|
| 51 |
COPY --from=builder /python-deps /usr/local
|
| 52 |
|
| 53 |
+
# ββ Hugging Face Spaces requirement ββββββββββββββββββββββββββ
|
| 54 |
+
# HF Spaces runs containers with a non-root user (UID=1000, GID=1000).
|
| 55 |
+
# We create that user and make /app writable so Flask can write uploads.
|
| 56 |
+
RUN useradd -m -u 1000 appuser
|
| 57 |
+
|
| 58 |
WORKDIR /app
|
| 59 |
|
| 60 |
# Copy application source code
|
| 61 |
COPY app.py .
|
| 62 |
COPY templates/ templates/
|
| 63 |
+
COPY static/ static/
|
| 64 |
+
COPY models/ models/
|
| 65 |
+
|
| 66 |
+
# Copy the pre-downloaded tflite model into the models directory
|
| 67 |
+
COPY --from=builder /models/detector.tflite models/detector.tflite
|
| 68 |
+
|
| 69 |
+
# Ensure the uploads directory exists and is owned by the runtime user
|
| 70 |
+
RUN mkdir -p static/uploads && \
|
| 71 |
+
chown -R appuser:appuser /app
|
| 72 |
|
| 73 |
+
USER appuser
|
|
|
|
| 74 |
|
| 75 |
+
# ββ Port ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 76 |
+
# Hugging Face Spaces Docker SDK expects port 7860.
|
| 77 |
+
EXPOSE 7860
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
ENV FLASK_ENV=production \
|
| 80 |
PYTHONUNBUFFERED=1 \
|
| 81 |
+
PYTHONDONTWRITEBYTECODE=1 \
|
| 82 |
+
# Silence unnecessary TF / oneDNN log noise
|
| 83 |
+
TF_CPP_MIN_LOG_LEVEL=2 \
|
| 84 |
+
TF_ENABLE_ONEDNN_OPTS=0
|
| 85 |
|
| 86 |
+
# 1 worker β global ai_engine singleton is shared; 180 s timeout for
|
| 87 |
+
# model load + first inference.
|
| 88 |
+
CMD ["gunicorn", \
|
| 89 |
+
"--bind", "0.0.0.0:7860", \
|
| 90 |
"--workers", "1", \
|
| 91 |
+
"--timeout", "180", \
|
| 92 |
+
"--log-level", "info", \
|
| 93 |
"app:app"]
|
app.py
CHANGED
|
@@ -215,4 +215,5 @@ def predict():
|
|
| 215 |
|
| 216 |
if __name__ == '__main__':
|
| 217 |
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 218 |
-
|
|
|
|
|
|
| 215 |
|
| 216 |
if __name__ == '__main__':
|
| 217 |
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 218 |
+
port = int(os.environ.get('PORT', 5000))
|
| 219 |
+
app.run(debug=True, host='0.0.0.0', port=port)
|