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 
 
 
 
 
 
 
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/usr/bin/env python3
"""

Automated Setup and Configuration for Unified AI Services

Helps set up the environment, validate configurations, and initialize services

"""

import os
import sys
import json
import asyncio
import subprocess
from pathlib import Path
from typing import Dict, List, Optional
import shutil

def print_header(title: str):
    """Print a formatted header"""
    print("\n" + "=" * 60)
    print(f"  {title}")
    print("=" * 60)

def print_step(step: str):
    """Print a step indicator"""
    print(f"\nπŸ”§ {step}")

def check_python_version():
    """Check Python version compatibility"""
    print_step("Checking Python version...")
    
    version = sys.version_info
    if version.major < 3 or (version.major == 3 and version.minor < 8):
        print("❌ Python 3.8 or higher is required")
        print(f"   Current version: {version.major}.{version.minor}.{version.micro}")
        return False
    
    print(f"βœ… Python {version.major}.{version.minor}.{version.micro} is compatible")
    return True

def create_directory_structure():
    """Create necessary directory structure"""
    print_step("Creating directory structure...")
    
    directories = [
        "services",
        "exports",
        "logs",
        "temp",
        "tests",
        "data"
    ]
    
    for directory in directories:
        path = Path(directory)
        if not path.exists():
            path.mkdir(parents=True, exist_ok=True)
            print(f"   βœ… Created directory: {directory}")
        else:
            print(f"   βœ“  Directory exists: {directory}")

def check_service_files():
    """Check if service files exist"""
    print_step("Checking service files...")
    
    required_files = {
        "services/ner_service.py": "NER Service",
        "services/ocr_service.py": "OCR Service", 
        "services/rag_service.py": "RAG Service",
        "app.py": "Unified Application",
        "configs.py": "Configuration Management"
    }
    
    missing_files = []
    
    for file_path, description in required_files.items():
        if Path(file_path).exists():
            print(f"   βœ… {description}: {file_path}")
        else:
            print(f"   ❌ {description}: {file_path} (MISSING)")
            missing_files.append(file_path)
    
    if missing_files:
        print(f"\n⚠️  Missing files detected:")
        for file_path in missing_files:
            print(f"   - {file_path}")
        print("\nPlease ensure all service files are in the correct locations.")
        return False
    
    return True

