Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,92 @@
|
|
| 1 |
"""
|
| 2 |
-
app.py
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
|
|
|
| 10 |
import os, pathlib, gradio as gr
|
| 11 |
from mcp import StdioServerParameters
|
| 12 |
-
from smolagents import MCPClient, CodeAgent
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 16 |
-
|
| 17 |
-
# ---------- Model resolution ----------
|
| 18 |
OPENAI_KEY = os.getenv("OPENAI_API_KEY")
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
HF_MODEL_ID = os.getenv("HF_MODEL_ID", "microsoft/Phi-3-mini-4k-instruct")
|
|
|
|
| 21 |
|
| 22 |
if OPENAI_KEY:
|
| 23 |
-
BASE_MODEL = LiteLLMModel(model_id="openai/
|
| 24 |
-
|
| 25 |
elif GEMINI_KEY:
|
| 26 |
-
BASE_MODEL = LiteLLMModel(model_id="google/
|
| 27 |
-
|
| 28 |
else:
|
| 29 |
-
BASE_MODEL = InferenceClientModel(model_id=HF_MODEL_ID,
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
def respond(msg: str,
|
| 34 |
-
"""Run prompt β CodeAgent β
|
| 35 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
| 36 |
|
| 37 |
with MCPClient(params) as tools:
|
| 38 |
agent = CodeAgent(tools=tools, model=BASE_MODEL)
|
| 39 |
raw = agent.run(msg)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
{"role": "user", "content": msg},
|
| 51 |
{"role": "assistant", "content": reply},
|
| 52 |
]
|
| 53 |
-
return
|
| 54 |
|
| 55 |
-
#
|
| 56 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 57 |
state = gr.State([])
|
| 58 |
-
gr.Markdown("## Enterprise SQL Agent β natural
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
box.submit(respond, [box, state], [chat, state])
|
| 62 |
|
| 63 |
-
with gr.Accordion("Example prompts"):
|
| 64 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
gr.Markdown(f"_Powered by MCP + smolagents β’
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
app.py β Enterprise SQL Agent (Gradio + smolagents + MCP)
|
| 3 |
+
|
| 4 |
+
SECRETS / ENV VARS
|
| 5 |
+
------------------
|
| 6 |
+
OPENAI_API_KEY β use OpenAI (default model gpt-4o, override with OPENAI_MODEL)
|
| 7 |
+
GOOGLE_API_KEY β use Gemini-Pro (override model with GOOGLE_MODEL)
|
| 8 |
+
HF_MODEL_ID β repo that exposes Chat-Completion (fallback if no keys)
|
| 9 |
+
HF_API_TOKEN β token if that repo is gated
|
| 10 |
+
|
| 11 |
+
FILE LAYOUT
|
| 12 |
+
-----------
|
| 13 |
+
app.py
|
| 14 |
+
mcp_server.py # your FastMCP SQL tool server
|
| 15 |
+
requirements.txt # see bottom of this file
|
| 16 |
"""
|
| 17 |
+
|
| 18 |
import os, pathlib, gradio as gr
|
| 19 |
from mcp import StdioServerParameters
|
| 20 |
+
from smolagents import MCPClient, CodeAgent
|
| 21 |
+
from smolagents.models import LiteLLMModel, InferenceClientModel
|
| 22 |
|
| 23 |
+
# βββββββββββ 1. Choose base LLM ββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
| 24 |
OPENAI_KEY = os.getenv("OPENAI_API_KEY")
|
| 25 |
+
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o") # change if not whitelisted
|
| 26 |
+
|
| 27 |
+
GEMINI_KEY = os.getenv("GOOGLE_API_KEY")
|
| 28 |
+
GEM_MODEL = os.getenv("GOOGLE_MODEL", "gemini-pro")
|
| 29 |
+
|
| 30 |
HF_MODEL_ID = os.getenv("HF_MODEL_ID", "microsoft/Phi-3-mini-4k-instruct")
|
| 31 |
+
HF_TOKEN = os.getenv("HF_API_TOKEN") # only for gated repos
|
| 32 |
|
| 33 |
if OPENAI_KEY:
|
| 34 |
+
BASE_MODEL = LiteLLMModel(model_id=f"openai/{OPENAI_MODEL}", api_key=OPENAI_KEY)
|
| 35 |
+
ACTIVE = f"OpenAI Β· {OPENAI_MODEL}"
|
| 36 |
elif GEMINI_KEY:
|
| 37 |
+
BASE_MODEL = LiteLLMModel(model_id=f"google/{GEM_MODEL}", api_key=GEMINI_KEY)
|
| 38 |
+
ACTIVE = f"Gemini Β· {GEM_MODEL}"
|
| 39 |
else:
|
| 40 |
+
BASE_MODEL = InferenceClientModel(model_id=HF_MODEL_ID, hf_api_token=HF_TOKEN)
|
| 41 |
+
ACTIVE = f"Hugging Face Β· {HF_MODEL_ID}"
|
| 42 |
+
|
| 43 |
+
# βββββββββββ 2. Path to MCP tool server ββββββββββββββββββββββββββββββββββ
|
| 44 |
+
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 45 |
|
| 46 |
+
# βββββββββββ 3. Gradio callback ββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
+
def respond(msg: str, history: list):
|
| 48 |
+
"""Run prompt β CodeAgent β MCP tools β safe string reply."""
|
| 49 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
| 50 |
|
| 51 |
with MCPClient(params) as tools:
|
| 52 |
agent = CodeAgent(tools=tools, model=BASE_MODEL)
|
| 53 |
raw = agent.run(msg)
|
| 54 |
+
|
| 55 |
+
# Ensure reply is always string for Chatbot
|
| 56 |
+
if not isinstance(raw, str):
|
| 57 |
+
import json, pprint
|
| 58 |
+
try:
|
| 59 |
+
raw = json.dumps(raw, indent=2, ensure_ascii=False)
|
| 60 |
+
except (TypeError, ValueError):
|
| 61 |
+
raw = pprint.pformat(raw)
|
| 62 |
+
reply = raw
|
| 63 |
+
|
| 64 |
+
history += [
|
| 65 |
{"role": "user", "content": msg},
|
| 66 |
{"role": "assistant", "content": reply},
|
| 67 |
]
|
| 68 |
+
return history, history
|
| 69 |
|
| 70 |
+
# βββββββββββ 4. Build the UI βββββββββββββββββββββββββββββββββββββββββββββ
|
| 71 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 72 |
state = gr.State([])
|
| 73 |
+
gr.Markdown("## π’ Enterprise SQL Agent β ask natural-language questions about your data")
|
| 74 |
+
|
| 75 |
+
chat = gr.Chatbot(type="messages", label="Conversation")
|
| 76 |
+
box = gr.Textbox(
|
| 77 |
+
placeholder="e.g. Who are my Northeast customers with no orders in 6 months?",
|
| 78 |
+
show_label=False,
|
| 79 |
+
)
|
| 80 |
box.submit(respond, [box, state], [chat, state])
|
| 81 |
|
| 82 |
+
with gr.Accordion("Example prompts", open=False):
|
| 83 |
+
gr.Markdown(
|
| 84 |
+
"* Who are my **Northeast** customers with no orders in 6 months?\n"
|
| 85 |
+
"* List customers sorted by **LastOrderDate**.\n"
|
| 86 |
+
"* Draft re-engagement emails for inactive accounts."
|
| 87 |
+
)
|
| 88 |
|
| 89 |
+
gr.Markdown(f"_Powered by MCP + smolagents + Gradio β’ Active model β **{ACTIVE}**_")
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
demo.launch()
|