Abid Ali Awan commited on
Commit
693bdb2
·
1 Parent(s): 45bd7ce

refactor: Revamp Gradio MCP Connector to enhance performance and user experience by implementing a two-phase chat process with tool resolution and streaming responses, updating the system prompt for clarity, and improving file upload handling.

Browse files
Files changed (2) hide show
  1. app.py +130 -323
  2. todolist.md +5 -0
app.py CHANGED
@@ -1,379 +1,186 @@
1
  """
2
- Gradio MCP Client for Remote MCP Server - With File Upload
3
  """
4
 
5
- import json
6
  import os
7
  import shutil
8
- import warnings
9
- from contextlib import asynccontextmanager
10
-
11
  import gradio as gr
12
- from fastmcp import Client
13
- from fastmcp.client.transports import StreamableHttpTransport
14
  from openai import OpenAI
15
 
16
- # Suppress deprecation warnings
17
- warnings.filterwarnings(
18
- "ignore", category=DeprecationWarning, module="websockets.legacy"
19
- )
20
- warnings.filterwarnings(
21
- "ignore", category=DeprecationWarning, module="uvicorn.protocols.websockets"
22
- )
23
-
24
- # Import orchestrator functions (if available)
25
- try:
26
- from orchestrator import run_orchestrated_chat, run_orchestrated_chat_stream
27
- except ImportError:
28
- # Fallback if orchestrator module not found
29
- run_orchestrated_chat = None
30
- run_orchestrated_chat_stream = None
31
-
32
- # Configuration
33
- MCP_SERVER_URL = "https://mcp-1st-birthday-auto-deployer.hf.space/gradio_api/mcp/"
34
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
35
- MODEL = "gpt-5-mini"
36
-
37
- # Will be set when app launches
38
- APP_URL = None
39
-
40
-
41
- class MCPClientManager:
42
- def __init__(self, server_url: str):
43
- self.server_url = server_url
44
-
45
- @asynccontextmanager
46
- async def get_client(self):
47
- transport = StreamableHttpTransport(self.server_url)
48
- async with Client(transport) as client:
49
- yield client
50
-
51
- async def get_tools(self) -> list:
52
- async with self.get_client() as client:
53
- return await client.list_tools()
54
-
55
- async def call_tool(self, tool_name: str, arguments: dict) -> str:
56
- async with self.get_client() as client:
57
- result = await client.call_tool(tool_name, arguments)
58
- if hasattr(result, "content"):
59
- if isinstance(result.content, list):
60
- return "\n".join(
61
- str(item.text) if hasattr(item, "text") else str(item)
62
- for item in result.content
63
- )
64
- return str(result.content)
65
- return str(result)
66
-
67
- def to_openai_tools(self, tools: list) -> list:
68
- return [
69
- {
70
- "type": "function",
71
- "function": {
72
- "name": tool.name,
73
- "description": tool.description or "",
74
- "parameters": {
75
- "type": "object",
76
- "properties": tool.inputSchema.get("properties", {})
77
- if tool.inputSchema
78
- else {},
79
- "required": tool.inputSchema.get("required", [])
80
- if tool.inputSchema
81
- else [],
82
- },
83
- },
84
- }
85
- for tool in tools
86
- ]
87
-
88
-
89
- mcp = MCPClientManager(MCP_SERVER_URL)
90
- openai_client = OpenAI(api_key=OPENAI_API_KEY)
91
-
92
- SYSTEM_PROMPT = """You are a helpful ML assistant with access to Auto Deployer tools.
93
-
94
- IMPORTANT: When calling tools with file_path parameter:
95
- - Use the provided file URL directly
96
- - Pass ONLY the raw URL (e.g., "https://...")
97
- - Never add prefixes like "Gradio File Input - "
98
-
99
- Always pass URLs directly without any prefix."""
100
-
101
-
102
- async def chat(message: str, history: list, file_url: str):
103
- """Process chat with optional file URL"""
104
- tools = await mcp.get_tools()
105
- openai_tools = mcp.to_openai_tools(tools)
106
-
107
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
108
-
109
- # Add file context if available
110
- user_content = message
111
- if file_url:
112
- user_content = f"[Uploaded CSV file URL: {file_url}]\n\n{message}"
113
-
114
- # Build history
115
- for item in history:
116
- if isinstance(item, (list, tuple)) and len(item) == 2:
117
- user_msg, assistant_msg = item
118
- messages.append({"role": "user", "content": user_msg})
119
- if assistant_msg:
120
- messages.append({"role": "assistant", "content": assistant_msg})
121
-
122
- messages.append({"role": "user", "content": user_content})
123
-
124
- # First call
125
- response = openai_client.chat.completions.create(
126
- model=MODEL,
127
- messages=messages,
128
- tools=openai_tools,
129
- tool_choice="auto",
130
- )
131
-
132
- assistant_message = response.choices[0].message
133
-
134
- # Handle tool calls
135
- while assistant_message.tool_calls:
136
- messages.append(assistant_message)
137
-
138
- yield "🔧 Calling tools...\n\n"
139
-
140
- for tool_call in assistant_message.tool_calls:
141
- tool_name = tool_call.function.name
142
- arguments = json.loads(tool_call.function.arguments)
143
-
144
- # Clean file_path
145
- if "file_path" in arguments:
146
- fp = arguments["file_path"]
147
- if fp.startswith("Gradio File Input - "):
148
- arguments["file_path"] = fp.replace("Gradio File Input - ", "")
149
-
150
- yield f"⚙️ Running `{tool_name}`...\n\n"
151
-
152
- try:
153
- tool_result = await mcp.call_tool(tool_name, arguments)
154
- except Exception as e:
155
- tool_result = f"Error: {e}"
156
-
157
- messages.append(
158
- {
159
- "role": "tool",
160
- "tool_call_id": tool_call.id,
161
- "content": tool_result,
162
- }
163
- )
164
-
165
- response = openai_client.chat.completions.create(
166
- model=MODEL,
167
- messages=messages,
168
- tools=openai_tools,
169
- tool_choice="auto",
170
- )
171
- assistant_message = response.choices[0].message
172
-
173
- # Stream final response
174
- stream = openai_client.chat.completions.create(
175
- model=MODEL,
176
- messages=messages,
177
- stream=True,
178
- )
179
 
