File size: 23,844 Bytes
c89cc5d
c3b3cde
aba9687
43b8ce7
aba9687
 
 
c89cc5d
aba9687
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12e19da
aba9687
 
 
12e19da
aba9687
 
 
12e19da
aba9687
 
 
 
 
 
 
12e19da
aba9687
12e19da
aba9687
 
 
12e19da
aba9687
 
 
 
12e19da
aba9687
12e19da
aba9687
 
 
12e19da
aba9687
 
 
12e19da
aba9687
12e19da
aba9687
 
 
12e19da
aba9687
 
 
12e19da
 
 
 
aba9687
12e19da
aba9687
 
 
 
12e19da
aba9687
 
12e19da
 
aba9687
 
 
 
0e92a82
12e19da
 
 
 
 
 
 
 
 
 
 
 
 
0e92a82
12e19da
 
 
 
 
 
0e92a82
aba9687
12e19da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aba9687
12e19da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aba9687
 
 
 
 
12e19da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aba9687
 
12e19da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e92a82
aba9687
12e19da
 
 
aba9687
12e19da
aba9687
 
12e19da
 
 
0e92a82
12e19da
 
 
aba9687
 
12e19da
 
 
 
6568dcc
aba9687
12e19da
 
 
 
6568dcc
aba9687
12e19da
 
 
 
 
 
 
6568dcc
aba9687
12e19da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e92a82
12e19da
 
 
 
6568dcc
43b8ce7
12e19da
 
0e92a82
 
12e19da
 
 
aba9687
12e19da
 
 
0e92a82
 
 
12e19da
0e92a82
 
12e19da
 
 
aba9687
6568dcc
12e19da
 
 
 
 
 
0e92a82
 
 
aba9687
6568dcc
12e19da
 
 
0e92a82
 
12e19da
 
0e92a82
12e19da
 
 
0e92a82
 
12e19da
 
aba9687
6568dcc
12e19da
 
 
 
 
 
 
 
 
aba9687
6568dcc
12e19da
 
 
 
 
 
 
 
0e92a82
 
12e19da
 
 
 
 
 
 
 
0e92a82
 
12e19da
 
 
 
 
 
0e92a82
12e19da
 
0e92a82
 
12e19da
 
 
 
 
aba9687
0e92a82
 
 
12e19da
 
 
 
 
 
 
 
 
 
aba9687
6568dcc
12e19da
 
 
 
 
 
 
0e92a82
aba9687
 
12e19da
 
0e92a82
12e19da
0e92a82
12e19da
 
aba9687
 
12e19da
0e92a82
aba9687
 
12e19da
 
 
0e92a82
 
12e19da
 
 
 
 
 
0e92a82
 
12e19da
 
 
 
 
 
 
 
0e92a82
 
12e19da
 
0e92a82
12e19da
 
0e92a82
 
12e19da
0e92a82
 
 
12e19da
 
 
 
 
0e92a82
 
12e19da
0e92a82
12e19da
0e92a82
12e19da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aba9687
 
 
 
 
 
 
 
 
 
12e19da
aba9687
 
12e19da
 
0e92a82
12e19da
aba9687
12e19da
 
 
0e92a82
12e19da
 
0e92a82
aba9687
 
 
0e92a82
aba9687
12e19da
 
 
 
0e92a82
12e19da
 
0e92a82
12e19da
0e92a82
 
 
12e19da
 
0e92a82
 
12e19da
0e92a82
12e19da
0e92a82
12e19da
 
0e92a82
aba9687
12e19da
 
 
 
 
 
 
 
 
 
 
 
 
0e92a82
12e19da
 
 
 
 
 
 
 
 
0e92a82
12e19da
 
0e92a82
12e19da
 
0e92a82
12e19da
0e92a82
 
 
12e19da
 
aba9687
 
 
12e19da
aba9687
12e19da
 
 
 
 
0e92a82
12e19da
0e92a82
12e19da
 
0e92a82
 
12e19da
0e92a82
12e19da
0e92a82
12e19da
0e92a82
12e19da
0e92a82
 
 
12e19da
0e92a82
 
 
12e19da
 
 
 
 
 
 
 
0e92a82
12e19da
 
 
0e92a82
12e19da
 
 
0e92a82
12e19da
 
aba9687
12e19da
 
 
 
0e92a82
12e19da
0e92a82
 
