broadfield-dev commited on
Commit
d1eaeb2
·
verified ·
1 Parent(s): b7f9791

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +352 -529
app.py CHANGED
@@ -1,592 +1,415 @@
1
- #!/usr/bin/env python3
2
- """
3
- Overthinker — Gradio.Server Backend with SQLite Session Isolation + HF Trace Upload
4
-
5
- """
6
-
7
  import os
8
- import re
9
- import json
10
  import uuid
 
11
  import sqlite3
 
12
  import requests
13
-
14
- from pathlib import Path
15
- from typing import Optional, Dict, List, Any
16
-
17
  from gradio import Server
18
- from fastapi import HTTPException
19
- from starlette.responses import HTMLResponse, PlainTextResponse
20
- from datasets import Dataset, concatenate_datasets, load_dataset
21
- import pandas as pd
22
- # ---------------------------------------------------------------------------
23
- # Application Setup
24
- # ---------------------------------------------------------------------------
25
- app = Server()
26
- PORT = 7860
27
- DATA_DIR = Path("data")
28
- DATA_DIR.mkdir(exist_ok=True)
29
-
30
- OPENROUTER_API_KEY = os.getenv('OPENROUTER_API_KEY', '')
31
- OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"
32
- DEFAULT_MODEL = "nvidia/nemotron-3-nano-30b-a3b"
33
 
34
- HF_TOKEN = os.getenv('HF_TOKEN', '')
35
- HF_DATASET_REPO = os.getenv('HF_DATASET_REPO', 'broadfield-dev/Overthinker-traces')
 
 
 
 
 
 
 
 
 
36
 
37
- # ---------------------------------------------------------------------------
38
- # Database Helpers
39
- # ---------------------------------------------------------------------------
40
 
41
- def get_db_path(session_id: str) -> Path:
42
- return DATA_DIR / f"session_{session_id}.db"
43
 
44
- def init_session(session_id: str):
45
  db_path = get_db_path(session_id)
46
- if db_path.exists():
47
- return
48
- conn = sqlite3.connect(str(db_path))
49
- conn.execute("""
50
- CREATE TABLE nodes (
51
- id TEXT PRIMARY KEY,
52
- parent_id TEXT,
53
- type TEXT NOT NULL,
54
- label TEXT NOT NULL,
55
- description TEXT DEFAULT '',
56
- emoji TEXT DEFAULT '\U0001f539',
57
- tips TEXT DEFAULT '[]',
58
- order_index INTEGER DEFAULT 0,
59
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
60
- )
61
- """)
62
- root_id = str(uuid.uuid4())
63
- conn.execute(
64
- "INSERT INTO nodes (id, parent_id, type, label, description, emoji) VALUES (?, ?, ?, ?, ?, ?)",
65
- (root_id, None, "root", "What decision do you want to explore?", "", "\U0001f333")
66
- )
67
  conn.commit()
68
  conn.close()
69
 
70
- def get_node_db(session_id: str, node_id: str) -> Optional[Dict]:
71
  db_path = get_db_path(session_id)
72
- if not db_path.exists():
73
- return None
74
- conn = sqlite3.connect(str(db_path))
75
  conn.row_factory = sqlite3.Row
76
- row = conn.execute("SELECT * FROM nodes WHERE id=?", (node_id,)).fetchone()
77
  conn.close()
78
- if row is None:
79
- return None
80
- result = dict(row)
81
- try:
82
- result['tips'] = json.loads(result.get('tips', '[]'))
83
- except:
84
- result['tips'] = []
85
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
- def get_children_db(session_id: str, parent_id: str) -> List[Dict]:
88
  db_path = get_db_path(session_id)
89
- if not db_path.exists():
90
- return []
91
- conn = sqlite3.connect(str(db_path))
92
  conn.row_factory = sqlite3.Row
93
- rows = conn.execute(
94
- "SELECT * FROM nodes WHERE parent_id=? ORDER BY order_index",
95
- (parent_id,)
96
- ).fetchall()
 
 
 
 
97
  conn.close()
98
- result = []
99
- for row in rows:
100
- d = dict(row)
101
- try:
102
- d['tips'] = json.loads(d.get('tips', '[]'))
103
- except:
104
- d['tips'] = []
105
- result.append(d)
106
- return result
107
-
108
- def add_node_db(session_id: str, parent_id: str, node_type: str, label: str,
109
- description: str = "", emoji: str = "\U0001f539",
110
- tips: list = None, order_index: int = 0) -> Dict:
111
- node_id = str(uuid.uuid4())
112
- tips_json = json.dumps(tips or [])
113
  db_path = get_db_path(session_id)
114
- conn = sqlite3.connect(str(db_path))
115
- conn.execute(
116
- "INSERT INTO nodes (id, parent_id, type, label, description, emoji, tips, order_index) VALUES (?,?,?,?,?,?,?,?)",
117
- (node_id, parent_id, node_type, label, description, emoji, tips_json, order_index)
118
- )
119
- conn.commit()
120
  conn.close()
121
- return {
122
- "id": node_id,
123
- "parent_id": parent_id,
124
- "type": node_type,
125
- "label": label,
126
- "description": description,
127
- "emoji": emoji,
128
- "tips": tips or [],
129
- "order_index": order_index
130
- }
131
 
132
- def update_root_db(session_id: str, label: str, description: str = ""):
133
  db_path = get_db_path(session_id)
134
- conn = sqlite3.connect(str(db_path))
 
135
  conn.execute(
136
- "UPDATE nodes SET label=?, description=? WHERE parent_id IS NULL",
137
- (label, description)
138
  )