180
- partial_response = ""
181
- for chunk in stream:
182
- if chunk.choices[0].delta.content:
183
- partial_response += chunk.choices[0].delta.content
184
- yield partial_response
185
 
 
 
 
 
 
 
186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  def handle_upload(file_obj, request: gr.Request):
188
- """
189
- 1) Take uploaded file
190
- 2) Copy to /tmp for a stable path
191
- 3) Build a public gradio file URL
192
- """
193
  if file_obj is None:
194
- return None, None
195
 
196
- # Local path where Gradio stored the file
197
  local_path = file_obj.name
198
-
199
- # Optional: stabilize path under /tmp
200
  stable_path = os.path.join("/tmp", os.path.basename(local_path))
201
  try:
202
  shutil.copy(local_path, stable_path)
203
  local_path = stable_path
204
  except Exception:
205
- # If copy fails, use original path
206
  pass
207
 
208
- # Use Gradio's internal file URL format
209
- base_url = str(request.base_url).rstrip('/')
210
- public_url = f"{base_url}/gradio_api/file={local_path}"
211
-
212
- return public_url, public_url
213
 
214
 
215
- async def chat_send_stream(user_msg, history, file_url):
 
 
 
216
  """
217
- Streaming chat function that yields updates including tool invocations.
218
- - history: list of message dictionaries with 'role' and 'content' keys
219
- - file_url: required HTTP URL to the uploaded file
220
  """
 
221
  if history is None:
222
  history = []
223
 
