ankanghosh commited on
Commit
b223511
·
verified ·
1 Parent(s): c72a64f

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +40 -3
utils.py CHANGED
@@ -5,7 +5,23 @@ import streamlit as st
5
  import openai
6
 
7
  def setup_gcp_auth():
8
- """Setup GCP authentication from HF Spaces, environment variables, or Streamlit secrets."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  try:
10
  # Option 1: HF Spaces environment variable
11
  if "GCP_CREDENTIALS" in os.environ:
@@ -59,7 +75,20 @@ def setup_gcp_auth():
59
  raise
60
 
61
  def setup_openai_auth():
62
- """Setup OpenAI API authentication from environment variables or Streamlit secrets."""
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  try:
64
  # Option 1: Standard environment variable
65
  if "OPENAI_API_KEY" in os.environ:
@@ -89,7 +118,15 @@ def setup_openai_auth():
89
  raise
90
 
91
  def setup_all_auth():
92
- """Setup all authentication in one call"""
 
 
 
 
 
 
 
 
93
  gcp_creds = setup_gcp_auth()
94
  setup_openai_auth()
95
  return gcp_creds
 
5
  import openai
6
 
7
  def setup_gcp_auth():
8
+ """
9
+ Setup Google Cloud Platform authentication using various methods.
10
+
11
+ This function tries multiple authentication methods in order of preference:
12
+ 1. HF Spaces environment variable (GCP_CREDENTIALS)
13
+ 2. Local environment variable pointing to a credentials file (GOOGLE_APPLICATION_CREDENTIALS)
14
+ 3. Streamlit secrets (gcp_credentials)
15
+
16
+ Each method is tried in sequence until one succeeds.
17
+
18
+ Returns:
19
+ google.oauth2.service_account.Credentials: GCP credentials object
20
+
21
+ Raises:
22
+ ValueError: If no valid credentials are found
23
+ Exception: For any authentication errors
24
+ """
25
  try:
26
  # Option 1: HF Spaces environment variable
27
  if "GCP_CREDENTIALS" in os.environ:
 
75
  raise
76
 
77
  def setup_openai_auth():
78
+ """
79
+ Setup OpenAI API authentication using various methods.
80
+
81
+ This function tries multiple authentication methods in order of preference:
82
+ 1. Standard environment variable (OPENAI_API_KEY)
83
+ 2. HF Spaces environment variable (OPENAI_KEY)
84
+ 3. Streamlit secrets (openai_api_key)
85
+
86
+ Each method is tried in sequence until one succeeds.
87
+
88
+ Raises:
89
+ ValueError: If no valid API key is found
90
+ Exception: For any authentication errors
91
+ """
92
  try:
93
  # Option 1: Standard environment variable
94
  if "OPENAI_API_KEY" in os.environ:
 
118
  raise
119
 
120
  def setup_all_auth():
121
+ """
122
+ Setup all authentication in one call.
123
+
124
+ This is a convenience function that sets up both GCP and OpenAI authentication.
125
+ It's typically called during Anveshak's initialization.
126
+
127
+ Returns:
128
+ google.oauth2.service_account.Credentials: GCP credentials object
129
+ """
130
  gcp_creds = setup_gcp_auth()
131
  setup_openai_auth()
132
  return gcp_creds