sum-guy-429's picture
Deploy
db54b68 verified
Raw History Blame Contribute Delete
3.86 kB
"""Base120 Explorer: browse the 120 Base120 reasoning operators (snapshot of base120 3.1.0)."""
import html
import re
import gradio as gr
import pandas as pd
from huggingface_hub import hf_hub_download
DATASET = "hummbl-hf/base120"
REVISION = "792d69f18da12e4235fb60fe76aa0d25e9e53cfc" # pinned dataset commit
path = hf_hub_download(DATASET, "data/operators.parquet", repo_type="dataset", revision=REVISION)
DF = pd.read_parquet(path).sort_values(["transformation", "index"]).reset_index(drop=True)
FAMILIES = {"P": "Perspective", "IN": "Inversion", "CO": "Composition",
"DE": "Decomposition", "RE": "Recursion", "SY": "Systems"}
FAMILY_CHOICES = ["ALL"] + [f"{k} — {v}" for k, v in FAMILIES.items()]
STOP = set("a an the and or of to in on for with is are be by as at from that this it into how what why".split())
def card(row) -> str:
return (f"<div style='border:1px solid #8884;border-radius:8px;padding:10px;margin:6px 0'>"
f"<b>{html.escape(row['code'])}</b> · <i>{html.escape(row['family_name'])}</i><br>"
f"<span style='font-size:1.1em'>{html.escape(row['name'])}</span><br>"
f"{html.escape(row['definition'])}</div>")
def browse(family: str, text: str):
d = DF
if family and family != "ALL":
d = d[d["transformation"] == family.split(" ")[0]]
if text:
t = text.lower()
d = d[d["name"].str.lower().str.contains(t, regex=False) | d["definition"].str.lower().str.contains(t, regex=False)]
cards = "".join(card(r) for _, r in d.iterrows()) or "<p>No operators match.</p>"
return f"<p>{len(d)} operator(s)</p>" + cards
def pick(problem: str, k: int):
words = {w for w in re.findall(r"[a-z]+", (problem or "").lower()) if w not in STOP and len(w) > 2}
if not words:
return "<p>Describe a problem in a sentence or two.</p>"
stems = {w[:5] for w in words}
def score(r):
toks = {t[:5] for t in re.findall(r"[a-z]+", (r["name"] + " " + r["definition"]).lower()) if len(t) > 2}
return len(stems & toks)
d = DF.assign(score=DF.apply(score, axis=1))
d = d[d.score > 0].sort_values(["score", "code"], ascending=[False, True]).head(int(k))
if d.empty:
return "<p>No keyword overlap. Try other words, or browse by family.</p>"
return ("<p><i>Keyword overlap only (no AI model); a starting point, not a recommendation.</i></p>"
+ "".join(card(r) for _, r in d.iterrows()))
def random_op():
return card(DF.sample(1).iloc[0])
with gr.Blocks(title="Base120 Explorer") as demo:
gr.Markdown(
"# Base120 Explorer\n"
"120 named reasoning operators in 6 transformation families "
"(**P** Perspective, **IN** Inversion, **CO** Composition, **DE** Decomposition, **RE** Recursion, **SY** Systems).\n\n"
f"Data: [`{DATASET}`](https://huggingface.co/datasets/{DATASET}) at revision `{REVISION[:7]}`, "
"a snapshot of the `base120` **3.1.0** PyPI package. Wording can differ slightly from other Base120 sources. "
"These are reasoning heuristics, not validated methods.")
with gr.Tab("Browse"):
with gr.Row():
fam = gr.Dropdown(FAMILY_CHOICES, value="ALL", label="Family")
q = gr.Textbox(label="Search name/definition")
out = gr.HTML(browse("ALL", ""))
fam.change(browse, [fam, q], out)
q.change(browse, [fam, q], out)
with gr.Tab("Keyword picker"):
prob = gr.Textbox(label="Describe your problem", lines=3)
k = gr.Slider(1, 15, value=6, step=1, label="How many")
btn = gr.Button("Suggest operators")
pout = gr.HTML()
btn.click(pick, [prob, k], pout)
with gr.Tab("Random"):
rb = gr.Button("Random operator")
rout = gr.HTML()
rb.click(random_op, None, rout)
if __name__ == "__main__":
demo.launch()