Update app/main.py
Browse files- app/main.py +20 -17
app/main.py
CHANGED
@@ -224,34 +224,35 @@ def init_vertex_ai():
|
|
224 |
# Initialize the client with the credentials
|
225 |
try:
|
226 |
client = genai.Client(vertexai=True, credentials=credentials, project=project_id, location="us-central1")
|
227 |
-
print(f"Initialized Vertex AI using GOOGLE_CREDENTIALS_JSON env var for project: {project_id}")
|
228 |
except Exception as client_err:
|
229 |
-
print(f"ERROR: Failed to initialize genai.Client: {client_err}")
|
230 |
raise
|
231 |
return True
|
232 |
except Exception as e:
|
233 |
-
print(f"Error loading credentials from GOOGLE_CREDENTIALS_JSON: {e}")
|
|
|
234 |
# Fall through to other methods if this fails
|
235 |
-
|
236 |
# Priority 2: Try to use the credential manager to get credentials from files
|
237 |
-
print(f"Trying credential manager (directory: {credential_manager.credentials_dir})")
|
238 |
credentials, project_id = credential_manager.get_next_credentials()
|
239 |
-
|
240 |
if credentials and project_id:
|
241 |
try:
|
242 |
client = genai.Client(vertexai=True, credentials=credentials, project=project_id, location="us-central1")
|
243 |
-
print(f"Initialized Vertex AI using Credential Manager for project: {project_id}")
|
244 |
return True
|
245 |
except Exception as e:
|
246 |
-
print(f"ERROR: Failed to initialize client with credentials from Credential Manager: {e}")
|
247 |
|
248 |
# Priority 3: Fall back to GOOGLE_APPLICATION_CREDENTIALS environment variable (file path)
|
249 |
file_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
|
250 |
if file_path:
|
251 |
-
print(f"Checking GOOGLE_APPLICATION_CREDENTIALS file path: {file_path}")
|
252 |
if os.path.exists(file_path):
|
253 |
try:
|
254 |
-
print(f"File exists, attempting to load credentials")
|
255 |
credentials = service_account.Credentials.from_service_account_file(
|
256 |
file_path,
|
257 |
scopes=['https://www.googleapis.com/auth/cloud-platform']
|
@@ -261,17 +262,17 @@ def init_vertex_ai():
|
|
261 |
|
262 |
try:
|
263 |
client = genai.Client(vertexai=True, credentials=credentials, project=project_id, location="us-central1")
|
264 |
-
print(f"Initialized Vertex AI using GOOGLE_APPLICATION_CREDENTIALS file path for project: {project_id}")
|
265 |
return True
|
266 |
except Exception as client_err:
|
267 |
-
print(f"ERROR: Failed to initialize client with credentials from file: {client_err}")
|
268 |
except Exception as e:
|
269 |
-
print(f"ERROR: Failed to load credentials from GOOGLE_APPLICATION_CREDENTIALS path {file_path}: {e}")
|
270 |
else:
|
271 |
print(f"ERROR: GOOGLE_APPLICATION_CREDENTIALS file does not exist at path: {file_path}")
|
272 |
|
273 |
-
# If none of the methods worked
|
274 |
-
print(f"ERROR: No valid credentials found. Tried GOOGLE_CREDENTIALS_JSON, Credential Manager ({credential_manager.credentials_dir}), and GOOGLE_APPLICATION_CREDENTIALS.")
|
275 |
return False
|
276 |
except Exception as e:
|
277 |
print(f"Error initializing authentication: {e}")
|
@@ -280,8 +281,10 @@ def init_vertex_ai():
|
|
280 |
# Initialize Vertex AI at startup
|
281 |
@app.on_event("startup")
|
282 |
async def startup_event():
|
283 |
-
if
|
284 |
-
print("
|
|
|
|
|
285 |
|
286 |
# Conversion functions
|
287 |
# Define supported roles for Gemini API
|
|
|
224 |
# Initialize the client with the credentials
|
225 |
try:
|
226 |
client = genai.Client(vertexai=True, credentials=credentials, project=project_id, location="us-central1")
|
227 |
+
# print(f"Initialized Vertex AI using GOOGLE_CREDENTIALS_JSON env var for project: {project_id}") # Reduced verbosity
|
228 |
except Exception as client_err:
|
229 |
+
print(f"ERROR: Failed to initialize genai.Client from GOOGLE_CREDENTIALS_JSON: {client_err}") # Added context
|
230 |
raise
|
231 |
return True
|
232 |
except Exception as e:
|
233 |
+
# print(f"Error loading credentials from GOOGLE_CREDENTIALS_JSON: {e}") # Reduced verbosity, error logged above
|
234 |
+
pass # Add pass to avoid empty block error
|
235 |
# Fall through to other methods if this fails
|
236 |
+
|
237 |
# Priority 2: Try to use the credential manager to get credentials from files
|
238 |
+
# print(f"Trying credential manager (directory: {credential_manager.credentials_dir})") # Reduced verbosity
|
239 |
credentials, project_id = credential_manager.get_next_credentials()
|
240 |
+
|
241 |
if credentials and project_id:
|
242 |
try:
|
243 |
client = genai.Client(vertexai=True, credentials=credentials, project=project_id, location="us-central1")
|
244 |
+
# print(f"Initialized Vertex AI using Credential Manager for project: {project_id}") # Reduced verbosity
|
245 |
return True
|
246 |
except Exception as e:
|
247 |
+
print(f"ERROR: Failed to initialize client with credentials from Credential Manager file ({credential_manager.credentials_dir}): {e}") # Added context
|
248 |
|
249 |
# Priority 3: Fall back to GOOGLE_APPLICATION_CREDENTIALS environment variable (file path)
|
250 |
file_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
|
251 |
if file_path:
|
252 |
+
# print(f"Checking GOOGLE_APPLICATION_CREDENTIALS file path: {file_path}") # Reduced verbosity
|
253 |
if os.path.exists(file_path):
|
254 |
try:
|
255 |
+
# print(f"File exists, attempting to load credentials") # Reduced verbosity
|
256 |
credentials = service_account.Credentials.from_service_account_file(
|
257 |
file_path,
|
258 |
scopes=['https://www.googleapis.com/auth/cloud-platform']
|
|
|
262 |
|
263 |
try:
|
264 |
client = genai.Client(vertexai=True, credentials=credentials, project=project_id, location="us-central1")
|
265 |
+
# print(f"Initialized Vertex AI using GOOGLE_APPLICATION_CREDENTIALS file path for project: {project_id}") # Reduced verbosity
|
266 |
return True
|
267 |
except Exception as client_err:
|
268 |
+
print(f"ERROR: Failed to initialize client with credentials from GOOGLE_APPLICATION_CREDENTIALS file ({file_path}): {client_err}") # Added context
|
269 |
except Exception as e:
|
270 |
+
print(f"ERROR: Failed to load credentials from GOOGLE_APPLICATION_CREDENTIALS path ({file_path}): {e}") # Added context
|
271 |
else:
|
272 |
print(f"ERROR: GOOGLE_APPLICATION_CREDENTIALS file does not exist at path: {file_path}")
|
273 |
|
274 |
+
# If none of the methods worked, this error is still useful
|
275 |
+
# print(f"ERROR: No valid credentials found. Tried GOOGLE_CREDENTIALS_JSON, Credential Manager ({credential_manager.credentials_dir}), and GOOGLE_APPLICATION_CREDENTIALS.")
|
276 |
return False
|
277 |
except Exception as e:
|
278 |
print(f"Error initializing authentication: {e}")
|
|
|
281 |
# Initialize Vertex AI at startup
|
282 |
@app.on_event("startup")
|
283 |
async def startup_event():
|
284 |
+
if init_vertex_ai():
|
285 |
+
print("INFO: Vertex AI client successfully initialized.")
|
286 |
+
else:
|
287 |
+
print("ERROR: Failed to initialize Vertex AI client. Please check credential configuration (GOOGLE_CREDENTIALS_JSON, /app/credentials/*.json, or GOOGLE_APPLICATION_CREDENTIALS) and logs for details.")
|
288 |
|
289 |
# Conversion functions
|
290 |
# Define supported roles for Gemini API
|