Spaces:
Runtime error
Runtime error
Refactor Dockerfile and start script: remove dos2unix dependency, streamline installation commands, and enhance startup process
Browse files- Dockerfile +12 -9
- start.sh +45 -108
Dockerfile
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
# Use official Python base image
|
| 2 |
FROM python:3.10-slim
|
| 3 |
|
| 4 |
-
# Install curl, git, and dependencies for Ollama
|
| 5 |
-
RUN apt-get update && apt-get install -y
|
| 6 |
-
curl
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Set working directory
|
| 10 |
WORKDIR /app
|
|
@@ -12,13 +15,13 @@ WORKDIR /app
|
|
| 12 |
# Copy app files
|
| 13 |
COPY requirements.txt ./requirements.txt
|
| 14 |
COPY fast.py ./fast.py
|
| 15 |
-
COPY
|
| 16 |
|
| 17 |
# Install Python dependencies
|
| 18 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
RUN chmod +x
|
| 22 |
|
| 23 |
# Create ollama home directory
|
| 24 |
RUN mkdir -p /tmp/ollama_home/.ollama
|
|
@@ -31,5 +34,5 @@ ENV OLLAMA_ORIGINS="*"
|
|
| 31 |
# Expose the port (required by Hugging Face Spaces)
|
| 32 |
EXPOSE 7860
|
| 33 |
|
| 34 |
-
# Start your app via the
|
| 35 |
-
CMD ["
|
|
|
|
| 1 |
# Use official Python base image
|
| 2 |
FROM python:3.10-slim
|
| 3 |
|
| 4 |
+
# Install curl, git, dos2unix and dependencies for Ollama
|
| 5 |
+
RUN apt-get update && apt-get install -y \
|
| 6 |
+
curl \
|
| 7 |
+
git \
|
| 8 |
+
dos2unix \
|
| 9 |
+
&& curl -fsSL https://ollama.com/install.sh | sh \
|
| 10 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
|
| 12 |
# Set working directory
|
| 13 |
WORKDIR /app
|
|
|
|
| 15 |
# Copy app files
|
| 16 |
COPY requirements.txt ./requirements.txt
|
| 17 |
COPY fast.py ./fast.py
|
| 18 |
+
COPY start.sh ./start.sh
|
| 19 |
|
| 20 |
# Install Python dependencies
|
| 21 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 22 |
|
| 23 |
+
# Fix line endings and make start script executable
|
| 24 |
+
RUN dos2unix start.sh && chmod +x start.sh
|
| 25 |
|
| 26 |
# Create ollama home directory
|
| 27 |
RUN mkdir -p /tmp/ollama_home/.ollama
|
|
|
|
| 34 |
# Expose the port (required by Hugging Face Spaces)
|
| 35 |
EXPOSE 7860
|
| 36 |
|
| 37 |
+
# Start your app via the startup script
|
| 38 |
+
CMD ["./start.sh"]
|
start.sh
CHANGED
|
@@ -1,114 +1,51 @@
|
|
| 1 |
-
#!/
|
| 2 |
-
import os
|
| 3 |
-
import subprocess
|
| 4 |
-
import time
|
| 5 |
-
import requests
|
| 6 |
-
import sys
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Create directories
|
| 15 |
-
ollama_dir = '/tmp/ollama_home/.ollama'
|
| 16 |
-
os.makedirs(ollama_dir, exist_ok=True)
|
| 17 |
-
os.makedirs(f'{ollama_dir}/models', exist_ok=True)
|
| 18 |
-
|
| 19 |
-
print(f"Environment setup complete. HOME={os.environ['HOME']}")
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Start Ollama in background
|
| 26 |
-
process = subprocess.Popen(
|
| 27 |
-
['ollama', 'serve'],
|
| 28 |
-
env=os.environ.copy(),
|
| 29 |
-
stdout=subprocess.PIPE,
|
| 30 |
-
stderr=subprocess.PIPE
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
# Wait for Ollama to start
|
| 34 |
-
for attempt in range(30):
|
| 35 |
-
try:
|
| 36 |
-
response = requests.get('http://localhost:11434/api/version', timeout=2)
|
| 37 |
-
if response.status_code == 200:
|
| 38 |
-
print(f"Ollama server started successfully! (attempt {attempt + 1})")
|
| 39 |
-
return process
|
| 40 |
-
except requests.exceptions.RequestException:
|
| 41 |
-
pass
|
| 42 |
-
|
| 43 |
-
print(f"Attempt {attempt + 1}/30: Waiting for Ollama...")
|
| 44 |
-
time.sleep(2)
|
| 45 |
-
|
| 46 |
-
# Check if process is still alive
|
| 47 |
-
if process.poll() is not None:
|
| 48 |
-
stdout, stderr = process.communicate()
|
| 49 |
-
print(f"Ollama process died. STDOUT: {stdout.decode()}")
|
| 50 |
-
print(f"STDERR: {stderr.decode()}")
|
| 51 |
-
return None
|
| 52 |
-
|
| 53 |
-
print("Failed to start Ollama server")
|
| 54 |
-
return None
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
try:
|
| 60 |
-
result = subprocess.run(
|
| 61 |
-
['ollama', 'pull', 'llava:7b'],
|
| 62 |
-
env=os.environ.copy(),
|
| 63 |
-
capture_output=True,
|
| 64 |
-
text=True,
|
| 65 |
-
timeout=600 # 10 minutes timeout
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
if result.returncode == 0:
|
| 69 |
-
print("Model pulled successfully!")
|
| 70 |
-
return True
|
| 71 |
-
else:
|
| 72 |
-
print(f"Failed to pull model: {result.stderr}")
|
| 73 |
-
return False
|
| 74 |
-
except subprocess.TimeoutExpired:
|
| 75 |
-
print("Model pull timed out")
|
| 76 |
-
return False
|
| 77 |
-
except Exception as e:
|
| 78 |
-
print(f"Error pulling model: {e}")
|
| 79 |
-
return False
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
print("Starting FastAPI server on port 7860...")
|
| 84 |
-
os.chdir('/app')
|
| 85 |
-
|
| 86 |
-
# Start FastAPI
|
| 87 |
-
subprocess.run([
|
| 88 |
-
'python3', '-m', 'uvicorn',
|
| 89 |
-
'fast:app',
|
| 90 |
-
'--host', '0.0.0.0',
|
| 91 |
-
'--port', '7860'
|
| 92 |
-
])
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
print("Failed to start Ollama. Exiting.")
|
| 105 |
-
sys.exit(1)
|
| 106 |
-
|
| 107 |
-
# Pull model
|
| 108 |
-
pull_model() # Continue even if this fails
|
| 109 |
-
|
| 110 |
-
# Start FastAPI
|
| 111 |
-
start_fastapi()
|
| 112 |
|
| 113 |
-
if
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
# Set Ollama environment variables
|
| 4 |
+
export OLLAMA_HOST=0.0.0.0:11434
|
| 5 |
+
export OLLAMA_ORIGINS="*"
|
| 6 |
+
export OLLAMA_DATA_DIR=/tmp/.ollama
|
| 7 |
+
export OLLAMA_MODELS=/tmp/.ollama/models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Create directories with proper permissions
|
| 10 |
+
mkdir -p /tmp/.ollama/models
|
| 11 |
+
chmod 755 /tmp/.ollama
|
| 12 |
+
chmod 755 /tmp/.ollama/models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Start Ollama server in background with explicit data directory
|
| 15 |
+
echo "Starting Ollama server..."
|
| 16 |
+
OLLAMA_DATA_DIR=/tmp/.ollama ollama serve &
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Store the PID
|
| 19 |
+
OLLAMA_PID=$!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Wait for Ollama to start
|
| 22 |
+
echo "Waiting for Ollama server to start..."
|
| 23 |
+
for i in {1..30}; do
|
| 24 |
+
if curl -s http://localhost:11434/api/version > /dev/null 2>&1; then
|
| 25 |
+
echo "Ollama server started successfully!"
|
| 26 |
+
break
|
| 27 |
+
fi
|
| 28 |
+
echo "Attempt $i/30: Waiting for Ollama..."
|
| 29 |
+
sleep 2
|
| 30 |
+
done
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Check if Ollama actually started
|
| 33 |
+
if ! curl -s http://localhost:11434/api/version > /dev/null 2>&1; then
|
| 34 |
+
echo "Error: Ollama server failed to start"
|
| 35 |
+
exit 1
|
| 36 |
+
fi
|
| 37 |
+
|
| 38 |
+
# Pull the LLaVA model for vision analysis
|
| 39 |
+
echo "Pulling LLaVA model for vision analysis..."
|
| 40 |
+
OLLAMA_DATA_DIR=/tmp/.ollama ollama pull llava:7b
|
| 41 |
+
|
| 42 |
+
if [ $? -eq 0 ]; then
|
| 43 |
+
echo "Model pulled successfully!"
|
| 44 |
+
else
|
| 45 |
+
echo "Warning: Failed to pull model, but continuing..."
|
| 46 |
+
fi
|
| 47 |
+
|
| 48 |
+
# Start FastAPI on port 7860 (HF Spaces requirement)
|
| 49 |
+
echo "Starting FastAPI server on port 7860..."
|
| 50 |
+
cd /app
|
| 51 |
+
python3 -m uvicorn fast:app --host 0.0.0.0 --port 7860
|