File size: 8,911 Bytes
88f8604
 
 
 
 
e27aa8e
88f8604
c3e5765
88f8604
 
 
 
 
c3e5765
88f8604
 
 
 
 
c3e5765
88f8604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3e5765
88f8604
 
 
 
 
 
 
 
 
 
c3e5765
88f8604
 
c3e5765
 
88f8604
 
 
 
c3e5765
88f8604
c3e5765
88f8604
 
c3e5765
 
88f8604
 
 
c3e5765
 
88f8604
c3e5765
88f8604
c3e5765
 
88f8604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3e5765
88f8604
 
 
 
 
c3e5765
88f8604
 
 
 
 
c3e5765
88f8604
c3e5765
88f8604
 
 
 
 
 
 
c3e5765
88f8604
 
 
 
 
 
 
 
c3e5765
88f8604
c3e5765
88f8604
 
 
 
 
 
 
c3e5765
88f8604
 
 
 
 
 
c3e5765
88f8604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3e5765
 
 
88f8604
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# πŸ“š Antigravity Notebook

**A NotebookLM clone powered by Apple's CLaRa-7B-Instruct for infinite context reasoning**

Antigravity Notebook enables you to create "Notebooks" where you can upload multiple disparate sources (PDFs, URLs, Text) and have an AI reason across **all of them simultaneously** using CLaRa's latent compression technology.

## 🌟 Key Features

### The "Infinite Context" Strategy
- **16x Compression**: CLaRa compresses text into latent representations, reducing context usage by ~16x
- **Whole-Notebook Reasoning**: When all sources fit in context (32k tokens), the AI reads **EVERYTHING**
- **Smart Retrieval**: For larger notebooks, intelligently selects the most relevant sources
- **Multi-Modal Ingestion**: Support for PDFs, URLs, and plain text

### NotebookLM-Style Interface
- **Notebook Organization**: Group related sources into project notebooks
- **Source Management**: Easy upload, URL scraping, and text input
- **Memory Usage Meter**: Visual gauge showing context utilization
- **Citation Tracking**: See which sources were used for each response

## πŸ—οΈ Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Streamlit UI                         β”‚
β”‚  (NotebookLM-style interface with sidebar + chat)      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
                     ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   FastAPI Backend                       β”‚
β”‚                                                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Notebooks   β”‚  β”‚   Sources    β”‚  β”‚     Chat     β”‚ β”‚
β”‚  β”‚   Router     β”‚  β”‚   Router     β”‚  β”‚   Router     β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        ↓                          ↓              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   CLaRa-7B    β”‚      β”‚  ContextManager  β”‚   β”‚   Storage    β”‚
β”‚  (Compress &  β”‚      β”‚  (Whole-Context  β”‚   β”‚   Service    β”‚
β”‚   Generate)   β”‚      β”‚    Strategy)     β”‚   β”‚ (Tensors)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        ↓                          ↓                  ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     PostgreSQL                          β”‚
β”‚  (Notebooks β†’ Sources β†’ LatentTensors β†’ ChatMessages)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## πŸš€ Quick Start

### Prerequisites
- Python 3.9+
- Docker & Docker Compose (for PostgreSQL)
- CUDA-capable GPU (recommended, 16GB+ VRAM for CLaRa-7B)

### Installation

1. **Clone the repository**
```bash
git clone <your-repo-url>
cd antigravity-notebook
```

2. **Install dependencies**
```bash
pip install -r requirements.txt
```

3. **Set up environment**
```bash
cp .env.example .env
# Edit .env with your configuration
```

4. **Start PostgreSQL**
```bash
docker-compose up -d
```

5. **Initialize database**
```bash
python -m backend.database
```

6. **Start the backend**
```bash
python -m backend.main
```

7. **Start the frontend** (in a new terminal)
```bash
streamlit run frontend/app_notebook.py
```

8. **Open your browser**
- Frontend: http://localhost:8501
- API Docs: http://localhost:8000/docs

## πŸ“– Usage

### Creating a Notebook
1. Open the Streamlit UI
2. Click "Create New Notebook" in the sidebar
3. Enter a name and description
4. Click "Create Notebook"

### Adding Sources

**Upload PDF:**
1. Select your notebook
2. Go to "Add Source" β†’ "PDF" tab
3. Upload your PDF file
4. Wait for processing (CLaRa compression)

**Add URL:**
1. Select your notebook
2. Go to "Add Source" β†’ "URL" tab
3. Paste the URL
4. Optionally add a custom title
5. Click "Add URL"

