File size: 1,689 Bytes
2758540
 
 
 
 
 
 
 
 
 
 
 
bcfb62f
2758540
 
 
 
e1496f9
 
 
2758540
 
 
 
 
 
 
 
 
 
 
 
 
 
19604c7
 
 
 
 
 
 
 
 
 
2758540
 
 
 
 
 
 
 
 
 
 
 
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
# Use an official Python runtime as a parent image
FROM python:3.10-slim

# Set environment variables for Python
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

# Hugging Face Spaces uses port 7860 by default
ENV PORT=7860

# Install system dependencies required for OpenCV and AI models
RUN apt-get update && apt-get install -y \
    libgl1 \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender-dev \
    gcc \
    portaudio19-dev \
    python3-dev \
    git \
    && rm -rf /var/lib/apt/lists/*

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt .

# Install Python dependencies
# We specifically install the CPU-only version of PyTorch to save space and avoid GPU driver issues on the free tier
RUN pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
RUN pip install --no-cache-dir -r requirements.txt

# Increase Hugging Face Hub download timeout to prevent 504 errors
ENV HF_HUB_DOWNLOAD_TIMEOUT=120

# Create scripts directory and copy just the preload script first to leverage Docker cache
RUN mkdir -p scripts
COPY scripts/preload_models.py scripts/

# Pre-download HF model weights into the image to prevent container startup timeouts
RUN python scripts/preload_models.py

# Copy the current directory contents into the container at /app
COPY . .

# Create directories for static files and databases
RUN mkdir -p static/thumbnails static/anomalies database/data
RUN chmod -R 777 static database

# Expose the port Hugging Face expects
EXPOSE 7860

# Command to run the FastAPI server
CMD uvicorn app:app --host 0.0.0.0 --port $PORT