Ashmi Banerjee commited on
Commit
a2de729
·
1 Parent(s): c59035e

updated gemini service creds

Browse files
Files changed (4) hide show
  1. .gitignore +1 -1
  2. app.py +1 -1
  3. setup/convert_creds.py +26 -0
  4. setup/vertex_ai_setup.py +10 -2
.gitignore CHANGED
@@ -181,4 +181,4 @@ __pycache__/
181
  gradio_cached_examples/
182
  models/__pycache__/
183
  setup/__pycache__/
184
- setup/gcp-default-creds.json
 
181
  gradio_cached_examples/
182
  models/__pycache__/
183
  setup/__pycache__/
184
+ setup/gcp_default_creds.json
app.py CHANGED
@@ -20,7 +20,7 @@ def generate_text(query_text, model_name: Optional[str] = "google/gemma-2b-it"):
20
 
21
  examples = [["I'm planning a vacation to France. Can you suggest a one-week itinerary including must-visit places and "
22
  "local cuisines to try?", "google/gemma-2b-it"],
23
- # ["I want to explore off-the-beaten-path destinations in Europe, any suggestions?", "gemini-1.0-pro"],
24
  ["Suggest some cities that can be visited from London and are very rich in history and culture.",
25
  "google/gemma-2b-it"],
26
  ]
 
20
 
21
  examples = [["I'm planning a vacation to France. Can you suggest a one-week itinerary including must-visit places and "
22
  "local cuisines to try?", "google/gemma-2b-it"],
23
+ ["I want to explore off-the-beaten-path destinations in Europe, any suggestions?", "gemini-1.0-pro"],
24
  ["Suggest some cities that can be visited from London and are very rich in history and culture.",
25
  "google/gemma-2b-it"],
26
  ]
setup/convert_creds.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import base64
3
+ import os
4
+ import dotenv
5
+
6
+ # load the service account key
7
+ with open(os.getcwd() +'/gcp_default_creds.json') as f:
8
+ service_key = json.load(f)
9
+
10
+ dotenv_file = dotenv.find_dotenv()
11
+ dotenv.load_dotenv(dotenv_file)
12
+
13
+ # convert json to a string
14
+ service_key = json.dumps(service_key)
15
+
16
+ # encode service key
17
+ encoded_service_key = base64.b64encode(service_key.encode('utf-8'))
18
+ encoded_service_key = str(encoded_service_key)[2:-1]
19
+
20
+ print(encoded_service_key)
21
+ # b'many_characters_here'
22
+ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = encoded_service_key
23
+ print(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) # outputs 'newvalue'
24
+
25
+ # Write changes to .env file.
26
+ dotenv.set_key(dotenv_file, "GOOGLE_APPLICATION_CREDENTIALS", os.environ["GOOGLE_APPLICATION_CREDENTIALS"])
setup/vertex_ai_setup.py CHANGED
@@ -3,16 +3,24 @@ from dotenv import load_dotenv
3
  from google.oauth2 import service_account
4
  import vertexai
5
  import os
 
 
6
 
7
  load_dotenv()
8
 
9
  # TODO: fix it in spaces
10
- os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "gcp-default-creds.json"
11
- GOOGLE_APPLICATION_CREDENTIALS = os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
12
  VERTEXAI_PROJECT = os.environ["VERTEXAI_PROJECT"]
13
 
14
 
 
 
 
 
 
 
15
  def initialize_vertexai_params(location: Optional[str] = "us-central1"):
 
16
  service_account.Credentials.from_service_account_file(
17
  filename=GOOGLE_APPLICATION_CREDENTIALS,
18
  scopes=["https://www.googleapis.com/auth/cloud-platform"],
 
3
  from google.oauth2 import service_account
4
  import vertexai
5
  import os
6
+ import json
7
+ import base64
8
 
9
  load_dotenv()
10
 
11
  # TODO: fix it in spaces
12
+
 
13
  VERTEXAI_PROJECT = os.environ["VERTEXAI_PROJECT"]
14
 
15
 
16
+ def decode_service_key():
17
+ encoded_key = os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
18
+ original_service_key = json.loads(base64.b64decode(encoded_key).decode('utf-8'))
19
+ return original_service_key
20
+
21
+
22
  def initialize_vertexai_params(location: Optional[str] = "us-central1"):
23
+ GOOGLE_APPLICATION_CREDENTIALS = decode_service_key()
24
  service_account.Credentials.from_service_account_file(
25
  filename=GOOGLE_APPLICATION_CREDENTIALS,
26
  scopes=["https://www.googleapis.com/auth/cloud-platform"],