139
  conn.commit()
140
  conn.close()
 
141
 
142
- def get_path_db(session_id: str, node_id: str) -> List[Dict]:
143
- path = []
144
- current_id = node_id
145
- while current_id:
146
- node = get_node_db(session_id, current_id)
147
- if node is None:
148
- break
149
- path.append(node)
150
- current_id = node.get("parent_id")
151
- path.reverse()
152
- return path
153
 
154
- def build_path_string(session_id: str, node_id: str) -> str:
155
- nodes = get_path_db(session_id, node_id)
156
- parts = []
157
- for n in nodes:
158
- t = n["type"]
159
- label = n["label"]
160
- if t == "root":
161
- parts.append(f"[ROOT] {label}")
162
- elif t == "input":
163
- parts.append(f"[INPUT] {label}")
164
- elif t == "outcome":
165
- parts.append(f"[OUTCOME] {label}")
166
- return "".join(parts)
167
-
168
- def get_root_node(session_id: str) -> Optional[Dict]:
169
- db_path = get_db_path(session_id)
170
- if not db_path.exists():
171
- return None
172
- conn = sqlite3.connect(str(db_path))
173
- conn.row_factory = sqlite3.Row
174
- row = conn.execute("SELECT * FROM nodes WHERE parent_id IS NULL LIMIT 1").fetchone()
175
- conn.close()
176
- if row is None:
177
- return None
178
- result = dict(row)
 
 
 
179
  try:
180
- result['tips'] = json.loads(result.get('tips', '[]'))
181
- except:
182
- result['tips'] = []
183
- return result
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
- def get_all_node_ids(session_id: str) -> List[str]:
186
- """Get IDs of all nodes in the tree (for full export)."""
187
- db_path = get_db_path(session_id)
188
- if not db_path.exists():
189
- return []
190
- conn = sqlite3.connect(str(db_path))
191
- rows = conn.execute("SELECT id FROM nodes").fetchall()
192
- conn.close()
193
- return [r[0] for r in rows]
194
 
195
- def build_tree_nested(session_id: str) -> Optional[Dict]:
196
- """Build a nested tree structure from the SQLite DB."""
197
- root = get_root_node(session_id)
198
- if not root:
199
- return None
200
- def build_tree(node):
201
- children = get_children_db(session_id, node['id'])
202
- node_copy = dict(node)
203
- if isinstance(node_copy.get('tips'), str):
204
- try:
205
- node_copy['tips'] = json.loads(node_copy['tips'])
206
- except:
207
- node_copy['tips'] = []
208
- node_copy['children'] = [build_tree(c) for c in children]
209
- return node_copy
210
- return build_tree(root)
211
-
212
- # ---------------------------------------------------------------------------
213
- # Prompt Builders (with path_context)
214
- # ---------------------------------------------------------------------------
215
-
216
- def build_root_prompt(decision: str) -> str:
217
- return f'''You are an AI that helps people explore decisions by generating decision trees.
218
-
219
- Generate a ROOT decision node for the following decision:
220
-
221
- "{decision}"
222
-
223
- Return ONLY valid JSON with exactly this structure (no markdown, no backticks):
224
- {{
225
- "label": "A concise label for this decision tree (3-6 words)",
226
- "description": "A 1-2 sentence description of this decision context",
227
- "emoji": "An emoji representing this decision",
228
- "tips": ["One actionable tip for approaching this decision"]
229
- }}'''
230
-
231
- def build_options_prompt(decision_label: str, decision_desc: str, count: int, path_context: str, comment: str = "") -> str:
232
- path_section = f'\nFull path from root to this node: "{path_context}"' if path_context else ''
233
- comment_section = f'\nUser context: "{comment}"' if comment else ''
234
- return f'''You are an AI that helps explore decisions by generating decision tree branches.
235
-
236
- Parent node: "{decision_label}"
237
- Description: "{decision_desc}"{path_section}{comment_section}
238
-
239
- Generate EXACTLY {count} child nodes that represent different OPTIONS or CHOICES the person could take.
240
-
241
- IMPORTANT: Frame each child as an OPTION or CHOICE, not as an outcome.
242
-
243
- Consider the full decision path above to ensure the options are contextually relevant.
244
-
245
- Return ONLY valid JSON with exactly this structure (no markdown, no backticks):
246
- {{
247
- "children": [
248
- {{
249
- "id": "child_1",
250
- "label": "Short option label (3-6 words)",
251
- "description": "1-2 sentence description",
252
- "emoji": "An emoji",
253
- "tips": ["One practical tip"]
254
- }},
255
- ...
256
- ]
257
- }}
258
-
259
- Ensure children have unique IDs like child_1, child_2, etc.'''
260
-
261
- def build_outcomes_prompt(decision_label: str, decision_desc: str, count: int, path_context: str, comment: str = "") -> str:
262
- path_section = f'\nFull path from root to this node: "{path_context}"' if path_context else ''
263
- comment_section = f'\nUser context: "{comment}"' if comment else ''
264
- return f'''You are an AI that helps explore decisions by generating decision tree branches.
265
-
266
- Parent node: "{decision_label}"
267
- Description: "{decision_desc}"{path_section}{comment_section}
268
-
269
- Generate EXACTLY {count} child nodes that represent a DIVERSE RANGE of possible OUTCOMES. Include a MIX of positive, neutral, and negative outcomes.
270
-
271
- IMPORTANT: Frame each child as an OUTCOME or CONSEQUENCE, not as a choice someone makes.
272
-
273
- Consider the full decision path above to ensure the outcomes are contextually relevant.
274
-
275
- Return ONLY valid JSON with exactly this structure (no markdown, no backticks):
276
- {{
277
- "children": [
278
- {{
279
- "id": "child_1",
280
- "label": "Short outcome label (3-6 words)",
281
- "description": "1-2 sentence description",
282
- "emoji": "An emoji",
283
- "tips": ["One practical tip"]
284
- }},
285
- ...
286
- ]
287
- }}
288
-
289
- Ensure children have unique IDs. Make sure the first child is POSITIVE, the second is NEUTRAL, and the third is NEGATIVE.'''
290
-
291
- # ---------------------------------------------------------------------------
292
- # AI Call (using OpenRouter via requests)
293
- # ---------------------------------------------------------------------------
294
-
295
- def call_api(prompt: str, system_prompt: str = "You are a helpful assistant that generates decision trees.") -> Optional[str]:
296
- if not OPENROUTER_API_KEY:
297
- print("[OpenRouter Error] No API key configured")
298
- return None
299
- try:
300
- headers = {
301
- 'Authorization': f'Bearer {OPENROUTER_API_KEY}',
302
- 'Content-Type': 'application/json',
303
- 'HTTP-Referer': 'http://localhost:7860',
304
- 'X-Title': 'Overthinker'
305
- }
306
- data = {
307
- 'model': DEFAULT_MODEL,
308
- 'messages': [
309
- {'role': 'system', 'content': system_prompt},
310
- {'role': 'user', 'content': prompt}
311
- ],
312
- 'temperature': 0.8,
313
- 'max_tokens': 2048
314
- }
315
- response = requests.post(
316
- OPENROUTER_URL,
317
- headers=headers,
318
- json=data,
319
- timeout=30
320
- )
321
- if response.status_code == 200:
322
- result = response.json()
323
- return result['choices'][0]['message']['content']
324
- else:
325
- print(f"[OpenRouter Error] {response.status_code}: {response.text}")
326
- except Exception as e:
327
- print(f"[OpenRouter Exception] {e}")
328
- return None
329
 
