File size: 28,344 Bytes
8f37414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
"""

ACE System - Streamlit Web Interface

Self-improving AI agent with beautiful UI

"""

import streamlit as st
import json
import os
from datetime import datetime
from typing import List, Dict, Optional, Literal
from dataclasses import dataclass, asdict
from enum import Enum
import requests
import plotly.graph_objects as go
import plotly.express as px
from collections import defaultdict

# ============================================================================
# CONFIGURATION
# ============================================================================

class Config:
    """System configuration"""
    OLLAMA_BASE_URL = "http://localhost:11434"
    # GENERATOR_MODEL = "llama3.2:3b"
    # REFLECTOR_MODEL = "llama3.2:3b"
    # CURATOR_MODEL = "llama3.2:3b"
    GENERATOR_MODEL = "aya"
    REFLECTOR_MODEL = "aya"
    CURATOR_MODEL = "aya"
    PLAYBOOK_PATH = "emergency_playbook.json"
    TEMPERATURE = 0.3
    MAX_TOKENS = 4000


class Language:
    """Language settings"""
    MESSAGES = {
        "en": {
            "title": "🤖 ACE - Self-Improving AI Agent",
            "subtitle": "Agentic Context Engineering System",
            "sidebar_title": "⚙️ Settings",
            "language": "Language",
            "model_settings": "Model Settings",
            "playbook_info": "📚 Playbook Information",
            "total_bullets": "Total Knowledge Items",
            "sections": "Sections",
            "avg_score": "Average Quality Score",
            "total_tags": "Total Evaluations",
            "query_input": "💬 Ask me anything about emergencies...",
            "ask_button": "🚀 Get Answer",
            "clear_button": "🗑️ Clear History",
            "export_button": "💾 Export Playbook",
            "import_button": "📥 Import Playbook",
            "chat_history": "💬 Chat History",
            "answer": "Answer",
            "quality": "Quality",
            "reasoning": "Reasoning Process",
            "bullets_used": "Knowledge Used",
            "improvements": "System Improvements",
            "playbook_viz": "📊 Knowledge Evolution",
            "section_dist": "Knowledge Distribution by Section",
            "quality_trend": "Quality Score Trends",
            "connection_error": "❌ Cannot connect to Ollama!",
            "connection_success": "✅ Connected to Ollama",
            "processing": "🔄 Processing your query...",
            "generator_phase": "Generating answer...",
            "reflector_phase": "Evaluating quality...",
            "curator_phase": "Learning improvements...",
            "complete": "✅ Complete!",
        },
        "ar": {
            "title": "🤖 ACE - نظام ذكي ذاتي التطوير",
            "subtitle": "نظام هندسة السياق الوكيل",
            "sidebar_title": "⚙️ الإعدادات",
            "language": "اللغة",
            "model_settings": "إعدادات النموذج",
            "playbook_info": "📚 معلومات دفتر المعرفة",
            "total_bullets": "إجمالي عناصر المعرفة",
            "sections": "الأقسام",
            "avg_score": "متوسط درجة الجودة",
            "total_tags": "إجمالي التقييمات",
            "query_input": "💬 اسألني أي شيء عن حالات الطوارئ...",
            "ask_button": "🚀 احصل على إجابة",
            "clear_button": "🗑️ مسح السجل",
            "export_button": "💾 تصدير دفتر المعرفة",
            "import_button": "📥 استيراد دفتر المعرفة",
            "chat_history": "💬 سجل المحادثة",
            "answer": "الإجابة",
            "quality": "الجودة",
            "reasoning": "عملية التفكير",
            "bullets_used": "المعرفة المستخدمة",
            "improvements": "تحسينات النظام",
            "playbook_viz": "📊 تطور المعرفة",
            "section_dist": "توزيع المعرفة حسب القسم",
            "quality_trend": "اتجاهات درجة الجودة",
            "connection_error": "❌ لا يمكن الاتصال بـ Ollama!",
            "connection_success": "✅ تم الاتصال بـ Ollama",
            "processing": "🔄 جاري معالجة استفسارك...",
            "generator_phase": "توليد الإجابة...",
            "reflector_phase": "تقييم الجودة...",
            "curator_phase": "تعلم التحسينات...",
            "complete": "✅ اكتمل!",
        }
    }
    
    @staticmethod
    def get(key, lang="en"):
        return Language.MESSAGES.get(lang, Language.MESSAGES["en"]).get(key, key)


