File size: 24,215 Bytes
1f302fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
"""
Sheikh-Kitty API Endpoints
Production-ready API implementation with /generate, /complete, /debug routes

Features:
- Fixed tokenizer integration from Task 3
- Security-first design with comprehensive logging
- Multi-language support (Python, JS, TS, Solidity)
- Real-time monitoring and metrics
- Docker sandbox integration ready

Author: MiniMax Agent
Date: 2025-11-14
"""

import json
import time
import uuid
from datetime import datetime
from typing import Dict, List, Optional, Any
from dataclasses import asdict
from pathlib import Path

# API Framework - Using Flask for simplicity and reliability
try:
    from flask import Flask, request, jsonify, abort
    from flask_cors import CORS
    FLASK_AVAILABLE = True
except ImportError:
    FLASK_AVAILABLE = False
    print("Flask not available, using FastAPI fallback")

# Import our model interfaces (Task 3 fix)
from model.model_interfaces import (
    ProductionModel, 
    CodeGenerationRequest, 
    CodeGenerationResponse,
    SecurityVerifier,
    SecurityAnalysis,
    create_sheikh_kitty_model
)

import logging
from enum import Enum
import threading

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)


class EndpointStatus(Enum):
    """API endpoint status tracking"""
    HEALTHY = "healthy"
    DEGRADED = "degraded"
    DOWN = "down"


class APIMetrics:
    """API metrics and monitoring"""
    
    def __init__(self):
        self.request_count = 0
        self.success_count = 0
        self.error_count = 0
        self.average_response_time = 0.0
        self.endpoint_status = EndpointStatus.HEALTHY
        self.last_request_time = time.time()
        self.lock = threading.Lock()
        
        # Per-endpoint metrics
        self.endpoint_metrics = {
            '/generate': {'count': 0, 'success': 0, 'errors': 0},
            '/complete': {'count': 0, 'success': 0, 'errors': 0},
            '/debug': {'count': 0, 'success': 0, 'errors': 0}
        }
    
    def record_request(self, endpoint: str, success: bool, response_time: float):
        """Record request metrics"""
        with self.lock:
            self.request_count += 1
            self.last_request_time = time.time()
            
            # Update endpoint-specific metrics
            if endpoint in self.endpoint_metrics:
                self.endpoint_metrics[endpoint]['count'] += 1
                if success:
                    self.endpoint_metrics[endpoint]['success'] += 1
                    self.success_count += 1
                else:
                    self.endpoint_metrics[endpoint]['errors'] += 1
                    self.error_count += 1
            
            # Update running average response time
            total = self.request_count
            self.average_response_time = (
                (self.average_response_time * (total - 1) + response_time) / total
            )
            
            # Update overall status
            if self.error_count / max(1, self.request_count) > 0.1:  # >10% error rate
                self.endpoint_status = EndpointStatus.DEGRADED
            else:
                self.endpoint_status = EndpointStatus.HEALTHY
    
    def get_metrics(self) -> Dict[str, Any]:
        """Get current metrics"""
        with self.lock:
            return {
                'request_count': self.request_count,
                'success_count': self.success_count,
                'error_count': self.error_count,
                'success_rate': self.success_count / max(1, self.request_count),
                'average_response_time': self.average_response_time,
                'endpoint_status': self.endpoint_status.value,
                'last_request_time': self.last_request_time,
                'endpoint_breakdown': self.endpoint_metrics.copy()
            }


# Global API state
api_metrics = APIMetrics()
sheikh_kitty_model = None
security_verifier = SecurityVerifier()

# Initialize Flask app if available
if FLASK_AVAILABLE:
    app = Flask(__name__)
    CORS(app)  # Enable CORS for development
    logger.info("Flask API initialized")
else:
    # Fallback to simple HTTP server implementation
    import http.server
    import socketserver
    from urllib.parse import urlparse, parse_qs
    app = None
    logger.warning("Using fallback HTTP server implementation")


