Spaces:
Sleeping
Sleeping
File size: 1,477 Bytes
4ded6c8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import os
import json
import tempfile
from pathlib import Path
def setup_credentials():
"""Setup Google credentials from Hugging Face secrets"""
# Get credentials JSON from environment variable
creds_json = os.getenv('GOOGLE_APPLICATION_CREDENTIALS_JSON')
if creds_json:
# Create a temporary file for credentials
temp_dir = tempfile.gettempdir()
creds_path = os.path.join(temp_dir, 'google_credentials.json')
# Write credentials to file
with open(creds_path, 'w') as f:
# Handle both string and already-parsed JSON
if isinstance(creds_json, str):
try:
# Try to parse if it's a JSON string
json_data = json.loads(creds_json)
json.dump(json_data, f)
except json.JSONDecodeError:
# If it's not valid JSON, write as-is
f.write(creds_json)
else:
json.dump(creds_json, f)
# Set environment variable to point to the file
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = creds_path
print(f"✅ Google credentials setup complete at: {creds_path}")
return creds_path
else:
print("⚠️ Warning: GOOGLE_APPLICATION_CREDENTIALS_JSON not found in environment")
return None
# Run setup when module is imported
setup_credentials()
|