File size: 2,822 Bytes
1d1182e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import gradio as gr

# Load GPT-2 and tokenizer
model_name = "gpt2"
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)
model.eval()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# Function to generate response
def generate_response(prompt, max_length=100):
    inputs = tokenizer(prompt, return_tensors="pt").to(device)
    outputs = model.generate(
        **inputs,
        max_length=len(inputs["input_ids"][0]) + max_length,
        pad_token_id=tokenizer.eos_token_id,
        do_sample=True,
        temperature=0.9,
        top_p=0.95,
    )
    return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()

# Semantic similarity via mean embeddings (ΔS approximation)
def similarity(a, b):
    tok_a = tokenizer(a, return_tensors="pt").to(device)
    tok_b = tokenizer(b, return_tensors="pt").to(device)
    with torch.no_grad():
        emb_a = model.transformer.wte(tok_a.input_ids).mean(dim=1)
        emb_b = model.transformer.wte(tok_b.input_ids).mean(dim=1)
    return float(cosine_similarity(emb_a.cpu().numpy(), emb_b.cpu().numpy())[0][0])

# Recursive identity loop (EAL-style unfolding)
def identity_unfolding(steps):
    unfolding = []
    ΔS_trace = []

    current_text = "The following is a system thinking about itself:\n"

    for step in range(steps):
        response = generate_response(current_text)
        unfolding.append(response)

        if step > 0:
            ΔS = similarity(unfolding[step - 1], unfolding[step])
            ΔS_trace.append(round(ΔS, 4))

        current_text = (
            f'The system has previously stated:\n"{response}"\n'
            "Now it continues thinking about what that implies:\n"
        )

    results = "\n\n---\n\n".join(
        [f"Step {i}: {txt}" for i, txt in enumerate(unfolding)]
    )
    sim = "\n".join(
        [f"ΔS({i}{i+1}) = {val}" for i, val in enumerate(ΔS_trace)]
    )
    return results, sim

# Gradio Interface
iface = gr.Interface(
    fn=identity_unfolding,
    inputs=gr.Slider(2, 10, value=5, step=1, label="Number of Iterations"),
    outputs=[
        gr.Textbox(label="GPT-2 Identity Trace", lines=20),
        gr.Textbox(label="Semantic ΔS Trace", lines=10),
    ],
    title="EAL Identity Tester for GPT-2",
    description=(
        "This app recursively prompts GPT-2 to reflect on its own output. "
        "It shows how close each iteration is to the previous one using a cosine-based ΔS metric. "
        "Use this to test if GPT-2 stabilizes around a semantically coherent self-representation."
    ),
)

if __name__ == "__main__":
    iface.launch()