Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app.py +31 -140
- core/__init__.py +2 -1
- core/formatter.py +72 -20
- core/logic_engine.py +92 -49
app.py
CHANGED
|
@@ -42,146 +42,37 @@ from datasets import load_dataset
|
|
| 42 |
from typing import Tuple, Optional
|
| 43 |
|
| 44 |
# ============ CONFIG ============
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
# Load custom CSS
|
| 48 |
-
css_custom = """
|
| 49 |
-
/* Amkyaw Core Ecosystem - Advanced Glassmorphism UI */
|
| 50 |
-
|
| 51 |
-
/* Dark theme base */
|
| 52 |
-
:root {
|
| 53 |
-
--bg-dark: #0a0a0f;
|
| 54 |
-
--bg-dark-secondary: #12121a;
|
| 55 |
-
--glass-bg: rgba(255, 255, 255, 0.03);
|
| 56 |
-
--glass-bg-hover: rgba(255, 255, 255, 0.08);
|
| 57 |
-
--glass-border: rgba(255, 255, 255, 0.08);
|
| 58 |
-
--primary: #8b5cf6;
|
| 59 |
-
--primary-hover: #a78bfa;
|
| 60 |
-
--text-main: #f1f5f9;
|
| 61 |
-
--text-muted: #94a3b8;
|
| 62 |
-
--text-muted-hover: #cbd5e1;
|
| 63 |
-
--accent: #f472b6;
|
| 64 |
-
|
| 65 |
-
/* Stage colors */
|
| 66 |
-
--negation-red: #ef4444;
|
| 67 |
-
--negation-bg: rgba(239, 68, 68, 0.1);
|
| 68 |
-
--structure-blue: #3b82f6;
|
| 69 |
-
--structure-bg: rgba(59, 130, 246, 0.1);
|
| 70 |
-
--optimized-green: #22c55e;
|
| 71 |
-
--optimized-bg: rgba(34, 197, 94, 0.1);
|
| 72 |
-
|
| 73 |
-
/* Myanmar-friendly settings */
|
| 74 |
-
--line-height: 1.8;
|
| 75 |
-
--font-myanmar: 'Noto Sans Myanmar', 'Myanmar Text', 'Padauk', sans-serif;
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
body {
|
| 79 |
-
background: var(--bg-dark);
|
| 80 |
-
background-image:
|
| 81 |
-
radial-gradient(ellipse at 20% 0%, rgba(139, 92, 246, 0.15) 0%, transparent 50%),
|
| 82 |
-
radial-gradient(ellipse at 80% 100%, rgba(244, 114, 182, 0.1) 0%, transparent 50%),
|
| 83 |
-
linear-gradient(180deg, var(--bg-dark) 0%, var(--bg-dark-secondary) 100%);
|
| 84 |
-
color: var(--text-main);
|
| 85 |
-
font-family: var(--font-myanmar);
|
| 86 |
-
line-height: var(--line-height);
|
| 87 |
-
min-height: 100vh;
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
/* Main container */
|
| 91 |
-
.gradio-container {
|
| 92 |
-
background: var(--glass-bg) !important;
|
| 93 |
-
backdrop-filter: blur(20px);
|
| 94 |
-
border: 1px solid var(--glass-border);
|
| 95 |
-
border-radius: 20px;
|
| 96 |
-
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
/* Input styling */
|
| 100 |
-
.input-text, .gradio-input-text {
|
| 101 |
-
background: rgba(255, 255, 255, 0.05) !important;
|
| 102 |
-
border: 1px solid var(--glass-border) !important;
|
| 103 |
-
color: var(--text-main) !important;
|
| 104 |
-
border-radius: 12px !important;
|
| 105 |
-
padding: 12px 16px !important;
|
| 106 |
-
font-family: var(--font-myanmar) !important;
|
| 107 |
-
}
|
| 108 |
-
|
| 109 |
-
.input-text:focus, .gradio-input-text:focus {
|
| 110 |
-
border-color: var(--primary) !important;
|
| 111 |
-
box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.2) !important;
|
| 112 |
-
}
|
| 113 |
-
|
| 114 |
-
.input-text::placeholder {
|
| 115 |
-
color: var(--text-muted) !important;
|
| 116 |
-
}
|
| 117 |
-
|
| 118 |
-
/* Button styling */
|
| 119 |
-
button.primary, .gradio-button-primary {
|
| 120 |
-
background: #8b5cf6 !important;
|
| 121 |
-
border: none !important;
|
| 122 |
-
color: white !important;
|
| 123 |
-
font-weight: 600 !important;
|
| 124 |
-
border-radius: 12px !important;
|
| 125 |
-
transition: all 0.3s ease !important;
|
| 126 |
-
padding: 10px 20px !important;
|
| 127 |
-
}
|
| 128 |
-
|
| 129 |
-
button.primary:hover, .gradio-button-primary:hover {
|
| 130 |
-
background: #a78bfa !important;
|
| 131 |
-
transform: translateY(-2px);
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
/* Logic Cards - Glassmorphism */
|
| 135 |
-
.logic-card {
|
| 136 |
-
background: rgba(255, 255, 255, 0.05) !important;
|
| 137 |
-
backdrop-filter: blur(10px);
|
| 138 |
-
border-radius: 16px;
|
| 139 |
-
padding: 20px;
|
| 140 |
-
margin: 12px 0;
|
| 141 |
-
border: 1px solid rgba(255, 255, 255, 0.1);
|
| 142 |
-
}
|
| 143 |
-
|
| 144 |
-
/* Negation Card - Red */
|
| 145 |
-
.logic-card.negation {
|
| 146 |
-
border-left: 4px solid #ef4444;
|
| 147 |
-
background: rgba(239, 68, 68, 0.1) !important;
|
| 148 |
-
}
|
| 149 |
-
|
| 150 |
-
/* Structure Card - Blue */
|
| 151 |
-
.logic-card.structural {
|
| 152 |
-
border-left: 4px solid #3b82f6;
|
| 153 |
-
background: rgba(59, 130, 246, 0.1) !important;
|
| 154 |
-
}
|
| 155 |
-
|
| 156 |
-
/* Optimized Card - Green */
|
| 157 |
-
.logic-card.optimized {
|
| 158 |
-
border-left: 4px solid #22c55e;
|
| 159 |
-
background: rgba(34, 197, 94, 0.1) !important;
|
| 160 |
-
}
|
| 161 |
-
|
| 162 |
-
/* Main response */
|
| 163 |
-
.main-response {
|
| 164 |
-
background: rgba(139, 92, 246, 0.15) !important;
|
| 165 |
-
backdrop-filter: blur(10px);
|
| 166 |
-
border-radius: 16px;
|
| 167 |
-
padding: 24px;
|
| 168 |
-
border: 1px solid rgba(139, 92, 246, 0.3);
|
| 169 |
-
}
|
| 170 |
-
|
| 171 |
-
/* Animations */
|
| 172 |
-
@keyframes fadeIn {
|
| 173 |
-
from { opacity: 0; transform: translateY(10px); }
|
| 174 |
-
to { opacity: 1; transform: translateY(0); }
|
| 175 |
-
}
|
| 176 |
-
|
| 177 |
-
.logic-card { animation: fadeIn 0.4s ease-out; }
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
# Cache for loaded dataset
|
| 187 |
_dataset_cache = None
|
|
@@ -290,7 +181,7 @@ def format_stage_html(title: str, content: str, icon: str, stage_type: str = "ne
|
|
| 290 |
def create_demo():
|
| 291 |
"""Create the Gradio demo interface"""
|
| 292 |
|
| 293 |
-
with gr.Blocks(css=
|
| 294 |
gr.Markdown("""
|
| 295 |
# π§ Amkyaw Core Ecosystem
|
| 296 |
## Logic Visualizer
|
|
|
|
| 42 |
from typing import Tuple, Optional
|
| 43 |
|
| 44 |
# ============ CONFIG ============
|
| 45 |
+
import os
|
| 46 |
+
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# Add current directory to path for imports
|
| 49 |
+
sys.path.insert(0, os.path.dirname(__file__))
|
| 50 |
+
|
| 51 |
+
# Load CSS from assets file
|
| 52 |
+
def load_css():
|
| 53 |
+
# Path relative to app.py location
|
| 54 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
| 55 |
+
css_path = os.path.join(base_dir, "assets", "style.css")
|
| 56 |
+
if os.path.exists(css_path):
|
| 57 |
+
with open(css_path, "r") as f:
|
| 58 |
+
return f.read()
|
| 59 |
+
# Fallback: try relative to current directory
|
| 60 |
+
if os.path.exists("assets/style.css"):
|
| 61 |
+
with open("assets/style.css", "r") as f:
|
| 62 |
+
return f.read()
|
| 63 |
+
return ""
|
| 64 |
+
|
| 65 |
+
# Import from core module
|
| 66 |
+
try:
|
| 67 |
+
from core.logic_engine import AmkyawLogicEngine
|
| 68 |
+
from core.formatter import format_logic_output
|
| 69 |
+
except ImportError:
|
| 70 |
+
# Fallback if module import fails
|
| 71 |
+
AmkyawLogicEngine = None
|
| 72 |
+
format_logic_output = None
|
| 73 |
+
|
| 74 |
+
# Dataset ID
|
| 75 |
+
DATASET_ID = "amkyawdev/amkyaw-core-ecosystem"
|
| 76 |
|
| 77 |
# Cache for loaded dataset
|
| 78 |
_dataset_cache = None
|
|
|
|
| 181 |
def create_demo():
|
| 182 |
"""Create the Gradio demo interface"""
|
| 183 |
|
| 184 |
+
with gr.Blocks(css=load_css(), title="Amkyaw Core - Logic Visualizer") as demo:
|
| 185 |
gr.Markdown("""
|
| 186 |
# π§ Amkyaw Core Ecosystem
|
| 187 |
## Logic Visualizer
|
core/__init__.py
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
# Amkyaw Core Package
|
|
|
|
|
|
| 1 |
+
# Amkyaw Core Package
|
| 2 |
+
# Empty file to enable module imports
|
core/formatter.py
CHANGED
|
@@ -1,41 +1,93 @@
|
|
| 1 |
"""
|
| 2 |
-
Formatter - Format JSONL data for UI display
|
|
|
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
from typing import Dict, Any
|
| 6 |
|
| 7 |
|
| 8 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
-
Format logic
|
| 11 |
|
| 12 |
Args:
|
| 13 |
-
|
| 14 |
|
| 15 |
Returns:
|
| 16 |
-
|
| 17 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if not result:
|
| 19 |
return "No result", ""
|
| 20 |
|
| 21 |
-
# Get thought trace
|
| 22 |
stages = result.get("stages", {})
|
| 23 |
|
| 24 |
-
|
| 25 |
-
"
|
| 26 |
-
stages.get("
|
| 27 |
-
"",
|
| 28 |
-
|
| 29 |
-
stages.get("structure", "Not defined"),
|
| 30 |
-
"",
|
| 31 |
-
"β¨ **Stage 3: Synthesis (α‘αα±α¬ααΊαΈαα―αΆαΈ)**",
|
| 32 |
-
stages.get("synthesis", "Not defined")
|
| 33 |
-
]
|
| 34 |
-
|
| 35 |
-
thought_trace = "\n".join(trace_lines)
|
| 36 |
-
output = result.get("output", stages.get("synthesis", ""))
|
| 37 |
|
| 38 |
-
return
|
| 39 |
|
| 40 |
|
| 41 |
def format_sample_data(sample: Dict) -> str:
|
|
|
|
| 1 |
"""
|
| 2 |
+
Formatter - Format JSONL data for UI display with Glassmorphism HTML
|
| 3 |
+
|
| 4 |
+
Wraps step_1_negation, step_2_fifty_fifty, step_3_optimized into HTML cards
|
| 5 |
"""
|
| 6 |
|
| 7 |
from typing import Dict, Any
|
| 8 |
|
| 9 |
|
| 10 |
+
def format_stage_html(title: str, content: str, icon: str, stage_type: str) -> str:
|
| 11 |
+
"""
|
| 12 |
+
Format a logic step as a Glassmorphism HTML card
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
title: Card title (e.g., "Step 1: Negation")
|
| 16 |
+
content: The logic step content
|
| 17 |
+
icon: Emoji icon
|
| 18 |
+
stage_type: "negation", "structural", or "optimized"
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
HTML string for the card
|
| 22 |
+
"""
|
| 23 |
+
html = f'''
|
| 24 |
+
<div class="logic-card {stage_type}">
|
| 25 |
+
<div class="card-header">
|
| 26 |
+
<span class="card-icon">{icon}</span>
|
| 27 |
+
<span class="card-title">{title}</span>
|
| 28 |
+
</div>
|
| 29 |
+
<div class="card-content">
|
| 30 |
+
{content}
|
| 31 |
+
</div>
|
| 32 |
+
</div>
|
| 33 |
+
'''
|
| 34 |
+
return html
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def format_logic_steps(logic_steps: Dict[str, str]) -> tuple:
|
| 38 |
"""
|
| 39 |
+
Format logic steps for UI display
|
| 40 |
|
| 41 |
Args:
|
| 42 |
+
logic_steps: Dict with step_1_negation, step_2_fifty_fifty, step_3_optimized
|
| 43 |
|
| 44 |
Returns:
|
| 45 |
+
Tuple of (optimized_output, negation_html, structural_html, optimized_html)
|
| 46 |
"""
|
| 47 |
+
if not logic_steps:
|
| 48 |
+
return "", "", "", ""
|
| 49 |
+
|
| 50 |
+
negation = logic_steps.get("step_1_negation", "")
|
| 51 |
+
structural = logic_steps.get("step_2_fifty_fifty", "")
|
| 52 |
+
optimized = logic_steps.get("step_3_optimized", "")
|
| 53 |
+
|
| 54 |
+
# Format HTML cards
|
| 55 |
+
negation_html = format_stage_html(
|
| 56 |
+
"Step 1: Negation (ααα·αΊαα»ααΊαααΊ)",
|
| 57 |
+
negation,
|
| 58 |
+
"π₯",
|
| 59 |
+
"negation"
|
| 60 |
+
)
|
| 61 |
+
structural_html = format_stage_html(
|
| 62 |
+
"Step 2: Structure (αα»αΎααΌα±)",
|
| 63 |
+
structural,
|
| 64 |
+
"βοΈ",
|
| 65 |
+
"structural"
|
| 66 |
+
)
|
| 67 |
+
optimized_html = format_stage_html(
|
| 68 |
+
"Step 3: Optimized (α‘αα±α¬ααΊαΈαα―αΆαΈ)",
|
| 69 |
+
optimized,
|
| 70 |
+
"β¨",
|
| 71 |
+
"optimized"
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
return optimized, negation_html, structural_html, optimized_html
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def format_logic_output(result: Dict[str, Any]) -> tuple:
|
| 78 |
+
"""Legacy function for backward compatibility"""
|
| 79 |
if not result:
|
| 80 |
return "No result", ""
|
| 81 |
|
|
|
|
| 82 |
stages = result.get("stages", {})
|
| 83 |
|
| 84 |
+
logic_steps = {
|
| 85 |
+
"step_1_negation": stages.get("negation", ""),
|
| 86 |
+
"step_2_fifty_fifty": stages.get("structure", ""),
|
| 87 |
+
"step_3_optimized": stages.get("synthesis", "")
|
| 88 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
return format_logic_steps(logic_steps)
|
| 91 |
|
| 92 |
|
| 93 |
def format_sample_data(sample: Dict) -> str:
|
core/logic_engine.py
CHANGED
|
@@ -1,17 +1,91 @@
|
|
| 1 |
"""
|
| 2 |
AmkyawLogic Engine - 3-Stage Reasoning Framework
|
|
|
|
|
|
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
-
from typing import Dict, List, Any
|
|
|
|
| 6 |
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def __init__(self):
|
| 17 |
self.stage_names = [
|
|
@@ -21,74 +95,43 @@ class AmkyawLogicEngine:
|
|
| 21 |
]
|
| 22 |
|
| 23 |
def process(self, instruction: str, dataset) -> Dict[str, Any]:
|
| 24 |
-
"""
|
| 25 |
-
Process an instruction through the 3-stage logic
|
| 26 |
-
|
| 27 |
-
Args:
|
| 28 |
-
instruction: The user's instruction/query
|
| 29 |
-
dataset: The amkyaw-core-ecosystem dataset
|
| 30 |
-
|
| 31 |
-
Returns:
|
| 32 |
-
Dict with reasoning stages and output
|
| 33 |
-
"""
|
| 34 |
result = {
|
| 35 |
"instruction": instruction,
|
| 36 |
"stages": {},
|
| 37 |
"output": ""
|
| 38 |
}
|
| 39 |
|
| 40 |
-
# Stage 1: Negation - What is NOT the answer
|
| 41 |
result["stages"]["negation"] = self._stage_negation(instruction, dataset)
|
| 42 |
-
|
| 43 |
-
# Stage 2: Structure - 50/50 equilibrium
|
| 44 |
result["stages"]["structure"] = self._stage_structure(instruction, dataset)
|
| 45 |
-
|
| 46 |
-
# Stage 3: Optimized Synthesis
|
| 47 |
result["stages"]["synthesis"] = self._stage_optimized(instruction, dataset)
|
| 48 |
-
|
| 49 |
-
# Final output
|
| 50 |
result["output"] = result["stages"]["synthesis"]
|
| 51 |
|
| 52 |
return result
|
| 53 |
|
| 54 |
def _stage_negation(self, instruction: str, dataset) -> str:
|
| 55 |
"""Stage 1: Negation - Define boundaries"""
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
if item.get("instruction", "").lower() in instruction.lower():
|
| 59 |
-
logic_steps = item.get("logic_steps", {})
|
| 60 |
-
return logic_steps.get("step_1_negation", "Not found in dataset")
|
| 61 |
-
return "Boundary not defined"
|
| 62 |
|
| 63 |
def _stage_structure(self, instruction: str, dataset) -> str:
|
| 64 |
"""Stage 2: Structure - 50/50 balance"""
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
logic_steps = item.get("logic_steps", {})
|
| 68 |
-
return logic_steps.get("step_2_fifty_fifty", "Structure not found")
|
| 69 |
-
return "Structure not defined"
|
| 70 |
|
| 71 |
def _stage_optimized(self, instruction: str, dataset) -> str:
|
| 72 |
"""Stage 3: Optimized Synthesis"""
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
logic_steps = item.get("logic_steps", {})
|
| 76 |
-
return logic_steps.get("step_3_optimized", item.get("label", ""))
|
| 77 |
-
return "Analysis complete"
|
| 78 |
|
| 79 |
def get_thought_trace(self, result: Dict) -> str:
|
| 80 |
"""Generate thought trace from result"""
|
| 81 |
-
trace = []
|
| 82 |
-
|
| 83 |
stages = result.get("stages", {})
|
| 84 |
|
| 85 |
-
trace
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
trace.append("\n⨠Stage 3 (Synthesis):")
|
| 92 |
-
trace.append(stages.get("synthesis", ""))
|
| 93 |
|
| 94 |
return "\n".join(trace)
|
|
|
|
| 1 |
"""
|
| 2 |
AmkyawLogic Engine - 3-Stage Reasoning Framework
|
| 3 |
+
|
| 4 |
+
Loads dataset: amkyawdev/amkyaw-core-ecosystem
|
| 5 |
+
Returns: step_1_negation, step_2_fifty_fifty, step_3_optimized
|
| 6 |
"""
|
| 7 |
|
| 8 |
+
from typing import Dict, List, Any, Optional
|
| 9 |
+
from datasets import load_dataset
|
| 10 |
|
| 11 |
+
DATASET_ID = "amkyawdev/amkyaw-core-ecosystem"
|
| 12 |
+
_dataset_cache = None
|
| 13 |
|
| 14 |
+
|
| 15 |
+
def get_dataset():
|
| 16 |
+
"""Load and cache dataset"""
|
| 17 |
+
global _dataset_cache
|
| 18 |
+
if _dataset_cache is None:
|
| 19 |
+
try:
|
| 20 |
+
_dataset_cache = load_dataset(DATASET_ID, split="train")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Error loading dataset: {e}")
|
| 23 |
+
return None
|
| 24 |
+
return _dataset_cache
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def search_dataset(user_input: str, dataset) -> Optional[Dict]:
|
| 28 |
+
"""
|
| 29 |
+
Search dataset for matching instruction
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
user_input: User's search query
|
| 33 |
+
dataset: The amkyaw-core-ecosystem dataset
|
| 34 |
+
|
| 35 |
+
Returns:
|
| 36 |
+
Dict with logic_steps or None if not found
|
| 37 |
"""
|
| 38 |
+
if dataset is None:
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
user_input_lower = user_input.lower().strip()
|
| 42 |
+
|
| 43 |
+
# Search through dataset
|
| 44 |
+
for item in dataset:
|
| 45 |
+
instruction = item.get("instruction", "").lower()
|
| 46 |
+
input_field = item.get("input", "").lower()
|
| 47 |
+
|
| 48 |
+
# Check if user input matches instruction (partial match)
|
| 49 |
+
if user_input_lower in instruction or instruction in user_input_lower:
|
| 50 |
+
return item
|
| 51 |
+
if input_field and (user_input_lower in input_field or input_field in user_input_lower):
|
| 52 |
+
return item
|
| 53 |
+
|
| 54 |
+
return None
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def get_logic_steps(user_input: str) -> Dict[str, str]:
|
| 58 |
"""
|
| 59 |
+
Get 3-stage logic steps from dataset
|
| 60 |
+
|
| 61 |
+
Args:
|
| 62 |
+
user_input: User's search query
|
| 63 |
+
|
| 64 |
+
Returns:
|
| 65 |
+
Dict with keys: step_1_negation, step_2_fifty_fifty, step_3_optimized
|
| 66 |
+
"""
|
| 67 |
+
dataset = get_dataset()
|
| 68 |
+
result = search_dataset(user_input, dataset)
|
| 69 |
+
|
| 70 |
+
if result:
|
| 71 |
+
logic_steps = result.get("logic_steps", {})
|
| 72 |
+
return {
|
| 73 |
+
"step_1_negation": logic_steps.get("step_1_negation", "Not found"),
|
| 74 |
+
"step_2_fifty_fifty": logic_steps.get("step_2_fifty_fifty", "Not found"),
|
| 75 |
+
"step_3_optimized": logic_steps.get("step_3_optimized", result.get("label", "Not found"))
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
# Mock response if no match
|
| 79 |
+
return {
|
| 80 |
+
"step_1_negation": f"'{user_input}' ααα―αα²α· α‘αα¬αααΊ α‘ααΌα¬αΈαα±α¬ α‘αα¬ααα―ααΊαα«α",
|
| 81 |
+
"step_2_fifty_fifty": f"'{user_input}' αααΊ αα»αΎααΌα±ααΌα
αΊααα«α",
|
| 82 |
+
"step_3_optimized": f"'{user_input}' α‘αα½ααΊ α‘αα±α¬ααΊαΈαα―αΆαΈ α‘ααΌα±αααΊ ααΌααΊαα¬α·αααΊαα»α±αΈααΎα―α αα±ααΊαα
αΊ α‘ααΎα‘ααα«ααΊααΌα
αΊααα«α"
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# Backward compatibility class
|
| 87 |
+
class AmkyawLogicEngine:
|
| 88 |
+
"""AmkyawLogic Engine implements the 3-stage reasoning"""
|
| 89 |
|
| 90 |
def __init__(self):
|
| 91 |
self.stage_names = [
|
|
|
|
| 95 |
]
|
| 96 |
|
| 97 |
def process(self, instruction: str, dataset) -> Dict[str, Any]:
|
| 98 |
+
"""Process an instruction through the 3-stage logic"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
result = {
|
| 100 |
"instruction": instruction,
|
| 101 |
"stages": {},
|
| 102 |
"output": ""
|
| 103 |
}
|
| 104 |
|
|
|
|
| 105 |
result["stages"]["negation"] = self._stage_negation(instruction, dataset)
|
|
|
|
|
|
|
| 106 |
result["stages"]["structure"] = self._stage_structure(instruction, dataset)
|
|
|
|
|
|
|
| 107 |
result["stages"]["synthesis"] = self._stage_optimized(instruction, dataset)
|
|
|
|
|
|
|
| 108 |
result["output"] = result["stages"]["synthesis"]
|
| 109 |
|
| 110 |
return result
|
| 111 |
|
| 112 |
def _stage_negation(self, instruction: str, dataset) -> str:
|
| 113 |
"""Stage 1: Negation - Define boundaries"""
|
| 114 |
+
logic = get_logic_steps(instruction)
|
| 115 |
+
return logic.get("step_1_negation", "Boundary not defined")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
def _stage_structure(self, instruction: str, dataset) -> str:
|
| 118 |
"""Stage 2: Structure - 50/50 balance"""
|
| 119 |
+
logic = get_logic_steps(instruction)
|
| 120 |
+
return logic.get("step_2_fifty_fifty", "Structure not defined")
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
def _stage_optimized(self, instruction: str, dataset) -> str:
|
| 123 |
"""Stage 3: Optimized Synthesis"""
|
| 124 |
+
logic = get_logic_steps(instruction)
|
| 125 |
+
return logic.get("step_3_optimized", "Analysis complete")
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
def get_thought_trace(self, result: Dict) -> str:
|
| 128 |
"""Generate thought trace from result"""
|
|
|
|
|
|
|
| 129 |
stages = result.get("stages", {})
|
| 130 |
|
| 131 |
+
trace = [
|
| 132 |
+
"π§ Stage 1 (Negation): " + stages.get("negation", ""),
|
| 133 |
+
"βοΈ Stage 2 (Structure): " + stages.get("structure", ""),
|
| 134 |
+
"β¨ Stage 3 (Synthesis): " + stages.get("synthesis", "")
|
| 135 |
+
]
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
return "\n".join(trace)
|