330
- def parse_json_response(text: str) -> Optional[dict]:
331
- if not text:
332
- return None
333
- text = text.strip()
334
- text = re.sub(r'```json\s*', '', text)
335
- text = re.sub(r'```\s*', '', text)
336
- text = text.strip()
337
- start = text.find('{')
338
- end = text.rfind('}')
339
- if start >= 0 and end > start:
340
- text = text[start:end+1]
341
- try:
342
- return json.loads(text)
343
- except json.JSONDecodeError as e:
344
- print(f"[JSON Parse Error] {e}")
345
- print(f"[Raw text] {text[:500]}")
346
- return None
347
-
348
- # ---------------------------------------------------------------------------
349
- # Routes (All POST, no GET except for serving index)
350
- # ---------------------------------------------------------------------------
351
-
352
- @app.get("/")
353
- async def index():
354
- html_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates", "index.html")
355
- if os.path.exists(html_path):
356
- with open(html_path, "r", encoding="utf-8") as f:
357
- return HTMLResponse(content=f.read(), status_code=200)
358
- return HTMLResponse(content="<h1>Overthinker</h1><p>index.html not found</p>", status_code=404)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
 
360
  @app.post("/root")
361
- async def create_root(request: dict):
362
- session_id = request.get('session_id', str(uuid.uuid4()))
363
- init_session(session_id)
364
- root = get_root_node(session_id)
365
- if root is None:
366
- raise HTTPException(status_code=500, detail="Could not initialize session.")
367
- return {"session_id": session_id, "node": root}
368
-
369
- @app.post("/create_tree")
370
- async def create_tree(request: dict):
371
- session_id = request.get('session_id', str(uuid.uuid4()))
372
- decision = request.get('decision', '')
373
- if not decision:
374
- raise HTTPException(status_code=400, detail="Decision text is required.")
375
- init_session(session_id)
376
- prompt = build_root_prompt(decision)
377
- ai_response = call_api(prompt)
378
- parsed = parse_json_response(ai_response) if ai_response else None
379
- if not parsed:
380
- raise HTTPException(status_code=500, detail="Failed to generate root node. Please check your API key and try again.")
381
- label = parsed.get('label', f'Overthinking: {decision[:40]}')
382
- description = parsed.get('description', f'You are overthinking: {decision}')
383
- emoji = parsed.get('emoji', '\U0001f333')
384
- tips = parsed.get('tips', ['Start by exploring options.'])
385
- update_root_db(session_id, label, description)
386
  db_path = get_db_path(session_id)
387
- conn = sqlite3.connect(str(db_path))
388
- conn.execute("UPDATE nodes SET emoji=?, tips=? WHERE parent_id IS NULL", (emoji, json.dumps(tips)))
 
 
 
 
389
  conn.commit()
390
  conn.close()
391
- root = get_root_node(session_id)
392
- return {'session_id': session_id, 'node': root}
393
-
394
- @app.post("/get_node")
395
- async def get_node_endpoint(request: dict):
396
- session_id = request.get('session_id')
397
- node_id = request.get('node_id')
398
- if not session_id or not node_id:
399
- raise HTTPException(status_code=400, detail="Missing session_id or node_id")
400
- init_session(session_id)
401
- node = get_node_db(session_id, node_id)
402
- if node is None:
403
- raise HTTPException(status_code=404, detail="Node not found")
404
- children = get_children_db(session_id, node_id)
405
- path_context = build_path_string(session_id, node_id)
406
- return {
407
- 'node': node,
408
- 'children': children,
409
- 'path_context': path_context
410
- }
411
 
