Spaces:
Sleeping
Sleeping
Update Dockerfile
Browse files- Dockerfile +67 -8
Dockerfile
CHANGED
|
@@ -1,31 +1,90 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
-
# Install system dependencies
|
| 7 |
RUN apt-get update && apt-get install -y \
|
| 8 |
git \
|
|
|
|
| 9 |
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
| 11 |
-
# Copy requirements
|
| 12 |
COPY requirements.txt .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 16 |
|
| 17 |
-
# Copy application
|
| 18 |
COPY . .
|
| 19 |
|
| 20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
RUN mkdir -p /tmp/transformers_cache /tmp/huggingface
|
| 22 |
|
| 23 |
# Set environment variables
|
| 24 |
ENV TRANSFORMERS_CACHE=/tmp/transformers_cache
|
| 25 |
ENV HF_HOME=/tmp/huggingface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Expose port 7860 (Hugging Face Spaces default)
|
| 28 |
EXPOSE 7860
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Run the application
|
| 31 |
-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
+
# Multi-stage build: Train models then run app
|
| 2 |
+
|
| 3 |
+
# ============================================
|
| 4 |
+
# Stage 1: Training Stage
|
| 5 |
+
# ============================================
|
| 6 |
+
FROM python:3.10-slim as trainer
|
| 7 |
|
| 8 |
WORKDIR /app
|
| 9 |
|
| 10 |
+
# Install system dependencies for training
|
| 11 |
RUN apt-get update && apt-get install -y \
|
| 12 |
git \
|
| 13 |
+
build-essential \
|
| 14 |
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
|
| 16 |
+
# Copy requirements and install
|
| 17 |
COPY requirements.txt .
|
| 18 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 19 |
+
|
| 20 |
+
# Copy only necessary files for training
|
| 21 |
+
COPY models/ models/
|
| 22 |
+
COPY data/ data/
|
| 23 |
+
|
| 24 |
+
# Create output directory
|
| 25 |
+
RUN mkdir -p models/model_weights/custom_models
|
| 26 |
|
| 27 |
+
# Train models
|
| 28 |
+
RUN echo "π Starting model training..." && \
|
| 29 |
+
python models/train_model.py && \
|
| 30 |
+
echo "β
Training completed!"
|
| 31 |
+
|
| 32 |
+
# Verify models were created and have predict_proba
|
| 33 |
+
RUN echo "π Verifying models..." && \
|
| 34 |
+
ls -lah models/model_weights/custom_models/ && \
|
| 35 |
+
python -c "import joblib; \
|
| 36 |
+
m_en = joblib.load('models/model_weights/custom_models/english_model.pkl'); \
|
| 37 |
+
m_bn = joblib.load('models/model_weights/custom_models/bengali_model.pkl'); \
|
| 38 |
+
print(f'β
English Model: {type(m_en).__name__}'); \
|
| 39 |
+
print(f'β
Has predict_proba: {hasattr(m_en, \"predict_proba\")}'); \
|
| 40 |
+
print(f'β
Bengali Model: {type(m_bn).__name__}'); \
|
| 41 |
+
print(f'β
Has predict_proba: {hasattr(m_bn, \"predict_proba\")}')"
|
| 42 |
+
|
| 43 |
+
# ============================================
|
| 44 |
+
# Stage 2: Runtime Stage
|
| 45 |
+
# ============================================
|
| 46 |
+
FROM python:3.10-slim
|
| 47 |
+
|
| 48 |
+
WORKDIR /app
|
| 49 |
+
|
| 50 |
+
# Install runtime system dependencies
|
| 51 |
+
RUN apt-get update && apt-get install -y \
|
| 52 |
+
git \
|
| 53 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 54 |
+
|
| 55 |
+
# Copy requirements and install
|
| 56 |
+
COPY requirements.txt .
|
| 57 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 58 |
|
| 59 |
+
# Copy application code
|
| 60 |
COPY . .
|
| 61 |
|
| 62 |
+
# Copy trained models from training stage
|
| 63 |
+
COPY --from=trainer /app/models/model_weights/custom_models/*.pkl models/model_weights/custom_models/
|
| 64 |
+
COPY --from=trainer /app/models/model_weights/custom_models/metadata.json models/model_weights/custom_models/
|
| 65 |
+
|
| 66 |
+
# Create cache directories for transformers
|
| 67 |
RUN mkdir -p /tmp/transformers_cache /tmp/huggingface
|
| 68 |
|
| 69 |
# Set environment variables
|
| 70 |
ENV TRANSFORMERS_CACHE=/tmp/transformers_cache
|
| 71 |
ENV HF_HOME=/tmp/huggingface
|
| 72 |
+
ENV PYTHONUNBUFFERED=1
|
| 73 |
+
|
| 74 |
+
# Verify models exist in final image
|
| 75 |
+
RUN echo "π Final verification..." && \
|
| 76 |
+
ls -lah models/model_weights/custom_models/ && \
|
| 77 |
+
python -c "import joblib; \
|
| 78 |
+
m = joblib.load('models/model_weights/custom_models/english_model.pkl'); \
|
| 79 |
+
print(f'β
Final Stage - Model loaded: {type(m).__name__}'); \
|
| 80 |
+
print(f'β
Has predict_proba: {hasattr(m, \"predict_proba\")}')"
|
| 81 |
|
| 82 |
# Expose port 7860 (Hugging Face Spaces default)
|
| 83 |
EXPOSE 7860
|
| 84 |
|
| 85 |
+
# Health check
|
| 86 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
| 87 |
+
CMD python -c "import requests; requests.get('http://localhost:7860/health', timeout=5)" || exit 1
|
| 88 |
+
|
| 89 |
# Run the application
|
| 90 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]
|