Spaces:
Sleeping
Sleeping
Commit ·
c59fd1e
0
Parent(s):
mcp sentiment analysis host
Browse files- .gitignore +1 -0
- host.py +40 -0
- requirements.txt +3 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
host.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
from smolagents import InferenceClientModel, CodeAgent, MCPClient
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
mcp_client = MCPClient(
|
| 13 |
+
{
|
| 14 |
+
"url": "https://vikaskapur-mcp-server-sentiment.hf.space/gradio_api/mcp/sse",
|
| 15 |
+
"transport": 'sse'
|
| 16 |
+
}
|
| 17 |
+
)
|
| 18 |
+
tools = mcp_client.get_tools()
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Print tools in a readable format
|
| 22 |
+
print("Tools retrieved:")
|
| 23 |
+
print("\n".join(f"{t.name}: {t.description}" for t in tools))
|
| 24 |
+
|
| 25 |
+
model = InferenceClientModel(token=os.getenv("HUGGINGFACE_API_TOKEN"))
|
| 26 |
+
print(f"Model: {model.model_id}")
|
| 27 |
+
|
| 28 |
+
agent = CodeAgent(tools=[*tools], model=model, additional_authorized_imports=["json", "ast", "urllib", "base64"])
|
| 29 |
+
|
| 30 |
+
demo = gr.ChatInterface(
|
| 31 |
+
fn=lambda message, history: str(agent.run(message)),
|
| 32 |
+
type="messages",
|
| 33 |
+
examples=["Analyze the sentiment of the following text 'This is awesome'"],
|
| 34 |
+
title="Agent with MCP Tools",
|
| 35 |
+
description="This is a simple agent that uses MCP tools to answer questions.",
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
demo.launch()
|
| 39 |
+
finally:
|
| 40 |
+
mcp_client.disconnect()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python-dotenv
|
| 2 |
+
gradio[mcp]
|
| 3 |
+
smolagents[mcp]
|