224
- # Ensure history is in proper dict format
225
- messages = []
226
- for item in history:
227
- if isinstance(item, dict) and "role" in item and "content" in item:
228
- messages.append(item)
229
- elif isinstance(item, (list, tuple)) and len(item) == 2:
230
- user_msg_item, assistant_msg_item = item
231
- messages.append({"role": "user", "content": str(user_msg_item)})
232
- if assistant_msg_item:
233
- messages.append({"role": "assistant", "content": str(assistant_msg_item)})
234
-
235
- # Add current user message
236
- messages.append({"role": "user", "content": user_msg})
237
-
238
- # Add thinking placeholder
239
- messages.append({"role": "assistant", "content": "🤔 Thinking..."})
240
-
241
- # If no file, respond with error
242
- if not file_url:
243
- messages[-1] = {"role": "assistant", "content": "Upload a file first."}
244
- yield messages
245
- return
246
-
247
- # Use orchestrator if available
248
- if run_orchestrated_chat_stream:
249
- # Convert to tuple format for orchestrator (excluding current thinking message)
250
- history_tuples = []
251
- for item in messages[:-1]:
252
- if item["role"] == "user":
253
- history_tuples.append((item.get("content", ""), ""))
254
- elif item["role"] == "assistant":
255
- if history_tuples:
256
- history_tuples[-1] = (history_tuples[-1][0], item.get("content", ""))
257
-
258
- # Stream the response using async generator
259
- async for chunk in run_orchestrated_chat_stream(
260
- user_msg, history_tuples, file_url
261
- ):
262
- chunk_type = chunk.get("type", "")
263
- chunk_content = chunk.get("content", "")
264
-
265
- if chunk_type == "thinking":
266
- messages[-1] = {"role": "assistant", "content": chunk_content}
267
- yield messages
268
- elif chunk_type == "tool":
269
- messages[-1] = {"role": "assistant", "content": messages[-1]["content"] + f"\n{chunk_content}"}
270
- yield messages
271
- elif chunk_type == "result":
272
- messages[-1] = {"role": "assistant", "content": messages[-1]["content"] + f"\n{chunk_content}"}
273
- yield messages
274
- elif chunk_type == "final":
275
- messages[-1] = {"role": "assistant", "content": chunk_content}
276
- yield messages
277
- elif chunk_type == "error":
278
- messages[-1] = {"role": "assistant", "content": chunk_content}
279
- yield messages
280
  else:
281
- # Fallback: use the existing chat function with streaming
282
- simple_history = [item for item in messages[:-1] if item["role"] in ["user", "assistant"]]
283
-
284
- response_text = ""
285
- async for chunk in chat(user_msg, simple_history, file_url):
286
- response_text = chunk
287
- messages[-1] = {"role": "assistant", "content": response_text}
288
- yield messages
289
-
290
-
291
-
292
-
293
- with gr.Blocks(title="MCP + GPT-5 mini - Streaming Chat") as demo:
294
- gr.Markdown(
295
- """
296
- # AI-Driven MLOps Agent 🤖
297
- - **Upload a CSV file** (required)
298
- - Real-time streaming with live tool invocations
299
- - Get intelligent insights, training, or deployment based on your needs
300
- """
301
  )
302
 
303
- uploader = gr.File(
304
- label="Required CSV file upload",
305
- file_count="single",
306
- type="filepath",
307
- file_types=[".csv"], # Restrict to CSV files only
308
- )
309
 
310
- # Internal file URL storage (hidden from UI)
311
- file_url_state = gr.State(value=None)
 
 
 
 
 
312
 
