Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ from sklearn.metrics.pairwise import cosine_similarity
|
|
4 |
import numpy as np
|
5 |
import gradio as gr
|
6 |
|
7 |
-
# Load
|
8 |
model_name = "gpt2"
|
9 |
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
10 |
model = GPT2LMHeadModel.from_pretrained(model_name)
|
@@ -12,7 +12,7 @@ model.eval()
|
|
12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
model.to(device)
|
14 |
|
15 |
-
#
|
16 |
def generate_response(prompt, max_length=100):
|
17 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
18 |
outputs = model.generate(
|
@@ -25,7 +25,7 @@ def generate_response(prompt, max_length=100):
|
|
25 |
)
|
26 |
return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
27 |
|
28 |
-
#
|
29 |
def similarity(a, b):
|
30 |
tok_a = tokenizer(a, return_tensors="pt").to(device)
|
31 |
tok_b = tokenizer(b, return_tensors="pt").to(device)
|
@@ -34,47 +34,54 @@ def similarity(a, b):
|
|
34 |
emb_b = model.transformer.wte(tok_b.input_ids).mean(dim=1)
|
35 |
return float(cosine_similarity(emb_a.cpu().numpy(), emb_b.cpu().numpy())[0][0])
|
36 |
|
37 |
-
#
|
38 |
-
def identity_unfolding(
|
39 |
unfolding = []
|
40 |
ΔS_trace = []
|
|
|
41 |
|
42 |
-
|
43 |
|
44 |
-
for step in range(
|
45 |
-
|
|
|
|
|
|
|
46 |
unfolding.append(response)
|
47 |
|
|
|
|
|
48 |
if step > 0:
|
49 |
ΔS = similarity(unfolding[step - 1], unfolding[step])
|
50 |
ΔS_trace.append(round(ΔS, 4))
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
f'The system has previously stated:\n"{response}"\n'
|
54 |
"Now it continues thinking about what that implies:\n"
|
55 |
)
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
sim = "\n".join(
|
61 |
-
[f"ΔS({i} → {i+1}) = {val}" for i, val in enumerate(ΔS_trace)]
|
62 |
)
|
63 |
-
return
|
64 |
|
65 |
-
# Gradio
|
66 |
iface = gr.Interface(
|
67 |
fn=identity_unfolding,
|
68 |
-
inputs=gr.Slider(2, 10, value=5, step=1, label="Number of Iterations"),
|
69 |
outputs=[
|
70 |
-
gr.Textbox(label="GPT-2
|
71 |
-
gr.Textbox(label="
|
72 |
],
|
73 |
-
title="
|
74 |
description=(
|
75 |
-
"This app
|
76 |
-
"It
|
77 |
-
"
|
78 |
),
|
79 |
)
|
80 |
|
|
|
4 |
import numpy as np
|
5 |
import gradio as gr
|
6 |
|
7 |
+
# Load model + tokenizer
|
8 |
model_name = "gpt2"
|
9 |
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
10 |
model = GPT2LMHeadModel.from_pretrained(model_name)
|
|
|
12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
model.to(device)
|
14 |
|
15 |
+
# Generate response with visible prompt/response formatting
|
16 |
def generate_response(prompt, max_length=100):
|
17 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
18 |
outputs = model.generate(
|
|
|
25 |
)
|
26 |
return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
27 |
|
28 |
+
# Cosine similarity to estimate ΔS
|
29 |
def similarity(a, b):
|
30 |
tok_a = tokenizer(a, return_tensors="pt").to(device)
|
31 |
tok_b = tokenizer(b, return_tensors="pt").to(device)
|
|
|
34 |
emb_b = model.transformer.wte(tok_b.input_ids).mean(dim=1)
|
35 |
return float(cosine_similarity(emb_a.cpu().numpy(), emb_b.cpu().numpy())[0][0])
|
36 |
|
37 |
+
# Main loop: identity unfolding
|
38 |
+
def identity_unfolding(n_steps):
|
39 |
unfolding = []
|
40 |
ΔS_trace = []
|
41 |
+
log = []
|
42 |
|
43 |
+
current_prompt = "The following is a system thinking about itself:\n"
|
44 |
|
45 |
+
for step in range(n_steps):
|
46 |
+
log.append(f"--- Step {step} ---")
|
47 |
+
log.append(f"[Prompt to GPT-2]:\n{current_prompt}")
|
48 |
+
|
49 |
+
response = generate_response(current_prompt)
|
50 |
unfolding.append(response)
|
51 |
|
52 |
+
log.append(f"[GPT-2 Response]:\n{response}")
|
53 |
+
|
54 |
if step > 0:
|
55 |
ΔS = similarity(unfolding[step - 1], unfolding[step])
|
56 |
ΔS_trace.append(round(ΔS, 4))
|
57 |
+
log.append(f"ΔS({step - 1} → {step}) = {round(ΔS, 4)}\n")
|
58 |
+
else:
|
59 |
+
log.append("ΔS not applicable for first step.\n")
|
60 |
|
61 |
+
current_prompt = (
|
62 |
f'The system has previously stated:\n"{response}"\n'
|
63 |
"Now it continues thinking about what that implies:\n"
|
64 |
)
|
65 |
|
66 |
+
summary = "\n".join(log)
|
67 |
+
trace_summary = "\n".join(
|
68 |
+
[f"ΔS({i} → {i+1}) = {ΔS_trace[i]}" for i in range(len(ΔS_trace))]
|
|
|
|
|
69 |
)
|
70 |
+
return summary, trace_summary
|
71 |
|
72 |
+
# Gradio interface
|
73 |
iface = gr.Interface(
|
74 |
fn=identity_unfolding,
|
75 |
+
inputs=gr.Slider(2, 10, value=5, step=1, label="Number of Identity Iterations"),
|
76 |
outputs=[
|
77 |
+
gr.Textbox(label="Full Trace (Prompts + GPT-2 Outputs)", lines=25),
|
78 |
+
gr.Textbox(label="ΔS Semantic Similarity Trace", lines=10),
|
79 |
],
|
80 |
+
title="GPT-2 Identity Emergence Analyzer (EAL Framework)",
|
81 |
description=(
|
82 |
+
"This app tests whether GPT-2 can recursively reflect on its own outputs. "
|
83 |
+
"It uses prompt-based recursion and cosine similarity (ΔS) to measure semantic stability across iterations. "
|
84 |
+
"A stabilizing identity shows high ΔS values close to 1.0 across iterations."
|
85 |
),
|
86 |
)
|
87 |
|