Spaces:
Running
Running
File size: 4,055 Bytes
84656d9 8916943 84656d9 fecfaaa 84656d9 8916943 84656d9 8916943 84656d9 8916943 84656d9 8916943 84656d9 8916943 84656d9 6af2c47 84656d9 6af2c47 8916943 84656d9 | 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | import os
import json
import requests
import gradio as gr
# ========== ้
็ฝฎ ==========
API_URL = "https://connect.patsnap.com/096456/Logic-mcp"
API_KEY = os.getenv("PATSNAP_API_KEY", "")
HEADERS = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
}
def call_mcp_tool(tool_name: str, arguments: dict) -> dict:
"""้็จ็ MCP ๅทฅๅ
ท่ฐ็จๅฝๆฐ"""
payload = {
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": tool_name,
"arguments": arguments,
},
}
response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
response.raise_for_status()
return response.json()
def search_drugs(drug_name, target, disease, phase, limit):
"""่ฐ็จ ls_drug_search ๅนถๆ ผๅผๅ็ปๆ"""
if not API_KEY:
return "โ Error: PATSNAP_API_KEY not set. Please add it as a Space Secret."
args = {}
if drug_name.strip():
args["drug"] = [drug_name.strip()]
if target.strip():
args["target"] = [target.strip()]
if disease.strip():
args["disease"] = [disease.strip()]
if phase:
args["highest_phase"] = [phase]
args["limit"] = int(limit)
try:
result = call_mcp_tool("ls_drug_search", args)
content = result.get("result", {}).get("content", [{}])
text = content[0].get("text", json.dumps(result)) if content else json.dumps(result)
data = json.loads(text) if isinstance(text, str) else text
total_hits = data.get("total_hits", 0)
results_list = data.get("results", [])
if not results_list:
return f"๐ญ No drugs found (total: {total_hits})"
output = f"### Search Results (showing {len(results_list)} of {total_hits} total hits)\n\n"
output += "| Drug Name | Highest Phase | Target | Disease | Organization |\n"
output += "|-----------|---------------|--------|---------|--------------|\n"
for row in results_list:
name = row.get("drug_name", row.get("name", "N/A"))
phase_val = row.get("highest_phase", "N/A")
target_val = row.get("target", "N/A")
disease_val = row.get("disease", "N/A")
org = row.get("organization", "N/A")
output += f"| {name} | {phase_val} | {target_val} | {disease_val} | {org} |\n"
return output
except Exception as e:
return f"โ Search failed: {str(e)}"
# ========== Gradio ็้ข ==========
with gr.Blocks(title="PatSnap Pharma Intelligence โ Drug Search") as demo:
gr.Markdown("""
# ๐ PatSnap Pharma Intelligence โ Drug Search
Search PatSnap's global pharmaceutical database for drugs matching your criteria.
**How to use**:
1. Fill in one or more search fields (drug name, target, disease, phase).
2. Click **Search** to retrieve matching drug candidates.
> ๐ **API Key required**: Set `PATSNAP_API_KEY` as a Space Secret.
""")
with gr.Row():
with gr.Column(scale=1):
drug_name = gr.Textbox(label="Drug Name", placeholder="e.g. pembrolizumab")
target = gr.Textbox(label="Target", placeholder="e.g. PD-1")
disease = gr.Textbox(label="Disease", placeholder="e.g. non-small cell lung cancer")
phase = gr.Dropdown(
choices=[
"discovery", "preclinical", "phase_1", "phase_2", "phase_3",
"approved", "phase_4"
],
label="Highest Phase",
value=None,
)
limit = gr.Slider(1, 50, value=10, step=1, label="Results Limit")
search_btn = gr.Button("๐ Search", variant="primary")
with gr.Column(scale=2):
output_md = gr.Markdown("### Results will appear here")
search_btn.click(
fn=search_drugs,
inputs=[drug_name, target, disease, phase, limit],
outputs=[output_md],
)
if __name__ == "__main__":
demo.launch() |