# Use python 3.10 or 3.11 FROM python:3.11-slim # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 ENV MLP_MODEL_PATH=ai_voice_detection/artifacts/best_mlp_classifier.pt # Set work directory WORKDIR /app # Install system dependencies (required for torchaudio to load mp3) RUN apt-get update && apt-get install -y \ ffmpeg \ libsndfile1 \ && rm -rf /var/lib/apt/lists/* # Create a user to avoid running as root (good practice for HF spaces) RUN useradd -m -u 1000 user && \ chown -R user:user /app USER user ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH # Install dependencies COPY --chown=user requirements.txt . RUN pip install --no-cache-dir --upgrade pip && \ # Install CPU-only PyTorch first (significantly reduces image size) pip install --no-cache-dir "torch>=2.2.0" "torchaudio>=2.2.0" --index-url https://download.pytorch.org/whl/cpu && \ # Explicitly install pydub to ensure it's present pip install --no-cache-dir pydub && \ # Install other dependencies, excluding ONLY torch to avoid overwriting with CUDA version grep -v "torch" requirements.txt > requirements_no_torch.txt && \ pip install --no-cache-dir -r requirements_no_torch.txt # Copy application code # We copy the entire current directory into /app. # HF Spaces usually mounts the repo at /app anyway, but COPY is safer for docker builds. COPY --chown=user . . # Expose port 7860 for Hugging Face Spaces EXPOSE 7860 # Command to run the application # We run from /app, so 'ai_voice_detection.api.main' is the module path relative to workdir # OR if we cd into ai_voice_detection... # Let's import as module. The structure is: # /app/ai_voice_detection/api/main.py # /app/ai_voice_detection/inference/... # So python path /app should work. # "api.main" implies ai_voice_detection is not part of module path if we are in ai_voice_detection. # If we are in /app, it should be "ai_voice_detection.api.main:app". # Let's adjust main.py imports check? # main.py does "from inference.predict import ...". # This typically requires that the parent directory of 'inference' is in PYTHONPATH. # That is /app/ai_voice_detection. # So we should probably set WORKDIR to /app/ai_voice_detection or add it to PYTHONPATH. # Setting PYTHONPATH is cleaner. ENV PYTHONPATH=$PYTHONPATH:/app/ai_voice_detection CMD ["uvicorn", "ai_voice_detection.api.main:app", "--host", "0.0.0.0", "--port", "7860"]