File size: 31,120 Bytes
87d431f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6ab02e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d431f
 
 
a6ab02e
87d431f
a6ab02e
87d431f
 
 
 
 
 
 
 
 
 
 
a6ab02e
 
 
 
 
87d431f
 
 
 
 
a6ab02e
 
 
 
 
 
 
 
 
 
 
 
 
87d431f
 
 
 
 
 
 
 
 
 
 
a6ab02e
87d431f
a6ab02e
87d431f
 
 
 
a6ab02e
87d431f
 
a6ab02e
 
 
 
 
 
 
 
 
 
 
 
87d431f
 
 
 
 
 
 
 
a6ab02e
87d431f
 
a6ab02e
 
87d431f
a6ab02e
 
 
 
87d431f
 
 
 
 
 
 
a6ab02e
 
87d431f
a6ab02e
 
 
 
 
 
 
 
 
 
87d431f
a6ab02e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d431f
a6ab02e
 
 
 
 
 
 
 
 
 
 
 
 
 
87d431f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6ab02e
87d431f
 
 
 
 
 
 
a6ab02e
87d431f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6ab02e
 
 
 
87d431f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6ab02e
87d431f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6ab02e
87d431f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6ab02e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d431f
 
a6ab02e
 
 
 
87d431f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6ab02e
87d431f
 
 
 
 
 
a6ab02e
87d431f
 
 
 
 
a6ab02e
 
 
 
 
87d431f
 
 
a6ab02e
87d431f
 
a6ab02e
 
87d431f
 
a6ab02e
87d431f
 
 
a6ab02e
87d431f
 
 
 
 
 
a6ab02e
 
87d431f
 
 
 
a6ab02e
87d431f
 
 
 
 
a6ab02e
 
87d431f
 
a6ab02e
87d431f
 
 
 
a6ab02e
87d431f
 
 
a6ab02e
 
 
87d431f
 
 
 
 
 
a6ab02e
87d431f
 
 
a6ab02e
87d431f
 
 
 
 
 
 
 
a6ab02e
 
 
 
 
 
 
 
 
 
 
87d431f
a6ab02e
 
87d431f
 
a6ab02e
87d431f
a6ab02e
 
87d431f
a6ab02e
 
87d431f
 
 
a6ab02e
87d431f
 
 
 
 
a6ab02e
 
 
 
 
 
 
 
 
87d431f
 
a6ab02e
 
87d431f
 
 
 
 
 
a6ab02e
 
87d431f
 
 
 
 
a6ab02e
87d431f
a6ab02e
 
87d431f
 
 
 
a6ab02e
87d431f
a6ab02e
87d431f
 
a6ab02e
87d431f
a6ab02e
 
87d431f
 
 
a6ab02e
 
87d431f
 
 
 
a6ab02e
 
87d431f
 
 
 
 
 
 
 
a6ab02e
87d431f
a6ab02e
87d431f
 
 
 
a6ab02e
87d431f
 
 
 
 
 
 
a6ab02e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d431f
 
 
 
 
 
 
 
a6ab02e
87d431f
 
 
 
 
 
 
a6ab02e
87d431f
 
 
 
 
 
a6ab02e
 
 
 
 
 
 
 
 
87d431f
 
 
a6ab02e
87d431f
 
 
a6ab02e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d431f
 
 
 
 
 
a6ab02e
87d431f
a6ab02e
 
 
 
 
 
 
 
 
 
 
 
 
87d431f
 
 
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
# File: lcars_enhanced_interface.py

import asyncio
import json
import os
import time
import uuid
from typing import Dict, List, Any, Optional
from dataclasses import dataclass
import threading
import pyttsx3
import re
from pathlib import Path

import gradio as gr
from rich.console import Console
from openai import OpenAI, AsyncOpenAI

# --- Configuration ---
LOCAL_BASE_URL = "http://localhost:1234/v1"
LOCAL_API_KEY = "not-needed"

# HuggingFace Spaces configuration
HF_INFERENCE_URL = "https://api-inference.huggingface.co/models/"
HF_API_KEY = os.getenv("HF_API_KEY", "")

