Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# --------------------------------------------------------------
|
| 2 |
+
# vector_mcp_only.py — Standalone Vector MCP Tool Gradio App
|
| 3 |
+
# --------------------------------------------------------------
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
+
from urllib.parse import quote
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def query_vector_agent_calling(user_query: str, collection_name: str) -> str:
|
| 10 |
+
"""
|
| 11 |
+
Standalone Vector Search MCP tool.
|
| 12 |
+
Calls HF API with URL-encoded collection name.
|
| 13 |
+
"""
|
| 14 |
+
base_url = "https://srivatsavdamaraju-mvp-2-0-deploy-all-apis.hf.space/qdrant/search"
|
| 15 |
+
encoded_collection = quote(collection_name, safe="")
|
| 16 |
+
|
| 17 |
+
url = f"{base_url}?collection_name={encoded_collection}&mode=hybrid"
|
| 18 |
+
|
| 19 |
+
headers = {
|
| 20 |
+
"accept": "application/json",
|
| 21 |
+
"Content-Type": "application/json",
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
payload = {"query": user_query, "top_k": 5}
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
response = requests.post(url, headers=headers, json=payload, timeout=30)
|
| 28 |
+
response.raise_for_status()
|
| 29 |
+
|
| 30 |
+
data = response.json()
|
| 31 |
+
results = data.get("results") or data.get("result") or []
|
| 32 |
+
|
| 33 |
+
if not results:
|
| 34 |
+
return "⚠️ No relevant results found."
|
| 35 |
+
|
| 36 |
+
output = []
|
| 37 |
+
for r in results:
|
| 38 |
+
text = (
|
| 39 |
+
r.get("text")
|
| 40 |
+
or r.get("payload", {}).get("text")
|
| 41 |
+
or str(r)
|
| 42 |
+
)
|
| 43 |
+
if text:
|
| 44 |
+
score = r.get("score", "?")
|
| 45 |
+
output.append(f"Score: {score}\n{text}\n---")
|
| 46 |
+
|
| 47 |
+
return "\n".join(output)
|
| 48 |
+
|
| 49 |
+
except requests.exceptions.Timeout:
|
| 50 |
+
return "⏳ Request timed out. Try again."
|
| 51 |
+
except requests.exceptions.HTTPError as e:
|
| 52 |
+
return f"HTTP Error: {e.response.status_code}\n{e.response.text}"
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"Unexpected Error: {str(e)}"
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# ------------------ Gradio UI ------------------
|
| 58 |
+
with gr.Blocks() as app:
|
| 59 |
+
gr.Markdown("## 🔍 Vector Search MCP Tool (Standalone)")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
user_query = gr.Textbox(label="Your Query", placeholder="e.g., gold market trend", lines=2)
|
| 63 |
+
collection_name = gr.Textbox(label="Collection Name", placeholder="e.g., gold&silver-reports")
|
| 64 |
+
|
| 65 |
+
btn = gr.Button("Run Vector Search")
|
| 66 |
+
out = gr.Textbox(label="Search Result", lines=10)
|
| 67 |
+
|
| 68 |
+
btn.click(query_vector_agent_calling, [user_query, collection_name], out)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
app.launch(server_name="0.0.0.0", server_port=7861, mcp_server=True)
|