Streamlit Docker app runs very slowly


I have a Docker Streamlit app that runs perfectly fine/fast locally or on AWS Fargate but runs very slowly on a Hugging Face space (CPU UPGRADE). The build is successful and all pages work fine, it is just very slow!

The app reads a small data sample from an AWS S3 bucket located in Australia so I do expect additional region latency once deployed on Hugging Face, but not as much as the 10x that I’m observing…

This is a private space so cannot link to the codebase but below is the Dockerfile:

FROM python:3.10


# install git-lfs
RUN apt-get update && apt-get install -y \
    git-lfs \
    && rm -rf /var/lib/apt/lists/* \
    && git lfs install

# install the AWS CLI
RUN curl "" -o "" \
    && unzip \
    && ./aws/install \
    && rm -rf aws

# install the app Python dependencies
COPY requirements.txt ./requirements.txt
RUN pip3 install  --no-cache-dir --upgrade -r requirements.txt

# create a non-root user
RUN useradd -m -u 1000 user
USER user
ENV HOME /home/user
ENV PATH $HOME/.local/bin:$PATH

# expose secrets as environment variables
RUN --mount=type=secret,id=GC_PROJECT,mode=0444,required=true \
  export GC_PROJECT=$(cat /run/secrets/GC_PROJECT)
RUN --mount=type=secret,id=GC_SA_ENCODED_KEY,mode=0444,required=true \
  export GC_SA_ENCODED_KEY=$(cat /run/secrets/GC_SA_ENCODED_KEY)
RUN --mount=type=secret,id=AWS_ACCESS_KEY_ID,mode=0444,required=true \
  export AWS_ACCESS_KEY_ID=$(cat /run/secrets/AWS_ACCESS_KEY_ID)
RUN --mount=type=secret,id=AWS_SECRET_ACCESS_KEY,mode=0444,required=true \
  export AWS_SECRET_ACCESS_KEY=$(cat /run/secrets/AWS_SECRET_ACCESS_KEY)
RUN --mount=type=secret,id=AWS_REGION,mode=0444,required=true \
  export AWS_REGION=$(cat /run/secrets/AWS_REGION)

# configure outerbounds
RUN --mount=type=secret,id=OUTERBOUNDS_SERVICE_TOKEN,mode=0444,required=true \
  outerbounds configure $(cat /run/secrets/OUTERBOUNDS_SERVICE_TOKEN)

# copy the app files
RUN mkdir app
COPY ./pages ./pages
COPY ./utils ./utils
COPY ./ ./
COPY ./ ./

# run the app
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
CMD streamlit run \
    --server.headless true \
    --server.enableCORS false \
    --server.enableXsrfProtection false \
    --server.fileWatcherType none \
    --browser.gatherUsageStats false \
    --server.maxUploadSize 200