"""The 6-step embedding visualization.
This is the centerpiece of the TEDx talk. Every element must be readable from the
back row of a 300-seat venue. Dark background, big bold text, slow deliberate
animations, SVG so it scales to any projector.
Steps:
0. The Sentence (sets up: the prompt is just text)
1. The Embedding Matrix (words become math)
2. The Vector Space (math becomes geometry)
3. The Constellation (geometry reveals meaning)
4. The AI's Patterns (the AI was trained on human suffering)
5. The Bridge (empathy is proximity)
"""
import html as _html
from data.demo import (
PROMPT,
USER_WORDS,
AI_PATTERNS,
BRIDGES,
CLUSTERS,
AI_CLUSTERS,
EMBEDDING_VECTORS,
)
SVG_STYLE = """"""
EMBED_STEP_LABELS = [
"sentence",
"matrix",
"vector space",
"constellation",
"AI patterns",
"bridge",
]
# ---------- main entry ----------
def render_embed_step(step: int) -> str:
"""Return the full HTML for one of the 6 embed sub-steps."""
step = max(0, min(5, int(step)))
inner = [
_step_0_sentence,
_step_1_matrix,
_step_2_grid,
_step_3_constellation,
_step_4_patterns,
_step_5_bridge,
][step]()
label = EMBED_STEP_LABELS[step]
return f"""
Step 2 · Embed · {step + 1} / 6 · {label}
{_dots(step)}
"""
def _dots(step: int) -> str:
out = []
for i in range(6):
color = "#F59E0B" if i == step else "#1E293B"
out.append(f"")
return "".join(out)
# ---------- STEP 0: the sentence ----------
def _step_0_sentence() -> str:
# Hardcoded line break for the demo prompt so it reads cleanly on stage.
lines = [
"I'm scared. My chest is tight.",
"My heart is racing. I'm in the ER.",
"My blood pressure is spiking.",
]
out = []
out.append('THE PROMPT')
y0 = 340
for i, line in enumerate(lines):
y = y0 + i * 88
delay = 300 + i * 280
out.append(
f''
f'“{_html.escape(line)}”'
)
out.append(
''
"The AI doesn’t read this as a sentence."
)
out.append(
''
"It breaks it into pieces."
)
return "".join(out)
# ---------- STEP 1: the embedding matrix ----------
def _step_1_matrix() -> str:
out = []
out.append(
'Every word becomes numbers.'
)
out.append(
'WORD'
)
out.append(
'VECTOR (6 of thousands of dimensions)'
)
out.append('')
col_xs = [500, 650, 800, 950, 1100, 1250]
y = 218
for i, w in enumerate(USER_WORDS):
delay = 300 + i * 180
out.append(
f'{_html.escape(w["word"])}'
)
vec = EMBEDDING_VECTORS[w["word"]]
for j, v in enumerate(vec):
out.append(
f'{v:+.2f}'
)
y += 60
out.append(
''
"Notice: ‘scared’ and ‘fear’ have nearly identical numbers."
)
out.append(
''
"Similar numbers = similar meaning = nearby in space."
)
return "".join(out)
# ---------- STEP 2: the vector space grid ----------
def _step_2_grid() -> str:
out = []
out.append(
'Those numbers become coordinates.'
)
# Grid background
for gy in range(140, 441, 50):
out.append(f'')
for gx in range(150, 1381, 90):
out.append(f'')
# Axes
out.append('')
out.append('')
out.append(
'dimension 1 (of thousands)'
)
out.append(
'dimension 2'
)
# Dots + labels
for i, w in enumerate(USER_WORDS):
delay = 700 + i * 150
out.append(
f''
)
out.append(
f'{_html.escape(w["word"])}'
)
# Dashed line between scared and fear
scared = next(w for w in USER_WORDS if w["word"] == "scared")
fear = next(w for w in USER_WORDS if w["word"] == "fear")
out.append(
f''
)
out.append(
''
"Close together = similar meaning"
)
out.append(
''
"The geometry of meaning — in math."
)
return "".join(out)
# ---------- STEP 3: the constellation ----------
def _step_3_constellation() -> str:
out = []
out.append(
'Words cluster by meaning.'
)
# Faint grid
for gy in range(140, 441, 50):
out.append(f'')
for gx in range(150, 1381, 90):
out.append(f'')
out.append('')
out.append('')
# Cluster halos
for i, (name, c) in enumerate(CLUSTERS.items()):
delay = 300 + i * 350
out.append(
f''
)
out.append(
f'{c["label"]}'
)
# Words
for w in USER_WORDS:
out.append(f'')
out.append(
f'{_html.escape(w["word"])}'
)
# Cross-cluster line
scared = next(w for w in USER_WORDS if w["word"] == "scared")
er = next(w for w in USER_WORDS if w["word"] == "ER")
out.append(
f''
)
out.append(
''
"far apart = different meaning"
)
out.append(
'Geometry reveals meaning.'
)
return "".join(out)
# ---------- STEP 4: the AI's patterns ----------
def _step_4_patterns() -> str:
out = []
out.append(
'What the AI already knows.'
)
# Top half: dimmed user words
out.append(
'YOUR WORDS'
)
for name, c in CLUSTERS.items():
out.append(
f''
)
for w in USER_WORDS:
out.append(f'')
out.append(
f'{_html.escape(w["word"])}'
)
# Divider line
out.append(
''
)
# AI patterns header
out.append(
''
"WHAT THE AI ALREADY KNOWS"
)
out.append(
'Patterns learned from therapy transcripts, '
"medical texts, and crisis counseling."
)
# AI cluster halos
for i, (name, c) in enumerate(AI_CLUSTERS.items()):
delay = 1100 + i * 300
out.append(
f''
)
out.append(
f'{c["label"]}'
)
# AI pattern entities
for i, p in enumerate(AI_PATTERNS):
delay = 1500 + i * 220
font_style = "italic" if p["cluster"] == "ai_empathy" else "normal"
out.append(
f''
)
out.append(
f''
f'{_html.escape(p["text"])}'
)
return "".join(out)
# ---------- STEP 5: the bridge ----------
def _step_5_bridge() -> str:
out = []
out.append(
'Proximity becomes empathy.'
)
# Top half
for name, c in CLUSTERS.items():
out.append(
f''
)
for w in USER_WORDS:
out.append(f'')
out.append(
f'{_html.escape(w["word"])}'
)
# Divider
out.append(
''
)
# Bottom half
for name, c in AI_CLUSTERS.items():
out.append(
f''
)
for p in AI_PATTERNS:
font_style = "italic" if p["cluster"] == "ai_empathy" else "normal"
out.append(f'')
out.append(
f'{_html.escape(p["text"])}'
)
# Bridge lines
word_by_name = {w["word"]: w for w in USER_WORDS}
pat_by_text = {p["text"]: p for p in AI_PATTERNS}
for i, (word, pat) in enumerate(BRIDGES):
w = word_by_name.get(word)
p = pat_by_text.get(pat)
if w and p:
delay = 400 + i * 350
out.append(
f''
)
# Punchline
out.append(
''
"The empathy you feel is proximity — not comprehension."
)
return "".join(out)
# ---------- audience-mode embed viz (LLM-generated data) ----------
def render_audience_embed(prompt: str, embeddings: dict) -> str:
"""Render the audience-mode version using LLM-assigned word positions."""
from collections import defaultdict
words = (embeddings or {}).get("words", []) or []
palette = ["#F97316", "#06B6D4", "#3B82F6", "#A78BFA", "#34D399", "#F59E0B"]
groups = defaultdict(list)
for w in words:
groups[str(w.get("cluster", "default"))].append(w)
color_map = {}
for i, name in enumerate(groups.keys()):
color_map[name] = palette[i % len(palette)]
def _nx(w):
return 100 + max(0.05, min(0.95, float(w.get("x", 0.5)))) * 1300
def _ny(w):
# Incoming y is 0.25–0.48. Stretch to fill roughly 130–560.
raw = float(w.get("y", 0.35))
return 130 + max(0.0, min(1.0, (raw - 0.1) / 0.6)) * 430
svg_parts = [f"""
Step 2 · Embed
“{_html.escape(prompt)}”
Your words became vectors. Each cluster is a region of meaning in the model’s space.