Spaces:
Running
Running
#!/usr/bin/env python3 | |
""" | |
Fix Supabase Connection String | |
""" | |
import os | |
from pathlib import Path | |
def fix_supabase_connection(): | |
"""Fix the Supabase connection string with correct hostname.""" | |
print("Fixing Supabase Connection String") | |
print("=" * 40) | |
# Correct connection string | |
correct_url = "postgresql+asyncpg://postgres:WiHcl5UgLmP1rLGZ@nqdhdqdtpvqfecbsjaar.supabase.co:5432/postgres" | |
print(f"Correct connection string:") | |
print(f"{correct_url}") | |
print() | |
# Create/update .env file | |
env_content = f"""# Unified Assistant Environment Configuration | |
# Generated by fix_connection.py | |
# Database Configuration | |
SUPABASE_DB_URL={correct_url} | |
DATABASE_URL=sqlite:///./unified_assistant.db | |
ENVIRONMENT=development | |
# Security Settings | |
SECRET_KEY=Qee7sf39ipUhe_1pKCnsMLPU-aanOt-xs0gx3bsBuFo | |
ALGORITHM=HS256 | |
ACCESS_TOKEN_EXPIRE_MINUTES=30 | |
REFRESH_TOKEN_EXPIRE_DAYS=7 | |
# AI Service Configuration | |
OPENAI_API_KEY=your_openai_api_key_here | |
OPENAI_MODEL=gpt-4o | |
EMBEDDING_MODEL=text-embedding-3-small | |
PRIMARY_AI_SERVICE=openai | |
# Application Settings | |
DEBUG=true | |
HOST=0.0.0.0 | |
PORT=7860 | |
# File Upload Settings | |
UPLOAD_DIR=./uploads | |
MAX_FILE_SIZE=10485760 | |
""" | |
# Write .env file | |
with open('.env', 'w') as f: | |
f.write(env_content) | |
print("[OK] Updated .env file with correct connection string") | |
# Create Hugging Face secrets template | |
hf_content = f"""# Hugging Face Spaces Secrets Template | |
# Copy these values to your Hugging Face Space secrets | |
# Go to: Your Space -> Settings -> Repository secrets | |
SUPABASE_DB_URL={correct_url} | |
ENVIRONMENT=production | |
OPENAI_API_KEY=your_openai_api_key_here | |
SECRET_KEY=Qee7sf39ipUhe_1pKCnsMLPU-aanOt-xs0gx3bsBuFo | |
DEBUG=false | |
HOST=0.0.0.0 | |
PORT=7860 | |
""" | |
with open('huggingface_secrets_template.txt', 'w') as f: | |
f.write(hf_content) | |
print("[OK] Updated huggingface_secrets_template.txt") | |
# Test the connection | |
print("\nTesting the corrected connection...") | |
# Set environment variable | |
os.environ["SUPABASE_DB_URL"] = correct_url | |
os.environ["ENVIRONMENT"] = "production" | |
try: | |
import asyncio | |
import asyncpg | |
from urllib.parse import urlparse | |
# Parse the URL | |
parsed = urlparse(correct_url) | |
host = parsed.hostname | |
port = parsed.port | |
user = parsed.username | |
password = parsed.password | |
database = parsed.path[1:] | |
print(f"Testing connection to: {host}:{port}") | |
async def test_connection(): | |
try: | |
conn = await asyncpg.connect( | |
host=host, | |
port=port, | |
user=user, | |
password=password, | |
database=database, | |
timeout=10 | |
) | |
result = await conn.fetchval("SELECT 1 as test") | |
print(f"[OK] Connection successful: {result}") | |
await conn.close() | |
return True | |
except Exception as e: | |
print(f"[ERROR] Connection failed: {e}") | |
return False | |
success = asyncio.run(test_connection()) | |
if success: | |
print("\n[SUCCESS] Connection test passed!") | |
print("\nNext steps:") | |
print("1. Replace 'your_openai_api_key_here' in .env with your actual OpenAI API key") | |
print("2. Test your setup: python test_database_config.py") | |
print("3. Start the application: python main.py") | |
else: | |
print("\n[ERROR] Connection test failed. Please check:") | |
print("1. Your internet connection") | |
print("2. Supabase project status") | |
print("3. Database credentials") | |
except Exception as e: | |
print(f"[ERROR] Test failed: {e}") | |
if __name__ == "__main__": | |
fix_supabase_connection() |