File size: 3,469 Bytes
9f1d9b2
 
 
 
 
 
 
 
 
 
 
 
83743a2
 
 
 
 
 
 
 
9f1d9b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3454e61
0679a72
 
9f1d9b2
 
 
 
 
 
4a561ea
 
9f1d9b2
4a561ea
 
9f1d9b2
3fdc682
 
9f1d9b2
 
 
 
 
 
 
 
 
 
 
dbd0a68
c904741
9f1d9b2
fbd4c25
 
 
dbd0a68
 
 
9f1d9b2
 
21d0e02
 
83743a2
 
 
 
 
 
 
 
 
9f1d9b2
dbd0a68
9f1d9b2
dbd0a68
 
9f1d9b2
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Optimized Dockerfile for Hugging Face Spaces
FROM python:3.12-slim

# Set working directory
WORKDIR /app

# Set environment variables for HF Spaces
ENV DEBIAN_FRONTEND=noninteractive \
    PYTHONUNBUFFERED=1 \
    PYTHONPATH=/app \
    GRADIO_SERVER_NAME=0.0.0.0 \
    GRADIO_SERVER_PORT=7860 \
    HF_HOME=/app/.cache/huggingface \
    HF_HUB_CACHE=/app/.cache/huggingface/hub \
    TRANSFORMERS_CACHE=/app/.cache/transformers \
    SENTENCE_TRANSFORMERS_HOME=/app/.cache/sentence_transformers \
    PATH="/root/.TinyTeX/bin/x86_64-linux:$PATH" \
    GRADIO_ALLOW_FLAGGING=never \
    GRADIO_ANALYTICS_ENABLED=False \
    HF_HUB_DISABLE_TELEMETRY=1

# Install system dependencies in single layer
RUN apt-get update && apt-get install -y --no-install-recommends \
    wget \
    curl \
    git \
    gcc \
    g++ \
    build-essential \
    pkg-config \
    portaudio19-dev \
    libasound2-dev \
    libsdl-pango-dev \
    libcairo2-dev \
    libpango1.0-dev \
    sox \
    ffmpeg \
    texlive-full \
    dvisvgm \
    ghostscript \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*


# Copy requirements and install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir  torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu \
    && pip install --no-cache-dir  -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu \
    && python -c "import gradio; print(f'Gradio version: {gradio.__version__}')" \
    && find /usr/local -name "*.pyc" -delete \
    && find /usr/local -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null || true

# Ensure Hugging Face cache directories are writable

# Create models directory and download models efficiently
RUN mkdir -p models && cd models \
    && echo "Downloading models for HF Spaces..." \
    && wget --progress=dot:giga --timeout=30 --tries=3 \
        -O kokoro-v0_19.onnx \
        "https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files/kokoro-v0_19.onnx" \
    && wget --progress=dot:giga --timeout=30 --tries=3 \
        -O voices.bin \
        "https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files/voices.bin" \
    && ls -la /app/models/

# Copy all project files and folders
COPY . .

# Run embedding creation script at build time
RUN python create_embeddings.py

# Ensure all files are writable (fix PermissionError for log file)
RUN chmod -R a+w /app

# Create output directory
RUN mkdir -p output tmp
# Ensure output and tmp directories are writable (fix PermissionError for session_id.txt)
RUN chmod -R a+w /app/output /app/tmp || true

RUN mkdir -p output tmp logs \
    && mkdir -p /app/.cache/huggingface/hub \
    && mkdir -p /app/.cache/transformers \
    && mkdir -p /app/.cache/sentence_transformers \
    && chmod -R 755 /app/.cache \
    && chmod 755 /app/models \
    && ls -la /app/models/ \
    && echo "Cache directories created with proper permissions"
# Add HF Spaces specific metadata
LABEL space.title="Text 2 Mnaim" \
      space.sdk="docker" \
      space.author="khanhthanhdev" \
      space.description="Text to science video using multi Agent"

# Expose the port that HF Spaces expects
EXPOSE 7860

# Health check optimized for HF Spaces
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=2 \
    CMD curl -f http://localhost:7860/ || exit 1

# Run the Gradio app with HF Spaces optimized settings
CMD ["python", "gradio_app.py"]