Spaces:
Paused
Paused
| # βββ Base image βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| FROM python:3.12-slim | |
| # βββ System packages ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # poppler-utils β pdf2image (PDF rendering) | |
| # tesseract-ocr β pytesseract (OCR) | |
| # curl β Ollama installer + health checks | |
| RUN apt-get update && apt-get install -y --no-install-recommends \ | |
| curl \ | |
| zstd \ | |
| poppler-utils \ | |
| tesseract-ocr \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # βββ Install Ollama βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| RUN curl -fsSL https://ollama.ai/install.sh | sh | |
| # βββ Working directory ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| WORKDIR /app | |
| # βββ Python dependencies ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Install CPU-only torch first to avoid pulling the huge CUDA wheel. | |
| # requirements.txt has `torch>=2.1.0`; pip will see 2.3.0+cpu already satisfies it. | |
| RUN pip install --no-cache-dir \ | |
| "torch==2.3.0+cpu" "torchvision==0.18.0+cpu" \ | |
| --index-url https://download.pytorch.org/whl/cpu | |
| COPY requirements.txt . | |
| RUN pip install --no-cache-dir -r requirements.txt | |
| # βββ Pre-download embedding model (cached before COPY . . so code changes don't bust this layer) βββ | |
| ENV SENTENCE_TRANSFORMERS_HOME=/app/.cache/sentence_transformers | |
| RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')" | |
| # βββ Application code βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| COPY . . | |
| # βββ Runtime directories & permissions βββββββββββββββββββββββββββββββββββββββ | |
| # /app/data β uploaded documents | |
| # /app/vectorstore β ChromaDB persistent store | |
| # /app/.ollama β Ollama model cache (survives within the same container) | |
| RUN mkdir -p data vectorstore .ollama && \ | |
| chmod -R 777 data vectorstore .ollama | |
| # Tell Ollama where to store model weights | |
| ENV OLLAMA_MODELS=/app/.ollama | |
| # Default model β override via Space secret OLLAMA_MODEL | |
| ENV OLLAMA_MODEL=llama3.2 | |
| ENV OLLAMA_NUM_CTX=8192 | |
| # βββ HuggingFace Spaces requires port 7860 βββββββββββββββββββββββββββββββββββ | |
| EXPOSE 7860 | |
| # βββ Startup βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # If GROQ_API_KEY is set (HF Space): skip Ollama entirely, start app directly. | |
| # Otherwise (local / no Groq): start Ollama, wait for it, then start app. | |
| CMD ["/bin/bash", "-c", "\ | |
| if [ -n \"$GROQ_API_KEY\" ]; then \ | |
| echo 'β Groq mode β skipping Ollama.' && \ | |
| exec python app.py; \ | |
| else \ | |
| ollama serve & \ | |
| echo 'β³ Waiting for Ollama...' && \ | |
| until curl -sf http://localhost:11434/api/version > /dev/null 2>&1; do sleep 1; done && \ | |
| echo 'β Ollama ready.' && \ | |
| exec python app.py; \ | |
| fi\ | |
| "] | |
| # backend.py = FastAPI REST API | frontend.py = Gradio UI | app.py = entrypoint | |