Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,76 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
(defaults to **microsoft/Phi‑3‑mini‑4k‑instruct**, ~3 B params, free‑tier‑OK)
|
| 11 |
-
* `smolagents[mcp]` for the agent loop
|
| 12 |
-
* **Optional**: set `HF_MODEL_ID` or `HF_API_TOKEN` in **Settings → Secrets**
|
| 13 |
-
if you want a different (or gated) model.
|
| 14 |
-
|
| 15 |
-
If you hit the free‑tier rate‑limit you can still point to OpenAI by setting the
|
| 16 |
-
env var `OPENAI_API_KEY` — the code will auto‑switch to OpenAI chat.
|
| 17 |
"""
|
| 18 |
-
|
| 19 |
-
import pathlib
|
| 20 |
-
import gradio as gr
|
| 21 |
from mcp import StdioServerParameters
|
| 22 |
from smolagents import MCPClient, CodeAgent, InferenceClientModel
|
| 23 |
|
| 24 |
-
#
|
| 25 |
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
OPENAI_KEY = os.getenv("OPENAI_API_KEY")
|
| 31 |
-
HF_MODEL_ID = os.getenv("HF_MODEL_ID", "microsoft/Phi-3-mini-4k-instruct")
|
| 32 |
-
|
| 33 |
-
if OPENAI_KEY:
|
| 34 |
-
from smolagents.models import OpenAIChatModel # lazy import only if needed
|
| 35 |
-
BASE_MODEL = OpenAIChatModel() # defaults gpt‑4o‑preview
|
| 36 |
-
else:
|
| 37 |
-
BASE_MODEL = InferenceClientModel(model_id=HF_MODEL_ID)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
def respond(message: str, history: list):
|
| 42 |
-
"""
|
| 43 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
| 44 |
-
|
| 45 |
with MCPClient(params) as tools:
|
| 46 |
-
agent
|
| 47 |
answer = agent.run(message)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
return history, history
|
| 53 |
|
| 54 |
-
|
| 55 |
-
# ---------- UI ---------------------------------------------------------------
|
| 56 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 57 |
-
|
| 58 |
-
gr.Markdown("## Enterprise SQL Agent — ask
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
with gr.Accordion("Example prompts"):
|
| 65 |
gr.Markdown(
|
| 66 |
-
""
|
| 67 |
-
*
|
| 68 |
-
*
|
| 69 |
-
* Draft re‑engagement emails for inactive accounts.
|
| 70 |
-
"""
|
| 71 |
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
"_Powered by MCP
|
| 75 |
-
|
| 76 |
-
)
|
| 77 |
)
|
|
|
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
Gradio front-end + smolagents CodeAgent
|
| 3 |
+
---------------------------------------
|
| 4 |
+
• If you set an OPENAI_API_KEY the agent will call OpenAI (GPT-4o by default).
|
| 5 |
+
• Otherwise it falls back to a free Hugging Face chat-completion model
|
| 6 |
+
(defaults to microsoft/Phi-3-mini-4k-instruct which the public Inference
|
| 7 |
+
API exposes for the Chat Completion task).
|
| 8 |
+
• You can override the fallback by defining HF_MODEL_ID and, if needed,
|
| 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 your MCP tool server
|
| 17 |
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 18 |
|
| 19 |
+
# Decide which base model to use
|
| 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: # --- OpenAI branch -------------
|
| 24 |
+
from smolagents.models import OpenAIChat # <- works in all versions
|
| 25 |
+
BASE_MODEL = OpenAIChat( # gpt-4o by default
|
| 26 |
+
model=os.getenv("OPENAI_MODEL", "gpt-4o-preview"),
|
| 27 |
+
temperature=0.3,
|
| 28 |
+
)
|
| 29 |
+
else: # --- Hugging Face branch ----
|
| 30 |
+
BASE_MODEL = InferenceClientModel( # uses HF Inference API
|
| 31 |
+
model_id=HF_MODEL_ID,
|
| 32 |
+
hf_api_token=os.getenv("HF_API_TOKEN"), # optional for gated repos
|
| 33 |
+
timeout=90,
|
| 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", "content": message},
|
| 46 |
+
{"role": "assistant", "content": answer},
|
| 47 |
+
]
|
| 48 |
return history, history
|
| 49 |
|
| 50 |
+
# ----------------- UI ---------------------------------------------------------
|
|
|
|
| 51 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 52 |
+
state = gr.State([])
|
| 53 |
+
gr.Markdown("## Enterprise SQL Agent — ask questions about your data 🏢➡️📊")
|
| 54 |
|
| 55 |
+
chat = gr.Chatbot(type="messages", label="Chat")
|
| 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 |
+
footer = (
|
| 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()
|