Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +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 |
-
# Path to mcp_server.py (must be beside this file)
|
| 15 |
-
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 16 |
|
| 17 |
-
#
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
| 34 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
|
|
|
| 35 |
with MCPClient(params) as tools:
|
| 36 |
agent = CodeAgent(tools=tools, model=BASE_MODEL)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
{"role": "
|
|
|
|
| 41 |
]
|
| 42 |
-
return
|
| 43 |
|
| 44 |
-
#
|
|
|
|
|
|
|
| 45 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 46 |
-
state
|
| 47 |
-
gr.Markdown("## Enterprise SQL Agent
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
box.submit(respond, [box, state], [chat, state])
|
| 51 |
|
| 52 |
-
with gr.Accordion("Example prompts"):
|
| 53 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
gr.Markdown(f"_Powered by MCP + smolagents
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
app.py – Enterprise SQL Agent (Gradio + smolagents + MCP)
|
| 3 |
+
|
| 4 |
+
ENV VARS SUPPORTED
|
| 5 |
+
------------------
|
| 6 |
+
OPENAI_API_KEY → use any OpenAI chat model (default = gpt-4o)
|
| 7 |
+
OPENAI_MODEL → override the OpenAI model ID (e.g. gpt-4-turbo)
|
| 8 |
+
GOOGLE_API_KEY → use Gemini-Pro via Google AI
|
| 9 |
+
GOOGLE_MODEL → override Gemini model ID (e.g. gemini-1.5-pro)
|
| 10 |
+
|
| 11 |
+
HF_MODEL_ID → fallback model repo (must expose Chat-Completion)
|
| 12 |
+
HF_API_TOKEN → token if that repo is gated
|
| 13 |
+
|
| 14 |
+
If no provider keys are set, the code falls back to a free HF model
|
| 15 |
+
`microsoft/Phi-3-mini-4k-instruct`.
|
| 16 |
+
|
| 17 |
+
MCP SERVER
|
| 18 |
+
----------
|
| 19 |
+
`mcp_server.py` must live in the same folder and expose your SQL tools.
|
| 20 |
"""
|
| 21 |
+
|
| 22 |
import os, pathlib, gradio as gr
|
| 23 |
from mcp import StdioServerParameters
|
| 24 |
+
from smolagents import MCPClient, CodeAgent
|
| 25 |
+
from smolagents.models import InferenceClientModel, LiteLLMModel
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# --------------------------------------------------------------------- #
|
| 28 |
+
# 1. Resolve the base LLM model, preferring OpenAI → Gemini → HF
|
| 29 |
+
# --------------------------------------------------------------------- #
|
| 30 |
OPENAI_KEY = os.getenv("OPENAI_API_KEY")
|
| 31 |
+
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o") # change if not whitelisted
|
| 32 |
+
GEMINI_KEY = os.getenv("GOOGLE_API_KEY")
|
| 33 |
+
GEM_MODEL = os.getenv("GOOGLE_MODEL", "gemini-pro")
|
| 34 |
HF_MODEL_ID = os.getenv("HF_MODEL_ID", "microsoft/Phi-3-mini-4k-instruct")
|
| 35 |
+
HF_TOKEN = os.getenv("HF_API_TOKEN") or None # only needed for gated repos
|
| 36 |
|
| 37 |
if OPENAI_KEY:
|
| 38 |
+
BASE_MODEL = LiteLLMModel(model_id=f"openai/{OPENAI_MODEL}", api_key=OPENAI_KEY)
|
| 39 |
+
ACTIVE = f"OpenAI · {OPENAI_MODEL}"
|
| 40 |
elif GEMINI_KEY:
|
| 41 |
+
BASE_MODEL = LiteLLMModel(model_id=f"google/{GEM_MODEL}", api_key=GEMINI_KEY)
|
| 42 |
+
ACTIVE = f"Gemini · {GEM_MODEL}"
|
| 43 |
else:
|
| 44 |
+
BASE_MODEL = InferenceClientModel(model_id=HF_MODEL_ID, hf_api_token=HF_TOKEN)
|
| 45 |
+
ACTIVE = f"Hugging Face · {HF_MODEL_ID}"
|
| 46 |
+
|
| 47 |
+
# --------------------------------------------------------------------- #
|
| 48 |
+
# 2. Path to your MCP server (must be alongside this file)
|
| 49 |
+
# --------------------------------------------------------------------- #
|
| 50 |
+
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 51 |
|
| 52 |
+
# --------------------------------------------------------------------- #
|
| 53 |
+
# 3. Gradio callback – run prompt → CodeAgent → SQL tools
|
| 54 |
+
# --------------------------------------------------------------------- #
|
| 55 |
+
def respond(msg: str, chat_history: list):
|
| 56 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
| 57 |
+
|
| 58 |
with MCPClient(params) as tools:
|
| 59 |
agent = CodeAgent(tools=tools, model=BASE_MODEL)
|
| 60 |
+
reply = agent.run(msg)
|
| 61 |
+
|
| 62 |
+
chat_history += [
|
| 63 |
+
{"role": "user", "content": msg},
|
| 64 |
+
{"role": "assistant", "content": reply},
|
| 65 |
]
|
| 66 |
+
return chat_history, chat_history
|
| 67 |
|
| 68 |
+
# --------------------------------------------------------------------- #
|
| 69 |
+
# 4. Build the UI
|
| 70 |
+
# --------------------------------------------------------------------- #
|
| 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="Chat")
|
| 76 |
+
box = gr.Textbox(
|
| 77 |
+
show_label=False,
|
| 78 |
+
placeholder="e.g. Who are my Northeast customers with no orders in 6 months?",
|
| 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 tools + smolagents · Active model → **{ACTIVE}**_")
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
demo.launch()
|