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,8 +12,16 @@ 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(
|
| 19 |
**inputs,
|
|
@@ -23,22 +31,27 @@ def generate_response(prompt, max_length=100):
|
|
| 23 |
temperature=0.9,
|
| 24 |
top_p=0.95,
|
| 25 |
)
|
| 26 |
-
|
|
|
|
|
|
|
| 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)
|
| 32 |
with torch.no_grad():
|
| 33 |
emb_a = model.transformer.wte(tok_a.input_ids).mean(dim=1)
|
| 34 |
emb_b = model.transformer.wte(tok_b.input_ids).mean(dim=1)
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# Main
|
| 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 |
|
|
@@ -67,7 +80,8 @@ def identity_unfolding(n_steps):
|
|
| 67 |
trace_summary = "\n".join(
|
| 68 |
[f"ΔS({i} → {i+1}) = {ΔS_trace[i]}" for i in range(len(ΔS_trace))]
|
| 69 |
)
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
# Gradio interface
|
| 73 |
iface = gr.Interface(
|
|
@@ -76,12 +90,13 @@ iface = gr.Interface(
|
|
| 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 |
-
"
|
| 85 |
),
|
| 86 |
)
|
| 87 |
|
|
|
|
| 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)
|
|
|
|
| 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) # Console log (local)
|
| 20 |
+
debug_log.append(str(msg)) # Collect for UI
|
| 21 |
+
|
| 22 |
+
# Generate a 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,
|
|
|
|
| 31 |
temperature=0.9,
|
| 32 |
top_p=0.95,
|
| 33 |
)
|
| 34 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
| 35 |
+
debug(f"Generated output:\n{result}")
|
| 36 |
+
return result
|
| 37 |
|
| 38 |
+
# Compute cosine similarity of mean token embeddings
|
| 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 between outputs: {score}")
|
| 47 |
+
return score
|
| 48 |
|
| 49 |
+
# Main identity unfolding loop
|
| 50 |
def identity_unfolding(n_steps):
|
| 51 |
unfolding = []
|
| 52 |
ΔS_trace = []
|
| 53 |
log = []
|
| 54 |
+
debug_log.clear()
|
| 55 |
|
| 56 |
current_prompt = "The following is a system thinking about itself:\n"
|
| 57 |
|
|
|
|
| 80 |
trace_summary = "\n".join(
|
| 81 |
[f"ΔS({i} → {i+1}) = {ΔS_trace[i]}" for i in range(len(ΔS_trace))]
|
| 82 |
)
|
| 83 |
+
debug_output = "\n".join(debug_log)
|
| 84 |
+
return summary, trace_summary, debug_output
|
| 85 |
|
| 86 |
# Gradio interface
|
| 87 |
iface = gr.Interface(
|
|
|
|
| 90 |
outputs=[
|
| 91 |
gr.Textbox(label="Full Trace (Prompts + GPT-2 Outputs)", lines=25),
|
| 92 |
gr.Textbox(label="ΔS Semantic Similarity Trace", lines=10),
|
| 93 |
+
gr.Textbox(label="Debug Log", lines=10),
|
| 94 |
],
|
| 95 |
title="GPT-2 Identity Emergence Analyzer (EAL Framework)",
|
| 96 |
description=(
|
| 97 |
"This app tests whether GPT-2 can recursively reflect on its own outputs. "
|
| 98 |
"It uses prompt-based recursion and cosine similarity (ΔS) to measure semantic stability across iterations. "
|
| 99 |
+
"Now includes a visible debug log."
|
| 100 |
),
|
| 101 |
)
|
| 102 |
|