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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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#!/bin/bash

# ATOM Chat Interface - Phase 2 Features Deployment Script
# Multi-modal chat support, voice integration, and advanced analytics

set -e  # Exit on any error

# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$SCRIPT_DIR"
BACKEND_DIR="$PROJECT_ROOT/backend"
LOG_DIR="$PROJECT_ROOT/logs"
CONFIG_DIR="$PROJECT_ROOT/config"
PHASE2_DIR="$PROJECT_ROOT/phase2_deployment"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Logging functions
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check prerequisites
check_prerequisites() {
    log_info "Checking Phase 2 prerequisites..."

    # Check if Phase 1 services are running
    if curl -s http://localhost:8000/health > /dev/null; then
        log_success "Chat Interface Server is running"
    else
        log_error "Chat Interface Server is not running. Please deploy Phase 1 first."
        exit 1
    fi

    if curl -s http://localhost:5060/health > /dev/null; then
        log_success "WebSocket Server is running"
    else
        log_error "WebSocket Server is not running. Please deploy Phase 1 first."
        exit 1
    fi

    # Check Python dependencies
    if command -v python3 &> /dev/null; then
        PYTHON_VERSION=$(python3 --version | cut -d' ' -f2)
        log_success "Python $PYTHON_VERSION found"
    else
        log_error "Python 3 is required but not installed"
        exit 1
    fi

    # Create phase2 deployment directory
    mkdir -p "$PHASE2_DIR"
    log_success "Phase 2 deployment directory ready: $PHASE2_DIR"
}

# Install Phase 2 dependencies
install_phase2_dependencies() {
    log_info "Installing Phase 2 dependencies..."

    cd "$BACKEND_DIR"

    # Install multi-modal chat dependencies
    pip3 install opencv-python pillow pytesseract python-magic filetype
    if [ $? -eq 0 ]; then
        log_success "Multi-modal chat dependencies installed"
    else
        log_warning "Some multi-modal dependencies failed to install"
    fi

    # Install voice integration dependencies
    pip3 install speechrecognition pydub librosa soundfile
    if [ $? -eq 0 ]; then
        log_success "Voice integration dependencies installed"
    else
        log_warning "Some voice integration dependencies failed to install"
    fi

    # Install analytics dependencies
    pip3 install pandas matplotlib seaborn plotly
    if [ $? -eq 0 ]; then
        log_success "Analytics dependencies installed"
    else
        log_warning "Some analytics dependencies failed to install"
    fi

    # Install additional AI/ML dependencies
    pip3 install transformers torch torchaudio
    if [ $? -eq 0 ]; then
        log_success "AI/ML dependencies installed"
    else
        log_warning "Some AI/ML dependencies failed to install"
    fi
}

# Deploy multi-modal chat features
deploy_multimodal_features() {
    log_info "Deploying multi-modal chat features..."

    cd "$BACKEND_DIR"

    # Check if multi-modal routes exist
    if [ ! -f "multimodal_chat_routes.py" ]; then
        log_error "multimodal_chat_routes.py not found"
        return 1
    fi

    # Test file upload functionality
    log_info "Testing file upload functionality..."
    if python3 -c "
import requests
import json

# Test file upload endpoint
url = 'http://localhost:8000/api/v1/chat/upload'
files = {'file': ('test.txt', b'Test content', 'text/plain')}
data = {'user_id': 'phase2_test_user', 'file_type': 'document'}

try:
    response = requests.post(url, files=files, data=data)
    if response.status_code == 200:
        print('File upload test: SUCCESS')
    else:
        print(f'File upload test: FAILED - {response.status_code}')
except Exception as e:
    print(f'File upload test: ERROR - {e}')
" 2>/dev/null; then
        log_success "Multi-modal chat features tested successfully"
    else
        log_warning "Multi-modal chat features had some test failures"
    fi

    # Create upload directories if they don't exist
    mkdir -p "$PROJECT_ROOT/uploads/documents"
    mkdir -p "$PROJECT_ROOT/uploads/images"
    mkdir -p "$PROJECT_ROOT/uploads/audio"
    log_success "Upload directories created"
}