412
  @app.post("/get_children")
413
- async def get_children(request: dict):
414
- session_id = request.get('session_id')
415
- node_id = request.get('node_id')
416
- count = request.get('count', 3)
417
- node_type = request.get('node_type', 'outcome')
418
- comment = request.get('comment', '')
419
- if not session_id or not node_id:
420
- raise HTTPException(status_code=400, detail="Missing session_id or node_id")
421
- init_session(session_id)
422
  parent = get_node_db(session_id, node_id)
423
- if parent is None:
424
  raise HTTPException(status_code=404, detail="Node not found")
425
- path_context = build_path_string(session_id, node_id)
426
- next_type_map = {'root': 'input', 'input': 'outcome', 'outcome': 'input'}
427
- next_type = next_type_map.get(node_type, 'outcome')
428
  parent_label = parent.get('label', 'Unknown')
429
  parent_desc = parent.get('description', '')
 
 
 
 
430
  if next_type == 'input':
431
- prompt = build_options_prompt(parent_label, parent_desc, count, path_context, comment)
432
  else:
433
- prompt = build_outcomes_prompt(parent_label, parent_desc, count, path_context, comment)
434
- ai_response = call_api(prompt)
435
- parsed = parse_json_response(ai_response) if ai_response else None
436
- if not parsed or 'children' not in parsed or not isinstance(parsed['children'], list):
437
- raise HTTPException(status_code=500, detail="Generation failed. Please check your API key and try again.")
438
- children_data = parsed['children']
439
- children = []
440
- for i, child in enumerate(children_data):
441
- label = child.get('label', 'Unknown')
442
- description = child.get('description', '')
443
- emoji = child.get('emoji', '\U0001f539')
444
- tips = child.get('tips', [f'Consider this {next_type}.'])
445
- existing = get_children_db(session_id, node_id)
446
- existing_labels = [c['label'] for c in existing]
447
- if label in existing_labels or label in [c['label'] for c in children]:
448
- label = f"{label} ({i+1})"
449
- child_node = add_node_db(session_id, node_id, next_type, label, description, emoji, tips, order_index=i)
450
- child_node['type'] = next_type
451
- children.append(child_node)
452
- return {'children': children, 'next_type': next_type}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
453
 
454
  @app.post("/add_options")
455
- async def add_options(request: dict):
456
- session_id = request.get('session_id')
457
- node_id = request.get('node_id')
458
- count = request.get('count', 3)
459
- comment = request.get('comment', '')
460
- if not session_id or not node_id:
461
- raise HTTPException(status_code=400, detail="Missing session_id or node_id")
462
- init_session(session_id)
463
  parent = get_node_db(session_id, node_id)
464
- if parent is None:
465
  raise HTTPException(status_code=404, detail="Node not found")
466
- path_context = build_path_string(session_id, node_id)
467
- next_type_map = {'root': 'input', 'input': 'outcome', 'outcome': 'input'}
468
- next_type = next_type_map.get(parent.get('type', 'root'), 'outcome')
469
- parent_label = parent.get('label', 'Unknown')
470
  parent_desc = parent.get('description', '')
 
 
 
471
  if next_type == 'input':
472
- prompt = build_options_prompt(parent_label, parent_desc, count, path_context, comment)
473
  else:
474
- prompt = build_outcomes_prompt(parent_label, parent_desc, count, path_context, comment)
475
- ai_response = call_api(prompt)
476
- parsed = parse_json_response(ai_response) if ai_response else None
477
- if not parsed or 'children' not in parsed or not isinstance(parsed['children'], list):
478
- raise HTTPException(status_code=500, detail="Failed to add options. Please try again.")
479
- children_data = parsed['children']
480
- children = []
481
- for i, child in enumerate(children_data):
482
- label = child.get('label', 'Unknown')
483
- description = child.get('description', '')
484
- emoji = child.get('emoji', '\U0001f539')
485
- tips = child.get('tips', [f'Additional {next_type}.'])
486
- existing = get_children_db(session_id, node_id)
487
- existing_labels = [c['label'] for c in existing]
488
- if label in existing_labels or label in [c['label'] for c in children]:
489
- label = f"{label} ({i+1})"
490
- child_node = add_node_db(session_id, node_id, next_type, label, description, emoji, tips, order_index=i)
491
- child_node['type'] = next_type
492
- children.append(child_node)
493
- return {'children': children, 'next_type': next_type}
 
 
 
 
 
 
 
 
 
 
 
 
494
 
495
  @app.post("/upload_trace")
496
- async def upload_trace(request: dict):
497
- """Serialize the full tree from SQLite and push to HuggingFace dataset."""
498
- session_id = request.get('session_id')
499
  if not session_id:
500
- raise HTTPException(status_code=400, detail="Missing session_id")
501
 
502
- if not HF_TOKEN or not HF_DATASET_REPO:
503
- raise HTTPException(status_code=500, detail="HF_TOKEN and HF_DATASET_REPO must be configured in environment.")
 
504
 
505
- tree = build_tree_nested(session_id)
506
- if tree is None:
507
- raise HTTPException(status_code=404, detail="No tree found for this session.")
 
 
508
 
509
- try:
 
 
 
 
 
 
 
 
 
 
 
 
510
 
