Spaces:
Running
Running
Kang Suhyun
commited on
[#69] Set GOOGLE_APPLICATION_CREDENTIALS with a temp credentials file (#70)
Browse files* [#69] Set GOOGLE_APPLICATION_CREDENTIALS with a temp credentials file
This change sets the GOOGLE_APPLICATION_CREDENTIALS environment variable to a temporary file that contains the service account credentials.
* Apply code review
- app.py +4 -0
- credentials.py +23 -0
app.py
CHANGED
@@ -8,6 +8,8 @@ from firebase_admin import firestore
|
|
8 |
import gradio as gr
|
9 |
import lingua
|
10 |
|
|
|
|
|
11 |
from leaderboard import build_leaderboard
|
12 |
from leaderboard import db
|
13 |
from leaderboard import SUPPORTED_TRANSLATION_LANGUAGES
|
@@ -186,6 +188,8 @@ with gr.Blocks(title="Arena", css=css) as app:
|
|
186 |
build_leaderboard()
|
187 |
|
188 |
if __name__ == "__main__":
|
|
|
|
|
189 |
# We need to enable queue to use generators.
|
190 |
app.queue()
|
191 |
app.launch(debug=True)
|
|
|
8 |
import gradio as gr
|
9 |
import lingua
|
10 |
|
11 |
+
import credentials
|
12 |
+
from credentials import set_credentials
|
13 |
from leaderboard import build_leaderboard
|
14 |
from leaderboard import db
|
15 |
from leaderboard import SUPPORTED_TRANSLATION_LANGUAGES
|
|
|
188 |
build_leaderboard()
|
189 |
|
190 |
if __name__ == "__main__":
|
191 |
+
set_credentials(credentials.CREDENTIALS, credentials.CREDENTIALS_PATH)
|
192 |
+
|
193 |
# We need to enable queue to use generators.
|
194 |
app.queue()
|
195 |
app.launch(debug=True)
|
credentials.py
CHANGED
@@ -5,6 +5,8 @@ required for authentication with GCP services.
|
|
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")
|
@@ -13,6 +15,27 @@ CREDENTIALS_PATH = os.environ.get("CREDENTIALS_PATH")
|
|
13 |
CREDENTIALS = os.environ.get("CREDENTIALS")
|
14 |
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def get_credentials_json():
|
17 |
if not CREDENTIALS and not CREDENTIALS_PATH:
|
18 |
raise ValueError(
|
|
|
5 |
|
6 |
import json
|
7 |
import os
|
8 |
+
import stat
|
9 |
+
import tempfile
|
10 |
|
11 |
# Path to local credentials file, used in local development.
|
12 |
CREDENTIALS_PATH = os.environ.get("CREDENTIALS_PATH")
|
|
|
15 |
CREDENTIALS = os.environ.get("CREDENTIALS")
|
16 |
|
17 |
|
18 |
+
def set_credentials(credentials: str = None, credentials_path: str = None):
|
19 |
+
if not credentials and not credentials_path:
|
20 |
+
raise ValueError(
|
21 |
+
"No credentials found. Ensure credentials or credentials path is set.")
|
22 |
+
|
23 |
+
if credentials_path:
|
24 |
+
if os.path.exists(credentials_path):
|
25 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credentials_path
|
26 |
+
return
|
27 |
+
raise FileNotFoundError(f"Credentials file not found: {credentials_path}")
|
28 |
+
|
29 |
+
# Create a temporary file to store credentials for
|
30 |
+
# services that don't accept string format credentials.
|
31 |
+
with tempfile.NamedTemporaryFile(mode="w", suffix=".json",
|
32 |
+
delete=False) as cred_file:
|
33 |
+
cred_file.write(credentials)
|
34 |
+
os.chmod(cred_file.name, stat.S_IRUSR)
|
35 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = cred_file.name
|
36 |
+
|
37 |
+
|
38 |
+
# TODO(#69): Replace get_credentials_json with set_credentials.
|
39 |
def get_credentials_json():
|
40 |
if not CREDENTIALS and not CREDENTIALS_PATH:
|
41 |
raise ValueError(
|