Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
|
|
| 1 |
import pathlib
|
| 2 |
import gradio as gr
|
| 3 |
from mcp import StdioServerParameters
|
| 4 |
from smolagents import MCPClient, CodeAgent
|
|
|
|
| 5 |
|
| 6 |
-
# Absolute path to the MCP server script that lives next to this file
|
| 7 |
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(message: str, history: list):
|
| 11 |
-
"""Send the user message to the MCP
|
| 12 |
-
# Launch the MCP server as a subprocess via stdio
|
| 13 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
|
|
|
|
|
|
| 14 |
with MCPClient(params) as tools:
|
| 15 |
-
agent = CodeAgent(tools=tools, model=
|
| 16 |
answer = agent.run(message)
|
| 17 |
|
| 18 |
-
# Update chat history using the new Gradio 'messages' format
|
| 19 |
history.append({"role": "user", "content": message})
|
| 20 |
history.append({"role": "assistant", "content": answer})
|
| 21 |
return history, history
|
|
@@ -23,11 +28,9 @@ def respond(message: str, history: list):
|
|
| 23 |
|
| 24 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 25 |
chat_state = gr.State([])
|
| 26 |
-
|
| 27 |
chatbot = gr.Chatbot(label="Enterprise SQL Agent", type="messages")
|
| 28 |
textbox = gr.Textbox(
|
| 29 |
-
placeholder="Ask: Who are my inactive Northeast customers?",
|
| 30 |
-
show_label=False,
|
| 31 |
)
|
| 32 |
textbox.submit(respond, [textbox, chat_state], [chatbot, chat_state])
|
| 33 |
|
|
@@ -38,7 +41,7 @@ with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
|
| 38 |
- List customers sorted by last order date.
|
| 39 |
- Find clients from the West with recent orders.
|
| 40 |
|
| 41 |
-
_Built with MCP, smolagents, and
|
| 42 |
"""
|
| 43 |
)
|
| 44 |
|
|
|
|
| 1 |
+
import os
|
| 2 |
import pathlib
|
| 3 |
import gradio as gr
|
| 4 |
from mcp import StdioServerParameters
|
| 5 |
from smolagents import MCPClient, CodeAgent
|
| 6 |
+
from smolagents.models.openai import OpenAIChatModel # ← add this
|
| 7 |
|
|
|
|
| 8 |
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 9 |
|
| 10 |
+
# Build an OpenAI model object once, reuse for all requests
|
| 11 |
+
# Ensure OPENAI_API_KEY is set in your HF Space secrets
|
| 12 |
+
openai_model = OpenAIChatModel(model="gpt-4o") # or "gpt-4o-mini", "gpt-3.5-turbo"
|
| 13 |
+
# choose any model your key can access
|
| 14 |
|
| 15 |
def respond(message: str, history: list):
|
| 16 |
+
"""Send the user's message to the MCP-powered agent and return reply."""
|
|
|
|
| 17 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
| 18 |
+
|
| 19 |
+
# Launch the MCP server and create a CodeAgent that uses the REAL model object
|
| 20 |
with MCPClient(params) as tools:
|
| 21 |
+
agent = CodeAgent(tools=tools, model=openai_model)
|
| 22 |
answer = agent.run(message)
|
| 23 |
|
|
|
|
| 24 |
history.append({"role": "user", "content": message})
|
| 25 |
history.append({"role": "assistant", "content": answer})
|
| 26 |
return history, history
|
|
|
|
| 28 |
|
| 29 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 30 |
chat_state = gr.State([])
|
|
|
|
| 31 |
chatbot = gr.Chatbot(label="Enterprise SQL Agent", type="messages")
|
| 32 |
textbox = gr.Textbox(
|
| 33 |
+
placeholder="Ask: Who are my inactive Northeast customers?", show_label=False
|
|
|
|
| 34 |
)
|
| 35 |
textbox.submit(respond, [textbox, chat_state], [chatbot, chat_state])
|
| 36 |
|
|
|
|
| 41 |
- List customers sorted by last order date.
|
| 42 |
- Find clients from the West with recent orders.
|
| 43 |
|
| 44 |
+
_Built with MCP, smolagents, Gradio, and OpenAI_
|
| 45 |
"""
|
| 46 |
)
|
| 47 |
|