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()