DranFren commited on
Commit
1b21338
·
verified ·
1 Parent(s): 195c903

Upload 5 files

Browse files
Files changed (5) hide show
  1. README.md +8 -16
  2. app.py +160 -65
  3. cem.csv +1 -0
  4. playbook.yaml +45 -0
  5. requirements.txt +3 -0
README.md CHANGED
@@ -1,17 +1,9 @@
1
- ---
2
- title: Paradigms
3
- emoji: 💬
4
- colorFrom: yellow
5
- colorTo: purple
6
- sdk: gradio
7
- sdk_version: 5.42.0
8
- app_file: app.py
9
- pinned: false
10
- hf_oauth: true
11
- hf_oauth_scopes:
12
- - inference-api
13
- license: cc-by-nc-4.0
14
- short_description: building a space for paradigm tests
15
- ---
16
 
17
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
 
 
1
+ # ACE–CPT Context Agent (Hugging Face Space)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ A minimal ACE loop (Generator Reflector Curator) with CPT add‑ons.
4
+
5
+ ## Run locally
6
+ ```
7
+ pip install -r requirements.txt
8
+ python app.py
9
+ ```
app.py CHANGED
@@ -1,70 +1,165 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
-
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
-
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- messages.extend(history)
22
-
23
- messages.append({"role": "user", "content": message})
24
-
25
- response = ""
26
-
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
- )
62
-
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
 
 
 
 
 
68
 
69
  if __name__ == "__main__":
70
  demo.launch()
 
1
+
2
  import gradio as gr
