Kang Suhyun suhyun.kang commited on
Commit
f796553
1 Parent(s): 34c6b56

[#23] Set GCP credentials (#24)

Browse files

* [#23] Set GCP credentials

To deploy the app to Hugging Face Spaces, we need to set the GCP credentials.

* document

---------

Co-authored-by: suhyun.kang <suhyun.kang@yanolja.group>

Files changed (4) hide show
  1. .gitignore +1 -0
  2. README.md +15 -2
  3. app.py +22 -2
  4. requirements.txt +5 -0
.gitignore CHANGED
@@ -1,3 +1,4 @@
1
  venv
2
  *.log
3
  __pycache__
 
 
1
  venv
2
  *.log
3
  __pycache__
4
+ credentials.json
README.md CHANGED
@@ -19,9 +19,22 @@
19
  Set your OpenAI API key as an environment variable and start the application:
20
 
21
  ```shell
22
- OPENAI_API_KEY=<your key> python3 app.py
23
  ```
24
 
25
- Replace `<your key>` with your GCP project ID.
26
 
27
  > To run the app with [auto-reloading](https://www.gradio.app/guides/developing-faster-with-reload-mode), use `gradio app.py --demo-name app` instead of `python3 app.py`.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  Set your OpenAI API key as an environment variable and start the application:
20
 
21
  ```shell
22
+ GOOGLE_CLOUD_PROJECT=<your project id> CREDENTIALS_PATH=<your crednetials path> OPENAI_API_KEY=<your key> python3 app.py
23
  ```
24
 
25
+ Replace `<your project id>`, `<your crednetials path>`, and `<your key>` with your GCP project ID, the path to your GCP credentials file, and your OpenAI API key respectively.
26
 
27
  > To run the app with [auto-reloading](https://www.gradio.app/guides/developing-faster-with-reload-mode), use `gradio app.py --demo-name app` instead of `python3 app.py`.
28
+
29
+ ## Handling GCP credentials for development and deployment
30
+
31
+ ### Local environment
32
+
33
+ 1. Store your credentials in a file on your local machine.
34
+ 1. Set the `CREDENTIALS_PATH` environment variable to point to this file.
35
+ 1. The application will read the credentials from this file when running locally.
36
+
37
+ ### Deployment environment
38
+
39
+ 1. Set the `CREDENTIALS` environment variable in your deployment platform's settings to your credentials JSON string.
40
+ 2. The application will parse and use these credentials when deployed.
app.py CHANGED
@@ -1,11 +1,13 @@
1
  """
2
  It provides a platform for comparing the responses of two LLMs.
3
  """
4
-
5
  import enum
 
 
6
  from uuid import uuid4
7
 
8
  import firebase_admin
 
9
  from firebase_admin import firestore
10
  import gradio as gr
11
 
@@ -13,8 +15,26 @@ from leaderboard import build_leaderboard
13
  import response
14
  from response import get_responses
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # TODO(#21): Fix auto-reload issue related to the initialization of Firebase.
17
- db_app = firebase_admin.initialize_app()
18
  db = firestore.client()
19
 
20
  SUPPORTED_TRANSLATION_LANGUAGES = [
 
1
  """
2
  It provides a platform for comparing the responses of two LLMs.
3
  """
 
4
  import enum
5
+ import json
6
+ import os
7
  from uuid import uuid4
8
 
9
  import firebase_admin
10
+ from firebase_admin import credentials
11
  from firebase_admin import firestore
12
  import gradio as gr
13
 
 
15
  import response
16
  from response import get_responses
17
 
18
+ # Path to local credentials file, used in local development.
19
+ CREDENTIALS_PATH = os.environ.get("CREDENTIALS_PATH")
20
+
21
+ # Credentials passed as an environment variable, used in deployment.
22
+ CREDENTIALS = os.environ.get("CREDENTIALS")
23
+
24
+
25
+ def get_credentials():
26
+ # Set credentials using a file in a local environment, if available.
27
+ if CREDENTIALS_PATH and os.path.exists(CREDENTIALS_PATH):
28
+ return credentials.Certificate(CREDENTIALS_PATH)
29
+
30
+ # Use environment variable for credentials when the file is not found,
31
+ # as credentials should not be public.
32
+ json_cred = json.loads(CREDENTIALS)
33
+ return credentials.Certificate(json_cred)
34
+
35
+
36
  # TODO(#21): Fix auto-reload issue related to the initialization of Firebase.
37
+ firebase_admin.initialize_app(get_credentials())
38
  db = firestore.client()
39
 
40
  SUPPORTED_TRANSLATION_LANGUAGES = [
requirements.txt CHANGED
@@ -27,14 +27,18 @@ google-api-core==2.16.2
27
  google-api-python-client==2.116.0
28
  google-auth==2.27.0
29
  google-auth-httplib2==0.2.0
 
 
30
  google-cloud-core==2.4.1
31
  google-cloud-firestore==2.14.0
 
32
  google-cloud-storage==2.14.0
33
  google-crc32c==1.5.0
34
  google-resumable-media==2.7.0
35
  googleapis-common-protos==1.62.0
36
  gradio==4.16.0
37
  gradio_client==0.8.1
 
38
  grpcio==1.60.1
39
  grpcio-status==1.60.1
40
  h11==0.14.0
@@ -86,6 +90,7 @@ rpds-py==0.17.1
86
  rsa==4.9
87
  ruff==0.2.0
88
  semantic-version==2.10.0
 
89
  shellingham==1.5.4
90
  six==1.16.0
91
  sniffio==1.3.0
 
27
  google-api-python-client==2.116.0
28
  google-auth==2.27.0
29
  google-auth-httplib2==0.2.0
30
+ google-cloud-aiplatform==1.40.0
31
+ google-cloud-bigquery==3.17.1
32
  google-cloud-core==2.4.1
33
  google-cloud-firestore==2.14.0
34
+ google-cloud-resource-manager==1.12.0
35
  google-cloud-storage==2.14.0
36
  google-crc32c==1.5.0
37
  google-resumable-media==2.7.0
38
  googleapis-common-protos==1.62.0
39
  gradio==4.16.0
40
  gradio_client==0.8.1
41
+ grpc-google-iam-v1==0.13.0
42
  grpcio==1.60.1
43
  grpcio-status==1.60.1
44
  h11==0.14.0
 
90
  rsa==4.9
91
  ruff==0.2.0
92
  semantic-version==2.10.0
93
+ shapely==2.0.2
94
  shellingham==1.5.4
95
  six==1.16.0
96
  sniffio==1.3.0