mgbam commited on
Commit
865daca
·
verified ·
1 Parent(s): 46d10fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -48
app.py CHANGED
@@ -1,76 +1,58 @@
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()
 
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()