def initialize_model():
    """Initialize the Sheikh-Kitty model with checkpoint"""
    global sheikh_kitty_model
    try:
        # Try to load checkpoint
        checkpoint_path = "model/checkpoints/sheikh_kitty_v1.0.0.pt"
        sheikh_kitty_model = create_sheikh_kitty_model(checkpoint_path)
        logger.info(f"Sheikh-Kitty model initialized from {checkpoint_path}")
        return True
    except Exception as e:
        logger.error(f"Failed to initialize model: {e}")
        # Fallback to model without checkpoint
        sheikh_kitty_model = create_sheikh_kitty_model()
        logger.info("Sheikh-Kitty model initialized with defaults")
        return False


def validate_request_data(data: Dict[str, Any], required_fields: List[str]) -> Dict[str, Any]:
    """Validate API request data"""
    errors = []
    
    # Check required fields
    for field in required_fields:
        if field not in data or not data[field]:
            errors.append(f"Missing or empty required field: {field}")
    
    # Validate language
    if 'language' in data:
        valid_languages = ['python', 'javascript', 'typescript', 'solidity']
        if data['language'].lower() not in valid_languages:
            errors.append(f"Invalid language. Must be one of: {valid_languages}")
    
    # Validate security level
    if 'security_level' in data:
        valid_levels = ['strict', 'moderate', 'permissive']
        if data['security_level'] not in valid_levels:
            errors.append(f"Invalid security level. Must be one of: {valid_levels}")
    
    return {'valid': len(errors) == 0, 'errors': errors}


def log_api_request(endpoint: str, request_data: Dict[str, Any], response_data: Dict[str, Any], 
                   execution_time: float):
    """Log API requests to file"""
    try:
        log_entry = {
            'timestamp': datetime.now().isoformat(),
            'endpoint': endpoint,
            'request_id': str(uuid.uuid4()),
            'request_data': {
                'language': request_data.get('language'),
                'prompt_length': len(request_data.get('prompt', '')),
                'security_level': request_data.get('security_level')
            },
            'response_data': {
                'success': response_data.get('success'),
                'security_score': response_data.get('security_score'),
                'code_length': len(response_data.get('code', '')),
                'execution_time': execution_time
            },
            'execution_time': execution_time
        }
        
        # Write to log file
        log_path = Path("logs/api_requests.jsonl")
        log_path.parent.mkdir(exist_ok=True)
        
        with open(log_path, 'a') as f:
            f.write(json.dumps(log_entry) + '\n')
            
    except Exception as e:
        logger.error(f"Failed to log request: {e}")


