"""
Operon Signal Cascade -- Multi-Stage Amplification Pipeline
===========================================================
Feed text through a multi-stage processing cascade with amplification
factors and checkpoint gates, or explore the classic MAPK three-tier
biological signaling pattern.
Run locally:
pip install gradio
python space-cascade/app.py
"""
import sys
import time
from pathlib import Path
import gradio as gr
_repo_root = Path(__file__).resolve().parent.parent
if str(_repo_root) not in sys.path:
sys.path.insert(0, str(_repo_root))
from operon_ai import (
Cascade,
CascadeStage,
CascadeResult,
StageResult,
StageStatus,
MAPKCascade,
)
# ---------------------------------------------------------------------------
# Presets
# ---------------------------------------------------------------------------
PRESETS: dict[str, dict] = {
"(custom)": {"text": "", "description": "Type your own input"},
"Safe text": {
"text": "Hello World From Operon",
"description": "Clean text passes all stages",
},
"XSS attempt": {
"text": "Hello ",
"description": "Blocked at sanitize checkpoint -- HTML tags stripped",
},
"Empty input": {
"text": "",
"description": "Blocked at validate stage -- empty/None input rejected",
},
"Long text": {
"text": (
"The Operon framework models AI agent infrastructure using biological "
"metaphors. Each organelle serves a distinct function: the Membrane "
"filters threats, the Mitochondria powers computation, the Ribosome "
"synthesizes prompts, the Chaperone validates output structure, and "
"the Lysosome recycles waste from failed operations."
),
"description": "Paragraph of text -- full pipeline with high token count",
},
"Single word": {
"text": "hello",
"description": "Minimal input -- low amplification, fast pipeline",
},
"Unicode text": {
"text": "Bonjour le monde! \u2603 \u2764",
"description": "Unicode characters pass normalization cleanly",
},
"Special characters": {
"text": "test@email.com & 100% success ",
"description": "Mixed special chars -- sanitize strips angle brackets",
},
"MAPK preset": {
"text": "signal",
"description": "Run with the MAPK tab for biological cascade demo",
},
}
# ---------------------------------------------------------------------------
# Styling
# ---------------------------------------------------------------------------
STATUS_COLORS = {
StageStatus.COMPLETED: ("#22c55e", "COMPLETED"),
StageStatus.FAILED: ("#ef4444", "FAILED"),
StageStatus.BLOCKED: ("#f59e0b", "BLOCKED"),
StageStatus.SKIPPED: ("#6b7280", "SKIPPED"),
StageStatus.PENDING: ("#94a3b8", "PENDING"),
StageStatus.RUNNING: ("#3b82f6", "RUNNING"),
}
def _status_badge(status: StageStatus) -> str:
color, label = STATUS_COLORS.get(status, ("#6b7280", "UNKNOWN"))
return (
f'{label}'
)
# ---------------------------------------------------------------------------
# Text pipeline processors
# ---------------------------------------------------------------------------
def _validate(text):
"""Validate input is non-empty."""
if text is None or (isinstance(text, str) and not text.strip()):
raise ValueError("Empty input")
return text
def _normalize(text):
"""Normalize whitespace and case."""
return " ".join(str(text).strip().split())
def _tokenize(text):
"""Split into tokens."""
tokens = str(text).split()
return {"text": text, "tokens": tokens, "count": len(tokens)}
def _sanitize_check(text) -> bool:
"""Checkpoint: reject input with HTML/script tags."""
s = str(text)
return "