# Available model options
MODEL_OPTIONS = {
    "Local LM Studio": LOCAL_BASE_URL,
    "Codellama 7B": "codellama/CodeLlama-7b-hf",
    "Mistral 7B": "mistralai/Mistral-7B-v0.1", 
    "Llama 2 7B": "meta-llama/Llama-2-7b-chat-hf",
    "Falcon 7B": "tiiuae/falcon-7b-instruct"
}

DEFAULT_TEMPERATURE = 0.7
DEFAULT_MAX_TOKENS = 5000

console = Console()

# --- Canvas Artifact Dataclass ---
@dataclass
class CanvasArtifact:
    id: str
    type: str  # 'code', 'diagram', 'text', 'image'
    content: str
    title: str
    timestamp: float
    metadata: Dict[str, Any]

# --- Enhanced LLMAgent with Canvas Support ---
class EnhancedLLMAgent:
    def __init__(self, model_id: str = "local-model", system_prompt: str = None, 
                 base_url: str = LOCAL_BASE_URL, api_key: str = LOCAL_API_KEY, 
                 use_huggingface: bool = False):
        
        self.use_huggingface = use_huggingface
        self.model_id = model_id
        self.system_prompt = system_prompt or """You are an advanced AI development assistant operating in a Star Trek LCARS interface. 
        You specialize in code generation, analysis, and collaborative development. 
        Always provide practical, executable code solutions when appropriate.
        Format code responses clearly with proper markdown code blocks and explain your reasoning."""
        
        if use_huggingface:
            # Use HuggingFace Inference API
            self.base_url = HF_INFERENCE_URL
            self.api_key = HF_API_KEY
            self.client = None
            console.log("[green]πŸš€ Using HuggingFace Inference API[/green]")
        else:
            # Use local LM Studio
            self.base_url = base_url
            self.api_key = api_key
            self.client = OpenAI(base_url=base_url, api_key=api_key)
            console.log(f"[green]πŸš€ Using Local LM Studio: {base_url}[/green]")
        
        # Enhanced conversation and canvas management
        self.conversations: Dict[str, List[Dict]] = {}
        self.canvas_artifacts: Dict[str, List[CanvasArtifact]] = {}
        self.max_history_length = 50
        
        # Speech synthesis
        try:
            self.tts_engine = pyttsx3.init()
            self.setup_tts()
            self.speech_enabled = True
            console.log("[green]TTS engine initialized successfully[/green]")
        except Exception as e:
            console.log(f"[red]TTS initialization failed: {e}[/red]")
            self.speech_enabled = False

    def setup_tts(self):
        """Configure text-to-speech engine"""
        try:
            voices = self.tts_engine.getProperty('voices')
            if voices:
                # Try to find a better voice
                for voice in voices:
                    if 'female' in voice.name.lower() or 'zira' in voice.name.lower():
                        self.tts_engine.setProperty('voice', voice.id)
                        break
                else:
                    self.tts_engine.setProperty('voice', voices[0].id)
            
            self.tts_engine.setProperty('rate', 180)
            self.tts_engine.setProperty('volume', 1.0)
        except Exception as e:
            console.log(f"[red]TTS setup error: {e}[/red]")

    def speak(self, text: str):
        """Convert text to speech in a non-blocking way"""
        if not hasattr(self, 'speech_enabled') or not self.speech_enabled:
            return
            
        def _speak():
            try:
                # Clean text for speech
                clean_text = re.sub(r'```.*?```', '', text, flags=re.DOTALL)
                clean_text = re.sub(r'`.*?`', '', clean_text)
                clean_text = re.sub(r'\n+', '. ', clean_text)
                clean_text = re.sub(r'\s+', ' ', clean_text)
                clean_text = clean_text.strip()
                
                if clean_text and len(clean_text) > 10:
                    console.log(f"[blue]Speaking: {clean_text[:100]}...[/blue]")
                    self.tts_engine.say(clean_text[:400])
                    self.tts_engine.runAndWait()
            except Exception as e:
                console.log(f"[red]TTS Error: {e}[/red]")
        
        thread = threading.Thread(target=_speak, daemon=True)
        thread.start()

    async def _local_inference(self, messages: List[Dict]) -> str:
        """Use local LM Studio"""
        try:
            async_client = AsyncOpenAI(base_url=self.base_url, api_key=self.api_key)
            response = await async_client.chat.completions.create(
                model=self.model_id,
                messages=messages,
                temperature=0.7,
                max_tokens=DEFAULT_MAX_TOKENS
            )
            return response.choices[0].message.content
        except Exception as e:
            return f"Local inference error: {str(e)}"

    async def _hf_inference(self, messages: List[Dict]) -> str:
        """Use HuggingFace Inference API"""
        try:
            import requests
            # Convert to HF format
            prompt = self._convert_messages_to_prompt(messages)
            
            headers = {
                "Authorization": f"Bearer {self.api_key}",
                "Content-Type": "application/json"
            }
            
            payload = {
                "inputs": prompt,
                "parameters": {
                    "max_new_tokens": DEFAULT_MAX_TOKENS,
                    "temperature": 0.7,
                    "do_sample": True,
                    "return_full_text": False
                }
            }
            
            model_url = f"{self.base_url}{self.model_id}"
            response = requests.post(model_url, headers=headers, json=payload, timeout=30)
            response.raise_for_status()
            result = response.json()
            return result[0]['generated_text']
        except Exception as e:
            return f"HuggingFace API Error: {str(e)}"

    def _convert_messages_to_prompt(self, messages: List[Dict]) -> str:
        """Convert conversation messages to a single prompt for HF"""
        prompt = ""
        for msg in messages:
            if msg["role"] == "system":
                prompt += f"System: {msg['content']}\n\n"
            elif msg["role"] == "user":
                prompt += f"User: {msg['content']}\n\n"
            elif msg["role"] == "assistant":
                prompt += f"Assistant: {msg['content']}\n\n"
        prompt += "Assistant:"
        return prompt

    def add_artifact_to_canvas(self, conversation_id: str, content: str, artifact_type: str = "code", title: str = None):
        """Add artifacts to the collaborative canvas"""
        if conversation_id not in self.canvas_artifacts:
            self.canvas_artifacts[conversation_id] = []
        
        artifact = CanvasArtifact(
            id=str(uuid.uuid4())[:8],
            type=artifact_type,
            content=content,
            title=title or f"{artifact_type}_{len(self.canvas_artifacts[conversation_id]) + 1}",
            timestamp=time.time(),
            metadata={"conversation_id": conversation_id}
        )
        
        self.canvas_artifacts[conversation_id].append(artifact)
        console.log(f"[green]Added artifact to canvas: {artifact.title}[/green]")
        return artifact

    def get_canvas_context(self, conversation_id: str) -> str:
        """Get formatted canvas context for LLM prompts"""
        if conversation_id not in self.canvas_artifacts or not self.canvas_artifacts[conversation_id]:
            return ""
        
        context_lines = ["\n=== COLLABORATIVE CANVAS ARTIFACTS ==="]
        for artifact in self.canvas_artifacts[conversation_id][-10:]:
            context_lines.append(f"\n--- {artifact.title} [{artifact.type.upper()}] ---")
            preview = artifact.content[:500] + "..." if len(artifact.content) > 500 else artifact.content
            context_lines.append(preview)
        
        return "\n".join(context_lines) + "\n=================================\n"

    async def chat_with_canvas(self, message: str, conversation_id: str = "default", include_canvas: bool = True) -> str:
        """Enhanced chat that works with both local and HF"""
        if conversation_id not in self.conversations:
            self.conversations[conversation_id] = []
        
        # Build messages with system prompt and canvas context
        messages = [{"role": "system", "content": self.system_prompt}]
        
        # Include canvas context if requested
        if include_canvas:
            canvas_context = self.get_canvas_context(conversation_id)
            if canvas_context:
                messages.append({"role": "system", "content": f"Current collaborative canvas state:\n{canvas_context}"})
        
        # Add conversation history
        for msg in self.conversations[conversation_id][-self.max_history_length:]:
            messages.append(msg)
        
        # Add current message
        messages.append({"role": "user", "content": message})
        
        try:
            if self.use_huggingface:
                response_text = await self._hf_inference(messages)
            else:
                response_text = await self._local_inference(messages)
            
            # Update conversation history
            self.conversations[conversation_id].extend([
                {"role": "user", "content": message},
                {"role": "assistant", "content": response_text}
            ])
            
            # Auto-extract and add code artifacts to canvas
            self._extract_artifacts_to_canvas(response_text, conversation_id)
            
            return response_text
            
        except Exception as e:
            error_msg = f"Error in chat_with_canvas: {str(e)}"
            console.log(f"[red]{error_msg}[/red]")
            return error_msg

    def _extract_artifacts_to_canvas(self, response: str, conversation_id: str):
        """Automatically extract code blocks and add to canvas"""
        code_blocks = re.findall(r'```(?:\w+)?\n(.*?)```', response, re.DOTALL)
        for i, code_block in enumerate(code_blocks):
            if len(code_block.strip()) > 10:
                lang_match = re.search(r'```(\w+)\n', response)
                lang = lang_match.group(1) if lang_match else "unknown"
                
                self.add_artifact_to_canvas(
                    conversation_id, 
                    code_block.strip(), 
                    "code", 
                    f"code_snippet_{lang}_{len(self.canvas_artifacts.get(conversation_id, [])) + 1}"
                )

    def clear_conversation(self, conversation_id: str = "default"):
        """Clear conversation but keep canvas artifacts"""
        if conversation_id in self.conversations:
            self.conversations[conversation_id] = []
            console.log(f"[yellow]Cleared conversation: {conversation_id}[/yellow]")

    def clear_canvas(self, conversation_id: str = "default"):
        """Clear canvas artifacts"""
        if conversation_id in self.canvas_artifacts:
            self.canvas_artifacts[conversation_id] = []
            console.log(f"[yellow]Cleared canvas: {conversation_id}[/yellow]")

    def get_canvas_summary(self, conversation_id: str) -> List[Dict]:
        """Get summary of canvas artifacts for display"""
        if conversation_id not in self.canvas_artifacts:
            return []
        
        artifacts = []
        for artifact in reversed(self.canvas_artifacts[conversation_id]):
            artifacts.append({
                "id": artifact.id,
                "type": artifact.type.upper(),
                "title": artifact.title,
                "preview": artifact.content[:100] + "..." if len(artifact.content) > 100 else artifact.content,
                "timestamp": time.strftime("%H:%M:%S", time.localtime(artifact.timestamp))
            })
        
        return artifacts

    def get_artifact_by_id(self, conversation_id: str, artifact_id: str) -> Optional[CanvasArtifact]:
        """Get specific artifact by ID"""
        if conversation_id not in self.canvas_artifacts:
            return None
            
        for artifact in self.canvas_artifacts[conversation_id]:
            if artifact.id == artifact_id:
                return artifact
        return None

    def update_config(self, base_url: str, api_key: str, model_id: str, temperature: float, max_tokens: int):
        """Update agent configuration"""
        self.base_url = base_url
        self.api_key = api_key
        self.model_id = model_id
        console.log(f"[blue]Updated config: {model_id} @ {base_url}[/blue]")

    @staticmethod
    async def fetch_available_models(base_url: str, api_key: str, use_huggingface: bool = False) -> List[str]:
        """Fetch available models - works for both local and HF"""
        if use_huggingface:
            # Return popular HF models
            return list(MODEL_OPTIONS.keys())[1:]  # Skip "Local LM Studio"
        else:
            # Fetch from local LM Studio
            try:
                console.log(f"[blue]Fetching models from {base_url}[/blue]")
                async_client = AsyncOpenAI(base_url=base_url, api_key=api_key)
                models = await async_client.models.list()
                model_list = [model.id for model in models.data]
                console.log(f"[green]Found {len(model_list)} local models[/green]")
                return model_list
            except Exception as e:
                console.log(f"[red]Error fetching local models: {e}[/red]")
                return ["local-model"]

