File size: 2,456 Bytes
6acaae5
 
936cdc4
 
 
 
6acaae5
 
936cdc4
6acaae5
 
 
 
 
 
 
 
72ac111
 
6acaae5
 
72ac111
6acaae5
 
 
 
 
32b998c
 
72ac111
6acaae5
 
 
 
 
 
 
 
72ac111
6acaae5
936cdc4
 
6acaae5
936cdc4
 
6acaae5
 
 
72ac111
 
6acaae5
 
 
 
72ac111
6acaae5
936cdc4
 
6acaae5
 
936cdc4
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
# Use a base Ubuntu image
FROM ubuntu:22.04

# Set environment variables to prevent interactive prompts during apt operations
ENV DEBIAN_FRONTEND=noninteractive

# Install system dependencies: curl for Ollama, wget for model download, git, python3 and pip
RUN apt update && apt install -y curl wget git python3 python3-pip

# --- PERMISSIONS FIX 1: CREATE USER ---
# Create a non-root user named 'user' with user ID 1000.
# This matches the user ID that Hugging Face Spaces uses to run the container.
# The -m flag creates a home directory at /home/user.
RUN useradd -m -u 1000 user

# Install Ollama using its official installation script
# This will be run as root, which is correct for system-wide installation.
RUN curl -fsSL https://ollama.com/install.sh | sh

# Set the working directory inside the container
WORKDIR /app

# Define environment variables for the model repository and filename
ENV MODEL_REPO="unsloth/gemma-3-4b-it-GGUF"
ENV MODEL_FILENAME="gemma-3-4b-it.Q4_K_M.gguf"
ENV MODEL_enableDL="?download=true"

# This hard code OK.... Download the GGUF model file directly from Hugging Face Hub.
RUN wget "https://huggingface.co/unsloth/gemma-3-4b-it-qat-GGUF/resolve/main/gemma-3-4b-it-qat-Q4_K_M.gguf?download=true" -O ${MODEL_FILENAME}

# --- PERMISSIONS FIX 2: COPY FILES WITH OWNERSHIP ---
# Copy all application files into the container.
# Use the --chown=user flag to set the owner of these files to the 'user' we created.
# This is critical for giving the application write permissions.
COPY --chown=user Modelfile .
COPY --chown=user app.py .
COPY --chown=user requirements.txt .
COPY --chown=user run.sh .

# Install Python dependencies required by your Gradio application
RUN pip install --no-cache-dir -r requirements.txt

# Make the startup script executable
RUN chmod +x run.sh

# --- PERMISSIONS FIX 3: SWITCH TO USER ---
# Switch the context of the Dockerfile to our non-root user.
# All subsequent commands (including the final CMD) will now be executed as 'user', not 'root'.
USER user

# Set environment variables for the user's home directory. This ensures that
# tools like pip and transformers cache files in a location writable by the user (/home/user/.cache).
ENV HOME=/home/user \
	PATH=/home/user/.local/bin:$PATH

# Expose the port that your Gradio application will listen on.
EXPOSE 7860

# Set the entrypoint for the container to execute our startup script.
# This will now be run as the 'user'.
CMD ["./run.sh"]