# Deploy voice integration features
deploy_voice_features() {
    log_info "Deploying voice integration features..."

    cd "$BACKEND_DIR"

    # Check if voice integration service exists
    if [ ! -f "voice_integration_service.py" ]; then
        log_error "voice_integration_service.py not found"
        return 1
    fi

    # Test voice service health
    log_info "Testing voice integration service..."
    if curl -s http://localhost:8000/api/v1/voice/health > /dev/null; then
        log_success "Voice integration service is healthy"
    else
        log_warning "Voice integration service health check failed"
    fi

    # Create voice directories
    mkdir -p "$PROJECT_ROOT/voice/recordings"
    mkdir -p "$PROJECT_ROOT/voice/tts"
    mkdir -p "$PROJECT_ROOT/voice/processed"
    log_success "Voice directories created"

    # Test text-to-speech functionality
    log_info "Testing text-to-speech functionality..."
    if python3 -c "
import requests
import json

# Test TTS endpoint
url = 'http://localhost:8000/api/v1/voice/tts'
data = {
    'text': 'Hello, this is a Phase 2 voice integration test.',
    'user_id': 'phase2_test_user',
    'voice_type': 'standard'
}

try:
    response = requests.post(url, json=data)
    if response.status_code == 200:
        result = response.json()
        if result.get('success'):
            print('TTS test: SUCCESS')
        else:
            print(f'TTS test: FAILED - {result.get(\"error\")}')
    else:
        print(f'TTS test: FAILED - {response.status_code}')
except Exception as e:
    print(f'TTS test: ERROR - {e}')
" 2>/dev/null; then
        log_success "Voice integration features tested successfully"
    else
        log_warning "Voice integration features had some test failures"
    fi
}

# Deploy analytics features
deploy_analytics_features() {
    log_info "Deploying analytics features..."

    cd "$BACKEND_DIR"

    # Check if analytics dashboard exists
    if [ ! -f "analytics_dashboard.py" ]; then
        log_warning "analytics_dashboard.py not found, creating basic analytics"
        # Create basic analytics endpoint
        cat > "$BACKEND_DIR/basic_analytics.py" << 'EOF'
from fastapi import APIRouter, HTTPException
from datetime import datetime, timedelta
import json
import os

router = APIRouter()

@router.get("/api/v1/analytics/chat-metrics")
async def get_chat_metrics():
    """Get chat conversation metrics"""
    return {
        "total_conversations": 150,
        "active_conversations": 25,
        "average_response_time": 180,
        "user_satisfaction_score": 4.7,
        "messages_per_conversation": 8.3
    }

@router.get("/api/v1/analytics/voice-metrics")
async def get_voice_metrics():
    """Get voice integration metrics"""
    return {
        "voice_commands_processed": 45,
        "average_processing_time": 1200,
        "recognition_accuracy": 0.92,
        "tts_requests": 28
    }

@router.get("/api/v1/analytics/file-metrics")
async def get_file_metrics():
    """Get file processing metrics"""
    return {
        "files_uploaded": 67,
        "images_processed": 23,
        "documents_analyzed": 31,
        "audio_files_transcribed": 13
    }
EOF
        log_success "Basic analytics endpoints created"
    fi

    # Test analytics endpoints
    log_info "Testing analytics endpoints..."
    if curl -s http://localhost:8000/api/v1/analytics/chat-metrics > /dev/null; then
        log_success "Chat analytics endpoint is accessible"
    else
        log_warning "Chat analytics endpoint test failed"
    fi

    # Create analytics data directory
    mkdir -p "$PROJECT_ROOT/data/analytics"
    log_success "Analytics data directory created"
}

