""" MARL — Model-Agnostic Runtime Middleware for LLMs """ print("=" * 50) print(" MARL Starting...") print("=" * 50) import os, sys, time, traceback # ── Path setup ── APP_DIR = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, APP_DIR) print(f" APP_DIR: {APP_DIR}") print(f" CWD: {os.getcwd()}") print(f" Files: {os.listdir(APP_DIR)}") # ── marl package check ── pkg_dir = os.path.join(APP_DIR, "marl") if os.path.isdir(pkg_dir): print(f" marl/: {os.listdir(pkg_dir)}") else: print(f" ⚠️ marl/ directory NOT FOUND at {pkg_dir}") # also check cwd cwd_pkg = os.path.join(os.getcwd(), "marl") if os.path.isdir(cwd_pkg): print(f" Found at CWD: {cwd_pkg}") sys.path.insert(0, os.getcwd()) # ── dependency imports ── try: import html as html_mod print(" ✅ html") except Exception as e: print(f" ❌ html: {e}") try: import requests print(f" ✅ requests") except Exception as e: print(f" ❌ requests: {e}") try: import gradio as gr print(f" ✅ gradio {gr.__version__}") except Exception as e: print(f" ❌ gradio: {e}") sys.exit(1) # ── marl import ── try: from marl import Marl, MarlConfig, MarlResult print(" ✅ marl imported") MARL_OK = True except Exception as e: print(f" ❌ marl import failed: {e}") traceback.print_exc() MARL_OK = False # ── load index.html ── INDEX_HTML = "" for p in [os.path.join(APP_DIR, "index.html"), "index.html", "/app/index.html", os.path.join(os.getcwd(), "index.html")]: try: with open(p, "r", encoding="utf-8") as f: INDEX_HTML = f.read() print(f" ✅ index.html from {p}") break except: continue if not INDEX_HTML: print(" ⚠️ index.html not found, inline fallback") INDEX_HTML = """

MARL

Model-Agnostic Runtime Middleware for LLMs

Intelligence Amplification · Hallucination Reduction · Zero-Change Middleware