# ============================================================================
# DATA MODELS
# ============================================================================

class TagType(str, Enum):
    HELPFUL = "helpful"
    HARMFUL = "harmful"
    NEUTRAL = "neutral"


@dataclass
class Bullet:
    id: str
    section: str
    content: str
    helpful: int = 0
    harmful: int = 0
    neutral: int = 0
    created_at: str = ""
    updated_at: str = ""
    
    def __post_init__(self):
        if not self.created_at:
            self.created_at = datetime.now().isoformat()
        if not self.updated_at:
            self.updated_at = datetime.now().isoformat()
    
    def add_tag(self, tag: TagType):
        if tag == TagType.HELPFUL:
            self.helpful += 1
        elif tag == TagType.HARMFUL:
            self.harmful += 1
        else:
            self.neutral += 1
        self.updated_at = datetime.now().isoformat()
    
    def score(self) -> float:
        total = self.helpful + self.harmful + self.neutral
        if total == 0:
            return 0.0
        return (self.helpful - self.harmful) / total


@dataclass
class BulletTag:
    bullet_id: str
    tag: TagType
    reason: str


@dataclass
class GeneratorOutput:
    reasoning: List[str]
    bullet_ids: List[str]
    final_answer: str


@dataclass
class Reflection:
    answer_quality: str
    strengths: List[str]
    weaknesses: List[str]
    bullet_tags: List[BulletTag]


@dataclass
class DeltaOperation:
    type: Literal["ADD", "UPDATE", "REMOVE"]
    section: str
    content: Optional[str] = None
    bullet_id: Optional[str] = None


@dataclass
class DeltaBatch:
    reasoning: str
    operations: List[DeltaOperation]


# ============================================================================
# PLAYBOOK MANAGEMENT
# ============================================================================

class Playbook:
    def __init__(self):
        self.bullets: Dict[str, Bullet] = {}
        self.sections: Dict[str, List[str]] = {}
        self._next_id = 1
    
    def add_bullet(self, section: str, content: str) -> str:
        bullet_id = f"B{self._next_id:04d}"
        self._next_id += 1
        
        bullet = Bullet(id=bullet_id, section=section, content=content)
        self.bullets[bullet_id] = bullet
        
        if section not in self.sections:
            self.sections[section] = []
        self.sections[section].append(bullet_id)
        
        return bullet_id
    
    def update_bullet(self, bullet_id: str, content: str):
        if bullet_id in self.bullets:
            self.bullets[bullet_id].content = content
            self.bullets[bullet_id].updated_at = datetime.now().isoformat()
    
    def remove_bullet(self, bullet_id: str):
        if bullet_id in self.bullets:
            bullet = self.bullets[bullet_id]
            section = bullet.section
            
            del self.bullets[bullet_id]
            if section in self.sections:
                self.sections[section] = [
                    bid for bid in self.sections[section] if bid != bullet_id
                ]
    
    def update_bullet_tag(self, bullet_id: str, tag: TagType):
        if bullet_id in self.bullets:
            self.bullets[bullet_id].add_tag(tag)
    
    def apply_delta(self, delta: DeltaBatch):
        for op in delta.operations:
            if op.type == "ADD" and op.content:
                self.add_bullet(op.section, op.content)
            elif op.type == "UPDATE" and op.bullet_id and op.content:
                self.update_bullet(op.bullet_id, op.content)
            elif op.type == "REMOVE" and op.bullet_id:
                self.remove_bullet(op.bullet_id)
    
    def as_prompt(self) -> str:
        if not self.bullets:
            return "No knowledge bullets available yet."
        
        lines = ["# Knowledge Playbook", ""]
        for section, bullet_ids in sorted(self.sections.items()):
            lines.append(f"## {section}")
            for bid in bullet_ids:
                bullet = self.bullets[bid]
                score = bullet.score()
                lines.append(f"- [{bid}] {bullet.content} (score: {score:.2f})")
            lines.append("")
        
        return "\n".join(lines)
    
    def stats(self) -> Dict:
        total_bullets = len(self.bullets)
        total_tags = sum(b.helpful + b.harmful + b.neutral for b in self.bullets.values())
        avg_score = sum(b.score() for b in self.bullets.values()) / total_bullets if total_bullets > 0 else 0
        
        return {
            "total_bullets": total_bullets,
            "total_sections": len(self.sections),
            "total_tags": total_tags,
            "average_score": avg_score
        }
    
    def save(self, filepath: str):
        data = {
            "bullets": {bid: asdict(b) for bid, b in self.bullets.items()},
            "sections": self.sections,
            "next_id": self._next_id
        }
        with open(filepath, 'w') as f:
            json.dump(data, f, indent=2)
    
    @classmethod
    def load(cls, filepath: str) -> 'Playbook':
        playbook = cls()
        if os.path.exists(filepath):
            with open(filepath, 'r') as f:
                data = json.load(f)
            
            playbook.bullets = {
                bid: Bullet(**bullet_data) 
                for bid, bullet_data in data.get("bullets", {}).items()
            }
            playbook.sections = data.get("sections", {})
            playbook._next_id = data.get("next_id", 1)
        
        return playbook


