surfiniaburger commited on
Commit
8ff7567
·
1 Parent(s): 0aa5212

auth logic

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py CHANGED
@@ -392,7 +392,38 @@ def check_internet_connection(host="8.8.8.8", port=53, timeout=3):
392
  return False
393
 
394
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395
  if __name__ == "__main__":
 
396
  # No local database initialization needed
397
 
398
  field_mode_ui = create_field_mode_ui()
 
392
  return False
393
 
394
 
395
+ def _setup_gcp_credentials():
396
+ """
397
+ Sets up Google Cloud credentials from an environment variable.
398
+ If GOOGLE_APPLICATION_CREDENTIALS contains a JSON string, it writes
399
+ it to a temporary file and updates the environment variable to point
400
+ to that file. This is useful for environments like Hugging Face Spaces
401
+ where secrets are provided as string values.
402
+ """
403
+ gcp_credentials_json = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
404
+ if gcp_credentials_json and gcp_credentials_json.strip().startswith("{"):
405
+ try:
406
+ # Validate JSON
407
+ credentials_dict = json.loads(gcp_credentials_json)
408
+
409
+ # Create a temporary file to store the credentials
410
+ temp_dir = tempfile.gettempdir()
411
+ temp_file_path = os.path.join(temp_dir, "gcp_credentials.json")
412
+
413
+ with open(temp_file_path, "w") as f:
414
+ json.dump(credentials_dict, f)
415
+
416
+ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = temp_file_path
417
+ print(f"✅ Google Cloud credentials successfully set from environment variable to: {temp_file_path}")
418
+ except json.JSONDecodeError as e:
419
+ print(f"❌ Error decoding GOOGLE_APPLICATION_CREDENTIALS JSON: {e}")
420
+ except Exception as e:
421
+ print(f"❌ An unexpected error occurred while setting up GCP credentials: {e}")
422
+ else:
423
+ print("ℹ️ GOOGLE_APPLICATION_CREDENTIALS environment variable not found or not a JSON string. Assuming credentials are set via other means (e.g., gcloud CLI, default ADC).")
424
+
425
  if __name__ == "__main__":
426
+ _setup_gcp_credentials()
427
  # No local database initialization needed
428
 
429
  field_mode_ui = create_field_mode_ui()