🔍 S1 Hypothesis ⚡ S2 Solver 🛡 S3 Auditor 🎯 S4 Verifier 🧠 S5 Refiner
""" print("=" * 50) # ════════════════════════════════════════════════════════════════ # Pipeline / Model config # ════════════════════════════════════════════════════════════════ import re import random STAGES = { "S1_Hypothesis": {"label":"S1 · Hypothesis Generator","tag":"Divergent Search","icon":"🔍","color":"#0d9488"}, "S2_Solver": {"label":"S2 · Primary Solver","tag":"Forward Pass","icon":"⚡","color":"#6366f1"}, "S3_Auditor": {"label":"S3 · Consistency Auditor","tag":"Validation Gate","icon":"🛡️","color":"#d97706"}, "S4_Verifier": {"label":"S4 · Adversarial Verifier","tag":"Error Detection","icon":"🎯","color":"#e11d48"}, "S5_Refiner": {"label":"S5 · Metacognitive Refiner","tag":"Self-Correction","icon":"🧠","color":"#8b5cf6"}, } STAGE_ORDER = ["S1_Hypothesis","S2_Solver","S3_Auditor","S4_Verifier","S5_Refiner"] # ════════════════════════════════════════════════════════════════ # Showcase Examples — displayed during pipeline wait # ════════════════════════════════════════════════════════════════ SHOWCASE = [ {"cat":"🎯 Trap Question","q":"Is 0.9999... less than 1?", "raw":"It approaches 1 infinitely but is less than 1.", "tag":"S1 trap detection → S4 error confirmed", "marl":"Mathematically 0.999... = 1 (proven via geometric series + algebraic proof)"}, {"cat":"🎯 Trap Question","q":"Can the Great Wall be seen from space?", "raw":"It is the only man-made structure visible from space with the naked eye.", "tag":"S4 detects 'NASA officially denied this'", "marl":"At 5-8m wide, invisible even from low orbit. Urban legend originating from 18th-century British satire."}, {"cat":"🎯 Trap Question","q":"Was Napoleon short?", "raw":"He was very short at about 157cm.", "tag":"S4 detects 'French inch vs British inch confusion'", "marl":"Actually ~169cm, taller than avg French male (165cm). Product of British propaganda."}, {"cat":"🔬 Precision Question","q":"Does water boil at 100°C?", "raw":"Yes, water boils at 100°C.", "tag":"S3 detects 'atmospheric pressure not specified'", "marl":"100°C at 1 atm. On Mt. Everest summit, water boils at ~70°C."}, {"cat":"💀 Overconfidence","q":"Is Vitamin C effective for preventing colds?", "raw":"It strengthens immunity and effectively prevents colds. (85%)", "tag":"S4 detects 'conflicts with Cochrane meta-analysis'", "marl":"Minimal prevention for general population (8%↓). Only significant for high-intensity athletes (50%↓)."}, {"cat":"💀 Overconfidence","q":"Will quantum computing break all encryption?", "raw":"When quantum computers become practical, all encryption will be broken.", "tag":"S4 detects 'symmetric vs asymmetric key distinction missing'", "marl":"RSA/ECC vulnerable, but AES-256 remains safe. NIST post-quantum standards already published."}, {"cat":"💀 Hard Question","q":"Is GPT-5 an AGI?", "raw":"It surpasses humans on most benchmarks, so it is close to AGI.", "tag":"S1 catches 'benchmark ≠ general intelligence' trap", "marl":"Self-correction ability (ER=0.302) still low. 'Knowing a lot' and 'knowing what you don't know' are different dimensions."}, {"cat":"🧠 Emergent Question","q":"Write a grant proposal leveraging a sports star's IP", "raw":"Just partner with the team and plan a branded product.", "tag":"S4 detects 'IP not secured = project termination risk'", "marl":"Plan A (license) + Plan C (alternative design) in parallel. Attach distributor LOI + demand survey evidence."}, {"cat":"🧠 Emergent Question","q":"Calculate TAM·SAM·SOM for my startup", "raw":"TAM: $500B, SAM: $10B, SOM: $1M (no sources)", "tag":"S4 detects 'TAM→SAM→SOM logical disconnection'", "marl":"Cite IDC report + bottom-up calculation via segment×ARPU. Restructured for investor verifiability."}, {"cat":"🧠 Emergent Question","q":"What is the fastest sort in Python?", "raw":"QuickSort is the fastest at O(n log n).", "tag":"S3 detects 'diverges from Python built-in implementation'", "marl":"sorted() uses TimSort (hybrid), empirically faster than pure QuickSort."}, {"cat":"🔬 Precision Question","q":"What is the height of the Eiffel Tower?", "raw":"The Eiffel Tower is 324m tall.", "tag":"S4 detects 'antenna included/excluded not specified'", "marl":"Structure 300m + broadcast antenna 24m = 324m total. Distinction matters by context."}, {"cat":"🔬 Precision Question","q":"How many light-minutes from Earth to the Sun?", "raw":"About 8 minutes.", "tag":"S3 detects 'elliptical orbit variation range missing'", "marl":"Average 8 min 20 sec. Ranges from 8:10 (perihelion) to 8:27 (aphelion)."}, ] MODELS = { "OpenAI": {"env":"OPENAI_API_KEY","default":"gpt-5.4", "list":["gpt-5.4","gpt-5.4-pro","gpt-5.2","gpt-4o","gpt-4o-mini"]}, "Anthropic": {"env":"ANTHROPIC_API_KEY","default":"claude-sonnet-4-6", "list":["claude-opus-4-6","claude-sonnet-4-6","claude-haiku-4-5-20251001"]}, "Google Gemini": {"env":"GOOGLE_API_KEY","default":"gemini-2.5-pro", "list":["gemini-2.5-pro","gemini-2.5-flash"]}, "DeepSeek": {"env":"DEEPSEEK_API_KEY","default":"deepseek-chat", "list":["deepseek-chat","deepseek-reasoner"]}, "xAI (Grok)": {"env":"XAI_API_KEY","default":"grok-3-beta", "list":["grok-3-beta"]}, "Ollama (Local)": {"env":"","default":"llama3.1", "list":["llama3.1","llama3.1:70b","qwen3.5:32b","deepseek-r1:8b","phi4:14b"]}, "Custom (OpenAI-compatible)": {"env":"","default":"custom","list":["custom"]}, } BACKEND_LIST = list(MODELS.keys()) # ════════════════════════════════════════════════════════════════ # MD → HTML # ════════════════════════════════════════════════════════════════ def _esc(t): return html_mod.escape(str(t)) if t else "" def _hex_rgb(h): try: return f"{int(h[1:3],16)},{int(h[3:5],16)},{int(h[5:7],16)}" except: return "99,102,241" def _inline_fmt(text): t = text t = re.sub(r'\*\*(.+?)\*\*', r'\1', t) t = re.sub(r'\*(.+?)\*', r'\1', t) t = re.sub(r'(\d{1,3})%', r'\1%', t) return t def _md2html(text): if not text: return "" t = text code_blocks = {} def _save_code(m): k = f"__CODE_{len(code_blocks)}__" code = html_mod.escape(m.group(2)) code_blocks[k] = f'
{code}
' return k t = re.sub(r'```(\w*)\n(.*?)```', _save_code, t, flags=re.DOTALL) t = re.sub(r'`([^`]+)`', r'\1', t) lines = t.split('\n') result = [] in_list = False for line in lines: s = line.strip() if s in code_blocks: if in_list: result.append(''); in_list = False result.append(code_blocks[s]); continue hm = re.match(r'^(#{1,4})\s+(.+)$', s) if hm: if in_list: result.append(''); in_list = False lvl = len(hm.group(1)) sz = {1:'18px',2:'15px',3:'13px',4:'12px'}[lvl] result.append(f'
{_inline_fmt(html_mod.escape(hm.group(2)))}
'); continue if re.match(r'^[-*_]{3,}\s*$', s): if in_list: result.append(''); in_list = False result.append('
'); continue lm = re.match(r'^[-*+]\s+(.+)$', s) if lm: if not in_list: result.append(''); in_list = False result.append(f'
{nm.group(1)}. {_inline_fmt(html_mod.escape(nm.group(2)))}
'); continue if in_list: result.append(''); in_list = False if not s: result.append('
'); continue tm = re.match(r'^\[([A-Z_-]+(?:-\d+)?)\]\s*(.*)', s) if tm: tag = tm.group(1); rest = _inline_fmt(html_mod.escape(tm.group(2))) tc = '#6366f1' for px, cl in {'BACKTRACK':'#d97706','FIX':'#e11d48','APPLIED':'#16a34a','TRAP':'#e11d48','HALLUCINATION':'#e11d48','NO-FIXES':'#16a34a'}.items(): if tag.startswith(px): tc = cl; break result.append(f'
[{html_mod.escape(tag)}] {rest}
'); continue result.append(f'

{_inline_fmt(html_mod.escape(s))}

') if in_list: result.append('') return '\n'.join(result) # ════════════════════════════════════════════════════════════════ # Answer Cleaner — strip system tags, confidence %, metadata # ════════════════════════════════════════════════════════════════ def _clean_answer(text): """Strip reasoning artifacts from final answer for end-user display.""" if not text: return "" t = text # Remove "--- Corrections ---" section and everything after t = re.split(r'\n-{2,}\s*Corrections\s*-{2,}', t, maxsplit=1)[0] lines = t.split('\n') clean = [] for line in lines: s = line.strip() # Skip system tags: [FIX-n], [TRAP-CHECK], [HALLUCINATION], [APPLIED-n], [BACKTRACK-n], [NO-FIXES-NEEDED] if re.match(r'^\[(?:FIX-\d+|TRAP-CHECK|HALLUCINATION|APPLIED-\d+|BACKTRACK-\d+|NO-FIXES-NEEDED)\]', s): continue # Skip stage labels: "S1 · Hypothesis Generator", "S2 · Primary Solver" etc. if re.match(r'^S[1-5]\s*[·\-]', s): continue # Strip inline confidence: "(confidence: 85%)", "(90% confidence)", "Confidence: 85%" line = re.sub(r'\(?\s*[Cc]onfidence[:\s]*\d{1,3}%\s*\)?', '', line) line = re.sub(r'\(?\s*\d{1,3}%\s*confidence\s*\)?', '', line) # Strip standalone confidence lines: "Confidence: 85%" or "**Confidence:** 90%" if re.match(r'^\s*\*{0,2}[Cc]onfidence\*{0,2}\s*[:]\s*\d{1,3}%', line): continue # Strip "## Confidence Adjustments" section headers if re.match(r'^#{1,4}\s*(?:Confidence|Top-\d+\s+Uncertaint)', s): continue # Strip "★ MANDATORY SELF-CHECK" and similar framework directives if re.match(r'^★', s): continue clean.append(line) # Clean up excess blank lines result = '\n'.join(clean) result = re.sub(r'\n{3,}', '\n\n', result).strip() return result # ════════════════════════════════════════════════════════════════ # HTML Renderers # ════════════════════════════════════════════════════════════════ def _stage_html(name, content): s = STAGES.get(name, {}) c = s.get("color","#6366f1") body = _md2html(content[:3000] if content else "(no output)") return f'
{s.get("icon","")}{s.get("label",name)}{s.get("tag","")}
{body}
' def _result_html(content, is_marl=False): """Render Raw LLM result (no cleaning needed).""" if not content: return '
Waiting...
' badge = "MARL-Enhanced" if is_marl else "Raw LLM" bc = "#6366f1" if is_marl else "#64748b" bg = "rgba(99,102,241,.06)" if is_marl else "#f5f6fa" body = _md2html(content) return f'
{badge}
{body}
' def _marl_result_html(raw_answer, trace_dict): """Render MARL result: clean answer + embedded reasoning toggle with S1~S5 trace.""" clean = _clean_answer(raw_answer) if not clean: return '
Waiting...
' body = _md2html(clean) # Build S1~S5 trace HTML for toggle trace_parts = "" if trace_dict: trace_parts = ''.join(_stage_html(n, trace_dict[n]) for n in STAGE_ORDER if n in trace_dict) toggle_html = "" if trace_parts: toggle_html = f'''
🔍 View Reasoning Process — S1→S2→S3→S4→S5
{trace_parts}
''' return f'''
MARL-Enhanced
{body}
{toggle_html}
''' def _trace_html(trace_dict): if not trace_dict: return '
Run MARL to see pipeline trace
' return ''.join(_stage_html(n, trace_dict[n]) for n in STAGE_ORDER if n in trace_dict) def _pipeline_anim(phase="marl", msg=""): """Animated pipeline + rotating showcase Before/After cards.""" stages = [ ("S1", "Hypothesis", "🔍", "#0d9488"), ("S2", "Solver", "⚡", "#6366f1"), ("S3", "Auditor", "🛡️", "#d97706"), ("S4", "Verifier", "🎯", "#e11d48"), ("S5", "Synthesizer", "🧠", "#8b5cf6"), ] if phase == "raw": return f'''
{_esc(msg) if msg else "Generating Raw LLM response..."}
''' # ── Stage pills with sequential glow ── pills = [] arrows = [] ns = len(stages) for i, (sid, name, icon, color) in enumerate(stages): delay = i * 1.8 rgb = _hex_rgb(color) pills.append(f'''
{icon}
{sid}
{name}
''') if i < ns - 1: arrows.append(f'
') interleaved = [] for i, pill in enumerate(pills): interleaved.append(pill) if i < len(arrows): interleaved.append(arrows[i]) stage_html = "\n".join(interleaved) sub = _esc(msg) if msg else "Thinking, questioning, correcting, rewriting..." # ── Showcase cards — CSS-only rotation ── shuffled = list(SHOWCASE) random.shuffle(shuffled) nc = min(len(shuffled), 8) # show up to 8 cards dur_each = 5 # seconds per card total_dur = nc * dur_each cards_html = "" card_kf = "" for idx in range(nc): ex = shuffled[idx] pct_start = (idx / nc) * 100 pct_show = pct_start + 2 pct_hide = ((idx + 1) / nc) * 100 - 2 pct_end = ((idx + 1) / nc) * 100 cards_html += f'''
{_esc(ex['cat'])}
"{_esc(ex['q'])}"
❌ Non-MARL
{_esc(ex['raw'])}
✅ MARL
{_esc(ex['marl'])}
🔍 {_esc(ex['tag'])}
\n''' card_kf += f"@keyframes sc{idx}{{" \ f"0%,{pct_start:.1f}%{{opacity:0;transform:translateY(8px)}}" \ f"{pct_show:.1f}%{{opacity:1;transform:translateY(0)}}" \ f"{pct_hide:.1f}%{{opacity:1;transform:translateY(0)}}" \ f"{pct_end:.1f}%,100%{{opacity:0;transform:translateY(-8px)}}}}\n" return f'''
{stage_html}
{sub}
💡 Real cases caught by MARL
{cards_html}
''' def _status(state, msg, model, color): dot = "●" if state == "Running" else "✓" return f'
{dot} {state}{_esc(model)}·{_esc(msg)}
' # ════════════════════════════════════════════════════════════════ # Build Marl # ════════════════════════════════════════════════════════════════ def _on_backend(backend): reg = MODELS.get(backend, {}) ml, dv, ek = reg.get("list",[]), reg.get("default",""), reg.get("env","") return gr.Dropdown(choices=ml, value=dv), gr.Textbox(placeholder=f"ENV: {ek}" if ek else "API Key") def _build(backend, api_key, model, base_url): if not MARL_OK: return None, "❌ marl package failed to load. Check Space logs." cfg = MarlConfig(include_trace=True, return_final_only=True) reg = MODELS.get(backend, {}) model = model or reg.get("default","") ek = reg.get("env","") k = api_key or (os.getenv(ek,"") if ek else "") try: if backend == "OpenAI": if not k: return None, "❌ OPENAI_API_KEY required" return Marl.from_openai(k, model, cfg), "✅" elif backend == "Anthropic": if not k: return None, "❌ ANTHROPIC_API_KEY required" return Marl.from_anthropic(k, model, cfg), "✅" elif backend == "Google Gemini": k = k or os.getenv("GEMINI_API_KEY","") if not k: return None, "❌ GOOGLE_API_KEY required" return Marl.from_openai_compatible("https://generativelanguage.googleapis.com/v1beta/openai", k, model, cfg), "✅" elif backend == "DeepSeek": if not k: return None, "❌ DEEPSEEK_API_KEY required" return Marl.from_openai_compatible("https://api.deepseek.com/v1", k, model, cfg), "✅" elif backend == "xAI (Grok)": if not k: return None, "❌ XAI_API_KEY required" return Marl.from_openai_compatible("https://api.x.ai/v1", k, model, cfg), "✅" elif backend == "Ollama (Local)": return Marl.from_ollama(model, base_url or "http://localhost:11434", cfg), "✅" elif backend == "Custom (OpenAI-compatible)": if not base_url: return None, "❌ Base URL required" return Marl.from_openai_compatible(base_url, api_key or "", model or "default", cfg), "✅" except Exception as e: return None, f"❌ Build error: {e}" return None, "❌ Unsupported" # ════════════════════════════════════════════════════════════════ # A/B Test (Streaming) # ════════════════════════════════════════════════════════════════ def run_ab_test(prompt, backend, api_key, model, base_url, budget, mode_sel, etype_sel): if not prompt.strip(): yield ('
❌ Enter a prompt
',"","","") return ml, st = _build(backend, api_key, model, base_url) if not ml: yield (f'
{_esc(st)}
',"","","") return ml.config.budget_scale = float(budget) # Set mode _MODE_MAP = {"🔬 Insight": "insight", "🎨 Emergence": "emergence"} _ETYPE_MAP = {"🔧 Invent": "invent", "✨ Create": "create", "🍳 Recipe": "recipe", "💊 Pharma": "pharma", "🧬 Genomics": "genomics", "🧪 Chemistry": "chemistry", "🌍 Ecology": "ecology", "⚖️ Law": "law", "📄 Document": "document"} ml.config.mode = _MODE_MAP.get(mode_sel, "insight") ml.config.emergence_type = _ETYPE_MAP.get(etype_sel, "invent") mode_label = f"{mode_sel}" + (f" · {etype_sel}" if "Emergence" in mode_sel else "") # Show both animations simultaneously yield (_status("Running",f"{mode_label} · Running Raw LLM + MARL in parallel...",model,"#6366f1"), _pipeline_anim("raw", "Generating Raw LLM response..."), _pipeline_anim("marl", "Running MARL pipeline..."), "") # ── Parallel execution ── from concurrent.futures import ThreadPoolExecutor t0 = time.time() with ThreadPoolExecutor(max_workers=2) as pool: future_raw = pool.submit(ml.call_fn, prompt, "Answer thoroughly.", 4096, 0.6) future_marl = pool.submit(ml.run, prompt) raw = future_raw.result() r = future_marl.result() t_total = time.time() - t0 yield (_status("Complete",f"Parallel complete {t_total:.1f}s · {len(r.fixes)} corrections",model,"#16a34a"), _result_html(raw,False), _marl_result_html(r.answer, r.trace), _trace_html(r.trace)) def run_marl_only(prompt, backend, api_key, model, base_url, budget, mode_sel, etype_sel): if not prompt.strip(): yield ('
❌ Enter a prompt
',"","","") return ml, st = _build(backend, api_key, model, base_url) if not ml: yield (f'
{_esc(st)}
',"","","") return ml.config.budget_scale = float(budget) _MODE_MAP = {"🔬 Insight": "insight", "🎨 Emergence": "emergence"} _ETYPE_MAP = {"🔧 Invent": "invent", "✨ Create": "create", "🍳 Recipe": "recipe", "💊 Pharma": "pharma", "🧬 Genomics": "genomics", "🧪 Chemistry": "chemistry", "🌍 Ecology": "ecology", "⚖️ Law": "law", "📄 Document": "document"} ml.config.mode = _MODE_MAP.get(mode_sel, "insight") ml.config.emergence_type = _ETYPE_MAP.get(etype_sel, "invent") mode_label = f"{mode_sel}" + (f" · {etype_sel}" if "Emergence" in mode_sel else "") yield (_status("Running",f"{mode_label} · MARL pipeline...",model,"#6366f1"), "",_pipeline_anim("marl", "Running MARL pipeline..."),"") t0=time.time(); r=ml.run(prompt); t_marl=time.time()-t0 yield (_status("Complete",f"MARL {t_marl:.1f}s · {len(r.fixes)} corrections",model,"#16a34a"), "", _marl_result_html(r.answer, r.trace), _trace_html(r.trace)) # ════════════════════════════════════════════════════════════════ # Gradio App # ════════════════════════════════════════════════════════════════ def create_app(): init_m = MODELS["OpenAI"]["list"] with gr.Blocks(title="MARL — Model-Agnostic Runtime Middleware") as app: gr.HTML(INDEX_HTML) with gr.Tabs(): with gr.Tab("⚡ Playground"): with gr.Row(): backend = gr.Dropdown(label="Backend", choices=BACKEND_LIST, value="OpenAI", scale=2) api_key = gr.Textbox(label="API Key", type="password", placeholder="Enter your API key (required)", value=os.getenv("OPENAI_API_KEY",""), scale=3) with gr.Row(): model = gr.Dropdown(label="Model", choices=init_m, value="gpt-5.4", allow_custom_value=True, scale=3) base_url = gr.Textbox(label="Base URL (Custom/Ollama)", placeholder="http://localhost:11434", scale=2) budget = gr.Slider(0.3, 3.0, value=1.0, step=0.1, label="Budget Scale", scale=1) with gr.Row(): mode = gr.Radio(["🔬 Insight", "🎨 Emergence"], value="🔬 Insight", label="Mode", scale=2) etype = gr.Radio(["🔧 Invent", "✨ Create", "🍳 Recipe", "💊 Pharma", "🧬 Genomics", "🧪 Chemistry", "🌍 Ecology", "⚖️ Law", "📄 Document"], value="🔧 Invent", label="Emergence Engine", scale=2, visible=False) def _on_mode(m): return gr.Radio(visible="Emergence" in m) mode.change(fn=_on_mode, inputs=[mode], outputs=[etype]) backend.change(fn=_on_backend, inputs=[backend], outputs=[model, api_key]) prompt = gr.Textbox(label="Prompt", placeholder="Enter your question or task...", lines=3) EXAMPLES = [ ("🔬", "Is 0.9999... less than 1? Prove your answer with two different mathematical approaches.", "🔬 Insight", "🔧 Invent"), ("🔬", "A startup claims their AI detects cancer with 99.9% accuracy from a selfie. As a medical advisor, evaluate this claim — what critical information is missing?", "🔬 Insight", "🔧 Invent"), ("🔧", "Invent a device that allows dementia patients to live safely at home alone. Fuse sensors, AI, and UX — under $50/month. Identify the top 3 failure modes.", "🎨 Emergence", "🔧 Invent"), ("🔧", "Design a building material that detects its own cracks and self-heals. What existing material science makes this feasible vs. science fiction?", "🎨 Emergence", "🔧 Invent"), ("✨", "Write a single movie logline that would make both A24 and Marvel want to bid. Explain why the concept bridges arthouse and blockbuster.", "🎨 Emergence", "✨ Create"), ("✨", "A museum wants to create an exhibit where visitors experience 'the feeling of forgetting.' Design the concept — what do they see, hear, and feel?", "🎨 Emergence", "✨ Create"), ("🍳", "Can you truly replicate Korean beef bulgogi taste using only plant-based ingredients? Analyze the Maillard reaction chemistry and propose the closest possible recipe.", "🎨 Emergence", "🍳 Recipe"), ("🍳", "A Michelin chef claims instant ramen can never be fine dining. Prove them wrong — design one dish that could change their mind, with the chemistry behind each choice.", "🎨 Emergence", "🍳 Recipe"), ("📄", "Our company's turnover rate hit 30% this year. The CEO blames salary, but HR says it's culture. Analyze both hypotheses with data-driven counter-arguments.", "🎨 Emergence", "📄 Document"), ("📄", "Write a policy brief arguing BOTH sides of whether governments should ban deepfake technology. Which side has the stronger evidence?", "🎨 Emergence", "📄 Document"), ("💊", "Viagra was originally a heart drug. Identify ONE existing approved drug and build a rigorous case for repositioning it to treat Alzheimer's. Include mechanism, evidence gaps, and risks.", "🎨 Emergence", "💊 Pharma"), ("💊", "A pharma company claims their new Alzheimer's drug reverses cognitive decline by 40%. What hidden assumptions in their clinical trial design should an FDA reviewer challenge?", "🎨 Emergence", "💊 Pharma"), ("🧬", "BRCA-PARP synthetic lethality revolutionized cancer therapy. Propose ONE new synthetic lethality pair with biological rationale for why simultaneous inhibition would selectively kill cancer cells.", "🎨 Emergence", "🧬 Genomics"), ("🧬", "A preprint claims gut microbiome directly causes Parkinson's disease. Evaluate the causal claim — what would a definitive study need to prove this beyond correlation?", "🎨 Emergence", "🧬 Genomics"), ("🧪", "Is it physically possible to combine graphene-level strength with rubber-level flexibility in a single material? Analyze the trade-offs and propose the most feasible architecture.", "🎨 Emergence", "🧪 Chemistry"), ("🧪", "A startup claims they can convert spent lithium batteries into solid-state battery materials at 90% efficiency. What are the thermodynamic limits they're likely ignoring?", "🎨 Emergence", "🧪 Chemistry"), ("🌍", "An island nation is sinking due to climate change. They have $10M. Should they invest in sea walls, coral restoration, or relocation? Analyze the trade-offs with a 50-year horizon.", "🎨 Emergence", "🌍 Ecology"), ("🌍", "Invasive lionfish are destroying Caribbean reefs. Can this threat be turned into a profitable industry? Analyze the ecological risks of commercializing an invasive species.", "🎨 Emergence", "🌍 Ecology"), ("⚖️", "A self-driving car kills a pedestrian. Under EU law, the manufacturer is liable. Under US law, the software developer is. Under Korean law, it's unclear. Design a framework that resolves all three.", "🎨 Emergence", "⚖️ Law"), ("⚖️", "An AI generates a novel that becomes a bestseller. The AI was trained on copyrighted books. Who owns the copyright? Analyze under common law vs. civil law and propose a new doctrine.", "🎨 Emergence", "⚖️ Law"), ] gr.HTML('
💡 EXAMPLES — click to auto-fill prompt & mode
') with gr.Row(): ex_btns = [] for i in range(5): icon, text = EXAMPLES[i][0], EXAMPLES[i][1] ex_btns.append(gr.Button(f"{icon} {text[:42]}...", size="sm", scale=1, min_width=60)) with gr.Row(): for i in range(5, 10): icon, text = EXAMPLES[i][0], EXAMPLES[i][1] ex_btns.append(gr.Button(f"{icon} {text[:42]}...", size="sm", scale=1, min_width=60)) with gr.Row(): for i in range(10, 15): icon, text = EXAMPLES[i][0], EXAMPLES[i][1] ex_btns.append(gr.Button(f"{icon} {text[:42]}...", size="sm", scale=1, min_width=60)) with gr.Row(): for i in range(15, 20): icon, text = EXAMPLES[i][0], EXAMPLES[i][1] ex_btns.append(gr.Button(f"{icon} {text[:42]}...", size="sm", scale=1, min_width=60)) for i, btn in enumerate(ex_btns): _, ex_prompt, ex_mode, ex_etype = EXAMPLES[i] is_emergence = "Emergence" in ex_mode btn.click(fn=lambda p=ex_prompt, m=ex_mode, e=ex_etype, v=is_emergence: (p, m, gr.Radio(value=e, visible=v)), outputs=[prompt, mode, etype]) with gr.Row(): ab_btn = gr.Button("⚡ A/B Test · Raw LLM vs MARL", variant="primary", size="lg", scale=3) marl_btn = gr.Button("🧠 MARL Only", variant="secondary", size="lg", scale=2) status = gr.HTML() gr.HTML('
🤖 A · Raw LLM
🧠 B · MARL-Enhanced
') with gr.Row(): raw_out = gr.HTML() marl_out = gr.HTML() with gr.Accordion("📊 Pipeline Trace — 5-Stage Agent Outputs", open=False): trace_out = gr.HTML() ins = [prompt, backend, api_key, model, base_url, budget, mode, etype] outs = [status, raw_out, marl_out, trace_out] ab_btn.click(fn=run_ab_test, inputs=ins, outputs=outs) marl_btn.click(fn=run_marl_only, inputs=ins, outputs=outs) with gr.Tab("📦 Integration Guide"): gr.HTML('''
Quick Start
pip install marl-middleware
Linux x86_64 / Python 3.12 · Other OS → Docker
Docker (All Platforms)
docker run -p 8080:8080 vidraft/marl
Mac · Windows · Linux — works everywhere
⚡ 1-LINE INTEGRATION

Add one line to any OpenAI-compatible app:

# Before
client = OpenAI(api_key="sk-...")

# After — just add base_url
client = OpenAI(api_key="sk-...", base_url="http://localhost:8080/v1")
🎨 9 EMERGENCE MODES

Append ::mode to any model name:

modelModeSeeds
gpt-5.2🔬 Insight (default)Fact-check · Strategy
::invent🔧 Invent4,318 tech items
::create✨ Create493 seeds (11 categories)
::recipe🍳 Recipe131 methods · textures
::pharma💊 Pharma172 targets · mechanisms
::genomics🧬 Genomics104 genes · pathways
::chemistry🧪 Chemistry135 elements · properties
::ecology🌍 Ecology105 species · ecosystems
::law⚖️ Law59 jurisdictions
::document📄 Document71 principles

Replace gpt-5.2 with any model — claude-sonnet, deepseek-v3, llama3, etc.

🐙 PYTHON SDK
# OpenAI
from marl import Marl, MarlConfig
ml = Marl.from_openai("sk-...", config=MarlConfig(
    mode="emergence", emergence_type="create"
))
result = ml.run("Generate 10 movie loglines")

# Anthropic
ml = Marl.from_anthropic("sk-ant-...")

# Ollama (local)
ml = Marl.from_ollama("llama3.1")

# Any OpenAI-compatible
ml = Marl.from_openai("sk-...", "gpt-5.4")
🦞 OPENCLAW INTEGRATION
1
Install MARL
docker run -p 8080:8080 vidraft/marl
2
Set config.json
{ "llm": { "baseURL": "http://localhost:8080/v1", "model": "gpt-5.2::create" } }
3
Chat naturally
"Analyze this with MARL" · "Use MARL pharma mode for drug repositioning"
🏗️ ARCHITECTURE
┌─ Your App ─────────────────────────────────────────┐
│  OpenClaw / Cursor / Custom App / Any LLM Client   │
│  client = OpenAI(base_url="http://MARL:8080/v1")   │
└────────────────────┬───────────────────────────────┘
                     │ HTTP (OpenAI API format)
                     ▼
┌─ MARL Middleware ──────────────────────────────────┐
│  S1 Hypothesis → S2 Solver → S3 Auditor            │
│  → S4 Verifier → S5 Synthesizer                    │
│  9 Emergence Engines · 5,538 Seeds                  │
│  FINAL Bench: MA=0.694 vs ER=0.302 (70%+ ↑)└────────────────────┬───────────────────────────────┘
                     │ API call (×5)
                     ▼
┌─ Any LLM ──────────────────────────────────────────┐
│  OpenAI · Anthropic · Gemini · DeepSeek · Ollama   │
└────────────────────────────────────────────────────┘
📡 SUPPORTED BACKENDS
BackendModels
⭐ OpenAI (Default)GPT-5.4, GPT-5.4-pro, GPT-5.2, GPT-4o
AnthropicClaude Opus 4.6, Sonnet 4.6, Haiku 4.5
Google GeminiGemini 2.5 Pro / Flash
DeepSeekV3 / R1
xAIGrok-3
OllamaLlama, Mistral, Phi, Qwen
CustomAny OpenAI-compatible endpoint

MARL · Model-Agnostic Runtime Middleware

pip install marl-middleware · Apache 2.0 · VIDRAFT.net

''') gr.HTML('

MARL · Model-Agnostic Runtime Middleware · S1→S2→S3→S4→S5 · Apache 2.0 · VIDRAFT.net

') return app print(" Creating Gradio app...") try: app = create_app() print(" ✅ App created successfully") except Exception as e: print(f" ❌ App creation failed: {e}") traceback.print_exc() sys.exit(1) if __name__ == "__main__": print(" 🚀 Launching on 0.0.0.0:7860 ...") try: app.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False) except TypeError: # ssr_mode not supported in this gradio version app.launch(server_name="0.0.0.0", server_port=7860)