511
-
512
- row = {
513
- 'session_id': session_id,
514
- 'tree_json': json.dumps(tree),
515
- 'created_at': str(tree.get('created_at', ''))
516
- }
517
- df = pd.DataFrame([row])
518
- new_dataset = Dataset.from_pandas(df)
519
-
520
- try:
521
- existing_dataset = load_dataset(HF_DATASET_REPO, split='train', token=HF_TOKEN)
522
- combined = concatenate_datasets([existing_dataset, new_dataset])
523
- except Exception:
524
- combined = new_dataset
525
-
526
- combined.push_to_hub(HF_DATASET_REPO, token=HF_TOKEN, private=False)
527
-
528
- return {'status': 'success', 'message': 'Trace uploaded successfully!'}
529
- except Exception as e:
530
- print(f"[Upload Trace Error] {e}")
531
- raise HTTPException(status_code=500, detail=f"Failed to upload trace: {str(e)}")
532
 
533
- @app.post("/export_json")
534
- async def export_json(request: dict):
535
- session_id = request.get('session_id')
536
- if not session_id:
537
- raise HTTPException(status_code=400, detail="Missing session_id")
538
- root = get_root_node(session_id)
539
- if not root:
540
- raise HTTPException(status_code=404, detail="No tree found")
541
- def build_tree(node):
542
- children = get_children_db(session_id, node['id'])
543
- node_copy = dict(node)
544
- node_copy['children'] = [build_tree(c) for c in children]
545
- return node_copy
546
- full_tree = build_tree(root)
547
- return full_tree
548
-
549
- @app.post("/export_path_json")
550
- async def export_path_json(request: dict):
551
- session_id = request.get('session_id')
552
- node_id = request.get('node_id')
553
- if not session_id or not node_id:
554
- raise HTTPException(status_code=400, detail="Missing session_id or node_id")
555
- path_nodes = get_path_db(session_id, node_id)
556
- return {'path': path_nodes}
557
-
558
- @app.post("/export_path_md")
559
- async def export_path_md(request: dict):
560
- session_id = request.get('session_id')
561
- node_id = request.get('node_id')
562
- if not session_id or not node_id:
563
- raise HTTPException(status_code=400, detail="Missing session_id or node_id")
564
- path = get_path_db(session_id, node_id)
565
- md = '# \U0001f9e0 Overthinker — Decision Path\n\n'
566
- for i, node in enumerate(path):
567
- indent = ' ' * i
568
- emoji = {'root': '\U0001f333', 'input': '\U0001f9e0', 'outcome': '\U0001f4ca'}.get(node.get('type', ''), '\U0001f4cc')
569
- md += f'{indent}{emoji} **{node.get("label", "")}**\n'
570
- if node.get('description'):
571
- md += f'{indent} > {node.get("description", "")}\n'
572
- if node.get('tips') and len(node['tips']) > 0:
573
- md += f'{indent} > \U0001f4a1 {node["tips"][0]}\n'
574
- md += '\n'
575
- return PlainTextResponse(content=md, status_code=200)
576
-
577
- # ---------------------------------------------------------------------------
578
- # Launch
579
- # ---------------------------------------------------------------------------
580
  if __name__ == "__main__":
581
- print(f"\U0001f9e0 Overthinker — SQLite Session Mode + HF Trace Upload on port {PORT}")
582
- print(f"\U0001f916 Model: {DEFAULT_MODEL}")
583
- print(f"\U0001f310 Open http://localhost:{PORT} in your browser")
584
- if not OPENROUTER_API_KEY:
585
- print("\u26a0\ufe0f No OPENROUTER_API_KEY found. Add to .env or environment. Generation will fail.")
586
- if not HF_TOKEN or not HF_DATASET_REPO:
587
- print("\u26a0\ufe0f No HF_TOKEN or HF_DATASET_REPO set. Upload will fail.")
588
- app.launch(
589
- server_port=PORT,
590
- show_error=True,
591
- share=False
592
- )
 
 
 
 
 
 
 
1
  import os
 
 
2
  import uuid
3
+ import json
4
  import sqlite3
5
+ import httpx
6
  import requests
7
+ from fastapi import FastAPI, Request, HTTPException
8
+ from fastapi.responses import HTMLResponse, PlainTextResponse, Response, JSONResponse
9
+ from fastapi.staticfiles import StaticFiles
 
10
  from gradio import Server
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Import static strings from bag.py
13
+ from bag import (
14
+ BASE_URL,
15
+ LLMS_TXT,
16
+ SITEMAP_XML,
17
+ ROBOTS_TXT,
18
+ OVERSEER_JSON,
19
+ VIDEO_PAGE_HTML
20
+ )
21
+
22
+ app = FastAPI()
23
 
24
+ # --- Database helpers ---
25
+ DATA_DIR = "data"
26
+ os.makedirs(DATA_DIR, exist_ok=True)
27
 
28
+ def get_db_path(session_id: str) -> str:
29
+ return os.path.join(DATA_DIR, f"session_{session_id}.db")
30
 
31
+ def init_session_db(session_id: str):
32
  db_path = get_db_path(session_id)
33
+ conn = sqlite3.connect(db_path)
34
+ conn.execute('''CREATE TABLE IF NOT EXISTS nodes (
35
+ id TEXT PRIMARY KEY,
36
+ parent_id TEXT,
37
+ node_type TEXT NOT NULL,
38
+ label TEXT NOT NULL,
39
+ description TEXT DEFAULT '',
40
+ emoji TEXT DEFAULT '',
41
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
42
+ )''')
43
+ conn.execute('''CREATE TABLE IF NOT EXISTS roots (
44
+ id TEXT PRIMARY KEY,
45
+ decision TEXT NOT NULL,
46
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
47
+ )''')
48
+ # Ensure root node exists
49
+ root = conn.execute("SELECT id FROM roots LIMIT 1").fetchone()
50
+ if not root:
51
+ root_id = str(uuid.uuid4())
52
+ conn.execute("INSERT INTO roots (id, decision) VALUES (?, 'New Decision')", (root_id,))
53
+ conn.execute("INSERT INTO nodes (id, parent_id, node_type, label, description) VALUES (?, NULL, 'root', 'What decision do you want to explore?', 'Enter a decision at the top of the page to begin.')", (root_id,))
54
  conn.commit()