# ============================================================================
# OLLAMA CLIENT
# ============================================================================

class OllamaClient:
    def __init__(self, base_url: str = Config.OLLAMA_BASE_URL):
        self.base_url = base_url
    
    def generate(

        self, 

        model: str, 

        prompt: str, 

        system: Optional[str] = None,

        temperature: float = Config.TEMPERATURE,

        max_tokens: int = Config.MAX_TOKENS

    ) -> str:
        url = f"{self.base_url}/api/generate"
        
        payload = {
            "model": model,
            "prompt": prompt,
            "stream": False,
            "options": {
                "temperature": temperature,
                "num_predict": max_tokens,
                "num_ctx": 8192
            }
        }
        
        if system:
            payload["system"] = system
        
        try:
            response = requests.post(url, json=payload, timeout=180)
            response.raise_for_status()
            return response.json()["response"]
        except Exception as e:
            return f"Error: {e}"
    
    def check_health(self) -> bool:
        try:
            response = requests.get(f"{self.base_url}/api/tags", timeout=5)
            return response.status_code == 200
        except:
            return False


# ============================================================================
# AGENTS
# ============================================================================

class StateInitializer:
    def execute(self, user_query: str, playbook: Playbook) -> Dict:
        return {
            "user_query": user_query,
            "playbook": playbook,
            "ground_truth": None,
            "generator_output": None,
            "reflector_output": None,
            "curator_output": None
        }


class Generator:
    def __init__(self, client: OllamaClient):
        self.client = client
    
    def execute(self, state: Dict, lang: str = "en") -> GeneratorOutput:
        user_query = state["user_query"]
        playbook = state["playbook"]
        
        bullet_context = []
        for bid, bullet in playbook.bullets.items():
            bullet_context.append(f"[{bid}] {bullet.content}")
        
        knowledge = "\n".join(bullet_context[:50])
        
        if lang == "ar":
            system_prompt = "أنت خبير في الاستجابة للطوارئ. قدم تعليمات كاملة ومفصلة. لا تختصر إجابتك أبداً."
            prompt = f"""أنت خبير في الاستجابة للطوارئ.



السؤال: {user_query}



المعرفة المتاحة:

{knowledge}



قدم إجابة كاملة ومفصلة مع جميع الخطوات الضرورية. كن دقيقاً وشاملاً."""
        else:
            system_prompt = "You are an emergency response expert. Provide complete, detailed emergency instructions. Never truncate your answer."
            prompt = f"""You are an emergency response expert.



Question: {user_query}



Available Knowledge:

{knowledge}



Provide a COMPLETE, detailed answer with ALL necessary steps. Be thorough and specific."""
        
        response = self.client.generate(
            model=Config.GENERATOR_MODEL,
            prompt=prompt,
            system=system_prompt,
            temperature=0.3,
            max_tokens=4000
        )
        
        used_bullets = []
        if response and isinstance(response, str):
            response_lower = response.lower()
            for bid, bullet in playbook.bullets.items():
                bullet_preview = str(bullet.content)[:30].lower()
                if bid in response or bullet_preview in response_lower:
                    used_bullets.append(bid)
        
        return GeneratorOutput(
            reasoning=["Analyzed emergency situation", "Found relevant protocols", "Provided complete response"],
            bullet_ids=used_bullets,
            final_answer=response if response else "Unable to generate response"
        )


