Sonu Prasad
Optimize backend for production
4c6e0cc
FROM python:3.11-slim
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
libmagic1 \
git \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /code
# Set environment variables for cache directories
ENV HF_HOME=/tmp/huggingface_cache
ENV SENTENCE_TRANSFORMERS_HOME=/tmp/huggingface_cache
ENV TRANSFORMERS_CACHE=/tmp/huggingface_cache
# Copy requirements first for better caching
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
# Pre-download the embedding model
RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2', cache_folder='/tmp/huggingface_cache/sentence_transformers')"
# Copy application code
COPY ./shared.py /code/shared.py
COPY ./ai_core.py /code/ai_core.py
COPY ./main.py /code/main.py
# Expose port for Hugging Face Spaces
EXPOSE 7860
# Run with uvicorn (single worker to avoid multiprocessing issues in HF Spaces)
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]