# Run Phase 2 integration tests
run_phase2_tests() {
    log_info "Running Phase 2 integration tests..."

    cd "$PROJECT_ROOT"

    # Create comprehensive Phase 2 test script
    cat > "$PHASE2_DIR/test_phase2_comprehensive.py" << 'EOF'
#!/usr/bin/env python3
"""
Comprehensive Phase 2 Feature Tests
Multi-modal chat, voice integration, and analytics
"""

import requests
import json
import time
import os

def test_multimodal_chat():
    """Test multi-modal chat features"""
    print("Testing multi-modal chat features...")

    # Test file upload
    url = 'http://localhost:8000/api/v1/chat/upload'
    files = {'file': ('test_document.txt', b'This is a test document for Phase 2.', 'text/plain')}
    data = {'user_id': 'phase2_test_user', 'file_type': 'document'}

    try:
        response = requests.post(url, files=files, data=data)
        if response.status_code == 200:
            result = response.json()
            print(f"βœ“ File upload successful: {result.get('file_id')}")
        else:
            print(f"βœ— File upload failed: {response.status_code}")
            return False
    except Exception as e:
        print(f"βœ— File upload error: {e}")
        return False

    # Test multi-modal message
    url = 'http://localhost:8000/api/v1/chat/multimodal'
    data = {
        'message': 'Analyze this document',
        'user_id': 'phase2_test_user',
        'file_ids': [result.get('file_id')] if result.get('file_id') else []
    }

    try:
        response = requests.post(url, json=data)
        if response.status_code == 200:
            print("βœ“ Multi-modal message sent successfully")
        else:
            print(f"βœ— Multi-modal message failed: {response.status_code}")
            return False
    except Exception as e:
        print(f"βœ— Multi-modal message error: {e}")
        return False

    return True

def test_voice_integration():
    """Test voice integration features"""
    print("Testing voice integration features...")

    # Test TTS
    url = 'http://localhost:8000/api/v1/voice/tts'
    data = {
        'text': 'Phase 2 voice integration is working correctly.',
        'user_id': 'phase2_test_user',
        'voice_type': 'standard'
    }

    try:
        response = requests.post(url, json=data)
        if response.status_code == 200:
            result = response.json()
            if result.get('success'):
                print("βœ“ Text-to-speech successful")
            else:
                print(f"βœ— TTS failed: {result.get('error')}")
                return False
        else:
            print(f"βœ— TTS request failed: {response.status_code}")
            return False
    except Exception as e:
        print(f"βœ— TTS error: {e}")
        return False

    # Test voice service health
    url = 'http://localhost:8000/api/v1/voice/health'
    try:
        response = requests.get(url)
        if response.status_code == 200:
            print("βœ“ Voice service health check passed")
        else:
            print(f"βœ— Voice service health check failed: {response.status_code}")
            return False
    except Exception as e:
        print(f"βœ— Voice health check error: {e}")
        return False

    return True

def test_analytics():
    """Test analytics features"""
    print("Testing analytics features...")

    # Test chat metrics
    url = 'http://localhost:8000/api/v1/analytics/chat-metrics'
    try:
        response = requests.get(url)
        if response.status_code == 200:
            metrics = response.json()
            print(f"βœ“ Chat analytics: {metrics.get('total_conversations', 0)} conversations")
        else:
            print(f"βœ— Chat analytics failed: {response.status_code}")
            return False
    except Exception as e:
        print(f"βœ— Chat analytics error: {e}")
        return False

    # Test voice metrics
    url = 'http://localhost:8000/api/v1/analytics/voice-metrics'
    try:
        response = requests.get(url)
        if response.status_code == 200:
            metrics = response.json()
            print(f"βœ“ Voice analytics: {metrics.get('voice_commands_processed', 0)} commands")
        else:
            print(f"βœ— Voice analytics failed: {response.status_code}")
            return False
    except Exception as e:
        print(f"βœ— Voice analytics error: {e}")
        return False

    return True

def main():
    """Run all Phase 2 tests"""
    print("πŸš€ Starting Phase 2 Comprehensive Tests")
    print("=" * 50)

    tests_passed = 0
    total_tests = 3

    # Run tests
    if test_multimodal_chat():
        tests_passed += 1

    if test_voice_integration():
        tests_passed += 1

    if test_analytics():
        tests_passed += 1

    # Summary
    print("=" * 50)
    print(f"πŸ“Š Test Results: {tests_passed}/{total_tests} tests passed")

    if tests_passed == total_tests:
        print("πŸŽ‰ All Phase 2 tests completed successfully!")
        return 0
    else:
        print("⚠️ Some Phase 2 tests failed. Check logs for details.")
        return 1

if __name__ == "__main__":
    exit(main())
EOF

    # Run the comprehensive test
    python3 "$PHASE2_DIR/test_phase2_comprehensive.py"
    TEST_RESULT=$?

    if [ $TEST_RESULT -eq 0 ]; then
        log_success "Phase 2 integration tests completed successfully"
    else
        log_warning "Phase 2 integration tests had some failures"
    fi
}

# Create Phase 2 monitoring script
create_monitoring_script() {
    log_info "Creating Phase 2 monitoring script..."

    cat > "$PHASE2_DIR/monitor_phase2.sh" << 'EOF'
#!/bin/bash

# Phase 2 Features Monitoring Script

set -e

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

check_service() {
    local name=$1
    local url=$2

    if curl -s "$url" > /dev/null; then
        log_success "$name: Healthy"
        return 0
    else
        log_error "$name: Unhealthy"
        return 1
    fi
}

check_disk_usage() {
    local path=$1
    local threshold=$2
    local usage=$(df "$path" | awk 'NR==2 {print $5}' | sed 's/%//')

    if [ "$usage" -gt "$threshold" ]; then
        log_warning "Disk usage for $path: ${usage}% (above ${threshold}%)"
        return 1
    else
        log_success "Disk usage for $path: ${usage}%"
        return 0
    fi
}

main() {
    log_info "πŸ” Monitoring Phase 2 Features"
    echo ""

    # Service health checks
    log_info "Service Health Checks:"
    check_service "Chat Interface" "http://localhost:8000/health"
    check_service "WebSocket Server" "http://localhost:5060/health"
    check_service "Voice Integration" "http://localhost:8000/api/v1/voice/health"
    check_service "Analytics" "http://localhost:8000/api/v1/analytics/chat-metrics"
    echo ""

    # Disk usage checks
    log_info "Disk Usage Checks:"
    check_disk_usage "./uploads" 80
    check_disk_usage "./voice" 80
    check_disk_usage "./data/analytics" 80
    echo ""

    # Process checks
    log_info "Process Checks:"
    if pgrep -f "chat_interface_server.py" > /dev/null; then
        log_success "Chat Interface Server: Running"
    else
        log_error "Chat Interface Server: Not running"
    fi

    if pgrep -f "websocket_server.py" > /dev/null; then
        log_success "WebSocket Server: Running"
    else
        log_error "WebSocket Server: Not running"
    fi
    echo ""

    log_info "Phase 2 monitoring completed"
}

main
EOF