def create_env_file():
    """Create or update .env file with user input"""
    print_step("Setting up environment configuration...")
    
    env_path = Path(".env")
    
    if env_path.exists():
        response = input("   .env file already exists. Overwrite? (y/N): ")
        if response.lower() != 'y':
            print("   Keeping existing .env file")
            return True
    
    print("\nπŸ“ Please provide the following configuration values:")
    print("   (Press Enter to use default values shown in brackets)")
    
    # Collect configuration values
    config_values = {}
    
    # Server Configuration
    print("\n🌐 Server Configuration:")
    config_values['HOST'] = input("   Host [0.0.0.0]: ") or "0.0.0.0"
    config_values['DEBUG'] = input("   Debug mode (true/false) [True]: ") or "True"
    config_values['MAIN_PORT'] = input("   Main app port [8000]: ") or "8000"
    config_values['NER_PORT'] = input("   NER service port [8500]: ") or "8500"
    config_values['OCR_PORT'] = input("   OCR service port [8400]: ") or "8400"
    config_values['RAG_PORT'] = input("   RAG service port [8401]: ") or "8401"
    
    # PostgreSQL Configuration
    print("\nπŸ—„οΈ  PostgreSQL Configuration:")
    config_values['POSTGRES_HOST'] = input("   PostgreSQL host: ")
    config_values['POSTGRES_PORT'] = input("   PostgreSQL port [5432]: ") or "5432"
    config_values['POSTGRES_USER'] = input("   PostgreSQL user: ")
    config_values['POSTGRES_PASSWORD'] = input("   PostgreSQL password: ")
    config_values['POSTGRES_DATABASE'] = input("   PostgreSQL database [postgres]: ") or "postgres"
    
    # Azure OpenAI Configuration
    print("\nπŸ€– Azure OpenAI Configuration:")
    config_values['AZURE_OPENAI_ENDPOINT'] = input("   Azure OpenAI endpoint: ")
    config_values['AZURE_OPENAI_API_KEY'] = input("   Azure OpenAI API key: ")
    config_values['EMBEDDING_MODEL'] = input("   Embedding model [text-embedding-3-large]: ") or "text-embedding-3-large"
    
    # DeepSeek Configuration
    print("\n🧠 DeepSeek Configuration:")
    config_values['DEEPSEEK_ENDPOINT'] = input("   DeepSeek endpoint: ")
    config_values['DEEPSEEK_API_KEY'] = input("   DeepSeek API key: ")
    config_values['DEEPSEEK_MODEL'] = input("   DeepSeek model [DeepSeek-R1-0528]: ") or "DeepSeek-R1-0528"
    
    # Azure Document Intelligence Configuration
    print("\nπŸ“„ Azure Document Intelligence Configuration:")
    config_values['AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT'] = input("   Document Intelligence endpoint: ")
    config_values['AZURE_DOCUMENT_INTELLIGENCE_KEY'] = input("   Document Intelligence API key: ")
    
    # Azure Storage Configuration
    print("\nπŸ’Ύ Azure Storage Configuration:")
    config_values['AZURE_STORAGE_ACCOUNT_URL'] = input("   Storage account URL: ")
    config_values['AZURE_BLOB_SAS_TOKEN'] = input("   Blob SAS token: ")
    config_values['BLOB_CONTAINER'] = input("   Blob container [historylog]: ") or "historylog"
    
    # Write .env file
    try:
        with open(".env", "w") as f:
            f.write("# =================================================================\n")
            f.write("# Unified AI Services - Environment Configuration\n")
            f.write("# Generated by setup.py\n")
            f.write("# =================================================================\n\n")
            
            f.write("# Server Configuration\n")
            f.write(f"HOST={config_values['HOST']}\n")
            f.write(f"DEBUG={config_values['DEBUG']}\n")
            f.write(f"MAIN_PORT={config_values['MAIN_PORT']}\n")
            f.write(f"NER_PORT={config_values['NER_PORT']}\n")
            f.write(f"OCR_PORT={config_values['OCR_PORT']}\n")
            f.write(f"RAG_PORT={config_values['RAG_PORT']}\n\n")
            
            f.write("# PostgreSQL Configuration\n")
            f.write(f"POSTGRES_HOST={config_values['POSTGRES_HOST']}\n")
            f.write(f"POSTGRES_PORT={config_values['POSTGRES_PORT']}\n")
            f.write(f"POSTGRES_USER={config_values['POSTGRES_USER']}\n")
            f.write(f"POSTGRES_PASSWORD={config_values['POSTGRES_PASSWORD']}\n")
            f.write(f"POSTGRES_DATABASE={config_values['POSTGRES_DATABASE']}\n\n")
            
            f.write("# Azure OpenAI Configuration\n")
            f.write(f"AZURE_OPENAI_ENDPOINT={config_values['AZURE_OPENAI_ENDPOINT']}\n")
            f.write(f"AZURE_OPENAI_API_KEY={config_values['AZURE_OPENAI_API_KEY']}\n")
            f.write(f"EMBEDDING_MODEL={config_values['EMBEDDING_MODEL']}\n")
            f.write(f"AZURE_OPENAI_DEPLOYMENT_NAME={config_values['EMBEDDING_MODEL']}\n\n")
            
            f.write("# DeepSeek Configuration\n")
            f.write(f"DEEPSEEK_ENDPOINT={config_values['DEEPSEEK_ENDPOINT']}\n")
            f.write(f"DEEPSEEK_API_KEY={config_values['DEEPSEEK_API_KEY']}\n")
            f.write(f"DEEPSEEK_MODEL={config_values['DEEPSEEK_MODEL']}\n\n")
            
            f.write("# Azure Document Intelligence Configuration\n")
            f.write(f"AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT={config_values['AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT']}\n")
            f.write(f"AZURE_DOCUMENT_INTELLIGENCE_KEY={config_values['AZURE_DOCUMENT_INTELLIGENCE_KEY']}\n\n")
            
            f.write("# Azure Storage Configuration\n")
            f.write(f"AZURE_STORAGE_ACCOUNT_URL={config_values['AZURE_STORAGE_ACCOUNT_URL']}\n")
            f.write(f"AZURE_BLOB_SAS_TOKEN=\"{config_values['AZURE_BLOB_SAS_TOKEN']}\"\n")
            f.write(f"BLOB_CONTAINER={config_values['BLOB_CONTAINER']}\n\n")
            
            f.write("# Processing Configuration\n")
            f.write("MAX_FILE_SIZE=50\n")
            f.write("REQUEST_TIMEOUT=300\n")
            f.write("LOG_LEVEL=INFO\n")
            f.write("ALLOWED_ORIGINS=*\n")
            
            f.write("\n# RAG Specific Configuration\n")
            f.write("PG_HOST=${POSTGRES_HOST}\n")
            f.write("PG_PORT=${POSTGRES_PORT}\n")
            f.write("PG_DATABASE=vectorsearch\n")
            f.write("PG_USER=${POSTGRES_USER}\n")
            f.write("PG_PASSWORD=${POSTGRES_PASSWORD}\n")
            f.write("PG_SSL_MODE=require\n")
            f.write("CHUNK_SIZE=1000\n")
            f.write("CHUNK_OVERLAP=200\n")
            f.write("MIN_CHUNK_SIZE=50\n")
        
        print("   βœ… .env file created successfully")
        return True
        
    except Exception as e:
        print(f"   ❌ Failed to create .env file: {e}")
        return False

