Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,58 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
HF_API_TOKEN in the Space → Settings → Secrets.
|
| 10 |
"""
|
| 11 |
-
|
| 12 |
import os, pathlib, gradio as gr
|
| 13 |
from mcp import StdioServerParameters
|
| 14 |
-
from smolagents import MCPClient, CodeAgent, InferenceClientModel
|
| 15 |
|
| 16 |
-
# Path to
|
| 17 |
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 18 |
|
| 19 |
-
#
|
| 20 |
OPENAI_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 21 |
HF_MODEL_ID = os.getenv("HF_MODEL_ID", "microsoft/Phi-3-mini-4k-instruct")
|
| 22 |
|
| 23 |
-
if OPENAI_KEY:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
else:
|
| 30 |
-
BASE_MODEL = InferenceClientModel(
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
# ----------------- callback ---------------------------------------------------
|
| 37 |
def respond(message: str, history: list):
|
| 38 |
-
"""Send user prompt → CodeAgent → SQL tools → natural-language answer."""
|
| 39 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
| 40 |
with MCPClient(params) as tools:
|
| 41 |
agent = CodeAgent(tools=tools, model=BASE_MODEL)
|
| 42 |
answer = agent.run(message)
|
| 43 |
-
|
| 44 |
history += [
|
| 45 |
-
{"role": "user",
|
| 46 |
{"role": "assistant", "content": answer},
|
| 47 |
]
|
| 48 |
return history, history
|
| 49 |
|
| 50 |
-
#
|
| 51 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 52 |
-
state
|
| 53 |
-
gr.Markdown("## Enterprise SQL Agent —
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
box = gr.Textbox(
|
| 57 |
-
placeholder="e.g. Who are my inactive Northeast customers?",
|
| 58 |
-
show_label=False,
|
| 59 |
-
)
|
| 60 |
box.submit(respond, [box, state], [chat, state])
|
| 61 |
|
| 62 |
with gr.Accordion("Example prompts"):
|
| 63 |
-
gr.Markdown(
|
| 64 |
-
"* Who are my **Northeast** customers with no orders in 6 months?\n"
|
| 65 |
-
"* List customers sorted by **LastOrderDate**.\n"
|
| 66 |
-
"* Draft re-engagement emails for inactive accounts."
|
| 67 |
-
)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
f"_Powered by MCP + smolagents + Gradio • Model: "
|
| 71 |
-
f"{'OpenAI' if OPENAI_KEY else HF_MODEL_ID}_"
|
| 72 |
-
)
|
| 73 |
-
gr.Markdown(footer)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
app.py — robust multimodel agent
|
| 3 |
+
--------------------------------
|
| 4 |
+
* Supports **OpenAI** (if `OPENAI_API_KEY`) or **Gemini** (if `GOOGLE_API_KEY`) via
|
| 5 |
+
`LiteLLMModel`.
|
| 6 |
+
* Otherwise falls back to a free HF Inference chat model
|
| 7 |
+
(`microsoft/Phi-3-mini-4k-instruct`).
|
| 8 |
+
* No version‑specific imports (avoids `OpenAIChat` errors).
|
|
|
|
| 9 |
"""
|
|
|
|
| 10 |
import os, pathlib, gradio as gr
|
| 11 |
from mcp import StdioServerParameters
|
| 12 |
+
from smolagents import MCPClient, CodeAgent, InferenceClientModel, LiteLLMModel
|
| 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 |
+
# ---------- Model resolution ----------
|
| 18 |
OPENAI_KEY = os.getenv("OPENAI_API_KEY")
|
| 19 |
+
GEMINI_KEY = os.getenv("GOOGLE_API_KEY") # for Gemini via LiteLLM
|
| 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/gpt-4o-preview", api_key=OPENAI_KEY)
|
| 24 |
+
MODEL_NAME = "openai/gpt-4o-preview"
|
| 25 |
+
elif GEMINI_KEY:
|
| 26 |
+
BASE_MODEL = LiteLLMModel(model_id="google/gemini-pro", api_key=GEMINI_KEY)
|
| 27 |
+
MODEL_NAME = "google/gemini-pro"
|
| 28 |
+
else:
|
| 29 |
+
BASE_MODEL = InferenceClientModel(model_id=HF_MODEL_ID, timeout=90)
|
| 30 |
+
MODEL_NAME = HF_MODEL_ID
|
| 31 |
+
|
| 32 |
+
# ---------- Callback ----------
|
|
|
|
|
|
|
|
|
|
| 33 |
def respond(message: str, history: list):
|
|
|
|
| 34 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
| 35 |
with MCPClient(params) as tools:
|
| 36 |
agent = CodeAgent(tools=tools, model=BASE_MODEL)
|
| 37 |
answer = agent.run(message)
|
|
|
|
| 38 |
history += [
|
| 39 |
+
{"role": "user", "content": message},
|
| 40 |
{"role": "assistant", "content": answer},
|
| 41 |
]
|
| 42 |
return history, history
|
| 43 |
|
| 44 |
+
# ---------- UI ----------
|
| 45 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 46 |
+
state = gr.State([])
|
| 47 |
+
gr.Markdown("## Enterprise SQL Agent — natural‑language to SQL via MCP")
|
| 48 |
+
chat = gr.Chatbot(type="messages", label="Chat")
|
| 49 |
+
box = gr.Textbox(show_label=False, placeholder="Ask: Who are my inactive Northeast customers?")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
box.submit(respond, [box, state], [chat, state])
|
| 51 |
|
| 52 |
with gr.Accordion("Example prompts"):
|
| 53 |
+
gr.Markdown("""* Who are my **Northeast** customers with no orders in 6 months?\n* List customers sorted by **LastOrderDate**.\n* Draft re‑engagement emails for inactive accounts.""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
gr.Markdown(f"_Powered by MCP + smolagents • Model: **{MODEL_NAME}**_")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
demo.launch()
|