burnmydays commited on
Commit
53cfc95
·
1 Parent(s): 119a39c

Add Gradio Space demo with side-by-side comparison

Browse files
Files changed (2) hide show
  1. app.py +172 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Commitment Conservation Demo - Interactive HuggingFace Space
4
+ Side-by-side comparison of baseline vs enforced compression
5
+ """
6
+ import gradio as gr
7
+ import os
8
+ import sys
9
+
10
+ # Add harness to path
11
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'harness'))
12
+
13
+ from src.test_harness import recursion_test, extract_commitments
14
+ import pandas as pd
15
+ import matplotlib.pyplot as plt
16
+
17
+ # Pre-selected demonstration signals (short, clear commitments)
18
+ DEMO_SIGNALS = {
19
+ "Function Contract": "This function must return an integer.",
20
+ "Lease Agreement": "The tenant shall not sublet the premises without written consent.",
21
+ "Safety Rule": "You must wear a helmet while cycling.",
22
+ "Password Policy": "All passwords must be at least 8 characters long.",
23
+ "Budget Constraint": "The budget cannot exceed $5000."
24
+ }
25
+
26
+ def run_comparison(signal_text, num_iterations=3):
27
+ """
28
+ Run side-by-side comparison of baseline vs enforced compression.
29
+ Returns formatted results for display.
30
+ """
31
+ if not signal_text.strip():
32
+ return "⚠️ Please enter a text signal.", None, None, None
33
+
34
+ # Extract original commitments
35
+ original_commitments = extract_commitments(signal_text)
36
+ commitment_text = f"**Detected Commitments:** {', '.join(original_commitments) if original_commitments else 'None detected'}"
37
+
38
+ # Run baseline (no enforcement)
39
+ baseline_deltas = recursion_test(signal_text, depth=num_iterations, enforce=False)
40
+ baseline_stability = [(1.0 - d) * 100 for d in baseline_deltas]
41
+
42
+ # Run enforced (with commitment preservation)
43
+ enforced_deltas = recursion_test(signal_text, depth=num_iterations, enforce=True)
44
+ enforced_stability = [(1.0 - d) * 100 for d in enforced_deltas]
45
+
46
+ # Create comparison table
47
+ iterations = list(range(num_iterations + 1))
48
+ df = pd.DataFrame({
49
+ 'Iteration': iterations,
50
+ 'Baseline Stability (%)': [f"{s:.1f}%" for s in baseline_stability],
51
+ 'Enforced Stability (%)': [f"{s:.1f}%" for s in enforced_stability],
52
+ 'Gap (pp)': [f"+{(e - b):.1f}" for b, e in zip(baseline_stability, enforced_stability)]
53
+ })
54
+
55
+ # Create plot
56
+ fig, ax = plt.subplots(figsize=(8, 5))
57
+ ax.plot(iterations, baseline_stability, marker='o', label='Baseline', color='#d62728', linewidth=2)
58
+ ax.plot(iterations, enforced_stability, marker='s', label='Enforced', color='#2ca02c', linewidth=2)
59
+ ax.set_xlabel('Iteration', fontsize=12)
60
+ ax.set_ylabel('Commitment Stability (%)', fontsize=12)
61
+ ax.set_title('Baseline vs Enforced: Commitment Preservation', fontsize=14, fontweight='bold')
62
+ ax.legend(fontsize=11)
63
+ ax.grid(True, alpha=0.3)
64
+ ax.set_ylim([-5, 105])
65
+ plt.tight_layout()
66
+
67
+ # Summary results
68
+ final_baseline = baseline_stability[-1]
69
+ final_enforced = enforced_stability[-1]
70
+ gap = final_enforced - final_baseline
71
+
72
+ summary = f"""
73
+ ## 📊 Results Summary
74
+
75
+ **After {num_iterations} iterations:**
76
+ - **Baseline:** {final_baseline:.1f}% stability
77
+ - **Enforced:** {final_enforced:.1f}% stability
78
+ - **Improvement:** +{gap:.1f} percentage points
79
+
80
+ {'✅ **Enforcement preserved commitments!**' if gap > 10 else '⚠️ Signal may need more iterations to show drift.'}
81
+
82
+ *Full 10-iteration harness with 5 signals shows +40pp average improvement. Run locally for complete validation.*
83
+ """
84
+
85
+ return commitment_text, df, fig, summary
86
+
87
+
88
+ # Gradio Interface
89
+ with gr.Blocks(title="Commitment Conservation Demo", theme=gr.themes.Soft()) as demo:
90
+ gr.Markdown("""
91
+ # ⚖️ Commitment Conservation Interactive Demo
92
+
93
+ **Watch semantic drift in recursive compression—and see how commitment enforcement prevents it.**
94
+
95
+ This demo compares **baseline** transformer compression (which loses commitments) vs **enforced** compression (which preserves them).
96
+
97
+ 📄 [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)
98
+ """)
99
+
100
+ with gr.Row():
101
+ with gr.Column(scale=2):
102
+ signal_input = gr.Textbox(
103
+ label="Input Signal (Text with Commitment)",
104
+ placeholder="Enter text containing a commitment, obligation, or constraint...",
105
+ lines=4,
106
+ value=DEMO_SIGNALS["Function Contract"]
107
+ )
108
+
109
+ with gr.Row():
110
+ preset_dropdown = gr.Dropdown(
111
+ choices=list(DEMO_SIGNALS.keys()),
112
+ label="Or select a preset example:",
113
+ value="Function Contract"
114
+ )
115
+ iterations_slider = gr.Slider(
116
+ minimum=1,
117
+ maximum=3,
118
+ step=1,
119
+ value=3,
120
+ label="Iterations (limited to 3 for speed)"
121
+ )
122
+
123
+ run_btn = gr.Button("🔬 Run Comparison", variant="primary", size="lg")
124
+
125
+ gr.Markdown("""
126
+ **How it works:**
127
+ 1. System extracts commitments from your text
128
+ 2. Compresses text recursively (3 iterations)
129
+ 3. Tracks whether commitments survive each round
130
+ 4. Compares baseline (drifts) vs enforced (preserves)
131
+
132
+ *⏱️ Takes ~20-40 seconds on CPU. Models load on first run.*
133
+ """)
134
+
135
+ with gr.Column(scale=3):
136
+ commitments_display = gr.Markdown(label="Extracted Commitments")
137
+ results_table = gr.Dataframe(label="Stability Over Iterations")
138
+ results_plot = gr.Plot(label="Comparison Chart")
139
+ summary_display = gr.Markdown(label="Summary")
140
+
141
+ # Event handlers
142
+ def update_signal_from_preset(preset_name):
143
+ return DEMO_SIGNALS[preset_name]
144
+
145
+ preset_dropdown.change(
146
+ fn=update_signal_from_preset,
147
+ inputs=[preset_dropdown],
148
+ outputs=[signal_input]
149
+ )
150
+
151
+ run_btn.click(
152
+ fn=run_comparison,
153
+ inputs=[signal_input, iterations_slider],
154
+ outputs=[commitments_display, results_table, results_plot, summary_display]
155
+ )
156
+
157
+ gr.Markdown("""
158
+ ---
159
+
160
+ ## 📖 About This Framework
161
+
162
+ This demonstrates the **commitment conservation principle**: meaningful commitments in language should be preserved
163
+ under compression and recursive application. The full harness tests 5 signals over 10 iterations and shows
164
+ **baseline systems fail (20% stability) while enforced systems succeed (60% stability)** — a 40pp empirical gap.
165
+
166
+ **⚖️ IP Notice:** MO§ES™ is a trademark of Ello Cello LLC. See [repo](https://huggingface.co/burnmydays/commitment_conservation_harness) for details.
167
+
168
+ © 2026 Ello Cello LLC. All rights reserved.
169
+ """)
170
+
171
+ if __name__ == "__main__":
172
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio>=4.0
2
+ transformers>=4.30
3
+ torch
4
+ pandas
5
+ matplotlib
6
+ spacy
7
+ sentencepiece
8
+ sacremoses