Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files99 unmodified lines100101102103104105106131 unmodified lines2382392402412422432442452462472482492502512526 unmodified lines25926026126226326426526626726826927027127227327427527627727827928028128228399 unmodified linescache = ec.VectorCache(CACHE_DIR, key)mat = _matrix_from_cache(cache, nouns)if mat is None:# The build step should have covered this; still serve if not.print(f"cold cache, embedding {len(nouns)} nouns "f"(pooling={POOLING!r})...", file=sys.stderr)vecs = get_embedder()([PREFIX_NOUN + n for n in nouns])131 unmodified lineswith gr.Blocks(title="Embed Compare — place nouns") as demo:gr.Markdown("# Where does your sentence land among 627 place nouns?\n""Embeds your text with **Octen-Embedding-0.6B** (Qwen3-Embedding base, ""Q4_K_M, llama.cpp) and ranks the generic place nouns in ""`generic_nouns.txt` by cosine similarity — the same model, pooling and ""ranking as the `embed_compare.py` CLI in this repo.\n\n""*Similarity is a rough signal: with last-token pooling, a long ""sentence mostly reflects how it ends.*")with gr.Tab("Text vs. nouns"):a = gr.Textbox(label="Text A", lines=3,placeholder="We hiked along the ridge above the fjord…")b = gr.Textbox(label="Text B (optional — compare two texts)",6 unmodified linesbtn.click(compare, [a, b, k], [out, summary], api_name="compare")a.submit(compare, [a, b, k], [out, summary])with gr.Tab("Noun → nearest nouns"):gr.Markdown("Pick a noun to see which other place nouns sit closest ""to it in embedding space.")with gr.Row():pick = gr.Dropdown(choices=noun_choices(), value="valley",label="Noun", filterable=True, scale=3)k2 = gr.Slider(5, 50, value=15, step=1, label="Neighbours", scale=2)btn2 = gr.Button("Find neighbours", variant="primary")out2 = gr.HTML()summary2 = gr.Markdown()btn2.click(lookup, [pick, k2], [out2, summary2], api_name="lookup")with gr.Accordion("How it works", open=False):gr.Markdown(f"- **Model:** `Octen-Embedding-0.6B.Q4_K_M.gguf` — 1024 dims, "f"last-token pooling, L2-normalised, no instruction prefix "f"(per the model card).\n"f"- **Nouns:** {len(ec.load_nouns(NOUNS_PATH))} entries from "f"`generic_nouns.txt`, embedded once at image build time.\n"f"- **Score:** cosine similarity. Both sides are unit length, so "f"this is the dot product.\n"f"- **CLI:** `python3 embed_compare.py \"your text\" --top 10`; "99 unmodified lines100101102103104105106107131 unmodified lines2392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712726 unmodified lines27928028128228328428528628728828929029129299 unmodified linescache = ec.VectorCache(CACHE_DIR, key)mat = _matrix_from_cache(cache, nouns)if mat is None:# No matching matrix for this model/pooling: build it here.# Slow on 1 vCPU (~6 min), which is why the default key ships.print(f"cold cache, embedding {len(nouns)} nouns "f"(pooling={POOLING!r})...", file=sys.stderr)vecs = get_embedder()([PREFIX_NOUN + n for n in nouns])131 unmodified lineswith gr.Blocks(title="Embed Compare — place nouns") as demo:gr.Markdown("# Explore 627 place nouns by embedding similarity\n""Every noun in `generic_nouns.txt` is embedded with ""**Octen-Embedding-0.6B** (Qwen3-Embedding base, Q4_K_M, llama.cpp) — ""the same model, pooling and ranking as the `embed_compare.py` CLI in ""this repo. Find a noun's nearest neighbours, or rank all of them ""against your own sentence.\n\n""*Similarity is a rough signal: with last-token pooling, a long ""sentence mostly reflects how it ends.*")with gr.Tab("Noun → nearest nouns"):gr.Markdown("Pick a noun to see which other place nouns sit closest ""to it in embedding space. Instant: this is a dot product ""against the precomputed matrix.")with gr.Row():pick = gr.Dropdown(choices=noun_choices(), value="valley",label="Noun", filterable=True, scale=3)k2 = gr.Slider(5, 50, value=15, step=1, label="Neighbours", scale=2)btn2 = gr.Button("Find neighbours", variant="primary")out2 = gr.HTML()summary2 = gr.Markdown()btn2.click(lookup, [pick, k2], [out2, summary2], api_name="lookup")with gr.Tab("Text vs. nouns"):gr.Markdown("Embeds your sentence and ranks all 627 nouns against it. This runs ""one forward pass on a single CPU core, so expect **about a ""minute** per request on this Space's free hardware.")a = gr.Textbox(label="Text A", lines=3,placeholder="We hiked along the ridge above the fjord…")b = gr.Textbox(label="Text B (optional — compare two texts)",6 unmodified linesbtn.click(compare, [a, b, k], [out, summary], api_name="compare")a.submit(compare, [a, b, k], [out, summary])with gr.Accordion("How it works", open=False):gr.Markdown(f"- **Model:** `Octen-Embedding-0.6B.Q4_K_M.gguf` — 1024 dims, "f"last-token pooling, L2-normalised, no instruction prefix "f"(per the model card).\n"f"- **Nouns:** {len(ec.load_nouns(NOUNS_PATH))} entries from "f"`generic_nouns.txt`. Their vectors ship with the repo as a "f"2.6 MB float32 matrix, so startup never re-embeds them.\n"f"- **Score:** cosine similarity. Both sides are unit length, so "f"this is the dot product.\n"f"- **CLI:** `python3 embed_compare.py \"your text\" --top 10`; "
app.py+28-19