mgbam commited on
Commit
80922f1
Β·
verified Β·
1 Parent(s): 64203c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -45
app.py CHANGED
@@ -1,82 +1,75 @@
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):
 
1
  """
2
+ app.py – Enterprise SQL Agent (Gradio + smolagents + MCP)
3
+
4
+ Secrets / ENV
5
+ ─────────────
6
+ OPENAI_API_KEY β†’ calls OpenAI (default model gpt-4o, override via OPENAI_MODEL)
7
+ GOOGLE_API_KEY β†’ calls Gemini (default model gemini-pro, override via GOOGLE_MODEL)
8
+ HF_MODEL_ID β†’ Hugging Face chat-completion model (fallback if no keys)
9
+ HF_API_TOKEN β†’ token for gated HF repo (optional)
 
 
 
 
 
 
10
  """
11
 
12
+ import os, pathlib, json, pprint, gradio as gr
13
  from mcp import StdioServerParameters
14
  from smolagents import MCPClient, CodeAgent
15
  from smolagents.models import LiteLLMModel, InferenceClientModel
16
 
17
+ # ─── 1. Pick the base LLM ───────────────────────────────────────────────
18
  OPENAI_KEY = os.getenv("OPENAI_API_KEY")
19
+ OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o")
20
 
21
  GEMINI_KEY = os.getenv("GOOGLE_API_KEY")
22
  GEM_MODEL = os.getenv("GOOGLE_MODEL", "gemini-pro")
23
 
24
  HF_MODEL_ID = os.getenv("HF_MODEL_ID", "microsoft/Phi-3-mini-4k-instruct")
25
+ HF_TOKEN = os.getenv("HF_API_TOKEN") # only if the repo is gated
26
 
27
  if OPENAI_KEY:
28
+ BASE_MODEL = LiteLLMModel(model_id=f"openai/{OPENAI_MODEL}",
29
+ api_key=OPENAI_KEY)
30
+ ACTIVE = f"OpenAI Β· {OPENAI_MODEL}"
31
  elif GEMINI_KEY:
32
+ BASE_MODEL = LiteLLMModel(model_id=f"google/{GEM_MODEL}",
33
+ api_key=GEMINI_KEY)
34
+ ACTIVE = f"Gemini Β· {GEM_MODEL}"
35
  else:
36
+ BASE_MODEL = InferenceClientModel(model_id=HF_MODEL_ID,
37
+ hf_api_token=HF_TOKEN,
38
+ timeout=90)
39
+ ACTIVE = f"Hugging Face Β· {HF_MODEL_ID}"
40
 
41
+ # ─── 2. Path to MCP server ──────────────────────────────────────────────
42
  SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
43
 
44
+ # ─── 3. Chat callback ───────────────────────────────────────────────────
45
+ def respond(message: str, history: list):
46
+ """Prompt β†’ CodeAgent β†’ MCP tools β†’ safe string reply."""
47
  params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
48
 
49
  with MCPClient(params) as tools:
50
+ answer = CodeAgent(tools=tools, model=BASE_MODEL).run(message)
 
51
 
52
+ # Always stringify for Gradio
53
+ if not isinstance(answer, str):
 
54
  try:
55
+ answer = json.dumps(answer, indent=2, ensure_ascii=False)
56
+ except Exception:
57
+ answer = pprint.pformat(answer, width=100)
 
58
 
59
  history += [
60
+ {"role": "user", "content": message},
61
+ {"role": "assistant", "content": answer},
62
  ]
63
  return history, history
64
 
65
+ # ─── 4. Build UI ────────────────────────────────────────────────────────
66
  with gr.Blocks(title="Enterprise SQL Agent") as demo:
67
  state = gr.State([])
68
+ gr.Markdown("## 🏒 Enterprise SQL Agent β€” ask questions about your data")
69
 
70
  chat = gr.Chatbot(type="messages", label="Conversation")
71
+ box = gr.Textbox(placeholder="e.g. Who are my inactive Northeast customers?",
72
+ show_label=False)
 
 
73
  box.submit(respond, [box, state], [chat, state])
74
 
75
  with gr.Accordion("Example prompts", open=False):