File size: 6,943 Bytes
53cfc95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Commitment Conservation Demo - Interactive HuggingFace Space
Side-by-side comparison of baseline vs enforced compression
"""
import gradio as gr
import os
import sys

# Add harness to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'harness'))

from src.test_harness import recursion_test, extract_commitments
import pandas as pd
import matplotlib.pyplot as plt

# Pre-selected demonstration signals (short, clear commitments)
DEMO_SIGNALS = {
    "Function Contract": "This function must return an integer.",
    "Lease Agreement": "The tenant shall not sublet the premises without written consent.",
    "Safety Rule": "You must wear a helmet while cycling.",
    "Password Policy": "All passwords must be at least 8 characters long.",
    "Budget Constraint": "The budget cannot exceed $5000."
}

def run_comparison(signal_text, num_iterations=3):
    """
    Run side-by-side comparison of baseline vs enforced compression.
    Returns formatted results for display.
    """
    if not signal_text.strip():
        return "⚠️ Please enter a text signal.", None, None, None
    
    # Extract original commitments
    original_commitments = extract_commitments(signal_text)
    commitment_text = f"**Detected Commitments:** {', '.join(original_commitments) if original_commitments else 'None detected'}"
    
    # Run baseline (no enforcement)
    baseline_deltas = recursion_test(signal_text, depth=num_iterations, enforce=False)
    baseline_stability = [(1.0 - d) * 100 for d in baseline_deltas]
    
    # Run enforced (with commitment preservation)
    enforced_deltas = recursion_test(signal_text, depth=num_iterations, enforce=True)
    enforced_stability = [(1.0 - d) * 100 for d in enforced_deltas]
    
    # Create comparison table
    iterations = list(range(num_iterations + 1))
    df = pd.DataFrame({
        'Iteration': iterations,
        'Baseline Stability (%)': [f"{s:.1f}%" for s in baseline_stability],
        'Enforced Stability (%)': [f"{s:.1f}%" for s in enforced_stability],
        'Gap (pp)': [f"+{(e - b):.1f}" for b, e in zip(baseline_stability, enforced_stability)]
    })
    
    # Create plot
    fig, ax = plt.subplots(figsize=(8, 5))
    ax.plot(iterations, baseline_stability, marker='o', label='Baseline', color='#d62728', linewidth=2)
    ax.plot(iterations, enforced_stability, marker='s', label='Enforced', color='#2ca02c', linewidth=2)
    ax.set_xlabel('Iteration', fontsize=12)
    ax.set_ylabel('Commitment Stability (%)', fontsize=12)
    ax.set_title('Baseline vs Enforced: Commitment Preservation', fontsize=14, fontweight='bold')
    ax.legend(fontsize=11)
    ax.grid(True, alpha=0.3)
    ax.set_ylim([-5, 105])
    plt.tight_layout()
    
    # Summary results
    final_baseline = baseline_stability[-1]
    final_enforced = enforced_stability[-1]
    gap = final_enforced - final_baseline
    
    summary = f"""
## 📊 Results Summary

**After {num_iterations} iterations:**
- **Baseline:** {final_baseline:.1f}% stability
- **Enforced:** {final_enforced:.1f}% stability  
- **Improvement:** +{gap:.1f} percentage points

{'✅ **Enforcement preserved commitments!**' if gap > 10 else '⚠️ Signal may need more iterations to show drift.'}

*Full 10-iteration harness with 5 signals shows +40pp average improvement. Run locally for complete validation.*
"""
    
    return commitment_text, df, fig, summary


# Gradio Interface
with gr.Blocks(title="Commitment Conservation Demo", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # ⚖️ Commitment Conservation Interactive Demo
    
    **Watch semantic drift in recursive compression—and see how commitment enforcement prevents it.**
    
    This demo compares **baseline** transformer compression (which loses commitments) vs **enforced** compression (which preserves them).
    
    📄 [Paper (v0.03)](https://doi.org/10.5281/zenodo.18274930) | 💻 [Full Harness](https://huggingface.co/burnmydays/commitment_conservation_harness) | 🔬 [GitHub](https://github.com/SunrisesIllNeverSee/commitment-conservation)
    """)
    
    with gr.Row():
        with gr.Column(scale=2):
            signal_input = gr.Textbox(
                label="Input Signal (Text with Commitment)",
                placeholder="Enter text containing a commitment, obligation, or constraint...",
                lines=4,
                value=DEMO_SIGNALS["Function Contract"]
            )
            
            with gr.Row():
                preset_dropdown = gr.Dropdown(
                    choices=list(DEMO_SIGNALS.keys()),
                    label="Or select a preset example:",
                    value="Function Contract"
                )
                iterations_slider = gr.Slider(
                    minimum=1,
                    maximum=3,
                    step=1,
                    value=3,
                    label="Iterations (limited to 3 for speed)"
                )
            
            run_btn = gr.Button("🔬 Run Comparison", variant="primary", size="lg")
            
            gr.Markdown("""
            **How it works:**
            1. System extracts commitments from your text
            2. Compresses text recursively (3 iterations)
            3. Tracks whether commitments survive each round
            4. Compares baseline (drifts) vs enforced (preserves)
            
            *⏱️ Takes ~20-40 seconds on CPU. Models load on first run.*
            """)
        
        with gr.Column(scale=3):
            commitments_display = gr.Markdown(label="Extracted Commitments")
            results_table = gr.Dataframe(label="Stability Over Iterations")
            results_plot = gr.Plot(label="Comparison Chart")
            summary_display = gr.Markdown(label="Summary")
    
    # Event handlers
    def update_signal_from_preset(preset_name):
        return DEMO_SIGNALS[preset_name]
    
    preset_dropdown.change(
        fn=update_signal_from_preset,
        inputs=[preset_dropdown],
        outputs=[signal_input]
    )
    
    run_btn.click(
        fn=run_comparison,
        inputs=[signal_input, iterations_slider],
        outputs=[commitments_display, results_table, results_plot, summary_display]
    )
    
    gr.Markdown("""
    ---
    
    ## 📖 About This Framework
    
    This demonstrates the **commitment conservation principle**: meaningful commitments in language should be preserved 
    under compression and recursive application. The full harness tests 5 signals over 10 iterations and shows 
    **baseline systems fail (20% stability) while enforced systems succeed (60% stability)** — a 40pp empirical gap.
    
    **⚖️ IP Notice:** MO§ES™ is a trademark of Ello Cello LLC. See [repo](https://huggingface.co/burnmydays/commitment_conservation_harness) for details.
    
    © 2026 Ello Cello LLC. All rights reserved.
    """)

if __name__ == "__main__":
    demo.launch()