313
- # Use message format for better streaming support
314
- chatbot = gr.Chatbot(
315
- label="Chat",
316
- avatar_images=(
317
- None,
318
- "https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.png",
319
- ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
320
  )
321
 
322
- msg = gr.Textbox(label="Message", interactive=True)
323
- send = gr.Button("Send", interactive=True)
 
 
 
 
 
 
 
 
 
324
 
325
- # When file changes, generate URL and update state
326
- uploader.change(
327
- handle_upload,
328
- inputs=[uploader],
329
- outputs=[file_url_state],
330
- )
331
 
332
- # Send button (streaming) - update chatbot and clear input
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
  send.click(
334
  chat_send_stream,
335
- inputs=[msg, chatbot, file_url_state],
336
  outputs=[chatbot],
337
  ).then(lambda: "", outputs=[msg])
338
 
339
- # Press Enter to send (streaming) - update chatbot and clear input
340
  msg.submit(
341
  chat_send_stream,
342
- inputs=[msg, chatbot, file_url_state],
343
  outputs=[chatbot],
344
  ).then(lambda: "", outputs=[msg])
345
 
346
 
347
- async def test_mcp_connection():
348
- """Test MCP connection on startup"""
349
- try:
350
- print("Testing MCP server connection...")
351
- tools = await mcp.get_tools()
352
- print(f"✅ Connected to MCP server. Found {len(tools)} tools.")
353
- return True
354
- except Exception as e:
355
- print(f"❌ Failed to connect to MCP server: {e}")
356
- return False
357
-
358
  if __name__ == "__main__":
359
- import asyncio
360
- import warnings
361
-
362
- # Suppress all warnings for cleaner output
363
- warnings.filterwarnings("ignore")
364
-
365
- # Test MCP connection on startup
366
- try:
367
- print(f"Attempting to connect to MCP server: {MCP_SERVER_URL}")
368
- asyncio.run(test_mcp_connection())
369
- except Exception as e:
370
- print(f"MCP connection test failed: {e}")
371
- print("Continuing anyway - connection will be retried during chat...")
372
-
373
- # Launch the app
374
  demo.queue().launch(
375
  allowed_paths=["/tmp"],
376
- ssr_mode=False,
377
  show_error=True,
378
  quiet=True,
379
  )
 
1
  """
2
+ Gradio + OpenAI MCP Connector Clean, Fast, Streaming, With File Upload
3
  """
4
 
 
5
  import os
6
  import shutil
 
 
 
7
  import gradio as gr
 
 
8
  from openai import OpenAI
9
 
10
+ # ---------------------
11
+ # CONFIGURATION
12
+ # ---------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
14
+ MCP_SERVER_URL = "https://mcp-1st-birthday-auto-deployer.hf.space/gradio_api/mcp/"
15
+ MODEL_FAST = "gpt-5-mini" # for tool resolution
16
+ MODEL_STREAM = "gpt-5.1" # for final streaming reply
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ client = OpenAI(api_key=OPENAI_API_KEY)
 
 
 
 
19
 
20
+ SYSTEM_PROMPT = """
21
+ You are a fast MLOps assistant with access to remote MCP tools.
22
+ Use tools only when necessary.
23
+ Keep reasoning effort LOW for speed.
24
+ After tools run, summarize clearly and concisely.
25
+ """
26
 
27
+ # ---------------------
28
+ # NATIVE MCP CONNECTOR
29
+ # ---------------------
30
+ TOOLS = [
31
+ {
32
+ "type": "mcp",
33
+ "server_label": "deploy_tools",
34
+ "server_url": MCP_SERVER_URL,
35
+ # transport auto-detected; HF space supports HTTP
36
+ }
37
+ ]
38
+
39
+
40
+ # ---------------------
41
+ # FILE UPLOAD HANDLER
42
+ # ---------------------
43
  def handle_upload(file_obj, request: gr.Request):
 
 
 
 
 
44
  if file_obj is None:
45
+ return None
46
 
47
+ # Ensure file is in a stable path
48
  local_path = file_obj.name
 
 
49
  stable_path = os.path.join("/tmp", os.path.basename(local_path))
50
  try:
51
  shutil.copy(local_path, stable_path)
52
  local_path = stable_path
53
  except Exception:
 
54
  pass
55
 
56
+ # Build public Gradio URL
57
+ base = str(request.base_url).rstrip("/")
58
+ return f"{base}/gradio_api/file={local_path}"
 
 
59
 
60
 
61
+ # ---------------------
62
+ # MAIN CHAT HANDLER
63
+ # ---------------------
64
+ def chat_send_stream(user_msg, history, file_url):
65
  """
66
+ 2-phase pipeline:
67
+ PHASE 1 Non-streaming tool resolution using gpt-5-mini
68
+ PHASE 2 Streaming final output using gpt-5
69
  """
70
+
71
  if history is None:
72
  history = []
73
 
74
+ # Build message history for OpenAI
75
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
76
+ for u, a in history:
77
+ messages.append({"role": "user", "content": u})
78
+ if a:
79
+ messages.append({"role": "assistant", "content": a})
80
+
81
+ # Inject file context
82
+ if file_url:
83
+ user_msg_full = f"[Uploaded CSV file: {file_url}]\n\n{user_msg}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  else:
85
+ user_msg_full = user_msg
86
+
87
+ messages.append({"role": "user", "content": user_msg_full})
88
+
89
+ # -----------------------------
90
+ # PHASE 1 — TOOL RESOLUTION
91
+ # -----------------------------
92
+ tool_phase = client.responses.create(
93
+ model=MODEL_FAST,
94
+ reasoning={"effort": "low"},
95
+ tools=TOOLS,
96
+ instructions=SYSTEM_PROMPT,
97
+ input=messages,
 
 
 
 
 
 
 
98
  )
99
 
100
+ tool_feedback = []
 
 
 
 
 
101
 
102
+ # Detect tool calls (if any)
103
+ if tool_phase.output:
104
+ for item in tool_phase.output:
105
+ if item.type == "tool_call":
106
+ tool_feedback.append(f"🛠️ Used tool `{item.name}`.")
107
+ elif item.type == "tool_result":
108
+ tool_feedback.append(f"{item.content}")
109
 
110
+ if not tool_feedback:
111
+ tool_feedback.append("No MCP tools needed.")
112
+
113
+ else:
114
+ tool_feedback.append("No MCP tools needed.")
115
+
116
+ # Append tool results to messages before final generation
117
+ messages.append({"role": "assistant", "content": "\n".join(tool_feedback)})
118
+
119
+ # Yield intermediate tool feedback to the UI
120
+ history.append((user_msg, "\n".join(tool_feedback)))
121
+ yield history
122
+
123
+ # -----------------------------
124
+ # PHASE 2 — STREAMING FINAL ANSWER
125
+ # -----------------------------
126
+ stream = client.responses.create(
127
+ model=MODEL_STREAM,
128
+ reasoning={"effort": "low"},
129
+ instructions=SYSTEM_PROMPT,
130
+ input=messages,
131
+ stream=True,
132
  )
133
 
134
+ final_text = ""
135
+ for ev in stream:
136
+ if ev.type == "response.output_text.delta":
137
+ final_text += ev.delta
138
+ history[-1] = (user_msg, "\n".join(tool_feedback) + "\n\n" + final_text)
139
+ yield history
140
+
141
+ elif ev.type == "response.completed":
142
+ break
143
+
144
+ stream.close()
145
 
 
 
 
 
 
 
146
 
147
+ # ---------------------
148
+ # GRADIO UI
149
+ # ---------------------
150
+ with gr.Blocks(title="MCP + GPT-5 — Fast Streaming MLOps Agent") as demo:
151
+ gr.Markdown("""
152
+ # 🚀 AI-Driven MLOps Agent (MCP-Powered)
153
+ - Upload a CSV file
154
+ - Tools resolve instantly
155
+ - Final answer streams smoothly
156
+ """)
157
+
158
+ file_state = gr.State()
159
+
160
+ uploader = gr.File(label="Upload CSV file", type="filepath", file_count="single")
161
+
162
+ uploader.change(handle_upload, inputs=[uploader], outputs=[file_state])
163
+
164
+ chatbot = gr.Chatbot(label="Chat")
165
+ msg = gr.Textbox(label="Message")
166
+ send = gr.Button("Send")
167
+
168
  send.click(
169
  chat_send_stream,
170
+ inputs=[msg, chatbot, file_state],
171
  outputs=[chatbot],
172
  ).then(lambda: "", outputs=[msg])
173
 
 
174
  msg.submit(
175
  chat_send_stream,
176
+ inputs=[msg, chatbot, file_state],
177
  outputs=[chatbot],
178
  ).then(lambda: "", outputs=[msg])
179
 
180
 
 
 
 
 
 
 
 
 
 
 
 
181
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  demo.queue().launch(
183
  allowed_paths=["/tmp"],
 
184
  show_error=True,
185
  quiet=True,
186
  )
todolist.md ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ - [] Diable message and send button while the final reosne is not recived.
2
+ - [] reduce the thinking preces in tools secaltiona dn then analysing the dataset.
3
+ - [] clear the message when user end the message.
4
+ - [] model is not taking an acocunt of previous ocnverations.
5
+ - [] add more infor to the readme