File size: 2,142 Bytes
773b38b
b0fecd5
773b38b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0fecd5
773b38b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee798a3
773b38b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Dockerfile optimized for Hugging Face Spaces
FROM python:3.11-slim

# Set environment variables for Hugging Face Spaces
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV DEBIAN_FRONTEND=noninteractive

# Hugging Face Spaces specific
ENV GRADIO_SERVER_NAME="0.0.0.0"
ENV GRADIO_SERVER_PORT=7860

# Set working directory
WORKDIR /app

# Install system dependencies required for OpenCV and other packages
RUN apt-get update && apt-get install -y \
    libgl1 \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender-dev \
    libgomp1 \
    libgtk-3-0 \
    libavcodec-dev \
    libavformat-dev \
    libswscale-dev \
    libgstreamer-plugins-base1.0-dev \
    libgstreamer1.0-dev \
    libpng-dev \
    libjpeg-dev \
    libopenexr-dev \
    libtiff-dev \
    libwebp-dev \
    curl \
    wget \
    git \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements first for better Docker layer caching
COPY requirements.txt .

# Install Python dependencies
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# Copy application files
COPY main.py .
COPY start_backend.py .


# Create directories for temporary files and models
RUN mkdir -p /tmp/uploads /tmp/models /tmp/logs

# Pre-download YOLOv8 model to avoid download during runtime
# This helps with cold start times on Hugging Face Spaces
RUN python -c "from ultralytics import YOLO; model = YOLO('yolov8s.pt'); print('YOLOv8 model downloaded successfully')"

# Expose port 7860 (required for Hugging Face Spaces)
EXPOSE 7860

# Create startup script for Hugging Face Spaces
RUN echo '#!/bin/bash\n\
echo "πŸš€ Starting Crowd Detection API on Hugging Face Spaces..."\n\
echo "πŸ“Š Loading AI models..."\n\
python -c "from ultralytics import YOLO; YOLO(\"yolov8s.pt\")" 2>/dev/null || echo "Model already cached"\n\
echo "βœ… Models loaded successfully"\n\
echo "🌐 Starting FastAPI server on port 7860..."\n\
exec python -m uvicorn main:app --host 0.0.0.0 --port 7860 --workers 1\n' > /app/start.sh

# Make startup script executable
# RUN chmod +x /app/start.sh

# Start the application
CMD ["python", "start_backend.py"]