**Add Text:**
1. Select your notebook
2. Go to "Add Source" β†’ "Text" tab
3. Enter a title and paste your text
4. Click "Add Text"

### Querying Your Notebook
1. Select a notebook with sources
2. Type your question in the chat input
3. The AI will reason across ALL your sources
4. View the response and see which sources were cited

## 🧠 How It Works

### Latent Compression
When you add a source:
1. Text is extracted (PDF/URL/Text)
2. Split into 2048-token chunks
3. Each chunk is compressed by CLaRa into a latent tensor (~128 tokens)
4. Latent tensors are saved to disk
5. Metadata is stored in PostgreSQL

### Context Management
When you query a notebook:
1. ContextManager fetches ALL latent tensors for the notebook
2. Calculates total token count
3. **If ≀ 32k tokens**: Stacks ALL tensors β†’ **Whole-Notebook Reasoning**
4. **If > 32k tokens**: Ranks tensors by relevance, selects top-N β†’ **Selective Retrieval**
5. Generates response using CLaRa with the selected context
6. Returns answer with source citations

## πŸ› οΈ API Endpoints

### Notebooks
- `POST /notebooks/` - Create notebook
- `GET /notebooks/` - List notebooks
- `GET /notebooks/{id}` - Get notebook details
- `GET /notebooks/{id}/stats` - Get context usage stats
- `PATCH /notebooks/{id}` - Update notebook
- `DELETE /notebooks/{id}` - Delete notebook

### Sources
- `POST /sources/notebooks/{id}/sources/upload` - Upload PDF
- `POST /sources/notebooks/{id}/sources/url` - Add URL
- `POST /sources/notebooks/{id}/sources/text` - Add text
- `GET /sources/notebooks/{id}/sources` - List sources
- `DELETE /sources/{id}` - Delete source

### Chat
- `POST /chat/notebooks/{id}/chat` - Query notebook
- `GET /chat/notebooks/{id}/messages` - Get chat history
- `DELETE /chat/notebooks/{id}/messages` - Clear chat history

## πŸ“Š Database Schema

```sql
notebooks
β”œβ”€β”€ id (UUID)
β”œβ”€β”€ name
β”œβ”€β”€ description
β”œβ”€β”€ created_at
└── updated_at

sources
β”œβ”€β”€ id (UUID)
β”œβ”€β”€ notebook_id (FK)
β”œβ”€β”€ source_type (pdf|url|text)
β”œβ”€β”€ filename
β”œβ”€β”€ url
β”œβ”€β”€ content_hash
└── metadata (JSONB)

latent_tensors
β”œβ”€β”€ id (UUID)
β”œβ”€β”€ source_id (FK)
β”œβ”€β”€ tensor_path
β”œβ”€β”€ segment_index
β”œβ”€β”€ token_count
└── metadata (JSONB)

chat_messages
β”œβ”€β”€ id (UUID)
β”œβ”€β”€ notebook_id (FK)
β”œβ”€β”€ role (user|assistant)
β”œβ”€β”€ content
└── sources_used (JSONB)
```

## βš™οΈ Configuration

Edit `.env` to configure:

```env
# Database
POSTGRES_USER=antigravity
POSTGRES_PASSWORD=antigravity123
POSTGRES_DB=antigravity_db

# CLaRa Model
MODEL_NAME=apple/CLaRa-7B-Instruct
DEVICE=cuda  # or cpu
MAX_CONTEXT_TOKENS=32768
COMPRESSION_RATIO=16

# Storage
LATENT_TENSOR_DIR=./data/latent_tensors

# API
API_PORT=8000
```

## 🎯 Performance

- **Ingestion**: ~30s for 50-page PDF
- **Query Response**: ~10s for full notebook
- **Capacity**: 10-20 average-sized books per notebook

## πŸ”¬ Technical Details

### Why CLaRa?
CLaRa (Compressing Long-range Attention) uses latent compression to represent text in a much smaller space, enabling:
- 16x compression ratio
- Preservation of semantic information
- Cross-document reasoning

### Context Budget
- **Standard**: 32,768 tokens (latent space)
- **Equivalent to**: ~500k original text tokens (with 16x compression)
- **Example**: Can fit 10-20 full books simultaneously

## 🀝 Contributing

Contributions welcome! Please open an issue or PR.

## πŸ“ License

MIT License - see LICENSE file

## πŸ™ Acknowledgments

- Apple for CLaRa-7B-Instruct
- Google for NotebookLM inspiration
- HuggingFace for model hosting

---

**Built with ❀️ by the Antigravity Team**