extonlawrence commited on
Commit
1558556
·
1 Parent(s): 8fe89c3

Add Ollama setup to script and use for in HF space

Browse files
Files changed (2) hide show
  1. Dockerfile +7 -2
  2. entrypoint.sh +33 -0
Dockerfile CHANGED
@@ -21,14 +21,19 @@ RUN touch /app/.env.local
21
 
22
  USER root
23
  RUN apt-get update
24
- RUN apt-get install -y libgomp1 libcurl4
 
 
 
 
 
 
25
 
26
  # ensure npm cache dir exists before adjusting ownership
27
  RUN mkdir -p /home/user/.npm && chown -R 1000:1000 /home/user/.npm
28
 
29
  USER user
30
 
31
-
32
  COPY --chown=1000 .env /app/.env
33
  COPY --chown=1000 entrypoint.sh /app/entrypoint.sh
34
  COPY --chown=1000 package.json /app/package.json
 
21
 
22
  USER root
23
  RUN apt-get update
24
+ RUN apt-get install -y libgomp1 libcurl4 curl
25
+
26
+ # Install Ollama
27
+ RUN curl -fsSL https://ollama.ai/install.sh | sh
28
+
29
+ # ensure ollama cache dir exists before adjusting ownership
30
+ RUN mkdir -p /home/user/.ollama && chown -R 1000:1000 /home/user/.ollama
31
 
32
  # ensure npm cache dir exists before adjusting ownership
33
  RUN mkdir -p /home/user/.npm && chown -R 1000:1000 /home/user/.npm
34
 
35
  USER user
36
 
 
37
  COPY --chown=1000 .env /app/.env
38
  COPY --chown=1000 entrypoint.sh /app/entrypoint.sh
39
  COPY --chown=1000 package.json /app/package.json
entrypoint.sh CHANGED
@@ -14,6 +14,39 @@ if [ "$INCLUDE_DB" = "true" ] ; then
14
  nohup mongod &
15
  fi;
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  export PUBLIC_VERSION=$(node -p "require('./package.json').version")
18
 
19
  dotenv -e /app/.env -c -- node /app/build/index.js -- --host 0.0.0.0 --port 3000
 
14
  nohup mongod &
15
  fi;
16
 
17
+ # Start Ollama service for HF space (local gpu)
18
+ echo "Starting local Ollama service"
19
+ nohup ollama serve > /tmp/ollama.log 2>&1 &
20
+ OLLAMA_PID=$!
21
+
22
+ # Wait for Ollama to be ready
23
+ MAX_RETRIES=30
24
+ RETRY_COUNT=0
25
+ until curl -s http://localhost:11434/api/tags > /dev/null 2>&1; do
26
+ RETRY_COUNT=$((RETRY_COUNT + 1))
27
+ if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then
28
+ echo "Ollama failed to start after $MAX_RETRIES attempts"
29
+ cat /tmp/ollama.log
30
+ exit 1
31
+ fi
32
+ sleep 2
33
+ done
34
+
35
+ # Pull models, ex.: OLLAMA_MODELS="llama3.1:8b,mistral:7b,codellama:13b"
36
+ OLLAMA_MODELS=${OLLAMA_MODELS:-llama3.1:8b}
37
+
38
+ IFS=',' read -ra MODEL_ARRAY <<< "$OLLAMA_MODELS"
39
+ for MODEL in "${MODEL_ARRAY[@]}"; do
40
+ MODEL=$(echo "$MODEL" | xargs) # trim whitespace
41
+ if ! ollama list | grep -q "$MODEL"; then
42
+ echo " Pulling model: $MODEL (this may take several minutes)..."
43
+ ollama pull "$MODEL"
44
+ echo " $MODEL pulled successfully!"
45
+ else
46
+ echo " $MODEL already exists"
47
+ fi
48
+ done
49
+
50
  export PUBLIC_VERSION=$(node -p "require('./package.json').version")
51
 
52
  dotenv -e /app/.env -c -- node /app/build/index.js -- --host 0.0.0.0 --port 3000