Spaces:
Running
Running
Use dedicated auth lib
Browse files- Dockerfile +13 -21
- app.py +3 -1
- download_gcs_object.py +0 -26
Dockerfile
CHANGED
@@ -1,25 +1,17 @@
|
|
1 |
-
FROM python:3.11-slim
|
2 |
-
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
COPY . .
|
|
|
|
|
|
|
6 |
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
|
7 |
EXPOSE 7860
|
8 |
-
ENV GRADIO_SERVER_NAME="0.0.0.0"
|
9 |
-
|
10 |
-
# Get service account key from HF Spaces' secrets
|
11 |
-
# https://huggingface.co/docs/hub/spaces-sdks-docker#buildtime
|
12 |
-
RUN --mount=type=secret,id=GCLOUD_SA_JSON,mode=0444,required=true \
|
13 |
-
cat /run/secrets/GCLOUD_SA_JSON > /usr/src/app/credentials.json
|
14 |
-
|
15 |
-
RUN --mount=type=secret,id=GCLOUD_OBJECT_SA_JSON,mode=0444,required=true \
|
16 |
-
cat /run/secrets/GCLOUD_OBJECT_SA_JSON > /usr/src/app/credentials_object.json
|
17 |
-
|
18 |
-
RUN --mount=type=secret,id=BUCKET_NAME,mode=0444,required=true \
|
19 |
-
--mount=type=secret,id=CLI_OBJECT_NAME,mode=0444,required=true \
|
20 |
-
python download_gcs_object.py --bucket-name $(cat /run/secrets/BUCKET_NAME) --object-name $(cat /run/secrets/CLI_OBJECT_NAME)
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
CMD ["python", "app.py"]
|
|
|
1 |
+
FROM python:3.11-slim AS gradio
|
2 |
+
RUN useradd -m -u 1000 app
|
3 |
+
USER app
|
4 |
+
ENV HOME=/home/app \
|
5 |
+
PATH=/home/app/.local/bin:$PATH
|
6 |
+
WORKDIR ${HOME}
|
7 |
COPY . .
|
8 |
+
# https://huggingface.co/docs/hub/spaces-sdks-docker#buildtime
|
9 |
+
RUN --mount=type=secret,id=EXTRA_INDEX_URL,mode=0444,required=true \
|
10 |
+
pip install --no-cache-dir --extra-index-url=$(cat /run/secrets/EXTRA_INDEX_URL) cycloud-sdk-python-auth > /dev/null 2>&1
|
11 |
RUN pip install --no-cache-dir -r requirements.txt
|
12 |
+
RUN --mount=type=secret,id=LLM_CREDENTIALS,mode=0444,required=true \
|
13 |
+
cat /run/secrets/LLM_CREDENTIALS > ${HOME}/credentials.json
|
14 |
EXPOSE 7860
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
ENV GRADIO_SERVER_NAME="0.0.0.0"
|
17 |
+
CMD ["python", "app.py"]
|
|
|
|
app.py
CHANGED
@@ -9,6 +9,7 @@ import httpx
|
|
9 |
from const import CLI_COMMAND, CSS, FOOTER, HEADER, MODELS, PLACEHOLDER
|
10 |
from openai import OpenAI
|
11 |
from PIL import Image
|
|
|
12 |
|
13 |
|
14 |
def get_token() -> str:
|
@@ -25,8 +26,9 @@ def get_token() -> str:
|
|
25 |
|
26 |
|
27 |
def get_headers() -> dict:
|
|
|
28 |
return {
|
29 |
-
"Authorization": f"Bearer {
|
30 |
"Accept": "application/json",
|
31 |
"Content-Type": "application/json",
|
32 |
}
|
|
|
9 |
from const import CLI_COMMAND, CSS, FOOTER, HEADER, MODELS, PLACEHOLDER
|
10 |
from openai import OpenAI
|
11 |
from PIL import Image
|
12 |
+
from cycloud.auth import load_default_credentials
|
13 |
|
14 |
|
15 |
def get_token() -> str:
|
|
|
26 |
|
27 |
|
28 |
def get_headers() -> dict:
|
29 |
+
creds = load_default_credentials()
|
30 |
return {
|
31 |
+
"Authorization": f"Bearer {creds.access_token}",
|
32 |
"Accept": "application/json",
|
33 |
"Content-Type": "application/json",
|
34 |
}
|
download_gcs_object.py
DELETED
@@ -1,26 +0,0 @@
|
|
1 |
-
import argparse
|
2 |
-
import json
|
3 |
-
|
4 |
-
from google.cloud import storage
|
5 |
-
from google.oauth2 import service_account
|
6 |
-
|
7 |
-
|
8 |
-
def download_gcs_object(bucket_name: str, object_name: str):
|
9 |
-
with open("/usr/src/app/credentials_object.json", "r") as f:
|
10 |
-
credentials_dict = json.load(f)
|
11 |
-
credentials = service_account.Credentials.from_service_account_info(
|
12 |
-
credentials_dict
|
13 |
-
)
|
14 |
-
client = storage.Client(
|
15 |
-
credentials=credentials, project=credentials_dict["project_id"]
|
16 |
-
)
|
17 |
-
blob = client.bucket(bucket_name).blob(object_name)
|
18 |
-
blob.download_to_filename(object_name)
|
19 |
-
|
20 |
-
|
21 |
-
if __name__ == "__main__":
|
22 |
-
parser = argparse.ArgumentParser()
|
23 |
-
parser.add_argument("--bucket-name", type=str, required=True)
|
24 |
-
parser.add_argument("--object-name", type=str, required=True)
|
25 |
-
args = parser.parse_args()
|
26 |
-
download_gcs_object(args.bucket_name, args.object_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|