File size: 3,997 Bytes
d7ed1ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/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()