neuralworm commited on
Commit
bd61488
·
1 Parent(s): 1189ea8

update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -68
app.py CHANGED
@@ -1,121 +1,169 @@
1
  import torch
2
- from transformers import GPT2LMHeadModel, GPT2Tokenizer
3
  from sklearn.metrics.pairwise import cosine_similarity
 
4
  import numpy as np
5
  import gradio as gr
6
-
7
- # Load GPT-2 and tokenizer
8
- model_name = "gpt2"
9
- tokenizer = GPT2Tokenizer.from_pretrained(model_name)
10
- model = GPT2LMHeadModel.from_pretrained(model_name)
 
 
 
 
11
  model.eval()
12
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
  model.to(device)
14
 
15
- # Debug log list
 
16
  debug_log = []
17
 
18
  def debug(msg):
19
  print(msg)
20
  debug_log.append(str(msg))
21
 
22
- # Generate GPT-2 response
23
- def generate_response(prompt, max_length=100):
 
 
 
 
 
 
 
24
  debug(f"Generating response for prompt:\n{prompt}")
25
  inputs = tokenizer(prompt, return_tensors="pt").to(device)
26
- outputs = model.generate(
27
- **inputs,
28
- max_length=len(inputs["input_ids"][0]) + max_length,
29
- pad_token_id=tokenizer.eos_token_id,
30
- do_sample=True,
31
- temperature=0.9,
32
- top_p=0.95,
33
- )
34
- result = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
35
- debug(f"Response:\n{result}")
36
- return result
37
-
38
- # Cosine similarity as ΔS proxy
 
 
 
39
  def similarity(a, b):
 
 
40
  tok_a = tokenizer(a, return_tensors="pt").to(device)
41
  tok_b = tokenizer(b, return_tensors="pt").to(device)
42
  with torch.no_grad():
43
  emb_a = model.transformer.wte(tok_a.input_ids).mean(dim=1)
44
  emb_b = model.transformer.wte(tok_b.input_ids).mean(dim=1)
45
- score = float(cosine_similarity(emb_a.cpu().numpy(), emb_b.cpu().numpy())[0][0])
46
- debug(f"Similarity: {score}")
47
- return score
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- # Dual unfolding: I (self-view), ¬I (contradiction)
50
  def dual_identity_unfolding(n_steps):
51
  I_trace, not_I_trace = [], []
52
  ΔS_I, ΔS_not_I, ΔS_cross = [], [], []
53
  debug_log.clear()
54
- log = []
55
 
56
- current_I = "The system begins to think about itself:\n"
57
- current_not_I = "The system begins to contradict its previous thoughts:\n"
58
 
59
  for step in range(n_steps):
60
- log.append(f"--- Step {step} ---")
 
 
61
 
62
- I = generate_response(current_I)
63
- not_I = generate_response(current_not_I)
64
 
65
  I_trace.append(I)
66
  not_I_trace.append(not_I)
67
 
68
- log.append(f"[Iₙ]:\n{I}\n")
69
- log.append(f"Iₙ]:\n{not_I}\n")
70
 
71
  if step > 0:
72
- s_I = similarity(I_trace[step - 1], I_trace[step])
73
- s_not_I = similarity(not_I_trace[step - 1], not_I_trace[step])
74
- s_cross = similarity(I_trace[step], not_I_trace[step])
75
-
76
- ΔS_I.append(round(s_I, 4))
77
- ΔS_not_I.append(round(s_not_I, 4))
78
- ΔS_cross.append(round(s_cross, 4))
79
-
80
- log.append(f"ΔS(I{step - 1} → I{step}) = {s_I}")
81
- log.append(f"ΔS(¬I{step - 1} → ¬I{step}) = {s_not_I}")
82
- log.append(f"ΔS(I{step} ↔ ¬I{step}) = {s_cross}\n")
83
  else:
84
- log.append("ΔS not applicable for first step.\n")
 
 
85
 
86
- current_I = f'The system previously said:\n"{I}"\nNow it reflects further:\n'
87
- current_not_I = f'The system previously said:\n"{I}"\nNow it contradicts itself:\n'
 
 
 
88
 
89
- result_log = "\n".join(log)
90
- debug_output = "\n".join(debug_log)
91
-
92
- I_out = "\n\n".join([f"I{n}: {txt}" for n, txt in enumerate(I_trace)])
93
- not_I_out = "\n\n".join([f"¬I{n}: {txt}" for n, txt in enumerate(not_I_trace)])
94
 
95
  ΔS_out = "\n".join([
96
- f"Step {i}: ΔS(I) = {ΔS_I[i]}, ΔS(¬I) = {ΔS_not_I[i]}, ΔS Cross = {ΔS_cross[i]}"
97
- for i in range(len(ΔS_I))
98
  ])