55
  conn.close()
56
 
57
+ def get_tree_nested(session_id: str) -> dict:
58
  db_path = get_db_path(session_id)
59
+ conn = sqlite3.connect(db_path)
 
 
60
  conn.row_factory = sqlite3.Row
61
+ rows = conn.execute("SELECT * FROM nodes ORDER BY created_at").fetchall()
62
  conn.close()
63
+ # Build tree recursively
64
+ node_map = {}
65
+ for row in rows:
66
+ node_map[row['id']] = {
67
+ 'id': row['id'],
68
+ 'parent_id': row['parent_id'],
69
+ 'type': row['node_type'],
70
+ 'label': row['label'],
71
+ 'description': row['description'],
72
+ 'emoji': row['emoji'],
73
+ 'children': []
74
+ }
75
+ root = None
76
+ for nid, node in node_map.items():
77
+ if node['parent_id'] is None:
78
+ root = node
79
+ else:
80
+ parent = node_map.get(node['parent_id'])
81
+ if parent:
82
+ parent['children'].append(node)
83
+ return root or {'id': 'error', 'label': 'No root found', 'children': []}
84
 
85
+ def build_path_string(session_id: str, node_id: str) -> str:
86
  db_path = get_db_path(session_id)
87
+ conn = sqlite3.connect(db_path)
 
 
88
  conn.row_factory = sqlite3.Row
89
+ path_parts = []
90
+ current_id = node_id
91
+ while current_id:
92
+ row = conn.execute("SELECT id, parent_id, node_type, label FROM nodes WHERE id=?", (current_id,)).fetchone()
93
+ if not row:
94
+ break
95
+ path_parts.append(f"[{row['node_type'].upper()}] {row['label']}")
96
+ current_id = row['parent_id']
97
  conn.close()
98
+ path_parts.reverse()
99
+ return " ".join(path_parts) if path_parts else node_id
100
+
101
+ def get_node_db(session_id: str, node_id: str) -> dict:
 
 
 
 
 
 
 
 
 
 
 
102
  db_path = get_db_path(session_id)
103
+ conn = sqlite3.connect(db_path)
104
+ conn.row_factory = sqlite3.Row
105
+ row = conn.execute("SELECT * FROM nodes WHERE id=?", (node_id,)).fetchone()
 
 
 
106
  conn.close()
107
+ if row:
108
+ return dict(row)
109
+ return None
 
 
 
 
 
 
 
110
 
111
+ def add_node_db(session_id: str, parent_id: str, node_type: str, label: str, description: str = "", emoji: str = ""):
112
  db_path = get_db_path(session_id)
113
+ conn = sqlite3.connect(db_path)
114
+ node_id = str(uuid.uuid4())
115
  conn.execute(
116
+ "INSERT INTO nodes (id, parent_id, node_type, label, description, emoji) VALUES (?, ?, ?, ?, ?, ?)",
117
+ (node_id, parent_id, node_type, label, description, emoji)
118
  )
119
  conn.commit()
120
  conn.close()
121
+ return node_id
122
 
123
+ # --- AI Generation ---
124
+ DEFAULT_MODEL = "nvidia/nemotron-3-nano-30b-a3b"
125
+ OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY", "")
 
 
 
 
 
 
 
 
126
 
127
+ def call_api(prompt: str, max_tokens: int = 1024) -> str:
128
+ if not OPENROUTER_API_KEY:
129
+ raise HTTPException(status_code=500, detail="OPENROUTER_API_KEY not set")
130
+ response = requests.post(
131
+ url="https://openrouter.ai/api/v1/chat/completions",
132
+ headers={
133
+ "Authorization": f"Bearer {OPENROUTER_API_KEY}",
134
+ "Content-Type": "application/json"
135
+ },
136
+ json={
137
+ "model": DEFAULT_MODEL,
138
+ "messages": [{"role": "user", "content": prompt}],
139
+ "max_tokens": max_tokens,
140
+ "temperature": 0.8
141
+ },
142
+ timeout=60
143
+ )
144
+ if response.status_code != 200:
145
+ raise HTTPException(status_code=500, detail=f"API error: {response.status_code} - {response.text}")
146
+ data = response.json()
147
+ choices = data.get("choices", [])
148
+ if not choices:
149
+ raise HTTPException(status_code=500, detail="No choices in response")
150
+ return choices[0].get("message", {}).get("content", "")
151
+
152
+ def parse_children(text: str) -> list:
153
+ """Parse AI response into list of dicts with label, description, emoji."""
154
+ children = []
155
  try:
156
+ # Try JSON parsing first
157
+ data = json.loads(text)
158
+ if isinstance(data, list):
159
+ children = data
160
+ elif isinstance(data, dict) and "children" in data:
161
+ children = data["children"]
162
+ except json.JSONDecodeError:
163
+ # Fallback: split by lines
164
+ lines = text.strip().split('\n')
165
+ for line in lines:
166
+ line = line.strip()
167
+ if line.startswith('-') or line.startswith('*'):
168
+ label = line[1:].strip()
169
+ if label:
170
+ children.append({"label": label, "description": "", "emoji": ""})
171
+ return children
172
 