class Reflector:
    def __init__(self, client: OllamaClient):
        self.client = client
    
    def execute(self, state: Dict) -> Reflection:
        user_query = state["user_query"]
        gen_output = state["generator_output"]
        playbook = state["playbook"]
        
        system_prompt = """You are a critical evaluator. Respond in JSON:

{

  "answer_quality": "excellent|good|fair|poor",

  "strengths": ["strength 1", "strength 2"],

  "weaknesses": ["weakness 1"],

  "bullet_tags": [

    {"bullet_id": "B0001", "tag": "helpful", "reason": "why"}

  ]

}"""
        
        bullet_context = "\n".join([
            f"[{bid}] {playbook.bullets[bid].content}" 
            for bid in gen_output.bullet_ids 
            if bid in playbook.bullets
        ])
        
        prompt = f"""Query: {user_query}

Bullets: {bullet_context if bullet_context else "None"}

Answer: {gen_output.final_answer[:500]}



Evaluate (JSON only):"""
        
        response = self.client.generate(
            model=Config.REFLECTOR_MODEL,
            prompt=prompt,
            system=system_prompt
        )
        
        try:
            if "```json" in response:
                response = response.split("```json")[1].split("```")[0].strip()
            elif "```" in response:
                response = response.split("```")[1].split("```")[0].strip()
            
            data = json.loads(response)
            bullet_tags = [
                BulletTag(
                    bullet_id=bt["bullet_id"],
                    tag=TagType(bt["tag"]),
                    reason=bt.get("reason", "")
                )
                for bt in data.get("bullet_tags", [])
            ]
            
            return Reflection(
                answer_quality=data.get("answer_quality", "unknown"),
                strengths=data.get("strengths", []),
                weaknesses=data.get("weaknesses", []),
                bullet_tags=bullet_tags
            )
        except:
            return Reflection(
                answer_quality="good",
                strengths=["Provided answer"],
                weaknesses=[],
                bullet_tags=[]
            )


class Curator:
    def __init__(self, client: OllamaClient):
        self.client = client
    
    def execute(self, state: Dict) -> DeltaBatch:
        reflection = state["reflector_output"]
        
        # Simplified curation for demo
        operations = []
        if reflection.answer_quality in ["fair", "poor"] and reflection.weaknesses:
            operations.append(DeltaOperation(
                type="ADD",
                section="Improvements",
                content=f"Address: {reflection.weaknesses[0]}"
            ))
        
        return DeltaBatch(
            reasoning="Learning from feedback",
            operations=operations
        )


# ============================================================================
# ACE ORCHESTRATOR
# ============================================================================

