Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from rapidfuzz import process, fuzz
|
| 4 |
+
|
| 5 |
+
# Load dataset once
|
| 6 |
+
DF = pd.read_csv("./data/food_purine_mcp_ready_v2.csv")
|
| 7 |
+
|
| 8 |
+
# ---------- MCP Tools -------------------------------------------------
|
| 9 |
+
def lookup_food(name: str):
|
| 10 |
+
"""
|
| 11 |
+
Return one row (food, purine, label) that exactly matches *name* (case-insensitive).
|
| 12 |
+
"""
|
| 13 |
+
row = DF.loc[DF["food"].str.lower() == name.lower()]
|
| 14 |
+
if row.empty:
|
| 15 |
+
return {"error": f"No exact match for '{name}'."}
|
| 16 |
+
return row.iloc[0].to_dict()
|
| 17 |
+
|
| 18 |
+
def fuzzy_search(query: str, k: int = 5, cutoff: int = 80):
|
| 19 |
+
"""
|
| 20 |
+
Fuzzy-match *query* against the food column.
|
| 21 |
+
Returns up to *k* rows with WRatio ≥ *cutoff*.
|
| 22 |
+
"""
|
| 23 |
+
choices = DF["food"].tolist()
|
| 24 |
+
matches = process.extract(
|
| 25 |
+
query, choices, scorer=fuzz.WRatio, score_cutoff=cutoff, limit=k
|
| 26 |
+
)
|
| 27 |
+
rows = DF.loc[DF["food"].isin([m[0] for m in matches])]
|
| 28 |
+
return rows.to_dict(orient="records")
|
| 29 |
+
|
| 30 |
+
# ---------- Minimal UI (optional) -------------------------------------
|
| 31 |
+
with gr.Blocks(title="Purine DB MCP") as demo:
|
| 32 |
+
gr.Markdown("## Purine Lookup Tools (MCP-enabled)")
|
| 33 |
+
with gr.Tab("Exact lookup"):
|
| 34 |
+
in1 = gr.Textbox(label="Food name")
|
| 35 |
+
out1 = gr.JSON()
|
| 36 |
+
in1.submit(lookup_food, in1, out1)
|
| 37 |
+
with gr.Tab("Fuzzy search"):
|
| 38 |
+
in2 = gr.Textbox(label="Fuzzy term")
|
| 39 |
+
out2 = gr.JSON()
|
| 40 |
+
in2.submit(fuzzy_search, in2, out2)
|
| 41 |
+
|
| 42 |
+
# ---------- Launch ----------------------------------------------------
|
| 43 |
+
demo.launch(
|
| 44 |
+
server_name="0.0.0.0", # expose on container/VM
|
| 45 |
+
share=False, # True if you want a public Gradio link
|
| 46 |
+
mcp_server=True, # 🌟 <- THIS turns it into an MCP endpoint
|
| 47 |
+
)
|