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") | |
print(f"DEBUG: First 20 chars: {bigquery_key[:20]}...") | |
# If BIGQUERY_KEY_PATH contains the actual JSON content (as in HF Spaces secrets) | |
if bigquery_key.strip().startswith("{"): | |
print("DEBUG: BIGQUERY_KEY_PATH appears to contain JSON content") | |
try: | |
# Validate JSON | |
json_obj = json.loads(bigquery_key) | |
print("DEBUG: Successfully parsed JSON content") | |
# Create a temporary credentials file from the environment variable | |
credentials_path = "/tmp/bigquery_credentials.json" | |
with open(credentials_path, "w") as f: | |
f.write(bigquery_key) | |
print(f"DEBUG: Wrote credentials to {credentials_path}") | |
# Set the credentials path to the temporary file | |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credentials_path | |
print(f"DEBUG: Set GOOGLE_APPLICATION_CREDENTIALS to {credentials_path}") | |
# Verify the file exists | |
if os.path.exists(credentials_path): | |
print(f"DEBUG: Confirmed credentials file exists at {credentials_path}") | |
with open(credentials_path, "r") as f: | |
first_line = f.readline() | |
print(f"DEBUG: First line of credentials file: {first_line[:20]}...") | |
else: | |
print(f"ERROR: Failed to create credentials file at {credentials_path}") | |
except json.JSONDecodeError as e: | |
print(f"ERROR: BIGQUERY_KEY_PATH contains invalid JSON: {str(e)}") | |
print("Please ensure your secret contains valid JSON content") | |
# If BIGQUERY_KEY_PATH is a file path | |
elif os.path.exists(bigquery_key): | |
print(f"DEBUG: BIGQUERY_KEY_PATH points to existing file: {bigquery_key}") | |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = bigquery_key | |
else: | |
print(f"WARNING: BIGQUERY_KEY_PATH is set but does not contain JSON and is not a valid file path: {bigquery_key}") | |
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") | |
# Final check for GOOGLE_APPLICATION_CREDENTIALS | |
if os.environ.get("GOOGLE_APPLICATION_CREDENTIALS"): | |
print(f"DEBUG: GOOGLE_APPLICATION_CREDENTIALS is set to: {os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')}") | |
if os.path.exists(os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")): | |
print("DEBUG: GOOGLE_APPLICATION_CREDENTIALS file exists") | |
else: | |
print("ERROR: GOOGLE_APPLICATION_CREDENTIALS file does not exist") | |
else: | |
print("WARNING: GOOGLE_APPLICATION_CREDENTIALS is not set") |