wlchee commited on
Commit
dfbcd98
·
verified ·
1 Parent(s): 52860b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -1,21 +1,25 @@
1
  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://wlchee-mcp-sentiment.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("mcp2"))
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=["Prime factorization of 68"],
21
  title="Agent with MCP Tools",
@@ -23,5 +27,7 @@ try:
23
  )
24
 
25
  demo.launch()
 
 
26
  finally:
27
- mcp_client.disconnect()
 
1
  import gradio as gr
2
  import os
3
+ from mcp.client.stdio import StdioServerParameters
4
+ from smolagents import ToolCollection, CodeAgent
5
+ from smolagents import CodeAgent, InferenceClientModel
6
+ from smolagents.mcp_client import MCPClient
7
 
8
+ model = InferenceClientModel(token=os.getenv("mcp2")
 
 
9
 
10
  try:
11
  mcp_client = MCPClient(
12
+ {"url": "https://wlchee-mcp-sentiment.hf.space/gradio_api/mcp/sse"}
13
  )
 
14
 
15
+ tools = mcp_client.get_tools()
16
  agent = CodeAgent(tools=[*tools], model=model)
17
 
18
+ def call_agent(message, history):
19
+ return str(agent.run(message))
20
+
21
  demo = gr.ChatInterface(
22
+ fn=call_agent,
23
  type="messages",
24
  examples=["Prime factorization of 68"],
25
  title="Agent with MCP Tools",
 
27
  )
28
 
29
  demo.launch()
30
+ except Exception as e:
31
+ raise e
32
  finally:
33
+ mcp_client.stop()