173
+ def build_options_prompt(path_context: str, parent_label: str, parent_desc: str, count: int, comment: str) -> str:
174
+ return f"""You are generating OPTIONS (choices/decisions) for a decision tree.
 
 
 
 
 
 
 
175
 
176
+ Full path from root to this node:
177
+ {path_context}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
 
179
+ Current node: {parent_label}
180
+ Description: {parent_desc}
181
+
182
+ Generate {count} distinct, creative options that follow from this node. Each option should be a possible action, choice, or path forward that makes sense given the full context above.
183
+
184
+ CRITICAL: Respond ONLY with a valid JSON array of objects. Each object must have:
185
+ - "label": A short, punchy title (2-6 words)
186
+ - "description": 1-2 sentence explanation of this option
187
+ - "emoji": A single emoji character representing this option
188
+
189
+ Example:
190
+ [
191
+ {{"label": "Start freelancing", "description": "Begin working independently as a freelancer", "emoji": "💼"}},
192
+ {{"label": "Take a course", "description": "Enroll in a structured learning program", "emoji": "📚"}}
193
+ ]
194
+
195
+ IMPORTANT: Your response must be ONLY the JSON array. No markdown, no explanations, no code blocks."""
196
+
197
+ def build_outcomes_prompt(path_context: str, parent_label: str, parent_desc: str, count: int, comment: str) -> str:
198
+ return f"""You are generating OUTCOMES (results/consequences) for a decision tree.
199
+
200
+ Full path from root to this node:
201
+ {path_context}
202
+
203
+ Current node: {parent_label}
204
+ Description: {parent_desc}
205
+
206
+ Generate {count} distinct, realistic outcomes that could result from this choice. Each outcome should feel like a natural consequence given the full decision history above.
207
+
208
+ CRITICAL: Respond ONLY with a valid JSON array of objects. Each object must have:
209
+ - "label": A short, punchy title (2-6 words)
210
+ - "description": 1-2 sentence explanation of this outcome
211
+ - "emoji": A single emoji character representing this outcome
212
+
213
+ Example:
214
+ [
215
+ {{"label": "Financial stability improves", "description": "The freelancer enjoys a steady income over time", "emoji": "💰"}},
216
+ {{"label": "Loneliness sets in", "description": "Working alone leads to feelings of isolation", "emoji": "😔"}}
217
+ ]
218
+
219
+ IMPORTANT: Your response must be ONLY the JSON array. No markdown, no explanations, no code blocks."""
220
+
221
+ # --- API Endpoints ---
222
+
223
+ @app.get("/llms.txt", response_class=PlainTextResponse)
224
+ async def get_llms_txt():
225
+ return PlainTextResponse(LLMS_TXT)
226
+
227
+ @app.get("/sitemap.xml", response_class=Response)
228
+ async def get_sitemap():
229
+ return Response(content=SITEMAP_XML, media_type="application/xml")
230
+
231
+ @app.get("/robots.txt", response_class=PlainTextResponse)
232
+ async def get_robots():
233
+ return PlainTextResponse(ROBOTS_TXT)
234
+
235
+ @app.get("/overthinker.json", response_class=Response)
236
+ async def get_overthinker_json():
237
+ return Response(content=OVERSEER_JSON, media_type="application/json")
238
+
239
+ @app.get("/video", response_class=HTMLResponse)
240
+ async def get_video():
241
+ return HTMLResponse(content=VIDEO_PAGE_HTML)
242
 
243
  @app.post("/root")
