Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| import sys | |
| from dotenv import load_dotenv | |
| # Load environment variables from .env file if it exists | |
| load_dotenv() | |
| # Check for required environment variables and provide guidance if missing | |
| required_vars = ["OPENAI_API_KEY", "PROJECT_ID", "DATASET_ID", "BIGQUERY_KEY_PATH"] | |
| missing_vars = [var for var in required_vars if not os.environ.get(var)] | |
| if missing_vars: | |
| print("ERROR: Missing required environment variables:") | |
| for var in missing_vars: | |
| print(f"- {var}") | |
| print("\nPlease set these variables in your environment or in a .env file.") | |
| print("Example .env file:") | |
| print(""" | |
| OPENAI_API_KEY=your_openai_api_key | |
| PROJECT_ID=your_gcp_project_id | |
| DATASET_ID=your_bigquery_dataset_id | |
| BIGQUERY_KEY_PATH=path/to/your/bigquery/credentials.json | |
| """) | |
| # Handle BigQuery credentials | |
| print("DEBUG: Handling BigQuery credentials...") | |
| if os.environ.get("BIGQUERY_KEY_PATH"): | |
| bigquery_key = os.environ.get("BIGQUERY_KEY_PATH") | |
| print(f"DEBUG: BIGQUERY_KEY_PATH is set, length: {len(bigquery_key)} characters") | |
| # Create a temporary credentials file from the environment variable | |
| credentials_path = "/tmp/bigquery_credentials.json" | |
| try: | |
| # Try to directly write the content to a file without JSON parsing | |
| with open(credentials_path, "w") as f: | |
| f.write(bigquery_key) | |
| print(f"DEBUG: Wrote credentials to {credentials_path}") | |
| os.environ["BIGQUERY_KEY_PATH"] = credentials_path | |
| print(f"DEBUG: Set BIGQUERY_KEY_PATH to {credentials_path}") | |
| # Verify the file exists | |
| if os.path.exists(credentials_path): | |
| print(f"DEBUG: Confirmed credentials file exists at {credentials_path}") | |
| # Try to validate the JSON by reading it back | |
| try: | |
| with open(credentials_path, "r") as f: | |
| json.load(f) | |
| print("DEBUG: Credentials file contains valid JSON") | |
| except json.JSONDecodeError as e: | |
| print(f"WARNING: Credentials file contains invalid JSON: {str(e)}") | |
| print("This might cause issues with BigQuery authentication") | |
| else: | |
| print(f"ERROR: Failed to create credentials file at {credentials_path}") | |
| except Exception as e: | |
| print(f"ERROR: Failed to process BigQuery credentials: {str(e)}") | |
| else: | |
| print("WARNING: BIGQUERY_KEY_PATH environment variable is not set") | |
| # BigQuery configuration | |
| BIGQUERY_KEY_PATH = os.environ.get("BIGQUERY_KEY_PATH") | |
| PROJECT_ID = os.environ.get("PROJECT_ID") | |
| DATASET_ID = os.environ.get("DATASET_ID") | |