File size: 1,716 Bytes
128ea0c
 
00056d8
128ea0c
 
 
 
00056d8
 
 
128ea0c
 
 
00056d8
 
128ea0c
 
00056d8
 
 
 
 
 
 
 
 
 
 
 
128ea0c
 
 
 
00056d8
 
 
 
 
128ea0c
00056d8
128ea0c
 
 
00056d8
 
128ea0c
00056d8
 
 
 
 
 
 
 
 
 
 
128ea0c
00056d8
128ea0c
00056d8
128ea0c
 
00056d8
 
128ea0c
 
 
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
FROM python:3.11-slim

# Install system dependencies
RUN apt-get update && apt-get install -y \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Create a non-root user
RUN useradd -m -u 1000 user
USER user
WORKDIR /app

# Copy application files
COPY --chown=user:user requirements.txt .
COPY --chown=user:user main.py .

# Install Python dependencies
RUN pip install --user --no-cache-dir -r requirements.txt

# Create app directory and set permissions
RUN mkdir -p /home/user/app && chown user:user /home/user/app

# Install Ollama for user
RUN curl -fsSL https://ollama.ai/install.sh | sh

# Set environment variables
ENV PATH="/home/user/.local/bin:${PATH}"
ENV OLLAMA_HOST="0.0.0.0"
ENV HOME="/home/user"

# Create startup script
RUN echo '#!/bin/bash\n\
\n\
echo "=== Starting Ollama FastAPI Server on Hugging Face === "\n\
\n\
# Set Ollama data directory to user home\n\
export OLLAMA_MODELS="/home/user/.ollama/models"\n\
\n\
# Start Ollama in background\n\
echo "Starting Ollama service..."\n\
ollama serve &\n\
\n\
# Wait for Ollama to start\n\
echo "Waiting for Ollama to start..."\n\
sleep 15\n\
\n\
# Check if Ollama is running\n\
if curl -s http://localhost:11434/api/tags > /dev/null; then\n\
    echo "Ollama is running!"\n\
    \n\
    # Pull a small model\n\
    echo "Pulling tinyllama:1.1b model..."\n\
    ollama pull tinyllama:1.1b &\n\
    \n\
else\n\
    echo "Ollama failed to start. Starting FastAPI without Ollama..."\n\
fi\n\
\n\
echo "Starting FastAPI server on port 7860..."\n\
# Start FastAPI server\n\
python -m uvicorn main:app --host 0.0.0.0 --port 7860\n\
' > start.sh && chmod +x start.sh

# Expose port (HF Spaces uses 7860)
EXPOSE 7860

# Start the application
CMD ["./start.sh"]