class ACEOrchestrator:
    def __init__(self, playbook_path: str = Config.PLAYBOOK_PATH):
        self.client = OllamaClient()
        self.playbook = Playbook.load(playbook_path)
        self.playbook_path = playbook_path
        
        self.state_initializer = StateInitializer()
        self.generator = Generator(self.client)
        self.reflector = Reflector(self.client)
        self.curator = Curator(self.client)
    
    def run_cycle(self, user_query: str, lang: str = "en") -> Dict:
        state = self.state_initializer.execute(user_query, self.playbook)
        
        gen_output = self.generator.execute(state, lang)
        state["generator_output"] = gen_output
        
        reflection = self.reflector.execute(state)
        state["reflector_output"] = reflection
        
        for bt in reflection.bullet_tags:
            self.playbook.update_bullet_tag(bt.bullet_id, bt.tag)
        
        delta = self.curator.execute(state)
        state["curator_output"] = delta
        
        self.playbook.apply_delta(delta)
        self.playbook.save(self.playbook_path)
        
        return {
            "answer": gen_output.final_answer,
            "quality": reflection.answer_quality,
            "reasoning": gen_output.reasoning,
            "bullets_used": gen_output.bullet_ids,
            "strengths": reflection.strengths,
            "weaknesses": reflection.weaknesses,
            "operations": delta.operations,
            "stats": self.playbook.stats()
        }


# ============================================================================
# STREAMLIT UI
# ============================================================================

def init_session_state():
    if 'chat_history' not in st.session_state:
        st.session_state.chat_history = []
    if 'ace' not in st.session_state:
        st.session_state.ace = ACEOrchestrator()
    if 'language' not in st.session_state:
        st.session_state.language = 'en'


def create_quality_badge(quality: str):
    colors = {
        "excellent": "🟢",
        "good": "🟡",
        "fair": "🟠",
        "poor": "🔴"
    }
    return f"{colors.get(quality, '⚪')} {quality.upper()}"


def plot_section_distribution(playbook: Playbook):
    section_counts = {section: len(bullets) for section, bullets in playbook.sections.items()}
    
    fig = go.Figure(data=[go.Bar(
        x=list(section_counts.keys()),
        y=list(section_counts.values()),
        marker_color='lightblue'
    )])
    
    fig.update_layout(
        title="Knowledge Items by Section",
        xaxis_title="Section",
        yaxis_title="Count",
        height=400
    )
    
    return fig


def plot_quality_scores(playbook: Playbook):
    scores = [bullet.score() for bullet in playbook.bullets.values()]
    
    fig = go.Figure(data=[go.Histogram(
        x=scores,
        nbinsx=20,
        marker_color='green'
    )])
    
    fig.update_layout(
        title="Quality Score Distribution",
        xaxis_title="Score",
        yaxis_title="Frequency",
        height=400
    )
    
    return fig


