Commit
·
1c78319
1
Parent(s):
e71d4de
Fix Space config and update weights
Browse files- .gitattributes +0 -33
- README.md +116 -6
- app.py +3 -1
- requirements.txt +1 -1
.gitattributes
CHANGED
|
@@ -1,35 +1,2 @@
|
|
| 1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 2 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
|
@@ -1,14 +1,124 @@
|
|
| 1 |
---
|
| 2 |
title: Cognitive Proxy
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: cc-by-4.0
|
| 11 |
-
|
| 12 |
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Cognitive Proxy
|
| 3 |
+
emoji: 🧠
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.19.2
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: cc-by-4.0
|
| 11 |
+
model: TinyLlama/TinyLlama-1.1B-Chat-v1.0
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# Brain Coordinates for Language Models
|
| 15 |
+
|
| 16 |
+
[](https://creativecommons.org/licenses/by/4.0/)
|
| 17 |
+
[](https://huggingface.co/spaces/ai-nthusiast/cognitive-proxy)
|
| 18 |
+
[](https://arxiv.org)
|
| 19 |
+
|
| 20 |
+
**MEG Phase-Locking as a Steering Geometry for LLMs**
|
| 21 |
+
|
| 22 |
+
**Author**: Sandro Andric
|
| 23 |
+
|
| 24 |
+
## Overview
|
| 25 |
+
|
| 26 |
+
We propose using human brain activity not as a score to optimize, but as a **coordinate system** for reading and steering model states. From MEG recordings of 21 subjects listening to naturalistic speech, we construct a brain atlas of Phase-Locking Value (PLV) patterns for 2,113 words and train lightweight adapters that project frozen LLM hidden states into this space.
|
| 27 |
+
|
| 28 |
+
**Key Results**:
|
| 29 |
+
- **Function-Content Axis**: Dominant axis (61% variance) separating syntactic binding from semantic access
|
| 30 |
+
- **Cross-Architecture Transfer**: GPT-2 (d=1.59) and TinyLlama (d=1.40), both p < 10^-22
|
| 31 |
+
- **Bidirectional Steering**: Control generation along brain-derived axes (p < 0.0001)
|
| 32 |
+
- **Scale-Dependent Structure**: Agency axis transfers to larger model only (d=-0.82)
|
| 33 |
+
|
| 34 |
+
---
|
| 35 |
+
|
| 36 |
+
## 1. Installation
|
| 37 |
+
|
| 38 |
+
Requires Python 3.9+ and PyTorch.
|
| 39 |
+
|
| 40 |
+
```bash
|
| 41 |
+
# Install dependencies
|
| 42 |
+
pip install torch transformers scikit-learn pandas scipy numpy streamlit plotly sentencepiece
|
| 43 |
+
|
| 44 |
+
# Ensure local modules are Importable
|
| 45 |
+
export PYTHONPATH=$PYTHONPATH:$(pwd)/src
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
## 2. Reproduction Pipeline
|
| 49 |
+
|
| 50 |
+
To reproduce the scientific results from scratch, execute the following steps in order.
|
| 51 |
+
|
| 52 |
+
### Step 1: Build the Cognitive Atlas
|
| 53 |
+
Constructs the "Brain Dictionary" from the MEG-MASC dataset.
|
| 54 |
+
* **Input**: MEG-MASC BIDS data (configured in `DATA_ROOT`).
|
| 55 |
+
* **Output**: `results/final_atlas_256.pkl` (and `_vocab.pkl`).
|
| 56 |
+
|
| 57 |
+
```bash
|
| 58 |
+
python experiments/build_clustered_atlas.py
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
### Step 2: Interpret the Axis (Phase 10.1)
|
| 62 |
+
Analyzes the semantics of the discovered brain clusters.
|
| 63 |
+
* **Output**: Correlation stats showing Cluster A = Function, Cluster B = Content.
|
| 64 |
+
|
| 65 |
+
```bash
|
| 66 |
+
python experiments/analyze_axis_correlations.py \
|
| 67 |
+
--pos-cluster Cluster_2 \
|
| 68 |
+
--neg-cluster Cluster_3
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
### Step 3: Train the Adapter
|
| 72 |
+
Trains the MLP mapping `GPT-2 Hidden -> Brain PLV`.
|
| 73 |
+
* **Input**: GPT-2 Tokenizer + Atlas.
|
| 74 |
+
* **Output**: `results/gpt2_adapter.pt`.
|
| 75 |
+
|
| 76 |
+
```bash
|
| 77 |
+
python experiments/train_gpt2_adapter.py
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
### Step 4: Validate the Alignment (Phase 10.2)
|
| 81 |
+
Performs the rigorous T-Test on held-out words.
|
| 82 |
+
* **Metric**: Cohen's d > 0.9 expected for Function vs Concrete.
|
| 83 |
+
|
| 84 |
+
```bash
|
| 85 |
+
python experiments/validate_adapter_stats.py \
|
| 86 |
+
--pos-cluster Cluster_2 \
|
| 87 |
+
--neg-cluster Cluster_3
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
### Step 5: Systematic Steering (Phase 10.3)
|
| 91 |
+
Generates text under "Neuro-Steering" conditions to measure causal effect.
|
| 92 |
+
|
| 93 |
+
```bash
|
| 94 |
+
python experiments/evaluate_steering_batch.py \
|
| 95 |
+
--pos-cluster Cluster_2 \
|
| 96 |
+
--neg-cluster Cluster_3 \
|
| 97 |
+
--alpha 50.0
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
---
|
| 101 |
+
|
| 102 |
+
## 3. Interactive Demo (Cognitive Proxy)
|
| 103 |
+
|
| 104 |
+
**Try it online**: [huggingface.co/spaces/ai-nthusiast/cognitive-proxy](https://huggingface.co/spaces/ai-nthusiast/cognitive-proxy)
|
| 105 |
+
|
| 106 |
+
Or run locally:
|
| 107 |
+
|
| 108 |
+
```bash
|
| 109 |
+
streamlit run src/ui/app_tinyllama_minimal.py
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
Features:
|
| 113 |
+
- **Compare**: See three generation variants side-by-side (semantic, baseline, syntactic)
|
| 114 |
+
- **Inspect**: Analyze text projection onto brain coordinate space with PLV visualization
|
| 115 |
+
- **Steer**: Manually control generation along the Function-Content axis
|
| 116 |
+
|
| 117 |
+
---
|
| 118 |
+
|
| 119 |
+
## 4. Directory Structure
|
| 120 |
+
|
| 121 |
+
* `src/`: Core libraries (`models`, `data`, `ui`).
|
| 122 |
+
* `experiments/`: Scientific scripts (Training, Validation).
|
| 123 |
+
* `results/`: Trained models (`.pt`) and atlases (`.pkl`).
|
| 124 |
+
* `artifacts/`: Project history, papers, and walkthroughs.
|
app.py
CHANGED
|
@@ -319,7 +319,8 @@ def generate_steered(prompt, alpha, max_tokens):
|
|
| 319 |
|
| 320 |
# Custom CSS to match Streamlit minimal design
|
| 321 |
custom_css = """
|
| 322 |
-
|
|
|
|
| 323 |
|
| 324 |
/* Global font */
|
| 325 |
.gradio-container, .gradio-container * {
|
|
@@ -414,6 +415,7 @@ textarea, input[type="text"] {
|
|
| 414 |
/* Remove gradio branding */
|
| 415 |
footer { display: none !important; }
|
| 416 |
.dark { display: none !important; }
|
|
|
|
| 417 |
"""
|
| 418 |
|
| 419 |
# Create interface
|
|
|
|
| 319 |
|
| 320 |
# Custom CSS to match Streamlit minimal design
|
| 321 |
custom_css = """
|
| 322 |
+
<style>
|
| 323 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&display=swap');
|
| 324 |
|
| 325 |
/* Global font */
|
| 326 |
.gradio-container, .gradio-container * {
|
|
|
|
| 415 |
/* Remove gradio branding */
|
| 416 |
footer { display: none !important; }
|
| 417 |
.dark { display: none !important; }
|
| 418 |
+
</style>
|
| 419 |
"""
|
| 420 |
|
| 421 |
# Create interface
|
requirements.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
gradio>=4.19.2
|
| 2 |
torch>=2.1.0
|
| 3 |
transformers>=4.36.0
|
| 4 |
-
numpy
|
| 5 |
scikit-learn>=1.3.0
|
| 6 |
plotly>=5.18.0
|
| 7 |
spaces
|
|
|
|
| 1 |
gradio>=4.19.2
|
| 2 |
torch>=2.1.0
|
| 3 |
transformers>=4.36.0
|
| 4 |
+
numpy<2.0.0
|
| 5 |
scikit-learn>=1.3.0
|
| 6 |
plotly>=5.18.0
|
| 7 |
spaces
|