# Hugging Face Space (SDK: docker) -- listens on 7860. # # NOTE: python:3.9 from the stock HF template does NOT work for this app. # FastAPI/Pydantic evaluate annotations like `str | None` at runtime, which # requires Python 3.10+. 3.11 matches the development environment. FROM python:3.11-slim WORKDIR /code # Install CPU-only torch first -- the default PyPI wheel pulls ~2.5GB of CUDA # libraries that a CPU Space can never use. RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu COPY ./requirements.txt /code/requirements.txt RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt # Set up a new user named "user" with user ID 1000 RUN useradd -m -u 1000 user USER user ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH # Model + SQLite both need writable paths. On a Space, $HOME is the only one. ENV HF_HOME=/home/user/.cache/huggingface \ DATA_DIR=/home/user/app/data \ MODEL_NAME=typeform/distilbert-base-uncased-mnli \ PRELOAD_MODEL=1 \ PYTHONUNBUFFERED=1 WORKDIR $HOME/app # Bake the model into the image so the Space answers the first request fast # instead of downloading ~250MB on cold start. RUN python -c "\ from transformers import pipeline; \ import os; \ pipeline('zero-shot-classification', model=os.environ['MODEL_NAME'], device=-1)" \ && echo "model cached" COPY --chown=user . $HOME/app RUN mkdir -p $HOME/app/data EXPOSE 7860 # app.main:app -- the FastAPI instance lives in the `app` package, not main.py CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]