12e19da
0e92a82
12e19da
0e92a82
12e19da
 
0e92a82
 
aba9687
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43b8ce7
12e19da
aba9687
12e19da
 
 
 
 
aba9687
 
c89cc5d
aba9687
 
 
 
 
 
 
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
import gradio as gr
from gradio_client import Client
import json
import time
import threading
import random
from datetime import datetime

# Backend engines
BACKEND_ENGINES = [
    "ChocoLaboratory/SANDBOXBACKEND2",
    "ChocoLaboratory/SANDBOX_BACKEND"
]

class SandboxManager:
    def __init__(self):
        self.client = None
        self.current_engine = None
        self.is_connected = False
        self.last_status = "Disconnected"
        
    def select_random_engine(self):
        """Randomly select an engine"""
        self.current_engine = random.choice(BACKEND_ENGINES)
        return self.current_engine
        
    def connect(self):
        engine = self.select_random_engine()
        try:
            self.client = Client(engine)
            self.is_connected = True
            return f"βœ“ Connected to: {engine}", engine
        except Exception as e:
            self.is_connected = False
            self.current_engine = None
            return f"βœ— Connection failed to {engine}: {str(e)}", "Not connected"
    
    def launch_sandbox(self, main_py_code, requirements_txt):
        if not self.is_connected:
            return "βœ— Not connected to backend. Please connect first.", ""
        
        try:
            result = self.client.predict(
                code=main_py_code,
                requirements=requirements_txt,
                api_name="/launch_sandbox"
            )
            return f"βœ“ Sandbox deployed successfully on {self.current_engine}\n\nResponse: {result}", self.get_status()
        except Exception as e:
            return f"βœ— Deployment failed: {str(e)}", ""
    
    def fetch_logs(self):
        if not self.is_connected:
            return "βœ— Not connected to backend"
        
        try:
            result = self.client.predict(api_name="/fetch_logs")
            timestamp = datetime.now().strftime("%H:%M:%S")
            return f"[{timestamp}] Logs from {self.current_engine}:\n\n{result}"
        except Exception as e:
            return f"[ERROR] Failed to fetch logs: {str(e)}"
    
    def kill_sandbox(self):
        if not self.is_connected:
            return "βœ— Not connected to backend", ""
        
        try:
            result = self.client.predict(api_name="/kill_sandbox")
            return f"βœ“ Sandbox terminated on {self.current_engine}\n\nResponse: {result}", self.get_status()
        except Exception as e:
            return f"βœ— Termination failed: {str(e)}", ""
    
    def get_status(self):
        if not self.is_connected:
            return "OFFLINE"
        
        try:
            result = self.client.predict(api_name="/status_sandbox")
            if "running" in str(result).lower():
                return f"RUNNING - {result}"
            else:
                return f"IDLE - {result}"
        except Exception as e:
            return f"ERROR - {str(e)}"

# Initialize sandbox manager
sandbox = SandboxManager()

# Professional code templates
SAMPLE_MAIN_PY = """#!/usr/bin/env python3
\"\"\"
Sandbox Compute Environment
Enterprise Python Application Template
\"\"\"

import os
import sys
import json
import logging
from datetime import datetime, timezone

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

def initialize_environment():
    \"\"\"Initialize the compute environment\"\"\"
    logger.info("Initializing sandbox environment...")
    
    env_info = {
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "python_version": sys.version,
        "platform": os.name,
        "working_directory": os.getcwd(),
        "environment_variables": dict(os.environ)
    }
    
    logger.info(f"Environment initialized: {env_info['timestamp']}")
    return env_info

def execute_business_logic():
    \"\"\"Main application business logic\"\"\"
    logger.info("Executing main application logic...")
    
    # Example: Data processing pipeline
    data_points = list(range(1, 101))
    processed_data = [x ** 2 for x in data_points if x % 2 == 0]
    
    result = {
        "status": "success",
        "data_points_processed": len(processed_data),
        "sum_of_squares": sum(processed_data),
        "processing_time": datetime.now(timezone.utc).isoformat()
    }
    
    logger.info(f"Processing complete: {result}")
    return result

def main():
    \"\"\"Main application entry point\"\"\"
    try:
        print("=" * 60)
        print("SANDBOX COMPUTE ENVIRONMENT - PRODUCTION RUNTIME")
        print("=" * 60)
        
        # Initialize
        env_info = initialize_environment()
        print(f"Environment: {env_info['platform']}")
        print(f"Python: {env_info['python_version'].split()[0]}")
        print(f"Started: {env_info['timestamp']}")
        
        print("\\n" + "-" * 40)
        print("EXECUTING APPLICATION LOGIC")
        print("-" * 40)
        
        # Execute business logic
        result = execute_business_logic()
        
        # Output results
        print(f"\\nStatus: {result['status'].upper()}")
        print(f"Data Points: {result['data_points_processed']}")
        print(f"Computation Result: {result['sum_of_squares']}")
        print(f"Completed: {result['processing_time']}")
        
        print("\\n" + "=" * 60)
        print("APPLICATION EXECUTION COMPLETED SUCCESSFULLY")
        print("=" * 60)
        
    except Exception as e:
        logger.error(f"Application failed: {str(e)}")
        raise

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

SAMPLE_REQUIREMENTS_TXT = """# Enterprise Python Dependencies
# Production-grade package specifications