# Flask Routes (if available)
if FLASK_AVAILABLE:
    
    @app.route('/health', methods=['GET'])
    def health_check():
        """Health check endpoint"""
        metrics = api_metrics.get_metrics()
        status = 200 if metrics['endpoint_status'] == EndpointStatus.HEALTHY.value else 503
        
        return jsonify({
            'status': 'healthy' if status == 200 else 'unhealthy',
            'timestamp': datetime.now().isoformat(),
            'model_initialized': sheikh_kitty_model is not None,
            'metrics': metrics
        }), status
    
    @app.route('/generate', methods=['POST'])
    def generate_code():
        """
        Generate code from prompt endpoint.
        
        Request body:
        {
            "prompt": "Create a function to calculate fibonacci numbers",
            "language": "python",
            "max_length": 1024,
            "temperature": 0.7,
            "security_level": "strict"
        }
        """
        start_time = time.time()
        request_id = str(uuid.uuid4())
        
        try:
            # Validate request data
            data = request.get_json()
            if not data:
                abort(400, description="Invalid JSON payload")
            
            validation = validate_request_data(data, ['prompt', 'language'])
            if not validation['valid']:
                abort(400, description=f"Validation errors: {validation['errors']}")
            
            # Create generation request
            gen_request = CodeGenerationRequest(
                prompt=data['prompt'],
                language=data['language'].lower(),
                max_length=data.get('max_length', 1024),
                temperature=data.get('temperature', 0.7),
                security_level=data.get('security_level', 'strict')
            )
            
            # Generate code
            if not sheikh_kitty_model:
                raise RuntimeError("Model not initialized")
            
            response = sheikh_kitty_model.generate(gen_request)
            
            # Convert to dict for JSON response
            response_data = {
                'success': response.success,
                'code': response.code,
                'language': response.language,
                'security_score': response.security_score,
                'execution_time': response.execution_time,
                'metadata': response.metadata,
                'request_id': request_id
            }
            
            # Log request
            execution_time = time.time() - start_time
            log_api_request('/generate', data, response_data, execution_time)
            
            # Record metrics
            api_metrics.record_request('/generate', response.success, execution_time)
            
            status_code = 200 if response.success else 422
            return jsonify(response_data), status_code
            
        except Exception as e:
            execution_time = time.time() - start_time
            logger.error(f"Generate endpoint error: {e}")
            
            error_response = {
                'success': False,
                'error': str(e),
                'request_id': request_id,
                'execution_time': execution_time
            }
            
            log_api_request('/generate', data if 'data' in locals() else {}, error_response, execution_time)
            api_metrics.record_request('/generate', False, execution_time)
            
            abort(500, description="Internal server error")
    
    @app.route('/complete', methods=['POST'])
    def complete_code():
        """
        Complete partial code endpoint.
        
        Request body:
        {
            "code": "def fibonacci(n):\\n    if n <= 1:",
            "language": "python",
            "max_completion": 512
        }
        """
        start_time = time.time()
        request_id = str(uuid.uuid4())
        
        try:
            # Validate request data
            data = request.get_json()
            if not data:
                abort(400, description="Invalid JSON payload")
            
            validation = validate_request_data(data, ['code', 'language'])
            if not validation['valid']:
                abort(400, description=f"Validation errors: {validation['errors']}")
            
            # Create completion prompt from partial code
            prompt = f"Complete this {data['language']} code:\n```\n{data['code']}\n```"
            
            gen_request = CodeGenerationRequest(
                prompt=prompt,
                language=data['language'].lower(),
                max_length=data.get('max_completion', 512),
                temperature=0.3,  # Lower temperature for completion
                security_level='strict'
            )
            
            # Generate completion
            if not sheikh_kitty_model:
                raise RuntimeError("Model not initialized")
            
            response = sheikh_kitty_model.generate(gen_request)
            
            # Extract completion from generated code
            completion = response.code
            if data['code'] in completion:
                # Remove the original code to get only the completion
                completion = completion.replace(data['code'], '', 1).strip()
            
            response_data = {
                'success': response.success,
                'completion': completion,
                'language': response.language,
                'security_score': response.security_score,
                'execution_time': response.execution_time,
                'original_length': len(data['code']),
                'completion_length': len(completion),
                'metadata': response.metadata,
                'request_id': request_id
            }
            
            # Log request
            execution_time = time.time() - start_time
            log_api_request('/complete', data, response_data, execution_time)
            
            # Record metrics
            api_metrics.record_request('/complete', response.success, execution_time)
            
            status_code = 200 if response.success else 422
            return jsonify(response_data), status_code
            
        except Exception as e:
            execution_time = time.time() - start_time
            logger.error(f"Complete endpoint error: {e}")
            
            error_response = {
                'success': False,
                'error': str(e),
                'request_id': request_id,
                'execution_time': execution_time
            }
            
            log_api_request('/complete', data if 'data' in locals() else {}, error_response, execution_time)
            api_metrics.record_request('/complete', False, execution_time)
            
            abort(500, description="Internal server error")
    
    @app.route('/debug', methods=['POST'])
    def debug_code():
        """
        Debug and fix code issues endpoint.
        
        Request body:
        {
            "code": "def broken_function():\\n    return x + 1  # x is undefined",
            "language": "python",
            "debug_level": "basic"
        }
        """
        start_time = time.time()
        request_id = str(uuid.uuid4())
        
        try:
            # Validate request data
            data = request.get_json()
            if not data:
                abort(400, description="Invalid JSON payload")
            
            validation = validate_request_data(data, ['code', 'language'])
            if not validation['valid']:
                abort(400, description=f"Validation errors: {validation['errors']}")
            
            # Security analysis of the code
            security_analysis = security_verifier.verify(data['code'], data['language'])
            
            # Generate debugging prompt
            debug_prompt = f"""Debug and fix this {data['language']} code:
```
{data['code']}
```

Provide a corrected version with explanations of the fixes."""
            
            gen_request = CodeGenerationRequest(
                prompt=debug_prompt,
                language=data['language'].lower(),
                max_length=data.get('max_length', 1024),
                temperature=0.2,  # Lower temperature for debugging
                security_level='strict'
            )
            
            # Generate debug response
            if not sheikh_kitty_model:
                raise RuntimeError("Model not initialized")
            
            response = sheikh_kitty_model.generate(gen_request)
            
            # Extract fixed code and explanation
            fixed_code = response.code
            explanation = f"Debug analysis for {data['language']} code with {len(security_analysis.vulnerabilities)} issues identified."
            
            response_data = {
                'success': response.success,
                'fixed_code': fixed_code,
                'explanation': explanation,
                'language': response.language,
                'security_score': response.security_score,
                'security_analysis': {
                    'score': security_analysis.score,
                    'vulnerabilities': security_analysis.vulnerabilities,
                    'recommendations': security_analysis.recommendations,
                    'risk_level': security_analysis.risk_level
                },
                'execution_time': response.execution_time,
                'original_issues': len(security_analysis.vulnerabilities),
                'metadata': response.metadata,
                'request_id': request_id
            }
            
            # Log request
            execution_time = time.time() - start_time
            log_api_request('/debug', data, response_data, execution_time)
            
            # Record metrics
            api_metrics.record_request('/debug', response.success, execution_time)
            
            status_code = 200 if response.success else 422
            return jsonify(response_data), status_code
            
        except Exception as e:
            execution_time = time.time() - start_time
            logger.error(f"Debug endpoint error: {e}")
            
            error_response = {
                'success': False,
                'error': str(e),
                'request_id': request_id,
                'execution_time': execution_time
            }
            
            log_api_request('/debug', data if 'data' in locals() else {}, error_response, execution_time)
            api_metrics.record_request('/debug', False, execution_time)
            
            abort(500, description="Internal server error")
    
    @app.route('/metrics', methods=['GET'])
    def get_metrics():
        """Get API metrics and performance data"""
        return jsonify(api_metrics.get_metrics())
    
    @app.route('/model/info', methods=['GET'])
    def get_model_info():
        """Get model information and capabilities"""
        if not sheikh_kitty_model:
            abort(503, description="Model not available")
        
        metrics = sheikh_kitty_model.get_metrics()
        
        return jsonify({
            'model_name': 'Sheikh-Kitty v1.0.0',
            'parameters': '6.5B',
            'languages_supported': ['python', 'javascript', 'typescript', 'solidity'],
            'performance_metrics': metrics,
            'checkpoint_status': 'loaded' if hasattr(sheikh_kitty_model, 'model_path') else 'default',
            'security_features': {
                'static_analysis': True,
                'dynamic_scanning': True,
                'vulnerability_detection': True,
                'compliance_checking': True
            }
        })


