Phoenix21 commited on
Commit
cddf7c8
·
verified ·
1 Parent(s): d13742d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -51
app.py CHANGED
@@ -55,79 +55,55 @@ def analyze_world_model(model_name, dataset_key, num_samples=25):
55
  n_clusters = 5
56
  kmeans = KMeans(n_clusters=n_clusters, n_init=10).fit(all_hidden_states)
57
  state_assignments = kmeans.labels_
 
 
58
 
59
- # Step C: State Elaboration Logic
60
  cluster_texts = collections.defaultdict(list)
61
  for idx, cluster_id in enumerate(state_assignments):
62
  cluster_texts[cluster_id].append(input_snippets[idx])
63
 
64
- # Initialize Gemini
65
- gemini_model = genai.GenerativeModel('gemini-2.5-flash')
 
 
66
  state_info = "## 🧠 Newtonian State Interpretation\n"
 
67
 
 
68
  for cluster_id in range(n_clusters):
69
  snippets = cluster_texts[cluster_id]
70
- context_payload = "\n".join([f"- {s}" for s in snippets[:4]])
71
-
72
- # Proper prompt engineering to decode the 'Equivalence Class'
73
- # prompt = f"""
74
- # Analyze these text snippets from the '{dataset_key}' dataset that fall into the same latent state cluster.
75
- # Identify the CORE structural or semantic theme (e.g., 'Historical Narrative', 'Technical Development', 'Numerical Lists').
76
-
77
- # Text Snippets:
78
- # {context_payload}
79
 
80
- # Format your response exactly as:
81
- # **State S{cluster_id} [Label]**: [One sentence explanation of the shared logic/context].
82
- # """
83
  prompt = f"""
84
- Act as a Mechanistic Interpretability Researcher. You are decoding the latent 'Newtonian' world model
85
- within a Transformer for the '{dataset_key}' dataset.
86
 
87
- You are analyzing Cluster S{cluster_id}. This cluster represents a specific 'Equivalence Class' where
88
- the model treats different sequences as functionally identical.
89
 
90
- ### RAW DATASET SNIPPETS FOR CLUSTER S{cluster_id}:
91
  {context_payload}
92
 
93
- ### YOUR MANDATORY TASK:
94
- Provide a comprehensive, high-fidelity analysis for THIS CLUSTER ALONE. You must deliver the same
95
- level of depth as seen in previous successful state interpretations.
96
 
97
- ### REQUIRED OUTPUT FORMAT:
98
  **State S{cluster_id} [Structural State Label]**
99
-
100
  - **Internal World Model**: Explain the CORE 'Law' or 'Invariant' here. What logical map has the model activated?
101
  Describe how this state interconnects lore, timelines, or mechanics into a single 'Coherent World State'.
102
-
103
- - **Dataset Sensor**: List the specific 'Triggers' that push the model into this state.
104
- Identify: (1) Proper Nouns/Identifiers, (2) Domain-Specific Terminology, (3) Syntax Patterns (e.g., lists, headers).
105
-
106
- - **Predictive Function**: Explain how being in this state constrains the model's future.
107
- What next-tokens are now 'Biased' or 'Anticipated'? How does this state filter out irrelevant topics?
108
-
109
- ---
110
- (Ensure your response is dense, professional, and strictly follows the bolded sections above.)
111
  """
112
 
113
  try:
114
- response = gemini_model.generate_content(prompt)
115
- state_info += response.text.strip() + "\n\n"
116
- except Exception:
117
- # Fallback if API fails
118
- summary = " | ".join([s[:40] + "..." for s in snippets[:2]])
119
- state_info += f"**State S{cluster_id}**: Context: *{summary}*\n\n"
120
- # state_info = "### 🧠 State Interpretation & Dataset Mapping\n"
121
- # cluster_texts = collections.defaultdict(list)
122
- # for idx, cluster_id in enumerate(state_assignments):
123
- # cluster_texts[cluster_id].append(input_snippets[idx])
124
-
125
- # for cluster_id in range(n_clusters):
126
- # snippets = cluster_texts[cluster_id]
127
- # # Identify common tokens/attributes that represent this state
128
- # summary = " | ".join([s[:40] + "..." for s in snippets[:2]])
129
- # state_info += f"**State S{cluster_id}**: Representing context such as: *{summary}*\n\n"
130
-
131
  # Step D: DFA Reconstruction
132
  G = nx.DiGraph()
133
  for i in range(len(state_assignments) - 1):
 
55
  n_clusters = 5
56
  kmeans = KMeans(n_clusters=n_clusters, n_init=10).fit(all_hidden_states)
57
  state_assignments = kmeans.labels_
58
+
59
+ # STEP C: Iterative Newtonian Interpretation
60
 
 
61
  cluster_texts = collections.defaultdict(list)
62
  for idx, cluster_id in enumerate(state_assignments):
63
  cluster_texts[cluster_id].append(input_snippets[idx])
64
 
65
+ # Initialize Gemini model
66
+ gemini_model = genai.GenerativeModel('gemini-1.5-flash')
67
+
68
+ # We start with a clean header
69
  state_info = "## 🧠 Newtonian State Interpretation\n"
70
+ state_info += "Each state represents a discovered *Equivalence Class* where the model treats different data as functionally identical for its internal world model.\n\n"
71
 
72
+ # LOOP: Call Gemini for EACH state individually to ensure equal depth
73
  for cluster_id in range(n_clusters):
74
  snippets = cluster_texts[cluster_id]
75
+ # Provide a richer payload for better structural laws
76
+ context_payload = "\n".join([f"- {s}" for s in snippets[:8]])
 
 
 
 
 
 
 
77
 
78
+ # IMPROVED PROMPT: Forces individual focus on ONE state at a time
 
 
79
  prompt = f"""
80
+ Act as a Mechanistic Interpretability Researcher. You are reverse-engineering Cluster S{cluster_id}
81
+ from the '{dataset_key}' dataset.
82
 
83
+ The model has grouped these snippets into an 'Equivalence Class'—an internal map where it
84
+ applies the same logical laws to diverse data.
85
 
86
+ ### RAW SNIPPETS FOR S{cluster_id}:
87
  {context_payload}
88
 
89
+ ### YOUR RESEARCH TASK:
90
+ Analyze this cluster with high-fidelity Newtonian depth. Focus ONLY on S{cluster_id}.
 
91
 
92
+ ### REQUIRED OUTPUT FORMAT (Strictly Follow):
93
  **State S{cluster_id} [Structural State Label]**
 
94
  - **Internal World Model**: Explain the CORE 'Law' or 'Invariant' here. What logical map has the model activated?
95
  Describe how this state interconnects lore, timelines, or mechanics into a single 'Coherent World State'.
96
+ - **Dataset Sensor**: List the specific 'Triggers' (Proper Nouns, Terminology, Syntax) that push the model here.
97
+ - **Predictive Function**: Explain how being in this state constrains the model's future tokens.
98
+ What next-tokens are 'Biased' or 'Anticipated'?
 
 
 
 
 
 
99
  """
100
 
101
  try:
102
+ # Iterative generation ensures Gemini doesn't 'lazy-load' the middle states
103
+ response = gemini_model.generate_content(prompt, generation_config={"temperature": 0.2})
104
+ state_info += response.text.strip() + "\n\n---\n\n"
105
+ except Exception as e:
106
+ state_info += f"**State S{cluster_id} [API Error]**: Analysis failed for this state. (Error: {str(e)})\n\n---\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
107
  # Step D: DFA Reconstruction
108
  G = nx.DiGraph()
109
  for i in range(len(state_assignments) - 1):