# Core utilities and date handling
requests>=2.31.0,<3.0.0
python-dateutil>=2.8.2,<3.0.0
pytz>=2023.3

# Data processing and analysis
numpy>=1.24.0,<2.0.0
pandas>=2.0.0,<3.0.0

# Logging and monitoring
structlog>=23.1.0
prometheus-client>=0.17.0

# Configuration management
pydantic>=2.0.0,<3.0.0
python-dotenv>=1.0.0

# Database connectivity (uncomment as needed)
# sqlalchemy>=2.0.0,<3.0.0
# psycopg2-binary>=2.9.0
# redis>=4.5.0

# Web frameworks (uncomment as needed)
# fastapi>=0.100.0,<1.0.0
# uvicorn[standard]>=0.23.0
# gunicorn>=21.0.0

# Scientific computing (uncomment as needed)
# scipy>=1.11.0,<2.0.0
# scikit-learn>=1.3.0,<2.0.0
# matplotlib>=3.7.0,<4.0.0

# Development and testing (uncomment as needed)
# pytest>=7.4.0,<8.0.0
# black>=23.0.0,<24.0.0
# mypy>=1.5.0,<2.0.0
# flake8>=6.0.0,<7.0.0
"""

# Professional industrial CSS
professional_css = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500;600&display=swap');

:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --secondary-color: #64748b;
    --success-color: #059669;
    --warning-color: #d97706;
    --danger-color: #dc2626;
    --background: #f8fafc;
    --surface: #ffffff;
    --border: #e2e8f0;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
}

.gradio-container {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
    background-color: var(--background) !important;
    color: var(--text-primary) !important;
    min-height: 100vh;
    line-height: 1.5;
}

/* Professional Header */
.platform-header {
    background: linear-gradient(135deg, #1e293b 0%, #334155 100%) !important;
    color: white !important;
    padding: 16px 24px !important;
    border-bottom: 3px solid var(--primary-color) !important;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;
}

.platform-title {
    font-size: 24px !important;
    font-weight: 700 !important;
    letter-spacing: -0.025em !important;
}

.platform-subtitle {
    font-size: 14px !important;
    opacity: 0.8 !important;
    margin-top: 4px !important;
}

/* Navigation */
.nav-breadcrumb {
    background-color: #f1f5f9 !important;
    padding: 12px 24px !important;
    font-size: 13px !important;
    color: var(--text-secondary) !important;
    border-bottom: 1px solid var(--border) !important;
}

/* Container System */
.enterprise-container {
    background-color: var(--surface) !important;
    border: 1px solid var(--border) !important;
    border-radius: 12px !important;
    margin: 20px !important;
    overflow: hidden !important;
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06) !important;
}

.container-header {
    background: linear-gradient(to right, #f8fafc, #f1f5f9) !important;
    border-bottom: 1px solid var(--border) !important;
    padding: 16px 24px !important;
    font-weight: 600 !important;
    font-size: 16px !important;
    color: var(--text-primary) !important;
    display: flex !important;
    align-items: center !important;
    gap: 8px !important;
}

.container-content {
    padding: 24px !important;
}

/* Professional Buttons */
.btn-primary {
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-hover) 100%) !important;
    color: white !important;
    border: none !important;
    border-radius: 8px !important;
    padding: 12px 24px !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    transition: all 0.2s ease !important;
    box-shadow: 0 2px 4px rgba(37, 99, 235, 0.2) !important;
}

.btn-primary:hover {
    transform: translateY(-1px) !important;
    box-shadow: 0 4px 8px rgba(37, 99, 235, 0.3) !important;
}

.btn-secondary {
    background-color: var(--surface) !important;
    color: var(--text-primary) !important;
    border: 1px solid var(--border) !important;
    border-radius: 8px !important;
    padding: 12px 24px !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    transition: all 0.2s ease !important;
}

.btn-secondary:hover {
    background-color: #f8fafc !important;
    border-color: var(--secondary-color) !important;
}

.btn-danger {
    background: linear-gradient(135deg, var(--danger-color) 0%, #b91c1c 100%) !important;
    color: white !important;
    border: none !important;
    border-radius: 8px !important;
    padding: 12px 24px !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    transition: all 0.2s ease !important;
    box-shadow: 0 2px 4px rgba(220, 38, 38, 0.2) !important;
}

/* Status Indicators */
.status-online {
    background: linear-gradient(135deg, #ecfdf5 0%, #d1fae5 100%) !important;
    color: var(--success-color) !important;
    border: 1px solid #86efac !important;
    border-radius: 8px !important;
    padding: 16px !important;
    font-weight: 600 !important;
    text-align: center !important;
}

.status-offline {
    background: linear-gradient(135deg, #fef2f2 0%, #fee2e2 100%) !important;
    color: var(--danger-color) !important;
    border: 1px solid #fca5a5 !important;
    border-radius: 8px !important;
    padding: 16px !important;
    font-weight: 600 !important;
    text-align: center !important;
}

.status-warning {
    background: linear-gradient(135deg, #fffbeb 0%, #fef3c7 100%) !important;
    color: var(--warning-color) !important;
    border: 1px solid #fde68a !important;
    border-radius: 8px !important;
    padding: 16px !important;
    font-weight: 600 !important;
    text-align: center !important;
}

/* Code Editor */
.code-editor-professional {
    font-family: 'JetBrains Mono', 'SF Mono', Monaco, 'Cascadia Code', monospace !important;
    background: #1e1e1e !important;
    border: 2px solid var(--border) !important;
    border-radius: 8px !important;
    font-size: 14px !important;
    line-height: 1.5 !important;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.06) !important;
}

.requirements-editor {
    font-family: 'JetBrains Mono', monospace !important;
    background: #2d2d2d !important;
    border: 2px solid var(--border) !important;
    border-radius: 8px !important;
    font-size: 13px !important;
    line-height: 1.4 !important;
}

/* Logs Terminal */
.terminal-logs {
    font-family: 'JetBrains Mono', monospace !important;
    background: #0f172a !important;
    color: #e2e8f0 !important;
    border: 2px solid #334155 !important;
    border-radius: 8px !important;
    font-size: 13px !important;
    line-height: 1.6 !important;
    padding: 16px !important;
}

/* Sidebar */
.monitoring-panel {
    background-color: var(--surface) !important;
    border: 1px solid var(--border) !important;
    border-radius: 12px !important;
    overflow: hidden !important;
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1) !important;
    height: fit-content !important;
}

.panel-header {
    background: linear-gradient(to right, #f1f5f9, #e2e8f0) !important;
    padding: 12px 16px !important;
    font-weight: 600 !important;
    font-size: 14px !important;
    color: var(--text-primary) !important;
    border-bottom: 1px solid var(--border) !important;
}

.panel-content {
    padding: 16px !important;
}

/* Form Elements */
.form-group {
    margin-bottom: 20px !important;
}

.form-label {
    font-size: 14px !important;
    font-weight: 500 !important;
    color: var(--text-primary) !important;
    margin-bottom: 6px !important;
    display: block !important;
}

.info-badge {
    background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%) !important;
    color: var(--primary-color) !important;
    border: 1px solid #93c5fd !important;
    border-radius: 6px !important;
    padding: 12px 16px !important;
    font-size: 13px !important;
    font-weight: 500 !important;
}

/* Metrics */
.metric-card {
    text-align: center !important;
    padding: 16px !important;
    border-right: 1px solid var(--border) !important;
}

.metric-card:last-child {
    border-right: none !important;
}

.metric-value {
    font-size: 28px !important;
    font-weight: 700 !important;
    color: var(--primary-color) !important;
    line-height: 1 !important;
}

.metric-label {
    font-size: 12px !important;
    color: var(--text-secondary) !important;
    margin-top: 4px !important;
    font-weight: 500 !important;
    text-transform: uppercase !important;
    letter-spacing: 0.025em !important;
}

/* Professional spacing */
.section-divider {
    height: 1px !important;
    background: var(--border) !important;
    margin: 24px 0 !important;
}

.mb-4 {
    margin-bottom: 16px !important;
}

.mt-4 {
    margin-top: 16px !important;
}
"""