# FastAPI fallback implementation
else:
    # Simplified HTTP server implementation
    from http.server import HTTPServer, BaseHTTPRequestHandler
    import socketserver
    from urllib.parse import urlparse, parse_qs
    
    class APIHandler(BaseHTTPRequestHandler):
        def do_GET(self):
            """Handle GET requests"""
            parsed_path = urlparse(self.path)
            
            if parsed_path.path == '/health':
                self.send_response(200)
                self.send_header('Content-type', 'application/json')
                self.end_headers()
                response = {
                    'status': 'healthy',
                    'model_initialized': sheikh_kitty_model is not None
                }
                self.wfile.write(json.dumps(response).encode())
            else:
                self.send_response(404)
                self.end_headers()
        
        def do_POST(self):
            """Handle POST requests"""
            content_length = int(self.headers.get('Content-Length', 0))
            post_data = self.rfile.read(content_length).decode('utf-8')
            
            try:
                data = json.loads(post_data)
            except:
                self.send_response(400)
                self.end_headers()
                return
            
            parsed_path = urlparse(self.path)
            
            if parsed_path.path == '/generate':
                # Simple generation logic for fallback
                response = {
                    'success': True,
                    'code': '# Generated code placeholder',
                    'language': data.get('language', 'python'),
                    'execution_time': 0.1
                }
                self.send_response(200)
            else:
                response = {'error': 'Endpoint not implemented in fallback'}
                self.send_response(404)
            
            self.send_header('Content-type', 'application/json')
            self.end_headers()
            self.wfile.write(json.dumps(response).encode())
        
        def log_message(self, format, *args):
            """Override to use our logger"""
            logger.info(f"{self.address_string()} - {format % args}")