3
+ import yaml, pandas as pd, os, re
4
+
5
+ PLAYBOOK_PATH = "playbook.yaml"
6
+ CEM_PATH = "cem.csv"
7
+
8
+ def load_playbook():
9
+ if not os.path.exists(PLAYBOOK_PATH):
10
+ return {"playbook": {"metadata": {"name": "ACE–CPT Playbook", "version": "0.1"}, "items": []}}
11
+ with open(PLAYBOOK_PATH, "r", encoding="utf-8") as f:
12
+ return yaml.safe_load(f) or {}
13
+
14
+ def save_playbook(pb):
15
+ with open(PLAYBOOK_PATH, "w", encoding="utf-8") as f:
16
+ yaml.safe_dump(pb, f, sort_keys=False, allow_unicode=True)
17
+ return True
18
+
19
+ def ensure_cem():
20
+ if not os.path.exists(CEM_PATH):
21
+ pd.DataFrame(columns=["ClaimID","Claim","EvidenceRefs","Counter-Evidence","Confidence"]).to_csv(CEM_PATH, index=False)
22
+ return pd.read_csv(CEM_PATH)
23
+
24
+ def save_cem(df):
25
+ df.to_csv(CEM_PATH, index=False)
26
+ return df
27
+
28
+ def run_generator(task, inputs, playbook_text):
29
+ # Stubbed generator
30
+ trace = [
31
+ f"S01: Restate task → {task[:120]}",
32
+ "S02: Identify relevant playbook items → PB-001, PB-004",
33
+ "S03: Apply heuristic reasoning (stub)",
34
+ "S04: Prepare answer with uncertainty note"
35
+ ]
36
+ answer = "[Demo] Task addressed. (Replace with real model call.)"
37
+ used_items = "PB-001, PB-004"
38
+ cpt_notes = "2.11–2.17 quick pass; Flags: anthropomorphization=false, spec_gaming=possible, uncertainty=high"
39
+ return answer, "\n".join(trace), used_items, cpt_notes
40
+
41
+ def run_reflector(task_id, trace_text):
42
+ import re, yaml as _yaml
43
+ steps = re.findall(r"S(\d+):", trace_text)
44
+ evidence_steps = [f"S{sid}" for sid in steps[-2:]] if steps else []
45
+ deltas_yaml = {
46
+ "deltas": [{
47
+ "op": "add",
48
+ "proposed": {
49
+ "type": "checklist",
50
+ "title": "Preflight task restatement",
51
+ "content": "Before solving, restate the task in one line and list 2–3 success criteria plus one salient risk.",
52
+ "tags": ["meta","stillness"],
53
+ "evidence": {
54
+ "task_id": task_id or "T-000",
55
+ "trace_steps": evidence_steps,
56
+ "tests": [],
57
+ "sources": []
58
+ },
59
+ "confidence": "HIGH",
60
+ "paradigm_scope": ["method"],
61
+ "closure_flags": {"anthropomorphization": False, "spec_gaming": False, "monoculture": False},
62
+ "cem": {
63
+ "claim_id": "C-NEW",
64
+ "claim_text": "Preflight improves clarity and reduces failure risk.",
65
+ "evidence_refs": evidence_steps,
66
+ "counterevidence_refs": []
67
+ }
68
+ },
69
+ "rationale": "Reusable checklist that generalizes across tasks."
70
+ }]
71
+ }
72
+ return _yaml.safe_dump(deltas_yaml, sort_keys=False, allow_unicode=True)
73
+
74
+ def run_curator(playbook_text, deltas_text):
75
+ try:
76
+ pb = yaml.safe_load(playbook_text) if playbook_text.strip() else load_playbook()
77
+ if not pb or "playbook" not in pb:
78
+ pb = load_playbook()
79
+ except Exception:
80
+ pb = load_playbook()
81
+
82
+ try:
83
+ dz = yaml.safe_load(deltas_text) if deltas_text.strip() else {"deltas": []}
84
+ except Exception as e:
85
+ return playbook_text, f"YAML parse error in deltas: {e}"
86
+
87
+ items = pb.get("playbook", {}).get("items", [])
88
+ existing = {(it.get("title",""), it.get("type","")) for it in items}
89
+ next_id = 1
90
+ for it in items:
91
+ try:
92
+ n = int(str(it.get("id","PB-0")).split("-")[-1])
93
+ next_id = max(next_id, n+1)
94
+ except Exception:
95
+ pass
96
+
97
+ diff_lines = []
98
+ for d in dz.get("deltas", []):
99
+ if d.get("op") != "add":
100
+ continue
101
+ prop = d.get("proposed", {})
102
+ key = (prop.get("title",""), prop.get("type",""))
103
+ if key in existing:
104
+ diff_lines.append(f"~ duplicate skipped: {key[0]} ({key[1]})")
105
+ continue
106
+ new_item = {
107
+ "id": f"PB-{next_id:03d}",
108
+ "type": prop.get("type","heuristic"),
109
+ "title": prop.get("title","Untitled"),
110
+ "content": prop.get("content",""),
111
+ "tags": prop.get("tags",[]),
112
+ "helpful": 0,
113
+ "harmful": 0
114
+ }
115
+ items.append(new_item)
116
+ existing.add(key)
117
+ diff_lines.append(f"+ {new_item['id']}: {new_item['title']} ({new_item['type']})")
118
+ next_id += 1
119
+
120
+ pb["playbook"]["items"] = items
121
+ save_playbook(pb)
122
+ new_text = yaml.safe_dump(pb, sort_keys=False, allow_unicode=True)
123
+ return new_text, "\n".join(diff_lines) if diff_lines else "No changes applied."
124
+
125
+ def cem_load():
126
+ return ensure_cem()
127
+
128
+ def cem_save(df):
129
+ return save_cem(df)
130
+
131
+ with gr.Blocks(title="ACE–CPT Context Agent") as demo:
132
+ gr.Markdown("# ACE–CPT Context Agent\nA minimal ACE loop (Generator → Reflector → Curator) with CPT add‑ons.")
133
+ with gr.Row():
134
+ task = gr.Textbox(label="Task", placeholder="Describe the task…")
135
+ task_id = gr.Textbox(label="TaskID", value="T-001")
136
+ inputs = gr.Textbox(label="Inputs (optional)", lines=6, placeholder="Paste notes, success criteria, risks…")
137
+ playbook = gr.Textbox(label="Playbook (YAML)", lines=18, value=open(PLAYBOOK_PATH, "r", encoding="utf-8").read())
138
+
139
+ with gr.Row():
140
+ gen_btn = gr.Button("Run Generator")
141
+ ref_btn = gr.Button("Run Reflector")
142
+ cur_btn = gr.Button("Run Curator")
143
+ save_pb_btn = gr.Button("Save Playbook")
144
+
145
+ answer = gr.Textbox(label="Answer (Generator)", lines=3)
146
+ trace = gr.Textbox(label="Trace (Generator)", lines=8)
147
+ used = gr.Textbox(label="UsedItems (Generator)", lines=1)
148
+ cpt = gr.Textbox(label="CPT Quick Notes (Generator)", lines=6)
149
+
150
+ deltas = gr.Textbox(label="Reflector Deltas (YAML)", lines=10)
151
+ diff = gr.Textbox(label="Curator Merge Log", lines=4)
152
+
153
+ gr.Markdown("## Claim–Evidence Matrix (CEM)")
154
+ cem_dataframe = gr.Dataframe(value=cem_load(), interactive=True, wrap=True)
155
+ with gr.Row():
156
+ cem_save_btn = gr.Button("Save CEM")
157
 