def install_dependencies():
    """Install Python dependencies"""
    print_step("Installing Python dependencies...")
    
    # Create requirements.txt if it doesn't exist
    requirements_path = Path("requirements.txt")
    if not requirements_path.exists():
        print("   Creating requirements.txt file...")
        
        requirements = [
            "fastapi>=0.104.1",
            "uvicorn[standard]>=0.24.0",
            "httpx>=0.25.0",
            "asyncpg>=0.29.0",
            "psutil>=5.9.0",
            "pydantic>=2.5.0",
            "python-dotenv>=1.0.0",
            "python-multipart>=0.0.6",
            "azure-ai-inference>=1.0.0",
            "azure-core>=1.29.0",
            "azure-storage-blob>=12.19.0",
            "azure-ai-documentintelligence>=1.0.0",
            "openai>=1.3.0",
            "beautifulsoup4>=4.12.0",
            "requests>=2.31.0",
            "numpy>=1.24.0",
            "Pillow>=10.0.0",
            "python-docx>=1.1.0",
            "lxml>=4.9.0",
            "aiofiles>=23.2.1"
        ]
        
        try:
            with open("requirements.txt", "w") as f:
                for req in requirements:
                    f.write(f"{req}\n")
            print("   βœ… requirements.txt created")
        except Exception as e:
            print(f"   ❌ Failed to create requirements.txt: {e}")
            return False
    
    # Install dependencies
    try:
        print("   Installing dependencies (this may take a few minutes)...")
        result = subprocess.run([
            sys.executable, "-m", "pip", "install", "-r", "requirements.txt"
        ], capture_output=True, text=True)
        
        if result.returncode == 0:
            print("   βœ… Dependencies installed successfully")
            return True
        else:
            print(f"   ❌ Failed to install dependencies:")
            print(f"   {result.stderr}")
            return False
            
    except Exception as e:
        print(f"   ❌ Error installing dependencies: {e}")
        return False

def validate_configuration():
    """Validate the configuration"""
    print_step("Validating configuration...")
    
    try:
        from configs import get_config, validate_environment
        
        config = get_config()
        config.print_configuration_summary()
        
        if validate_environment():
            print("\n   βœ… Configuration validation passed!")
            return True
        else:
            print("\n   ❌ Configuration validation failed!")
            print("   Please check your .env file and update missing values.")
            return False
            
    except ImportError as e:
        print(f"   ❌ Failed to import configuration module: {e}")
        return False
    except Exception as e:
        print(f"   ❌ Configuration validation error: {e}")
        return False

async def test_database_connection():
    """Test database connection"""
    print_step("Testing database connection...")
    
    try:
        from configs import get_config
        import asyncpg
        
        config = get_config()
        
        # Test connection
        conn = await asyncpg.connect(
            host=config.ner.POSTGRES_HOST,
            port=config.ner.POSTGRES_PORT,
            database=config.ner.POSTGRES_DATABASE,
            user=config.ner.POSTGRES_USER,
            password=config.ner.POSTGRES_PASSWORD,
            ssl='require',
            timeout=10
        )
        
        # Test basic query
        version = await conn.fetchval("SELECT version()")
        await conn.close()
        
        print("   βœ… Database connection successful")
        print(f"   Database version: {version[:50]}...")
        return True
        
    except Exception as e:
        print(f"   ❌ Database connection failed: {e}")
        print("\n   πŸ’‘ Troubleshooting tips:")
        print("   1. Check your PostgreSQL server is running")
        print("   2. Verify host, port, username, and password")
        print("   3. Ensure your IP is allowlisted in firewall rules")
        print("   4. Check SSL configuration")
        return False

