Spaces:
Running
Running
Kang Suhyun
suhyun.kang
commited on
[#48] Fix Sceret Manager initialization failure on Space (#49)
Browse files* [#48] Fix Sceret Manager initialization failure on Space
Changes:
- Add proper credentials to Secret Manager
- Add credentials module to handle credentials
* Use oauth
---------
Co-authored-by: suhyun.kang <[email protected]>
- credentials.py +31 -0
- leaderboard.py +2 -20
- response.py +9 -4
credentials.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This module handles the retrieval of credentials
|
3 |
+
required for authentication with GCP services.
|
4 |
+
"""
|
5 |
+
|
6 |
+
import json
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Path to local credentials file, used in local development.
|
10 |
+
CREDENTIALS_PATH = os.environ.get("CREDENTIALS_PATH")
|
11 |
+
|
12 |
+
# Credentials passed as an environment variable, used in deployment.
|
13 |
+
CREDENTIALS = os.environ.get("CREDENTIALS")
|
14 |
+
|
15 |
+
|
16 |
+
def get_credentials_json():
|
17 |
+
if not CREDENTIALS and not CREDENTIALS_PATH:
|
18 |
+
raise ValueError(
|
19 |
+
"No credentials found. Ensure CREDENTIALS or CREDENTIALS_PATH is set.")
|
20 |
+
|
21 |
+
# Use the environment variable for credentials when a file cannot be used
|
22 |
+
# in the environment, as credentials should not be made public.
|
23 |
+
if CREDENTIALS:
|
24 |
+
return json.loads(CREDENTIALS)
|
25 |
+
|
26 |
+
if not os.path.exists(CREDENTIALS_PATH):
|
27 |
+
raise FileNotFoundError(f"Credentials file not found: {CREDENTIALS_PATH}")
|
28 |
+
|
29 |
+
# Set credentials using a file in a local environment.
|
30 |
+
with open(CREDENTIALS_PATH, "r", encoding="utf-8") as cred_file:
|
31 |
+
return json.load(cred_file)
|
leaderboard.py
CHANGED
@@ -4,9 +4,7 @@ It provides a leaderboard component.
|
|
4 |
|
5 |
from collections import defaultdict
|
6 |
import enum
|
7 |
-
import json
|
8 |
import math
|
9 |
-
import os
|
10 |
|
11 |
import firebase_admin
|
12 |
from firebase_admin import credentials
|
@@ -14,26 +12,10 @@ from firebase_admin import firestore
|
|
14 |
import gradio as gr
|
15 |
import pandas as pd
|
16 |
|
17 |
-
|
18 |
-
CREDENTIALS_PATH = os.environ.get("CREDENTIALS_PATH")
|
19 |
-
|
20 |
-
# Credentials passed as an environment variable, used in deployment.
|
21 |
-
CREDENTIALS = os.environ.get("CREDENTIALS")
|
22 |
-
|
23 |
-
|
24 |
-
def get_credentials():
|
25 |
-
# Set credentials using a file in a local environment, if available.
|
26 |
-
if CREDENTIALS_PATH and os.path.exists(CREDENTIALS_PATH):
|
27 |
-
return credentials.Certificate(CREDENTIALS_PATH)
|
28 |
-
|
29 |
-
# Use environment variable for credentials when the file is not found,
|
30 |
-
# as credentials should not be public.
|
31 |
-
json_cred = json.loads(CREDENTIALS)
|
32 |
-
return credentials.Certificate(json_cred)
|
33 |
-
|
34 |
|
35 |
# TODO(#21): Fix auto-reload issue related to the initialization of Firebase.
|
36 |
-
firebase_admin.initialize_app(
|
37 |
db = firestore.client()
|
38 |
|
39 |
|
|
|
4 |
|
5 |
from collections import defaultdict
|
6 |
import enum
|
|
|
7 |
import math
|
|
|
8 |
|
9 |
import firebase_admin
|
10 |
from firebase_admin import credentials
|
|
|
12 |
import gradio as gr
|
13 |
import pandas as pd
|
14 |
|
15 |
+
from credentials import get_credentials_json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# TODO(#21): Fix auto-reload issue related to the initialization of Firebase.
|
18 |
+
firebase_admin.initialize_app(credentials.Certificate(get_credentials_json()))
|
19 |
db = firestore.client()
|
20 |
|
21 |
|
response.py
CHANGED
@@ -8,16 +8,21 @@ import os
|
|
8 |
from random import sample
|
9 |
|
10 |
from google.cloud import secretmanager
|
|
|
11 |
import gradio as gr
|
12 |
from litellm import completion
|
13 |
|
|
|
|
|
14 |
GOOGLE_CLOUD_PROJECT = os.environ.get("GOOGLE_CLOUD_PROJECT")
|
15 |
MODELS_SECRET = os.environ.get("MODELS_SECRET")
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
decoded_secret = models_secret.payload.data.decode("UTF-8")
|
22 |
|
23 |
supported_models = json.loads(decoded_secret)
|
|
|
8 |
from random import sample
|
9 |
|
10 |
from google.cloud import secretmanager
|
11 |
+
from google.oauth2 import service_account
|
12 |
import gradio as gr
|
13 |
from litellm import completion
|
14 |
|
15 |
+
from credentials import get_credentials_json
|
16 |
+
|
17 |
GOOGLE_CLOUD_PROJECT = os.environ.get("GOOGLE_CLOUD_PROJECT")
|
18 |
MODELS_SECRET = os.environ.get("MODELS_SECRET")
|
19 |
|
20 |
+
secretmanager_client = secretmanager.SecretManagerServiceClient(
|
21 |
+
credentials=service_account.Credentials.from_service_account_info(
|
22 |
+
get_credentials_json()))
|
23 |
+
models_secret = secretmanager_client.access_secret_version(
|
24 |
+
name=secretmanager_client.secret_version_path(GOOGLE_CLOUD_PROJECT,
|
25 |
+
MODELS_SECRET, "latest"))
|
26 |
decoded_secret = models_secret.payload.data.decode("UTF-8")
|
27 |
|
28 |
supported_models = json.loads(decoded_secret)
|