FROM python:3.10-slim WORKDIR /app # Install system dependencies including FFmpeg RUN apt-get update && apt-get install -y --no-install-recommends \ ffmpeg \ build-essential \ git \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* # Create a non-root user to run the application RUN useradd -m appuser # Set environment variables for controlling cache locations ENV TRANSFORMERS_CACHE=/tmp/.cache/huggingface/transformers ENV HF_HOME=/tmp/.cache/huggingface ENV HF_DATASETS_CACHE=/tmp/.cache/huggingface/datasets ENV SENTENCE_TRANSFORMERS_HOME=/tmp/.cache/torch/sentence_transformers ENV TORCH_HOME=/tmp/.cache/torch # Create cache directories with proper permissions RUN mkdir -p /tmp/.cache/huggingface/transformers && \ mkdir -p /tmp/.cache/huggingface/datasets && \ mkdir -p /tmp/.cache/torch/sentence_transformers && \ mkdir -p /tmp/.cache/torch/hub && \ chmod -R 777 /tmp/.cache # Copy requirements first for better caching COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Install TensorFlow (CPU version to save space) RUN pip install --no-cache-dir tensorflow-cpu==2.12.0 # Install additional dependencies directly RUN pip install --no-cache-dir sentence-transformers>=2.2.2 # Download sentence-transformers model RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')" # Run NLTK downloads RUN python -m nltk.downloader punkt wordnet # Copy the rest of the application COPY . . # Make sure the non-root user can access the application files RUN chown -R appuser:appuser /app RUN chmod -R 755 /app # Switch to non-root user USER appuser # Expose the port Hugging Face Spaces expects (7860) EXPOSE 7860 # Run using uvicorn CMD ["uvicorn", "fastapi_app:app", "--host", "0.0.0.0", "--port", "7860"]