def main():
    st.set_page_config(
        page_title="ACE System",
        page_icon="🤖",
        layout="wide",
        initial_sidebar_state="expanded"
    )
    
    init_session_state()
    
    # Custom CSS
    st.markdown("""

        <style>

        .main {

            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

        }

        .stApp {

            background: white;

        }

        .chat-message {

            padding: 1.5rem;

            border-radius: 0.5rem;

            margin-bottom: 1rem;

            border: 1px solid #e0e0e0;

        }

        .user-message {

            background-color: #e3f2fd;

        }

        .assistant-message {

            background-color: #f5f5f5;

        }

        </style>

    """, unsafe_allow_html=True)
    
    # Sidebar
    with st.sidebar:
        lang = st.session_state.language
        st.title(Language.get("sidebar_title", lang))
        
        # Language selector
        language_option = st.selectbox(
            Language.get("language", lang),
            options=["English", "العربية"],
            index=0 if lang == "en" else 1
        )
        st.session_state.language = "en" if language_option == "English" else "ar"
        lang = st.session_state.language
        
        st.divider()
        
        # Connection status
        client = OllamaClient()
        if client.check_health():
            st.success(Language.get("connection_success", lang))
        else:
            st.error(Language.get("connection_error", lang))
        
        st.divider()
        
        # Playbook stats
        st.subheader(Language.get("playbook_info", lang))
        stats = st.session_state.ace.playbook.stats()
        
        col1, col2 = st.columns(2)
        with col1:
            st.metric(Language.get("total_bullets", lang), stats["total_bullets"])
            st.metric(Language.get("sections", lang), stats["total_sections"])
        with col2:
            st.metric(Language.get("avg_score", lang), f"{stats['average_score']:.2f}")
            st.metric(Language.get("total_tags", lang), stats["total_tags"])
        
        st.divider()
        
        # Export/Import
        if st.button(Language.get("export_button", lang), use_container_width=True):
            playbook_data = json.dumps({
                "bullets": {bid: asdict(b) for bid, b in st.session_state.ace.playbook.bullets.items()},
                "sections": st.session_state.ace.playbook.sections,
                "next_id": st.session_state.ace.playbook._next_id
            }, indent=2)
            st.download_button(
                "Download JSON",
                playbook_data,
                "playbook_export.json",
                "application/json"
            )
        
        if st.button(Language.get("clear_button", lang), use_container_width=True):
            st.session_state.chat_history = []
            st.rerun()
    
    # Main content
    lang = st.session_state.language
    st.title(Language.get("title", lang))
    st.caption(Language.get("subtitle", lang))
    
    # Tabs
    tab1, tab2 = st.tabs([Language.get("chat_history", lang), Language.get("playbook_viz", lang)])
    
    with tab1:
        # Chat interface
        query = st.text_input(
            Language.get("query_input", lang),
            key="query_input",
            placeholder="e.g., What should I do if someone is choking?"
        )
        
        if st.button(Language.get("ask_button", lang), type="primary", use_container_width=True):
            if query:
                with st.spinner(Language.get("processing", lang)):
                    # Progress
                    progress_bar = st.progress(0)
                    status = st.empty()
                    
                    status.text(Language.get("generator_phase", lang))
                    progress_bar.progress(33)
                    
                    result = st.session_state.ace.run_cycle(query, lang)
                    
                    status.text(Language.get("reflector_phase", lang))
                    progress_bar.progress(66)
                    
                    status.text(Language.get("curator_phase", lang))
                    progress_bar.progress(100)
                    
                    status.text(Language.get("complete", lang))
                    
                    st.session_state.chat_history.append({
                        "query": query,
                        "result": result,
                        "timestamp": datetime.now().isoformat()
                    })
                    
                    progress_bar.empty()
                    status.empty()
        
        # Display chat history
        for i, chat in enumerate(reversed(st.session_state.chat_history)):
            with st.container():
                st.markdown(f"<div class='chat-message user-message'>", unsafe_allow_html=True)
                st.markdown(f"**💬 Query:** {chat['query']}")
                st.markdown(f"*{chat['timestamp'][:19]}*")
                st.markdown("</div>", unsafe_allow_html=True)
                
                st.markdown(f"<div class='chat-message assistant-message'>", unsafe_allow_html=True)
                
                col1, col2 = st.columns([3, 1])
                with col1:
                    st.markdown(f"**🤖 {Language.get('answer', lang)}:**")
                with col2:
                    st.markdown(create_quality_badge(chat['result']['quality']))
                
                st.markdown(chat['result']['answer'])
                
                with st.expander(Language.get("reasoning", lang)):
                    for step in chat['result']['reasoning']:
                        st.markdown(f"- {step}")
                
                with st.expander(Language.get("bullets_used", lang)):
                    st.code(", ".join(chat['result']['bullets_used']) if chat['result']['bullets_used'] else "None")
                
                with st.expander(Language.get("improvements", lang)):
                    if chat['result']['operations']:
                        for op in chat['result']['operations']:
                            st.markdown(f"- **{op.type}**: {op.section}")
                    else:
                        st.info("No improvements needed")
                
                st.markdown("</div>", unsafe_allow_html=True)
                st.divider()
    
    with tab2:
        # Visualizations
        st.subheader(Language.get("playbook_viz", lang))
        
        col1, col2 = st.columns(2)
        
        with col1:
            st.plotly_chart(
                plot_section_distribution(st.session_state.ace.playbook),
                use_container_width=True
            )
        
        with col2:
            st.plotly_chart(
                plot_quality_scores(st.session_state.ace.playbook),
                use_container_width=True
            )


if __name__ == "__main__":
    main()