mgbam commited on
Commit
ff342dc
Β·
verified Β·
1 Parent(s): 2a067a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -40
app.py CHANGED
@@ -1,69 +1,92 @@
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(msg: str, chat_history: list):
34
- """Run prompt β†’ CodeAgent β†’ string reply safe for Gradio Chatbot."""
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
- # Ensure the reply is always a string to avoid Gradio validation errors
41
- if not isinstance(raw, str):
42
- import json, pprint
43
- try:
44
- raw = json.dumps(raw, indent=2)
45
- except TypeError:
46
- raw = pprint.pformat(raw)
47
- reply = raw
48
-
49
- chat_history += [
 
50
  {"role": "user", "content": msg},
51
  {"role": "assistant", "content": reply},
52
  ]
53
- return chat_history, chat_history
54
 
55
- # ---------- UI ----------
56
  with gr.Blocks(title="Enterprise SQL Agent") as demo:
57
  state = gr.State([])
58
- gr.Markdown("## Enterprise SQL Agent β€” natural‑language to SQL via MCP")
59
- chat = gr.Chatbot(type="messages", label="Chat")
60
- box = gr.Textbox(show_label=False, placeholder="Ask: Who are my inactive Northeast customers?")
 
 
 
 
61
  box.submit(respond, [box, state], [chat, state])
62
 
63
- with gr.Accordion("Example prompts"):
64
- 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.""")
 
 
 
 
65
 
66
- gr.Markdown(f"_Powered by MCP + smolagents β€’ Model: **{MODEL_NAME}**_")
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()