mgbam commited on
Commit
a2913a9
·
verified ·
1 Parent(s): 9b856ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -21
app.py CHANGED
@@ -1,50 +1,79 @@
1
- import pathlib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import os
 
3
  import gradio as gr
4
  from mcp import StdioServerParameters
5
  from smolagents import MCPClient, CodeAgent, InferenceClientModel
6
 
7
- # Absolute path to sibling mcp_server.py
8
  SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
9
 
10
- # Lazy‑load a Hugging Face Inference model (defaults to Qwen if no id given)
11
- HF_MODEL_ID = os.getenv("HF_MODEL_ID", "smolagents/Qwen2.5-VL-3B-Instruct-Agentic") # small & free-tier friendly
 
 
 
 
 
 
 
 
 
12
 
13
 
 
14
  def respond(message: str, history: list):
15
- """Route the prompt through an MCP‑enabled CodeAgent and return the answer."""
16
  params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
17
 
18
  with MCPClient(params) as tools:
19
- model = InferenceClientModel(model_id=HF_MODEL_ID)
20
- agent = CodeAgent(tools=tools, model=model)
21
  answer = agent.run(message)
22
 
23
- # Update chat in OpenAI‑style messages
24
  history.append({"role": "user", "content": message})
25
  history.append({"role": "assistant", "content": answer})
26
  return history, history
27
 
28
 
 
29
  with gr.Blocks(title="Enterprise SQL Agent") as demo:
30
  chat_state = gr.State([])
31
- chatbot = gr.Chatbot(type="messages", label="Enterprise SQL Agent")
32
 
33
- textbox = gr.Textbox(
34
- placeholder="Ask: Who are my inactive Northeast customers?",
35
- show_label=False,
36
- )
37
  textbox.submit(respond, [textbox, chat_state], [chatbot, chat_state])
38
 
 
 
 
 
 
 
 
 
 
39
  gr.Markdown(
40
- """
41
- ### Example Prompts
42
- - Who are my Northeast customers who haven’t ordered in 6 months?
43
- - List customers sorted by last order date.
44
- - Find clients from the West with recent orders.
45
-
46
- _Powered by smolagents + MCP + Hugging Face Inference API_
47
- """
48
  )
49
 
50
  if __name__ == "__main__":
 
1
+ """
2
+ app.py — Gradio front‑end + smolagents CodeAgent
3
+ ================================================
4
+
5
+ This version avoids private/gated models and works on any free Hugging Face
6
+ Space **without extra secrets**. It relies on:
7
+
8
+ * `mcp_server.py` sitting next to this file
9
+ * A public chat‑completion capable model exposed via the HF Inference API
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
  import os
19
+ import pathlib
20
  import gradio as gr
21
  from mcp import StdioServerParameters
22
  from smolagents import MCPClient, CodeAgent, InferenceClientModel
23
 
24
+ # ---------- Tool server ------------------------------------------------------
25
  SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
26
 
27
+ # ---------- Model selection --------------------------------------------------
28
+ # 1) Use OpenAI automatically if OPENAI_API_KEY is set.
29
+ # 2) Otherwise fall back to a public HF Inference model that supports chat‑completion.
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
+ # ---------- Gradio callback ---------------------------------------------------
41
  def respond(message: str, history: list):
42
+ """Run the user prompt through a CodeAgent that can call MCP SQL tools."""
43
  params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
44
 
45
  with MCPClient(params) as tools:
46
+ agent = CodeAgent(tools=tools, model=BASE_MODEL)
 
47
  answer = agent.run(message)
48
 
49
+ # Append to chat history (OpenAI messages format)
50
  history.append({"role": "user", "content": message})
51
  history.append({"role": "assistant", "content": answer})
52
  return history, history
53
 
54
 
55
+ # ---------- UI ---------------------------------------------------------------
56
  with gr.Blocks(title="Enterprise SQL Agent") as demo:
57
  chat_state = gr.State([])
58
+ gr.Markdown("## Enterprise SQL Agent — ask natural‑language questions about your data 🏢➡️📊")
59
 
60
+ chatbot = gr.Chatbot(type="messages", label="Chat")
61
+ textbox = gr.Textbox(placeholder="e.g. Who are my inactive Northeast customers?", show_label=False)
 
 
62
  textbox.submit(respond, [textbox, chat_state], [chatbot, chat_state])
63
 
64
+ with gr.Accordion("Example prompts"):
65
+ gr.Markdown(
66
+ """
67
+ * Who are my **Northeast** customers with no orders in 6 months?
68
+ * List customers sorted by **LastOrderDate**.
69
+ * Draft re‑engagement emails for inactive accounts.
70
+ """
71
+ )
72
+
73
  gr.Markdown(
74
+ "_Powered by MCP + smolagents + Gradio • Model: {}_".format(
75
+ "OpenAI (gpt‑4o)" if OPENAI_KEY else HF_MODEL_ID
76
+ )
 
 
 
 
 
77
  )
78
 
79
  if __name__ == "__main__":