def auto_refresh_status():
    return sandbox.get_status()

def auto_refresh_logs():
    return sandbox.fetch_logs()

def load_sample_files():
    """Load professional sample files"""
    return SAMPLE_MAIN_PY, SAMPLE_REQUIREMENTS_TXT

# Build the professional interface
with gr.Blocks(css=professional_css, title="Sandbox Compute Platform", theme=gr.themes.Default()) as demo:
    
    # Platform Header
    gr.HTML("""
    <div class="platform-header">
        <div class="platform-title">Sandbox Compute Platform</div>
        <div class="platform-subtitle">Enterprise-grade cloud computing infrastructure</div>
    </div>
    <div class="nav-breadcrumb">
        Dashboard β†’ Compute Services β†’ Sandbox Management Console
    </div>
    """)
    
    with gr.Row():
        # Main Content Area
        with gr.Column(scale=3):
            # Infrastructure Management
            with gr.Group(elem_classes=["enterprise-container"]):
                gr.HTML('<div class="container-header">πŸ”§ Infrastructure Management</div>')
                with gr.Group(elem_classes=["container-content"]):
                    with gr.Row():
                        connect_btn = gr.Button("Initialize Connection", elem_classes=["btn-primary"])
                        load_samples_btn = gr.Button("Load Templates", elem_classes=["btn-secondary"])
                    
                    gr.HTML('<div class="form-label mt-4">Active Compute Engine</div>')
                    current_engine_display = gr.Textbox(
                        value="No engine selected",
                        interactive=False,
                        elem_classes=["info-badge"],
                        show_label=False
                    )
                    
                    gr.HTML('<div class="form-label mt-4">Connection Status</div>')
                    connection_status = gr.Textbox(
                        value="DISCONNECTED", 
                        interactive=False,
                        elem_classes=["status-offline"],
                        show_label=False
                    )
            
            # Development Environment
            with gr.Group(elem_classes=["enterprise-container"]):
                gr.HTML('<div class="container-header">πŸ’» Development Environment</div>')
                with gr.Group(elem_classes=["container-content"]):
                    with gr.Tab("main.py"):
                        gr.HTML('<div class="form-label">Application Source Code</div>')
                        main_py_editor = gr.Code(
                            value="# Enterprise Python Application\nprint('Sandbox Compute Platform - Ready')",
                            language="python",
                            lines=20,
                            elem_classes=["code-editor-professional"],
                            show_label=False
                        )
                    
                    with gr.Tab("requirements.txt"):
                        gr.HTML('<div class="form-label">Dependency Specifications</div>')
                        requirements_editor = gr.Textbox(
                            value="# Production dependencies\n# Specify exact versions for reproducibility",
                            lines=20,
                            elem_classes=["requirements-editor"],
                            show_label=False,
                            max_lines=25
                        )
                    
                    # Deployment Controls
                    gr.HTML('<div class="section-divider"></div>')
                    with gr.Row():
                        launch_btn = gr.Button("Deploy Application", elem_classes=["btn-primary"], size="lg")
                        kill_btn = gr.Button("Terminate Instance", elem_classes=["btn-danger"], size="lg")
                    
                    gr.HTML('<div class="form-label mt-4">Deployment Output</div>')
                    launch_output = gr.Textbox(
                        lines=6,
                        interactive=False,
                        placeholder="Deployment logs and status information will appear here...",
                        elem_classes=["terminal-logs"],
                        show_label=False
                    )
        
        # Monitoring Panel
        with gr.Column(scale=1):
            # System Status
            with gr.Group(elem_classes=["monitoring-panel"]):
                gr.HTML('<div class="panel-header">πŸ“Š System Monitor</div>')
                with gr.Group(elem_classes=["panel-content"]):
                    gr.HTML('<div class="form-label">Instance Status</div>')
                    status_display = gr.Textbox(
                        value="OFFLINE",
                        interactive=False,
                        elem_classes=["status-offline"],
                        show_label=False
                    )
                    
                    status_refresh_btn = gr.Button("Refresh Status", elem_classes=["btn-secondary"], size="sm")
                    
                    gr.HTML('<div class="section-divider"></div>')
                    
                    gr.HTML('<div class="form-label">Monitoring Settings</div>')
                    auto_refresh_status_cb = gr.Checkbox(
                        label="Auto-refresh status (5s interval)", 
                        value=False
                    )
                    auto_refresh_logs_cb = gr.Checkbox(
                        label="Auto-refresh logs (10s interval)", 
                        value=False
                    )
            
            # System Metrics
            with gr.Group(elem_classes=["monitoring-panel"]):
                gr.HTML('<div class="panel-header">πŸ“ˆ Performance Metrics</div>')
                gr.HTML('''
                <div style="padding: 16px;">
                    <div class="metric-card">
                        <div class="metric-value">--</div>
                        <div class="metric-label">CPU Usage</div>
                    </div>
                    <div class="metric-card">
                        <div class="metric-value">--</div>
                        <div class="metric-label">Memory</div>
                    </div>
                    <div class="metric-card">
                        <div class="metric-value">--</div>
                        <div class="metric-label">Uptime</div>
                    </div>
                </div>
                ''')
    
    # System Logs
    with gr.Group(elem_classes=["enterprise-container"]):
        gr.HTML('<div class="container-header">πŸ“‹ System Logs</div>')
        with gr.Group(elem_classes=["container-content"]):
            with gr.Row():
                fetch_logs_btn = gr.Button("Fetch Application Logs", elem_classes=["btn-secondary"])
                gr.HTML('<div style="flex-grow: 1;"></div>')
            
            gr.HTML('<div class="form-label mt-4">Runtime Logs</div>')
            logs_display = gr.Textbox(
                lines=14,
                interactive=False,
                elem_classes=["terminal-logs"],
                placeholder="[SYSTEM] Application logs will be displayed here...\n[INFO] Logs are captured in real-time from your running application\n[DEBUG] Use 'Fetch Application Logs' to retrieve the latest output",
                show_label=False
            )
    
    # Event Handlers
    connect_btn.click(
        fn=sandbox.connect,
        outputs=[connection_status, current_engine_display]
    )
    
    load_samples_btn.click(
        fn=load_sample_files,
        outputs=[main_py_editor, requirements_editor]
    )
    
    launch_btn.click(
        fn=sandbox.launch_sandbox,
        inputs=[main_py_editor, requirements_editor],
        outputs=[launch_output, status_display]
    )
    
    kill_btn.click(
        fn=sandbox.kill_sandbox,
        outputs=[launch_output, status_display]
    )
    
    status_refresh_btn.click(
        fn=sandbox.get_status,
        outputs=[status_display]
    )
    
    fetch_logs_btn.click(
        fn=sandbox.fetch_logs,
        outputs=[logs_display]
    )
    
    # Auto-refresh Timers
    status_timer = gr.Timer(5)
    logs_timer = gr.Timer(10)
    
    status_timer.tick(
        fn=auto_refresh_status,
        outputs=[status_display],
        show_progress=False
    )
    
    logs_timer.tick(
        fn=auto_refresh_logs,
        outputs=[logs_display],
        show_progress=False
    )
    
    # Timer Controls
    auto_refresh_status_cb.change(
        lambda enabled: gr.Timer(5 if enabled else None),
        inputs=[auto_refresh_status_cb],
        outputs=[status_timer]
    )
    
    auto_refresh_logs_cb.change(
        lambda enabled: gr.Timer(10 if enabled else None),
        inputs=[auto_refresh_logs_cb],
        outputs=[logs_timer]
    )

    # Professional Footer
    gr.HTML("""
    <div style="text-align: center; margin-top: 40px; padding: 24px; background: linear-gradient(to right, #f8fafc, #f1f5f9); border-top: 1px solid #e2e8f0; color: #64748b; font-size: 13px;">
        <p style="font-weight: 500; margin-bottom: 8px;">Sandbox Compute Platform</p>
        <p style="font-size: 12px; margin-top: 12px; opacity: 0.8;">
            Runtime Engines: SANDBOXBACKEND2, SANDBOX_BACKEND 
        </p>
    </div>
    """)

if __name__ == "__main__":
    demo.launch(
        share=True,
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True
    )