def create_startup_script():
    """Create startup script for easy service management"""
    print_step("Creating startup script...")
    
    # Create startup script for Windows
    if sys.platform == "win32":
        script_content = """@echo off

echo Starting Unified AI Services...

echo.



echo Starting in 3 seconds...

timeout /t 3 /nobreak >nul



echo Starting unified application...

python app.py



pause

"""
        with open("start_services.bat", "w") as f:
            f.write(script_content)
        print("   βœ… Created start_services.bat")
    
    # Create startup script for Unix/Linux/Mac
    else:
        script_content = """#!/bin/bash



echo "Starting Unified AI Services..."

echo



echo "Starting in 3 seconds..."

sleep 3



echo "Starting unified application..."

python app.py

"""
        with open("start_services.sh", "w") as f:
            f.write(script_content)
        
        # Make executable
        os.chmod("start_services.sh", 0o755)
        print("   βœ… Created start_services.sh")
    
    return True

def create_test_script():
    """Create test script for easy testing"""
    print_step("Creating test script...")
    
    # Create test script for Windows
    if sys.platform == "win32":
        script_content = """@echo off

echo Running Unified System Tests...

echo.



echo Make sure the unified application is running first!

echo Press any key to continue or Ctrl+C to cancel...

pause >nul



echo Running comprehensive tests...

python test_unified.py



pause

"""
        with open("run_tests.bat", "w") as f:
            f.write(script_content)
        print("   βœ… Created run_tests.bat")
    
    # Create test script for Unix/Linux/Mac
    else:
        script_content = """#!/bin/bash



echo "Running Unified System Tests..."

echo



echo "Make sure the unified application is running first!"

read -p "Press Enter to continue or Ctrl+C to cancel..."



echo "Running comprehensive tests..."

python test_unified.py

"""
        with open("run_tests.sh", "w") as f:
            f.write(script_content)
        
        # Make executable
        os.chmod("run_tests.sh", 0o755)
        print("   βœ… Created run_tests.sh")
    
    return True

def main():
    """Main setup function"""
    print_header("Unified AI Services - Automated Setup")
    
    print("This script will help you set up the Unified AI Services application.")
    print("It will:")
    print("  β€’ Check your Python environment")
    print("  β€’ Create necessary directories")
    print("  β€’ Check for required service files")
    print("  β€’ Set up configuration (.env file)")
    print("  β€’ Install Python dependencies")
    print("  β€’ Validate configuration")
    print("  β€’ Test database connection")
    print("  β€’ Create startup and test scripts")
    
    response = input("\nProceed with setup? (Y/n): ")
    if response.lower() == 'n':
        print("Setup cancelled.")
        return
    
    setup_steps = [
        ("Python Version Check", check_python_version),
        ("Directory Structure", create_directory_structure),
        ("Service Files Check", check_service_files),
        ("Environment Configuration", create_env_file),
        ("Dependencies Installation", install_dependencies),
        ("Configuration Validation", validate_configuration),
        ("Startup Scripts", create_startup_script),
        ("Test Scripts", create_test_script),
    ]
    
    failed_steps = []
    
    for step_name, step_func in setup_steps:
        try:
            if not step_func():
                failed_steps.append(step_name)
        except Exception as e:
            print(f"   ❌ {step_name} failed with exception: {e}")
            failed_steps.append(step_name)
    
    # Database connection test (optional)
    print_step("Testing database connection (optional)...")
    try:
        asyncio.run(test_database_connection())
    except Exception as e:
        print(f"   ⚠️  Database test skipped: {e}")
    
    # Final summary
    print_header("Setup Summary")
    
    if not failed_steps:
        print("πŸŽ‰ Setup completed successfully!")
        print("\nNext steps:")
        print("1. Review the .env file and update any missing values")
        print("2. Start the unified application:")
        if sys.platform == "win32":
            print("   β€’ Double-click start_services.bat")
            print("   β€’ Or run: python app.py")
        else:
            print("   β€’ Run: ./start_services.sh")
            print("   β€’ Or run: python app.py")
        print("3. Test the system:")
        if sys.platform == "win32":
            print("   β€’ Double-click run_tests.bat")
            print("   β€’ Or run: python test_unified.py")
        else:
            print("   β€’ Run: ./run_tests.sh")
            print("   β€’ Or run: python test_unified.py")
        print("4. Access the API documentation at: http://localhost:8000/docs")
        
    else:
        print("⚠️  Setup completed with some issues:")
        for step in failed_steps:
            print(f"   ❌ {step}")
        
        print("\nPlease resolve the failed steps before proceeding.")
        print("You may need to:")
        print("β€’ Check your internet connection for dependency installation")
        print("β€’ Verify your Azure service credentials")
        print("β€’ Ensure PostgreSQL is accessible")
        print("β€’ Check file permissions")

if __name__ == "__main__":
    main()