244
+ async def create_root(request: Request):
245
+ body = await request.json()
246
+ session_id = body.get("session_id", str(uuid.uuid4()))
247
+ decision = body.get("decision", "")
248
+ init_session_db(session_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  db_path = get_db_path(session_id)
250
+ conn = sqlite3.connect(db_path)
251
+ if decision:
252
+ conn.execute("UPDATE roots SET decision=? WHERE rowid=1", (decision,))
253
+ root_row = conn.execute("SELECT id FROM roots LIMIT 1").fetchone()
254
+ if root_row:
255
+ conn.execute("UPDATE nodes SET label=? WHERE id=?", (decision, root_row[0]))
256
  conn.commit()
257
  conn.close()
258
+ tree = get_tree_nested(session_id)
259
+ path = build_path_string(session_id, tree['id'])
260
+ return {"session_id": session_id, "tree": tree, "path": path}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
 
262
  @app.post("/get_children")
263
+ async def get_children(request: Request):
264
+ body = await request.json()
265
+ session_id = body.get("session_id")
266
+ node_id = body.get("node_id")
267
+ count = body.get("count", 3)
268
+ node_type = body.get("node_type", "outcome")
269
+ comment = body.get("comment", "")
270
+
271
+ init_session_db(session_id)
272
  parent = get_node_db(session_id, node_id)
273
+ if not parent:
274
  raise HTTPException(status_code=404, detail="Node not found")
275
+
 
 
276
  parent_label = parent.get('label', 'Unknown')
277
  parent_desc = parent.get('description', '')
278
+ path_context = build_path_string(session_id, node_id)
279
+
280
+ next_type = "input" if node_type == "outcome" else "outcome"
281
+
282
  if next_type == 'input':
283
+ prompt = build_options_prompt(path_context, parent_label, parent_desc, count, comment)
284
  else:
285
+ prompt = build_outcomes_prompt(path_context, parent_label, parent_desc, count, comment)
286
+
287
+ try:
288
+ text = call_api(prompt, max_tokens=2048)
289
+ children = parse_children(text)
290
+ if not children:
291
+ raise HTTPException(status_code=500, detail="Generation failed. AI returned empty results.")
292
+ except Exception as e:
293
+ raise HTTPException(status_code=500, detail=f"Generation failed: {str(e)}")
294
+
295
+ # Save children to DB
296
+ child_ids = []
297
+ for child in children:
298
+ cid = add_node_db(session_id, node_id, next_type, child.get('label', ''), child.get('description', ''), child.get('emoji', ''))
299
+ child_ids.append(cid)
300
+
301
+ # Fetch saved children
302
+ db_path = get_db_path(session_id)
303
+ conn = sqlite3.connect(db_path)
304
+ conn.row_factory = sqlite3.Row
305
+ saved_children = []
306
+ for cid in child_ids:
307
+ row = conn.execute("SELECT * FROM nodes WHERE id=?", (cid,)).fetchone()
308
+ if row:
309
+ saved_children.append(dict(row))
310
+ conn.close()
311
+
312
+ parent_label = parent.get('label', '')
313
+ parent_desc = parent.get('description', '')
314
+ path_context = build_path_string(session_id, node_id)
315
+ next_type = "input" if node_type == "outcome" else "outcome"
316
+
317
+ return {
318
+ "children": saved_children,
319
+ "parent_label": parent_label,
320
+ "parent_desc": parent_desc,
321
+ "path_context": path_context,
322
+ "next_type": next_type
323
+ }
324
 
325
  @app.post("/add_options")
326
+ async def add_options(request: Request):
327
+ body = await request.json()
328
+ session_id = body.get("session_id")
329
+ node_id = body.get("node_id")
330
+ count = body.get("count", 3)
331
+ comment = body.get("comment", "")
332
+
333
+ init_session_db(session_id)
334
  parent = get_node_db(session_id, node_id)
335
+ if not parent:
336
  raise HTTPException(status_code=404, detail="Node not found")
337
+
338
+ parent_label = parent.get('label', '')
 
 
339
  parent_desc = parent.get('description', '')
340
+ path_context = build_path_string(session_id, node_id)
341
+ next_type = "input" if parent['node_type'] == "outcome" else "outcome"
342
+
343
  if next_type == 'input':
344
+ prompt = build_options_prompt(path_context, parent_label, parent_desc, count, comment)
345
  else:
346
+ prompt = build_outcomes_prompt(path_context, parent_label, parent_desc, count, comment)
347
+
348
+ try:
349
+ text = call_api(prompt, max_tokens=2048)
350
+ children = parse_children(text)
351
+ if not children:
352
+ raise HTTPException(status_code=500, detail="Generation failed. AI returned empty results.")
353
+ except Exception as e:
354
+ raise HTTPException(status_code=500, detail=f"Generation failed: {str(e)}")
355
+
356
+ child_ids = []
357
+ for child in children:
358
+ cid = add_node_db(session_id, node_id, next_type, child.get('label', ''), child.get('description', ''), child.get('emoji', ''))
359
+ child_ids.append(cid)
360
+
361
+ db_path = get_db_path(session_id)
362
+ conn = sqlite3.connect(db_path)
363
+ conn.row_factory = sqlite3.Row
364
+ saved_children = []
365
+ for cid in child_ids:
366
+ row = conn.execute("SELECT * FROM nodes WHERE id=?", (cid,)).fetchone()
367
+ if row:
368
+ saved_children.append(dict(row))
369
+ conn.close()
370
+
371
+ return {
372
+ "children": saved_children,
373
+ "parent_label": parent_label,
374
+ "parent_desc": parent_desc,
375
+ "path_context": path_context,
376
+ "next_type": next_type
377
+ }
378
 
379
  @app.post("/upload_trace")
380
+ async def upload_trace(request: Request):
381
+ body = await request.json()
382
+ session_id = body.get("session_id")
383
  if not session_id:
384
+ raise HTTPException(status_code=400, detail="session_id required")
385
 
386
+ tree = get_tree_nested(session_id)
387
+ if not tree:
388
+ raise HTTPException(status_code=404, detail="No tree found")
389
 
390
+ # Upload to Hugging Face Dataset via REST API
391
+ hf_token = os.environ.get("HF_TOKEN", "")
392
+ dataset_repo = os.environ.get("HF_DATASET_REPO", "build-small-hackathon/Overthinker-trace")
393
+ if not hf_token or not dataset_repo:
394
+ raise HTTPException(status_code=500, detail="HF_TOKEN or HF_DATASET_REPO not set")
395
 
396
+ import json as json_module
397
+ trace_data = json_module.dumps(tree, indent=2)
398
+ filename = f"trace_{session_id}.json"
399
+
400
+ url = f"https://huggingface.co/api/datasets/{dataset_repo}/upload"
401
+ files = {'file': (filename, trace_data, 'application/json')}
402
+ headers = {'Authorization': f'Bearer {hf_token}'}
403
+
404
+ response = requests.post(url, headers=headers, files=files)
405
+ if response.status_code not in (200, 201):
406
+ raise HTTPException(status_code=500, detail=f"Upload failed: {response.status_code} - {response.text}")
407
+
408
+ return {"status": "ok", "filename": filename}
409
 
410
+ # --- Serve static frontend ---
411
+ app.mount("/", StaticFiles(directory="templates", html=True), name="templates")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
413
  if __name__ == "__main__":
414
+ import uvicorn
415
+ uvicorn.run(app, host="0.0.0.0", port=7860)