Spaces:
Running
Running
| import gradio as gr | |
| import torch | |
| from mcp.client.stdio import StdioServerParameters | |
| from smolagents import ToolCollection, CodeAgent | |
| from smolagents import CodeAgent, ToolCallingAgent, InferenceClientModel, TransformersModel | |
| from smolagents.mcp_client import MCPClient | |
| # model = InferenceClientModel() | |
| hf_model = "Qwen/Qwen2.5-7B-Instruct" | |
| model = TransformersModel(model_id=hf_model, device_map="cuda", torch_dtype=torch.bfloat16) | |
| test_question = """What's this video about? https://www.youtube.com/watch?v=R3GfuzLMPkA""" | |
| tool_call_prompt = """Be careful when calling for tools. This is how a parameter of string type is set - 'parameter': '720'. | |
| A parameter of int type is set like this - 'parameter': 720. A parameter of the bool type is set like this - 'parameter': True. | |
| """ | |
| try: | |
| mcp_client = MCPClient( | |
| {"url": "https://artyomboyko-test-mcp-2.hf.space/gradio_api/mcp/sse"} | |
| ) | |
| tools = mcp_client.get_tools() | |
| # agent = ToolCallingAgent(tools=[*tools], model=model) | |
| agent = ToolCallingAgent(tools=[*tools], model=model, max_steps=20, planning_interval=1) | |
| agent.prompt_templates["system_prompt"] = agent.prompt_templates["system_prompt"] + tool_call_prompt | |
| def call_agent(message, history): | |
| return str(agent.run(message)) | |
| demo = gr.ChatInterface( | |
| fn=call_agent, | |
| type="messages", | |
| examples=[test_question], | |
| title="Agent with MCP Tools", | |
| description="This is a simple agent that uses MCP tools to answer questions.", | |
| ) | |
| demo.launch() | |
| except Exception as e: | |
| raise e | |
| finally: | |
| mcp_client.stop() | |