Antoine Chaffin commited on
Commit
9579ef1
·
1 Parent(s): efeb0dd

Use gradio

Browse files
Files changed (1) hide show
  1. app.py +38 -76
app.py CHANGED
@@ -1,82 +1,44 @@
1
- """
2
- MCPyLate Server
3
- A Model Context Protocol server that provides search functionality using PyLate.
4
- """
5
-
6
- # import subprocess
7
-
8
- # subprocess.run(
9
- # "pip install flash-attn --no-build-isolation",
10
- # env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
11
- # shell=True,
12
- # )
13
  from typing import Any, Dict, List, Optional
14
 
 
15
  from core import MCPyLate
16
  from huggingface_hub import snapshot_download
17
- from mcp.server.fastmcp import FastMCP
18
-
19
-
20
- def register_tools(mcp: FastMCP, pylate: MCPyLate):
21
- """Register all tools with the MCP server."""
22
-
23
- @mcp.tool(
24
- name="pylate_search_leetcode",
25
- description="Perform a multi-vector search on the leetcode index. Returns top‑k hits with docid, score, and snippet.",
26
- )
27
- def pylate_search_leetcode(
28
- query: str, k: int = 10, index_name: Optional[str] = None
29
- ) -> List[Dict[str, Any]]:
30
- """
31
- Search the PyLate with multi-vector models and return top-k hits
32
- Args:
33
- query: Search query string
34
- k: Number of results to return (default: 10)
35
- index_name: Name of index to search (default: use default index)
36
- Returns:
37
- List of search results with docid, score, text snippet, and index name
38
- """
39
- return pylate.search(query, k)
40
-
41
- @mcp.tool(
42
- name="get_document",
43
- description="Retrieve a full document by its document ID from a Pyserini index.",
44
- )
45
- def get_document(
46
- docid: str, index_name: Optional[str] = None
47
- ) -> Optional[Dict[str, Any]]:
48
- """
49
- Retrieve the full text of a document by its ID.
50
-
51
- Args:
52
- docid: Document ID to retrieve
53
- index_name: Name of index to search (default: use default index)
54
-
55
- Returns:
56
- Document with full text, or None if not found
57
- """
58
- return pylate.get_document(docid, index_name)
59
-
60
-
61
- def main():
62
- """Main entry point for the server."""
63
- snapshot_download(
64
- repo_id="lightonai/leetcode_reasonmoderncolbert",
65
- local_dir="indexes/",
66
- repo_type="dataset",
67
- )
68
- try:
69
- mcp = FastMCP("pylate-search-server")
70
-
71
- mcpylate = MCPyLate()
72
- register_tools(mcp, mcpylate)
73
-
74
- mcp.run(transport="stdio")
75
-
76
- except Exception as e:
77
- print(e)
78
- raise
79
 
 
 
 
 
80
 
81
- if __name__ == "__main__":
82
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from typing import Any, Dict, List, Optional
2
 
3
+ import gradio as gr
4
  from core import MCPyLate
5
  from huggingface_hub import snapshot_download
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ """
8
+ MCPyLate Server
9
+ A Model Context Protocol server that provides search functionality using PyLate.
10
+ """
11
 
12
+ mcpylate = MCPyLate()
13
+
14
+
15
+ def pylate_search_leetcode(
16
+ query: str, k: int = 10, index_name: Optional[str] = None
17
+ ) -> List[Dict[str, Any]]:
18
+ """
19
+ Search the PyLate with multi-vector models in the leetcode collection containing code problems solutions and return top-k hits
20
+ Args:
21
+ query: Search query string
22
+ k: Number of results to return (default: 10)
23
+ index_name: Name of index to search (default: use default index)
24
+ Returns:
25
+ List of search results with docid, score, text snippet, and index name
26
+ """
27
+ return mcpylate.search(query, k)
28
+
29
+
30
+ snapshot_download(
31
+ repo_id="lightonai/leetcode_reasonmoderncolbert",
32
+ local_dir="indexes/",
33
+ repo_type="dataset",
34
+ )
35
+
36
+ demo = gr.Interface(
37
+ fn=pylate_search_leetcode,
38
+ inputs=["text"],
39
+ outputs="text",
40
+ title="MSMARCO Search",
41
+ description="Search in leetcode database index using PyLate",
42
+ )
43
+
44
+ demo.launch(mcp_server=True)