Spaces:
Running
Running
Claude Fable 5.1
Embed every evidence table, read the winners' shot-by-shot analysis, and use the imported hook clips' tags
d38fa83 Download scripts/backfill_embeddings.py from userIdc2024/creative-brain-os: direct link, hf CLI and curl.
- Browser
- Download file 3.16 kB
-
https://huggingface.co/spaces/userIdc2024/creative-brain-os/resolve/main/scripts/backfill_embeddings.py
- Command line
-
hf download hf://spaces/userIdc2024/creative-brain-os/scripts/backfill_embeddings.py
-
curl -L -o backfill_embeddings.py https://huggingface.co/spaces/userIdc2024/creative-brain-os/resolve/main/scripts/backfill_embeddings.py
3.16 kB
| """Backfill semantic embeddings into the Supabase vector(1536) columns so | |
| retrieval can rank by meaning, not word overlap. | |
| Usage: | |
| python scripts/backfill_embeddings.py # all tables, all missing rows | |
| python scripts/backfill_embeddings.py hooks broll # only these tables | |
| python scripts/backfill_embeddings.py hooks 5 # a table + max rows (for testing) | |
| python scripts/backfill_embeddings.py concepts --all # re-embed rows that ALREADY have one | |
| Idempotent by default: only embeds rows whose embedding is currently NULL, so | |
| re-running it after adding rows costs nothing for the rows already done. | |
| That default is also its one trap: an EDITED row keeps its old embedding and is | |
| skipped, so it goes on ranking by what it used to say. Pass --all after editing a | |
| table to re-embed every row in it. | |
| """ | |
| from __future__ import annotations | |
| import sys | |
| from app.repositories.intelligence import embedding_text | |
| from app.services.gemini import GeminiService | |
| from app.services.supabase_store import SupabaseStore | |
| # Every table match_rows can search (intelligence.VECTOR_TABLES). `hooks` was | |
| # renamed hooks_text, and videos/hooks_video/leads were never listed: 2000+ rows | |
| # sat with a null embedding, so vector retrieval returned nothing for them and | |
| # the pool fell back to the newest 150 by insertion order. | |
| TABLES = ["videos", "hooks_video", "hooks_text", "leads", "sops", "frameworks", | |
| "learning_content", "broll", "pain_points", "concepts"] | |
| BATCH = 32 | |
| def _vec_literal(vec: list[float]) -> str: | |
| return "[" + ",".join(f"{x:.6f}" for x in vec) + "]" | |
| def backfill( | |
| table: str, | |
| gemini: GeminiService, | |
| store: SupabaseStore, | |
| max_rows: int | None, | |
| refresh: bool = False, | |
| ) -> int: | |
| # PostgREST caps a page at 1000 rows; hooks_video alone has 1700+. | |
| rows: list[dict] = [] | |
| while True: | |
| page = store.select(table, limit=1000, offset=len(rows)) | |
| rows.extend(page) | |
| if len(page) < 1000: | |
| break | |
| pending = [ | |
| r | |
| for r in rows | |
| if (refresh or not r.get("embedding")) and embedding_text(table, r).strip() | |
| ] | |
| if max_rows: | |
| pending = pending[:max_rows] | |
| print(f"[{table}] {len(pending)} rows to embed (of {len(rows)} total)") | |
| done = 0 | |
| for i in range(0, len(pending), BATCH): | |
| chunk = pending[i : i + BATCH] | |
| vecs = gemini.embed([embedding_text(table, r)[:8000] for r in chunk]) | |
| for row, vec in zip(chunk, vecs): | |
| store.update(table, {"id": row["id"]}, {"embedding": _vec_literal(vec)}) | |
| done += len(chunk) | |
| print(f" [{table}] {done}/{len(pending)}") | |
| return done | |
| def main() -> None: | |
| args = [a for a in sys.argv[1:]] | |
| refresh = "--all" in args | |
| args = [a for a in args if a != "--all"] | |
| max_rows = None | |
| if args and args[-1].isdigit(): | |
| max_rows = int(args.pop()) | |
| tables = args or TABLES | |
| gemini, store = GeminiService(), SupabaseStore() | |
| total = sum(backfill(t, gemini, store, max_rows, refresh) for t in tables) | |
| print(f"Done. Embedded {total} rows across {len(tables)} table(s).") | |
| if __name__ == "__main__": | |
| main() | |