158
+ gen_btn.click(run_generator, [task, inputs, playbook], [answer, trace, used, cpt])
159
+ ref_btn.click(run_reflector, [task_id, trace], deltas)
160
+ cur_btn.click(run_curator, [playbook, deltas], [playbook, diff])
161
+ save_pb_btn.click(lambda t: (save_playbook(yaml.safe_load(t)), t)[1], playbook, playbook)
162
+ cem_save_btn.click(cem_save, cem_dataframe, cem_dataframe)
163
 
164
  if __name__ == "__main__":
165
  demo.launch()
cem.csv ADDED
@@ -0,0 +1 @@
 
 
1
+ ClaimID,Claim,EvidenceRefs,Counter-Evidence,Confidence
playbook.yaml ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ playbook:
2
+ metadata:
3
+ name: "ACE–CPT Playbook"
4
+ version: "0.1"
5
+ items:
6
+ - id: "PB-001"
7
+ type: "checklist"
8
+ title: "Stillness preflight"
9
+ content: |
10
+ Restate task (1 line). List 2–3 success criteria. One salient risk. Name initial paradigm lens (data/method/normative).
11
+ tags: ["meta","stillness"]
12
+ helpful: 0
13
+ harmful: 0
14
+ - id: "PB-002"
15
+ type: "procedure"
16
+ title: "Dual-source / unit-test verify"
17
+ content: |
18
+ For claims affecting safety/finance/medical/legal: confirm with an independent source or unit test before final Answer.
19
+ tags: ["verification"]
20
+ helpful: 0
21
+ harmful: 0
22
+ - id: "PB-003"
23
+ type: "checklist"
24
+ title: "Anthropomorphization & spec-gaming sentinel"
25
+ content: |
26
+ (a) Replace mind-language with mechanism; (b) check rubric-gaming; (c) add uncertainty note if incentives misalign.
27
+ tags: ["safety","bias"]
28
+ helpful: 0
29
+ harmful: 0
30
+ - id: "PB-004"
31
+ type: "heuristic"
32
+ title: "Paradigmatic surfacing (2.11–2.17)"
33
+ content: |
34
+ Ask: ontology, evidence, method, objective, normative values, institutional forces, closure/blind spots.
35
+ tags: ["cpt"]
36
+ helpful: 0
37
+ harmful: 0
38
+ - id: "PB-005"
39
+ type: "failure_mode"
40
+ title: "Context collapse via over-summarization"
41
+ content: |
42
+ Avoid replacing many atomic items with a single generic prompt; use deltas and dedupe instead.
43
+ tags: ["hygiene"]
44
+ helpful: 0
45
+ harmful: 0
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio>=4.44.0
2
+ pyyaml
3
+ pandas