captain-awesome commited on
Commit
14fff99
·
verified ·
1 Parent(s): bc40118

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -61,11 +61,11 @@ from smolagents.mcp_client import MCPClient
61
  from transformers import AutoModelForCausalLM
62
  import torch
63
 
64
- async def main():
65
  mcp_client = MCPClient({"url": "https://captain-awesome-alquranchapters.hf.space/gradio_api/mcp/sse"})
66
  await mcp_client.connect() # or use async context manager if supported
67
 
68
- tools = await mcp_client.get_tools()
69
 
70
  model_id = "unsloth/Llama-3.2-1B"
71
  model = AutoModelForCausalLM.from_pretrained(
@@ -73,6 +73,7 @@ async def main():
73
  torch_dtype=torch.bfloat16,
74
  device_map="auto"
75
  )
 
76
 
77
  agent = CodeAgent(tools=tools, model=model)
78
 
@@ -81,15 +82,15 @@ async def main():
81
  return str(response)
82
 
83
  demo = gr.ChatInterface(
84
- fn=gradio_fn,
85
  type="messages",
86
  title="Agent with MCP Tools",
87
  description="This is a simple agent that uses MCP tools to get chapters of the Quran.",
88
  )
 
 
89
 
90
- demo.launch(share=True)
91
-
92
- await mcp_client.disconnect()
93
 
94
- if __name__ == "__main__":
95
- asyncio.run(main())
 
61
  from transformers import AutoModelForCausalLM
62
  import torch
63
 
64
+ try:
65
  mcp_client = MCPClient({"url": "https://captain-awesome-alquranchapters.hf.space/gradio_api/mcp/sse"})
66
  await mcp_client.connect() # or use async context manager if supported
67
 
68
+ tools = mcp_client.get_tools()
69
 
70
  model_id = "unsloth/Llama-3.2-1B"
71
  model = AutoModelForCausalLM.from_pretrained(
 
73
  torch_dtype=torch.bfloat16,
74
  device_map="auto"
75
  )
76
+
77
 
78
  agent = CodeAgent(tools=tools, model=model)
79
 
 
82
  return str(response)
83
 
84
  demo = gr.ChatInterface(
85
+ fn=lambda message, history: str(agent.run(message)),
86
  type="messages",
87
  title="Agent with MCP Tools",
88
  description="This is a simple agent that uses MCP tools to get chapters of the Quran.",
89
  )
90
+
91
+ demo.launch()
92
 
93
+ # demo.launch(share=True)
 
 
94
 
95
+ finally:
96
+ mcp_client.disconnect()