# --- LCARS Styled Gradio Interface ---
class LcarsInterface:
    def __init__(self):
        # Start with HuggingFace by default for Spaces
        self.use_huggingface = True
        self.agent = EnhancedLLMAgent(use_huggingface=self.use_huggingface)
        self.current_conversation = "default"

    def create_interface(self):
        """Create the full LCARS-styled interface"""
        
        lcars_css = """
        :root {
            --lcars-orange: #FF9900;
            --lcars-red: #FF0033;
            --lcars-blue: #6699FF;
            --lcars-purple: #CC99FF;
            --lcars-pale-blue: #99CCFF;
            --lcars-black: #000000;
            --lcars-dark-blue: #3366CC;
            --lcars-gray: #424242;
            --lcars-yellow: #FFFF66;
        }
        
        body {
            background: var(--lcars-black);
            color: var(--lcars-orange);
            font-family: 'Antonio', 'LCD', 'Courier New', monospace;
            margin: 0;
            padding: 0;
        }
        
        .gradio-container {
            background: var(--lcars-black) !important;
            min-height: 100vh;
        }
        
        .lcars-container {
            background: var(--lcars-black);
            border: 4px solid var(--lcars-orange);
            border-radius: 0 30px 0 0;
            min-height: 100vh;
            padding: 20px;
        }
        
        .lcars-header {
            background: linear-gradient(90deg, var(--lcars-red), var(--lcars-orange));
            padding: 20px 40px;
            border-radius: 0 60px 0 0;
            margin: -20px -20px 20px -20px;
            border-bottom: 6px solid var(--lcars-blue);
        }
        
        .lcars-title {
            font-size: 2.5em;
            font-weight: bold;
            color: var(--lcars-black);
            margin: 0;
        }
        
        .lcars-subtitle {
            font-size: 1.2em;
            color: var(--lcars-black);
            margin: 10px 0 0 0;
        }
        
        .lcars-panel {
            background: rgba(66, 66, 66, 0.9);
            border: 2px solid var(--lcars-orange);
            border-radius: 0 20px 0 20px;
            padding: 15px;
            margin-bottom: 15px;
        }
        
        .lcars-button {
            background: var(--lcars-orange);
            color: var(--lcars-black) !important;
            border: none !important;
            border-radius: 0 15px 0 15px !important;
            padding: 10px 20px !important;
            font-family: inherit !important;
            font-weight: bold !important;
            margin: 5px !important;
        }
        
        .lcars-button:hover {
            background: var(--lcars-red) !important;
        }
        
        .lcars-input {
            background: var(--lcars-black) !important;
            color: var(--lcars-orange) !important;
            border: 2px solid var(--lcars-blue) !important;
            border-radius: 0 10px 0 10px !important;
            padding: 10px !important;
        }
        
        .lcars-chatbot {
            background: var(--lcars-black) !important;
            border: 2px solid var(--lcars-purple) !important;
            border-radius: 0 15px 0 15px !important;
        }
        
        .status-indicator {
            display: inline-block;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: var(--lcars-red);
            margin-right: 8px;
        }
        
        .status-online {
            background: var(--lcars-blue);
            animation: pulse 2s infinite;
        }
        
        @keyframes pulse {
            0% { opacity: 1; }
            50% { opacity: 0.5; }
            100% { opacity: 1; }
        }
        """

        with gr.Blocks(css=lcars_css, theme=gr.themes.Default(), title="LCARS Terminal") as interface:
            
            with gr.Column(elem_classes="lcars-container"):
                # Header
                with gr.Row(elem_classes="lcars-header"):
                    gr.Markdown("""
                    <div style="text-align: center; width: 100%;">
                        <div class="lcars-title">πŸš€ LCARS TERMINAL</div>
                        <div class="lcars-subtitle">STARFLEET AI DEVELOPMENT CONSOLE</div>
                        <div style="margin-top: 10px;">
                            <span class="status-indicator status-online"></span>
                            <span style="color: var(--lcars-black); font-weight: bold;">SYSTEM ONLINE</span>
                        </div>
                    </div>
                    """)
                
                # Connection Type Selector
                with gr.Row(elem_classes="lcars-panel"):
                    gr.Markdown("### 🌐 CONNECTION TYPE")
                    connection_type = gr.Radio(
                        choices=["HuggingFace Inference", "Local LM Studio"],
                        value="HuggingFace Inference",
                        label="Select Connection Type",
                        elem_classes="lcars-input"
                    )
                
                # Main Content
                with gr.Row():
                    # Left Sidebar
                    with gr.Column(scale=1):
                        # Configuration Panel
                        with gr.Column(elem_classes="lcars-panel"):
                            gr.Markdown("### πŸ”§ CONFIGURATION")
                            
                            # Connection-specific settings
                            with gr.Row(visible=False) as local_settings:
                                base_url = gr.Textbox(
                                    value=LOCAL_BASE_URL,
                                    label="LM Studio URL",
                                    elem_classes="lcars-input"
                                )
                                api_key = gr.Textbox(
                                    value=LOCAL_API_KEY,
                                    label="API Key",
                                    type="password",
                                    elem_classes="lcars-input"
                                )
                            
                            with gr.Row(visible=True) as hf_settings:
                                hf_api_key = gr.Textbox(
                                    value=HF_API_KEY,
                                    label="HuggingFace API Key",
                                    type="password",
                                    elem_classes="lcars-input",
                                    placeholder="Get from https://huggingface.co/settings/tokens"
                                )
                            
                            with gr.Row():
                                model_dropdown = gr.Dropdown(
                                    choices=list(MODEL_OPTIONS.keys())[1:],
                                    value=list(MODEL_OPTIONS.keys())[1],
                                    label="AI Model",
                                    elem_classes="lcars-input"
                                )
                                fetch_models_btn = gr.Button("πŸ“‘ Fetch Models", elem_classes="lcars-button")
                            
                            with gr.Row():
                                temperature = gr.Slider(0.0, 2.0, value=0.7, label="Temperature")
                                max_tokens = gr.Slider(128, 8192, value=2000, step=128, label="Max Tokens")
                            
                            with gr.Row():
                                update_config_btn = gr.Button("πŸ’Ύ Apply Config", elem_classes="lcars-button")
                                speech_toggle = gr.Checkbox(value=True, label="πŸ”Š Speech Output")
                        
                        # Canvas Artifacts
                        with gr.Column(elem_classes="lcars-panel"):
                            gr.Markdown("### 🎨 CANVAS ARTIFACTS")
                            artifact_display = gr.JSON(label="")
                            with gr.Row():
                                refresh_artifacts_btn = gr.Button("πŸ”„ Refresh", elem_classes="lcars-button")
                                clear_canvas_btn = gr.Button("πŸ—‘οΈ Clear Canvas", elem_classes="lcars-button")
                    
                    # Main Content Area
                    with gr.Column(scale=2):
                        # Code Canvas
                        with gr.Accordion("πŸ’» COLLABORATIVE CODE CANVAS", open=True):
                            code_editor = gr.Code(
                                value="# Welcome to LCARS Collaborative Canvas\n\nprint('Hello, Starfleet!')",
                                language="python",
                                lines=15,
                                label=""
                            )
                            
                            with gr.Row():
                                load_to_chat_btn = gr.Button("πŸ’¬ Discuss Code", elem_classes="lcars-button")
                                analyze_btn = gr.Button("πŸ” Analyze", elem_classes="lcars-button")
                                optimize_btn = gr.Button("⚑ Optimize", elem_classes="lcars-button")
                        
                        # Chat Interface
                        with gr.Column(elem_classes="lcars-panel"):
                            gr.Markdown("### πŸ’¬ MISSION LOG")
                            chatbot = gr.Chatbot(label="", height=300)
                            
                            with gr.Row():
                                message_input = gr.Textbox(
                                    placeholder="Enter your command or query...",
                                    show_label=False,
                                    lines=2,
                                    scale=4
                                )
                                send_btn = gr.Button("πŸš€ SEND", elem_classes="lcars-button", scale=1)
                        
                        # Status
                        with gr.Row():
                            status_display = gr.Textbox(
                                value="LCARS terminal operational. Awaiting commands.",
                                label="Status",
                                max_lines=2
                            )
                            with gr.Column(scale=0):
                                clear_chat_btn = gr.Button("πŸ—‘οΈ Clear Chat", elem_classes="lcars-button")
                                new_session_btn = gr.Button("πŸ†• New Session", elem_classes="lcars-button")
            
            # === EVENT HANDLERS ===
            
            def switch_connection(connection_type):
                if connection_type == "Local LM Studio":
                    return [
                        gr.update(visible=True),
                        gr.update(visible=False),
                        gr.update(choices=["Fetching local models..."], value="Fetching local models...")
                    ]
                else:
                    return [
                        gr.update(visible=False),
                        gr.update(visible=True),
                        gr.update(choices=list(MODEL_OPTIONS.keys())[1:], value=list(MODEL_OPTIONS.keys())[1])
                    ]

            async def fetch_models_updated(connection_type, base_url_val, api_key_val, hf_api_key_val):
                if connection_type == "Local LM Studio":
                    models = await EnhancedLLMAgent.fetch_available_models(
                        base_url_val, api_key_val, use_huggingface=False
                    )
                else:
                    models = await EnhancedLLMAgent.fetch_available_models(
                        "", hf_api_key_val, use_huggingface=True
                    )
                
                if models:
                    return gr.update(choices=models, value=models[0])
                return gr.update(choices=["No models found"])

            def update_agent_connection(connection_type, model_id, base_url_val, api_key_val, hf_api_key_val):
                use_hf = connection_type == "HuggingFace Inference"
                self.use_huggingface = use_hf
                
                if use_hf:
                    self.agent = EnhancedLLMAgent(
                        model_id=model_id,
                        use_huggingface=True,
                        api_key=hf_api_key_val
                    )
                    return f"βœ… Switched to HuggingFace: {model_id}"
                else:
                    self.agent = EnhancedLLMAgent(
                        model_id=model_id, 
                        base_url=base_url_val,
                        api_key=api_key_val,
                        use_huggingface=False
                    )
                    return f"βœ… Switched to Local: {base_url_val}"

            async def process_message(message, history, speech_enabled):
                if not message.strip():
                    return "", history, "Please enter a message"
                
                history = history + [[message, None]]
                
                try:
                    response = await self.agent.chat_with_canvas(
                        message, self.current_conversation, include_canvas=True
                    )
                    
                    history[-1][1] = response
                    
                    if speech_enabled and self.agent.speech_enabled:
                        self.agent.speak(response)
                    
                    artifacts = self.agent.get_canvas_summary(self.current_conversation)
                    status = f"βœ… Response received. Canvas artifacts: {len(artifacts)}"
                    return "", history, status, artifacts
                    
                except Exception as e:
                    error_msg = f"❌ Error: {str(e)}"
                    history[-1][1] = error_msg
                    return "", history, error_msg, self.agent.get_canvas_summary(self.current_conversation)

            def get_artifacts():
                return self.agent.get_canvas_summary(self.current_conversation)

            def clear_canvas():
                self.agent.clear_canvas(self.current_conversation)
                return [], "βœ… Canvas cleared"

            def clear_chat():
                self.agent.clear_conversation(self.current_conversation)
                return [], "βœ… Chat cleared"

            def new_session():
                self.agent.clear_conversation(self.current_conversation)
                self.agent.clear_canvas(self.current_conversation)
                return [], "# New session started\n\nprint('Ready!')", "πŸ†• New session started", []

            # Connect events
            connection_type.change(switch_connection, inputs=connection_type, 
                                 outputs=[local_settings, hf_settings, model_dropdown])

            fetch_models_btn.click(fetch_models_updated, 
                                 inputs=[connection_type, base_url, api_key, hf_api_key],
                                 outputs=model_dropdown)

            model_dropdown.change(update_agent_connection,
                                inputs=[connection_type, model_dropdown, base_url, api_key, hf_api_key],
                                outputs=status_display)

            send_btn.click(process_message,
                         inputs=[message_input, chatbot, speech_toggle],
                         outputs=[message_input, chatbot, status_display, artifact_display])

            message_input.submit(process_message,
                               inputs=[message_input, chatbot, speech_toggle],
                               outputs=[message_input, chatbot, status_display, artifact_display])

            refresh_artifacts_btn.click(get_artifacts, outputs=artifact_display)
            clear_canvas_btn.click(clear_canvas, outputs=[artifact_display, status_display])
            clear_chat_btn.click(clear_chat, outputs=[chatbot, status_display])
            new_session_btn.click(new_session, outputs=[chatbot, code_editor, status_display, artifact_display])

            interface.load(get_artifacts, outputs=artifact_display)
        
        return interface

# --- Main Application ---
def main():
    console.log("[bold blue]πŸš€ Starting LCARS Terminal...[/bold blue]")
    
    is_space = os.getenv('SPACE_ID') is not None
    
    if is_space:
        console.log("[green]🌐 Detected HuggingFace Space[/green]")
    else:
        console.log("[blue]πŸ’» Running locally[/blue]")
    
    interface = LcarsInterface()
    demo = interface.create_interface()
    
    demo.launch(
        share=is_space
    )

if __name__ == "__main__":
    main()