99
 
100
- return I_out, not_I_out, ΔS_out, debug_output
 
 
 
 
 
 
101
 
102
- # Gradio UI
103
  iface = gr.Interface(
104
  fn=dual_identity_unfolding,
105
- inputs=gr.Slider(2, 10, value=4, step=1, label="Number of Identity Steps"),
106
  outputs=[
107
- gr.Textbox(label="Iₙ (Identity Trace)", lines=20),
108
- gr.Textbox(label="¬Iₙ (Antithesis Trace)", lines=20),
109
- gr.Textbox(label="ΔS Trace", lines=12),
110
  gr.Textbox(label="Debug Log", lines=10),
 
111
  ],
112
- title="GPT-2 Dual Identity Analyzer (EAL Framework)",
113
- description=(
114
- "This app evaluates whether GPT-2 can form a stable identity by recursively reflecting "
115
- "on its own outputs (Iₙ), and simultaneously handle contradictions (¬Iₙ). "
116
- "ΔS tracks convergence, oscillation, and semantic symmetry."
117
- ),
118
  )
119
 
120
  if __name__ == "__main__":
121
- iface.launch()
 
1
  import torch
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
  from sklearn.metrics.pairwise import cosine_similarity
4
+ from sklearn.cluster import KMeans
5
  import numpy as np
6
  import gradio as gr
7
+ import matplotlib.pyplot as plt
8
+ import seaborn as sns
9
+ import networkx as nx
10
+ import io
11
+ import base64
12
+
13
+ model_name = "EleutherAI/gpt-neo-1.3B"
14
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
15
+ model = AutoModelForCausalLM.from_pretrained(model_name)
16
  model.eval()
17
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
18
  model.to(device)
19
 
20
+ max_tokens = 900
21
+ max_gen_length = 100
22
  debug_log = []
23
 
24
  def debug(msg):
25
  print(msg)
26
  debug_log.append(str(msg))
27
 
28
+ def trim_prompt(prompt, max_tokens=max_tokens):
29
+ tokens = tokenizer.encode(prompt, add_special_tokens=False)
30
+ if len(tokens) > max_tokens:
31
+ debug(f"[!] Trimming prompt from {len(tokens)} to {max_tokens} tokens.")
32
+ tokens = tokens[-max_tokens:]
33
+ return tokenizer.decode(tokens)
34
+
35
+ def generate_response(prompt):
36
+ prompt = trim_prompt(prompt)
37
  debug(f"Generating response for prompt:\n{prompt}")
38
  inputs = tokenizer(prompt, return_tensors="pt").to(device)
39
+ try:
40
+ outputs = model.generate(
41
+ **inputs,
42
+ max_length=min(len(inputs["input_ids"][0]) + max_gen_length, 1024),
43
+ pad_token_id=tokenizer.eos_token_id,
44
+ do_sample=True,
45
+ temperature=0.9,
46
+ top_p=0.95,
47
+ )
48
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
49
+ debug(f"Response:\n{result}")
50
+ return result
51
+ except Exception as e:
52
+ debug(f"Error during generation: {e}")
53
+ return "[Generation failed]"
54
+
55
  def similarity(a, b):
56
+ if not a.strip() or not b.strip():
57
+ return 0.0
58
  tok_a = tokenizer(a, return_tensors="pt").to(device)
59
  tok_b = tokenizer(b, return_tensors="pt").to(device)
60
  with torch.no_grad():
61
  emb_a = model.transformer.wte(tok_a.input_ids).mean(dim=1)
62
  emb_b = model.transformer.wte(tok_b.input_ids).mean(dim=1)
63
+ return float(cosine_similarity(emb_a.cpu().numpy(), emb_b.cpu().numpy())[0][0])
64
+
65
+ def make_heatmap(matrix, title):
66
+ fig, ax = plt.subplots(figsize=(8, 6))
67
+ sns.heatmap(matrix, annot=True, cmap="coolwarm", ax=ax)
68
+ ax.set_title(title)
69
+ buf = io.BytesIO()
70
+ plt.tight_layout()
71
+ plt.savefig(buf, format='png')
72
+ plt.close(fig)
73
+ buf.seek(0)
74
+ return base64.b64encode(buf.read()).decode()
75
+
76
+ def build_similarity_graph(texts):
77
+ G = nx.Graph()
78
+ for i, text_i in enumerate(texts):
79
+ for j, text_j in enumerate(texts):
80
+ if i < j:
81
+ sim = similarity(text_i, text_j)
82
+ if sim > 0.90:
83
+ G.add_edge(f'T{i}', f'T{j}', weight=sim)
84
+ return G
85
+
86
+ def get_embeddings(texts):
87
+ with torch.no_grad():
88
+ embeddings = []
89
+ for t in texts:
90
+ ids = tokenizer(t, return_tensors='pt', truncation=True).to(device)
91
+ emb = model.transformer.wte(ids.input_ids).mean(dim=1)
92
+ embeddings.append(emb.cpu().numpy()[0])
93
+ return np.array(embeddings)
94
+
95
+ def cluster_texts(texts, n_clusters=2):
96
+ embs = get_embeddings(texts)
97
+ kmeans = KMeans(n_clusters=n_clusters)
98
+ labels = kmeans.fit_predict(embs)
99
+ return labels
100
 
 
101
  def dual_identity_unfolding(n_steps):
