yusufs commited on
Commit
493a5f1
·
1 Parent(s): c6efe6a

feat(hf_token): set hf token during build

Browse files
Files changed (2) hide show
  1. Dockerfile +9 -7
  2. download_model.py +3 -0
Dockerfile CHANGED
@@ -1,8 +1,5 @@
1
  FROM python:3.12
2
 
3
- # Declare your environment variables with the ARG directive
4
- ARG HF_TOKEN
5
-
6
  RUN useradd -m -u 1000 user
7
  USER user
8
  ENV PATH="/home/user/.local/bin:$PATH"
@@ -14,12 +11,17 @@ RUN pip install --no-cache-dir -r requirements.txt --extra-index-url https://dow
14
 
15
  COPY --chown=user . /app
16
 
17
-
18
  # Download at build time,
19
  # to ensure during restart we won't have to wait for the download from HF (only wait for docker pull).
20
- RUN export HF_HOME=/app/.cache/huggingface
21
- RUN export HF_TOKEN=${HF_TOKEN}
22
- RUN python /app/download_model.py
 
 
 
 
 
 
23
 
24
  EXPOSE 7860
25
 
 
1
  FROM python:3.12
2
 
 
 
 
3
  RUN useradd -m -u 1000 user
4
  USER user
5
  ENV PATH="/home/user/.local/bin:$PATH"
 
11
 
12
  COPY --chown=user . /app
13
 
 
14
  # Download at build time,
15
  # to ensure during restart we won't have to wait for the download from HF (only wait for docker pull).
16
+ # In Docker Spaces, the secrets management is different for security reasons.
17
+ # Once you create a secret in the Settings tab,
18
+ # you can expose the secret by adding the following line in your Dockerfile:
19
+ #
20
+ # For example, if SECRET_EXAMPLE is the name of the secret you created in the Settings tab,
21
+ # you can read it at build time by mounting it to a file, then reading it with $(cat /run/secrets/SECRET_EXAMPLE).
22
+ # https://huggingface.co/docs/hub/en/spaces-sdks-docker#buildtime
23
+ RUN --mount=type=secret,id=HF_TOKEN,mode=0444,required=true \
24
+ HF_TOKEN=$(cat /run/secrets/HF_TOKEN) python /app/download_model.py
25
 
26
  EXPOSE 7860
27
 
download_model.py CHANGED
@@ -2,6 +2,9 @@ import os
2
  from huggingface_hub import snapshot_download
3
 
4
  hf_token: str = os.getenv("HF_TOKEN")
 
 
 
5
  hf_token = hf_token.strip()
6
  if hf_token == "":
7
  raise ValueError("HF_TOKEN is empty")
 
2
  from huggingface_hub import snapshot_download
3
 
4
  hf_token: str = os.getenv("HF_TOKEN")
5
+ if hf_token is None:
6
+ raise ValueError("HF_TOKEN is not set")
7
+
8
  hf_token = hf_token.strip()
9
  if hf_token == "":
10
  raise ValueError("HF_TOKEN is empty")