mathieunlp's picture
Create app.py
159acbe verified
raw
history blame
1.7 kB
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
)