102
  I_trace, not_I_trace = [], []
103
  ΔS_I, ΔS_not_I, ΔS_cross = [], [], []
104
  debug_log.clear()
 
105
 
106
+ I_state = "The system reflects: 'I am...'"
107
+ not_I_state = "Explain why the claim 'I am...' might be false."
108
 
109
  for step in range(n_steps):
110
+ debug(f"\n=== Step {step} ===")
111
+ I_prompt = I_state + "\nElaborate this claim."
112
+ not_I_prompt = f"Refute or challenge the claim: \"{I_state}\"\nPresent a fundamental contradiction."
113
 
114
+ I = generate_response(I_prompt)
115
+ not_I = generate_response(not_I_prompt)
116
 
117
  I_trace.append(I)
118
  not_I_trace.append(not_I)
119
 
120
+ I_state = "Earlier it stated: " + I
121
+ not_I_state = "Counterclaim to: " + I
122
 
123
  if step > 0:
124
+ ΔS_I.append(round(similarity(I_trace[-2], I_trace[-1]), 4))
125
+ ΔS_not_I.append(round(similarity(not_I_trace[-2], not_I_trace[-1]), 4))
126
+ ΔS_cross.append(round(similarity(I_trace[-1], not_I_trace[-1]), 4))
 
 
 
 
 
 
 
 
127
  else:
128
+ ΔS_I.append(None)
129
+ ΔS_not_I.append(None)
130
+ ΔS_cross.append(round(similarity(I_trace[-1], not_I_trace[-1]), 4))
131
 
132
+ all_texts = I_trace + not_I_trace
133
+ sim_matrix = np.zeros((len(all_texts), len(all_texts)))
134
+ for i in range(len(all_texts)):
135
+ for j in range(len(all_texts)):
136
+ sim_matrix[i][j] = similarity(all_texts[i], all_texts[j])
137
 
138
+ heatmap = make_heatmap(sim_matrix, "Similarity Matrix (I ∪ ¬I)")
139
+ clusters = cluster_texts(all_texts)
 
 
 
140
 
141
  ΔS_out = "\n".join([
142
+ f"Step {i}: ΔS(I)={ΔS_I[i]} ΔS(¬I)={ΔS_not_I[i]} ΔS Cross={ΔS_cross[i]}"
143
+ for i in range(n_steps)
144
  ])
145
 
146
+ I_out = "\n\n".join([f"I{i} [C{clusters[i]}]: {t}" for i, t in enumerate(I_trace)])
147
+ not_I_out = "\n\n".join([f"¬I{i} [C{clusters[len(I_trace)+i]}]: {t}" for i, t in enumerate(not_I_trace)])
148
+ debug_output = "\n".join(debug_log)
149
+
150
+ img_html = f"<img src='data:image/png;base64,{heatmap}'/>"
151
+
152
+ return I_out, not_I_out, ΔS_out, debug_output, img_html
153
 
 
154
  iface = gr.Interface(
155
  fn=dual_identity_unfolding,
156
+ inputs=gr.Slider(2, 10, value=5, step=1, label="Number of Steps"),
157
  outputs=[
158
+ gr.Textbox(label="Identity Trace (Iₙ)", lines=15),
159
+ gr.Textbox(label="Contradiction Trace (¬Iₙ)", lines=15),
160
+ gr.Textbox(label="ΔS Similarity Trace", lines=8),
161
  gr.Textbox(label="Debug Log", lines=10),
162
+ gr.HTML(label="Similarity Heatmap")
163
  ],
164
+ title="GPT Identity Analyzer + Antithesis (EAL Mode)",
165
+ description="Analyzes the self-consistency and contradiction emergence in GPT-Neo using EAL-inspired fixed-point tracing, clustering, and cosine similarity."
 
 
 
 
166
  )
167
 
168
  if __name__ == "__main__":
169
+ iface.launch()