Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,18 +5,18 @@ from abc import ABC, abstractmethod
|
|
| 5 |
from typing import List, Dict, Any, Generator
|
| 6 |
|
| 7 |
# =====================================================================
|
| 8 |
-
# 1. CORE
|
| 9 |
# =====================================================================
|
| 10 |
|
| 11 |
class Tool(ABC):
|
| 12 |
@property
|
| 13 |
@abstractmethod
|
| 14 |
def name(self) -> str: pass
|
| 15 |
-
|
| 16 |
@property
|
| 17 |
@abstractmethod
|
| 18 |
def description(self) -> str: pass
|
| 19 |
-
|
| 20 |
@abstractmethod
|
| 21 |
def execute(self, argument: str) -> str: pass
|
| 22 |
|
|
@@ -24,12 +24,12 @@ class ShortTermMemory:
|
|
| 24 |
def __init__(self, max_items: int = 10):
|
| 25 |
self.max_items = max_items
|
| 26 |
self.history: List[Dict[str, str]] = []
|
| 27 |
-
|
| 28 |
def add(self, role: str, content: str):
|
| 29 |
self.history.append({"role": role, "content": content})
|
| 30 |
if len(self.history) > self.max_items:
|
| 31 |
self.history.pop(0)
|
| 32 |
-
|
| 33 |
def clear(self):
|
| 34 |
self.history.clear()
|
| 35 |
|
|
@@ -50,10 +50,10 @@ class BaseAgentLoop(ABC):
|
|
| 50 |
class WebSearchTool(Tool):
|
| 51 |
@property
|
| 52 |
def name(self) -> str: return "Web_Search"
|
| 53 |
-
|
| 54 |
@property
|
| 55 |
def description(self) -> str: return "Searches the web for quantitative demographics or facts."
|
| 56 |
-
|
| 57 |
def execute(self, argument: str) -> str:
|
| 58 |
arg = argument.lower()
|
| 59 |
if "tokyo" in arg:
|
|
@@ -65,10 +65,10 @@ class WebSearchTool(Tool):
|
|
| 65 |
class CalculatorTool(Tool):
|
| 66 |
@property
|
| 67 |
def name(self) -> str: return "Calculator"
|
| 68 |
-
|
| 69 |
@property
|
| 70 |
def description(self) -> str: return "Evaluates standard arithmetic expressions natively."
|
| 71 |
-
|
| 72 |
def execute(self, argument: str) -> str:
|
| 73 |
clean_arg = re.sub(r'[^0-9\+\-\*\/\.\(\)]', '', argument)
|
| 74 |
if not clean_arg:
|
|
@@ -211,3 +211,59 @@ def orchestrate_comparison(query: str) -> Generator[List[str], None, None]:
|
|
| 211 |
if not plan_done:
|
| 212 |
try:
|
| 213 |
step = next(plan_gen)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from typing import List, Dict, Any, Generator
|
| 6 |
|
| 7 |
# =====================================================================
|
| 8 |
+
# 1. CORE FRAMEWORK & BASE LAYER
|
| 9 |
# =====================================================================
|
| 10 |
|
| 11 |
class Tool(ABC):
|
| 12 |
@property
|
| 13 |
@abstractmethod
|
| 14 |
def name(self) -> str: pass
|
| 15 |
+
|
| 16 |
@property
|
| 17 |
@abstractmethod
|
| 18 |
def description(self) -> str: pass
|
| 19 |
+
|
| 20 |
@abstractmethod
|
| 21 |
def execute(self, argument: str) -> str: pass
|
| 22 |
|
|
|
|
| 24 |
def __init__(self, max_items: int = 10):
|
| 25 |
self.max_items = max_items
|
| 26 |
self.history: List[Dict[str, str]] = []
|
| 27 |
+
|
| 28 |
def add(self, role: str, content: str):
|
| 29 |
self.history.append({"role": role, "content": content})
|
| 30 |
if len(self.history) > self.max_items:
|
| 31 |
self.history.pop(0)
|
| 32 |
+
|
| 33 |
def clear(self):
|
| 34 |
self.history.clear()
|
| 35 |
|
|
|
|
| 50 |
class WebSearchTool(Tool):
|
| 51 |
@property
|
| 52 |
def name(self) -> str: return "Web_Search"
|
| 53 |
+
|
| 54 |
@property
|
| 55 |
def description(self) -> str: return "Searches the web for quantitative demographics or facts."
|
| 56 |
+
|
| 57 |
def execute(self, argument: str) -> str:
|
| 58 |
arg = argument.lower()
|
| 59 |
if "tokyo" in arg:
|
|
|
|
| 65 |
class CalculatorTool(Tool):
|
| 66 |
@property
|
| 67 |
def name(self) -> str: return "Calculator"
|
| 68 |
+
|
| 69 |
@property
|
| 70 |
def description(self) -> str: return "Evaluates standard arithmetic expressions natively."
|
| 71 |
+
|
| 72 |
def execute(self, argument: str) -> str:
|
| 73 |
clean_arg = re.sub(r'[^0-9\+\-\*\/\.\(\)]', '', argument)
|
| 74 |
if not clean_arg:
|
|
|
|
| 211 |
if not plan_done:
|
| 212 |
try:
|
| 213 |
step = next(plan_gen)
|
| 214 |
+
plan_log += f"[{step['type']}]\n• {step['content']}\n\n"
|
| 215 |
+
except StopIteration:
|
| 216 |
+
plan_done = True
|
| 217 |
+
if not reflect_done:
|
| 218 |
+
try:
|
| 219 |
+
step = next(reflect_gen)
|
| 220 |
+
reflect_log += f"[{step['type']}]\n• {step['content']}\n\n"
|
| 221 |
+
except StopIteration:
|
| 222 |
+
reflect_done = True
|
| 223 |
+
yield [react_log, plan_log, reflect_log]
|
| 224 |
+
|
| 225 |
+
# =====================================================================
|
| 226 |
+
# 5. GRADIO UI
|
| 227 |
+
# =====================================================================
|
| 228 |
+
|
| 229 |
+
with gr.Blocks(title="LoopLens - Agent Architecture Visualizer") as demo:
|
| 230 |
+
gr.Markdown("# 🔁 LoopLens")
|
| 231 |
+
gr.Markdown("Visualize how different agent architectures (ReAct, Plan-Execute, Reflection) process the same query.")
|
| 232 |
+
|
| 233 |
+
with gr.Tab("Single Architecture"):
|
| 234 |
+
arch_dd = gr.Dropdown(
|
| 235 |
+
choices=["ReAct (Reason + Act)", "Plan-Execute", "Reflection"],
|
| 236 |
+
value="ReAct (Reason + Act)",
|
| 237 |
+
label="Select Architecture"
|
| 238 |
+
)
|
| 239 |
+
query_in = gr.Textbox(
|
| 240 |
+
label="Query",
|
| 241 |
+
value="How many times larger is Tokyo's population than Paris's?",
|
| 242 |
+
lines=2
|
| 243 |
+
)
|
| 244 |
+
out_single = gr.Textbox(label="Execution Log", lines=25)
|
| 245 |
+
btn_single = gr.Button("▶️ Run Single Agent", variant="primary")
|
| 246 |
+
btn_single.click(
|
| 247 |
+
fn=orchestrate_single,
|
| 248 |
+
inputs=[arch_dd, query_in],
|
| 249 |
+
outputs=out_single
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
with gr.Tab("Compare All Architectures"):
|
| 253 |
+
query_cmp = gr.Textbox(
|
| 254 |
+
label="Query",
|
| 255 |
+
value="How many times larger is Tokyo's population than Paris's?",
|
| 256 |
+
lines=2
|
| 257 |
+
)
|
| 258 |
+
out_react = gr.Textbox(label="ReAct Log", lines=15)
|
| 259 |
+
out_plan = gr.Textbox(label="Plan-Execute Log", lines=15)
|
| 260 |
+
out_reflect = gr.Textbox(label="Reflection Log", lines=15)
|
| 261 |
+
btn_cmp = gr.Button("▶️ Run Comparison", variant="primary")
|
| 262 |
+
btn_cmp.click(
|
| 263 |
+
fn=orchestrate_comparison,
|
| 264 |
+
inputs=query_cmp,
|
| 265 |
+
outputs=[out_react, out_plan, out_reflect]
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
if __name__ == "__main__":
|
| 269 |
+
demo.launch()
|