alihmaou commited on
Commit
8018fc5
·
1 Parent(s): e90d811

Smol adaptations to avoid reloading the server

Browse files
Files changed (1) hide show
  1. app.py +24 -18
app.py CHANGED
@@ -2,26 +2,32 @@ import gradio as gr
2
  import os
3
 
4
  from mcp import StdioServerParameters
5
- from smolagents import InferenceClientModel, CodeAgent, ToolCollection, MCPClient
6
 
 
7
 
8
- try:
9
- mcp_client = MCPClient(
10
- {"url": "https://alihmaou-mcp-tools.hf.space/gradio_api/mcp/sse"} # This is the MCP Server we created in the previous section
11
- )
12
- tools = mcp_client.get_tools()
 
 
13
 
14
- model = InferenceClientModel(token=os.getenv("HUGGINGFACE_API_TOKEN"))
15
- agent = CodeAgent(tools=[*tools], model=model)
 
 
 
16
 
17
- demo = gr.ChatInterface(
18
- fn=lambda message, history: str(agent.run(message)),
19
- type="messages",
20
- examples=["Combien de nombres premiers entre 0 et 500 ?"],
21
- title="MCP Tools tester",
22
- description="MCP tools tester connected to https://alihmaou-mcp-tools.hf.space/gradio_api/mcp/sse",
23
- )
 
24
 
25
- demo.launch()
26
- finally:
27
- mcp_client.disconnect()
 
2
  import os
3
 
4
  from mcp import StdioServerParameters
5
+ from smolagents import InferenceClientModel, CodeAgent, MCPClient
6
 
7
+ HF_MCP_URL = "https://alihmaou-mcp-tools.hf.space/gradio_api/mcp/sse"
8
 
9
+ mcp_client = MCPClient({"url": HF_MCP_URL})
10
+ model = InferenceClientModel(token=os.getenv("HUGGINGFACE_API_TOKEN"))
11
+ tools = mcp_client.get_tools()
12
+ agent = CodeAgent(tools=tools, model=model)
13
+
14
+ def run_agent(message, history):
15
+ return str(agent.run(message))
16
 
17
+ def reload_tools():
18
+ global tools, agent
19
+ tools = mcp_client.get_tools()
20
+ agent = CodeAgent(tools=tools, model=model)
21
+ return "\n".join(f"- **{t.name}**: {t.description}" for t in tools)
22
 
23
+ with gr.Blocks() as demo:
24
+ with gr.Row():
25
+ with gr.Column(scale=2):
26
+ chatbot = gr.ChatInterface(fn=run_agent)
27
+ with gr.Column(scale=1):
28
+ tool_list = gr.Markdown(value=reload_tools())
29
+ reload_btn = gr.Button("🔄 Recharger les outils MCP")
30
+ reload_btn.click(fn=reload_tools, outputs=tool_list)
31
 
32
+ demo.launch()
33
+ mcp_client.disconnect()