File size: 1,703 Bytes
159acbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
import pandas as pd
from rapidfuzz import process, fuzz

# Load dataset once
DF = pd.read_csv("./data/food_purine_mcp_ready_v2.csv")

# ---------- MCP Tools -------------------------------------------------
def lookup_food(name: str):
    """
    Return one row (food, purine, label) that exactly matches *name* (case-insensitive).
    """
    row = DF.loc[DF["food"].str.lower() == name.lower()]
    if row.empty:
        return {"error": f"No exact match for '{name}'."}
    return row.iloc[0].to_dict()

def fuzzy_search(query: str, k: int = 5, cutoff: int = 80):
    """
    Fuzzy-match *query* against the food column.
    Returns up to *k* rows with WRatio ≥ *cutoff*.
    """
    choices = DF["food"].tolist()
    matches = process.extract(
        query, choices, scorer=fuzz.WRatio, score_cutoff=cutoff, limit=k
    )
    rows = DF.loc[DF["food"].isin([m[0] for m in matches])]
    return rows.to_dict(orient="records")

# ---------- Minimal UI (optional) -------------------------------------
with gr.Blocks(title="Purine DB MCP") as demo:
    gr.Markdown("## Purine Lookup Tools (MCP-enabled)")
    with gr.Tab("Exact lookup"):
        in1 = gr.Textbox(label="Food name")
        out1 = gr.JSON()
        in1.submit(lookup_food, in1, out1)
    with gr.Tab("Fuzzy search"):
        in2 = gr.Textbox(label="Fuzzy term")
        out2 = gr.JSON()
        in2.submit(fuzzy_search, in2, out2)

# ---------- Launch ----------------------------------------------------
demo.launch(
    server_name="0.0.0.0",  # expose on container/VM
    share=False,            # True if you want a public Gradio link
    mcp_server=True,        # 🌟 <- THIS turns it into an MCP endpoint
)