Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,14 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# Log in to Hugging Face using the API key
|
| 17 |
-
login(hf_api_key)
|
| 18 |
-
|
| 19 |
-
mcp_client = None
|
| 20 |
-
|
| 21 |
-
# Initialize MCPClient with URL
|
| 22 |
-
try:
|
| 23 |
-
mcp_client = MCPClient(
|
| 24 |
-
{"url": "https://wlchee-mcp-sentiment.hf.space/gradio_api/mcp/sse"},
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# Retrieve tools from the MCP client
|
| 28 |
-
tools = mcp_client.get_tools()
|
| 29 |
-
if not tools:
|
| 30 |
-
raise Exception("No tools retrieved from the MCP client.")
|
| 31 |
-
|
| 32 |
-
# Create the model and agent
|
| 33 |
-
model = InferenceClientModel(model_id="Qwen/Qwen2.5-Omni-7B")
|
| 34 |
-
agent = CodeAgent(tools=[*tools], model=model)
|
| 35 |
-
|
| 36 |
-
# Define the Gradio interface
|
| 37 |
-
demo = gr.ChatInterface(
|
| 38 |
-
fn=lambda message, history: str(agent.run(message)),
|
| 39 |
-
type="messages",
|
| 40 |
-
examples=["Prime factorization of 68"],
|
| 41 |
-
title="Agent with MCP Tools",
|
| 42 |
-
description="This is a simple agent that uses MCP tools to answer questions.",
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# Launch the interface
|
| 46 |
-
demo.launch()
|
| 47 |
-
|
| 48 |
-
finally:
|
| 49 |
-
# Ensure MCPClient disconnects on script termination
|
| 50 |
-
if mcp_client:
|
| 51 |
-
mcp_client.disconnect(share=True,ssr_mode=False)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
if __name__ == "__main__":
|
| 4 |
+
with gr.Blocks() as demo:
|
| 5 |
+
gr.ChatInterface(
|
| 6 |
+
fn=lambda x: x,
|
| 7 |
+
title="MCP Client",
|
| 8 |
+
chatbot=gr.Chatbot(),
|
| 9 |
+
textbox=gr.Textbox(),
|
| 10 |
+
)
|
| 11 |
+
demo.launch(
|
| 12 |
+
share=True,
|
| 13 |
+
mcp_server="https://wlchee-mcp-sentiment.hf.space/gradio_api/mcp/sse" # Replace with your MCP server URL
|
| 14 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|