File size: 1,743 Bytes
c59fd1e
 
d3d8b0b
c59fd1e
 
 
 
 
d3d8b0b
 
 
 
 
 
 
c59fd1e
d3d8b0b
c59fd1e
d3d8b0b
 
818d0b1
d3d8b0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
import json
import logging
import os
from dotenv import load_dotenv

from smolagents import InferenceClientModel, CodeAgent, MCPClient

logging.basicConfig(
    level=logging.INFO,  # Set the logging level
    format="%(asctime)s - %(levelname)s - %(message)s",  # Define the log format
    handlers=[
        logging.StreamHandler()  # Ensure logs are displayed on the console
    ]
)

load_dotenv()

def run(message, agent):
    result = str(agent.run(message))
    logging.info(f"Input: {message}, Output: {result}")
    return result


if __name__ == "__main__":
    logging.info("Starting the agent with MCP tools...")
    try:
        mcp_client = MCPClient(
            {
                "url": "https://vikaskapur-mcp-server-sentiment.hf.space/gradio_api/mcp/sse",
                "transport": 'sse'
            }
        )
        tools = mcp_client.get_tools()

        # Print tools in a readable format
        logging.info("Tools retrieved:")
        logging.info("\n".join(f"{t.name}: {t.description}" for t in tools))

        model = InferenceClientModel(token=os.getenv("HUGGINGFACE_API_TOKEN"))
        logging.info(f"Model: {model.model_id}")

        agent = CodeAgent(tools=[*tools], model=model, additional_authorized_imports=["json", "ast", "urllib", "base64"], )

        demo = gr.Interface(
            fn=lambda message: run(message, agent),
            inputs="text",
            outputs="text",
            examples=["Analyze the sentiment of the following text 'This is awesome'"],
            title="Agent with MCP Tools",
            description="This is a simple agent that uses MCP tools to answer questions.",
        )

        demo.launch()
    finally:
        mcp_client.disconnect()