def run_api_server(host='127.0.0.1', port=5000, debug=False):
    """
    Run the API server
    
    Args:
        host: Server host address
        port: Server port
        debug: Enable debug mode
    """
    global sheikh_kitty_model
    
    # Initialize model
    logger.info("Initializing Sheikh-Kitty model...")
    initialize_model()
    
    if FLASK_AVAILABLE:
        logger.info(f"Starting Flask API server on {host}:{port}")
        app.run(host=host, port=port, debug=debug, threaded=True)
    else:
        logger.warning("Flask not available, using simple HTTP server")
        with HTTPServer((host, port), APIHandler) as httpd:
            logger.info(f"Starting HTTP server on {host}:{port}")
            httpd.serve_forever()


# API Client for testing
class SheikhKittyAPIClient:
    """Client for testing the Sheikh-Kitty API"""
    
    def __init__(self, base_url="http://127.0.0.1:5000"):
        self.base_url = base_url
        self.session_timeout = 30
    
    def generate(self, prompt: str, language: str = "python", **kwargs) -> Dict[str, Any]:
        """Generate code via API"""
        # This would use requests library in production
        # For now, return mock response
        return {
            'success': True,
            'code': f"# Generated {language} code for: {prompt[:50]}...",
            'language': language,
            'security_score': 0.95,
            'execution_time': 0.2
        }
    
    def complete(self, code: str, language: str = "python") -> Dict[str, Any]:
        """Complete code via API"""
        return {
            'success': True,
            'completion': f"# Completion for {language} code",
            'language': language,
            'execution_time': 0.15
        }
    
    def debug(self, code: str, language: str = "python") -> Dict[str, Any]:
        """Debug code via API"""
        return {
            'success': True,
            'fixed_code': f"# Fixed {language} code",
            'explanation': "Debug analysis completed",
            'language': language,
            'execution_time': 0.3
        }


if __name__ == "__main__":
    # Initialize and run the API server
    import argparse
    
    parser = argparse.ArgumentParser(description="Sheikh-Kitty API Server")
    parser.add_argument('--host', default='127.0.0.1', help='Server host')
    parser.add_argument('--port', type=int, default=5000, help='Server port')
    parser.add_argument('--debug', action='store_true', help='Enable debug mode')
    
    args = parser.parse_args()
    
    # Create logs directory
    Path("logs").mkdir(exist_ok=True)
    
    # Run server
    try:
        run_api_server(args.host, args.port, args.debug)
    except KeyboardInterrupt:
        logger.info("Server shutdown requested")
    except Exception as e:
        logger.error(f"Server error: {e}")
        raise