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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import json
|
4 |
+
import asyncio
|
5 |
+
import traceback
|
6 |
+
import uuid
|
7 |
+
import shutil
|
8 |
+
import base64
|
9 |
+
import multiprocessing
|
10 |
+
from pathlib import Path
|
11 |
+
import glob
|
12 |
+
from PIL import Image
|
13 |
+
import io
|
14 |
+
import sqlite3
|
15 |
+
import pandas as pd
|
16 |
+
import gradio as gr
|
17 |
+
from dotenv import load_dotenv
|
18 |
+
|
19 |
+
from dynamic_agent import AgentFactory
|
20 |
+
from agno_kb import AgnoKnowledgeBase
|
21 |
+
from agno.tools.mcp import MCPTools
|
22 |
+
|
23 |
+
# Load environment variables
|
24 |
+
load_dotenv()
|
25 |
+
|
26 |
+
# Set event loop policy for Windows
|
27 |
+
if sys.platform.startswith("win"):
|
28 |
+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
29 |
+
|
30 |
+
# MCP server runner from mcp_tools.py
|
31 |
+
def run_mcp_server():
|
32 |
+
import mcp_tools
|
33 |
+
|
34 |
+
# Create a directory for storing session files
|
35 |
+
SESSION_FILES_DIR = Path("session_files")
|
36 |
+
SESSION_FILES_DIR.mkdir(exist_ok=True)
|
37 |
+
|
38 |
+
# Define the images folder path
|
39 |
+
IMAGES_FOLDER_PATH = Path("./plots")
|
40 |
+
|
41 |
+
# Available tools for agent configuration
|
42 |
+
available_tools = ["MCP Server Tool", "KB Configuration"]
|
43 |
+
|
44 |
+
|
45 |
+
DB_PATH = "flipkart_mobiles.db"
|
46 |
+
TABLE_NAME = "mobiles"
|
47 |
+
|
48 |
+
def load_entire_database():
|
49 |
+
try:
|
50 |
+
conn = sqlite3.connect(DB_PATH)
|
51 |
+
df = pd.read_sql_query(f"SELECT * FROM {TABLE_NAME}", conn)
|
52 |
+
conn.close()
|
53 |
+
return df
|
54 |
+
except Exception as e:
|
55 |
+
return pd.DataFrame({"Error": [str(e)]})
|
56 |
+
|
57 |
+
def clear_database_view():
|
58 |
+
# Return an empty DataFrame to clear/hide the display
|
59 |
+
return pd.DataFrame()
|
60 |
+
|
61 |
+
# Print the last assistant message from response
|
62 |
+
async def last_message(response):
|
63 |
+
last_msg = next(
|
64 |
+
(m.content for m in reversed(response.messages or []) if m.role == "assistant"),
|
65 |
+
None
|
66 |
+
)
|
67 |
+
return last_msg
|
68 |
+
|
69 |
+
|
70 |
+
# === Deployment Handler (FIXED) ===
|
71 |
+
def handle_deploy_and_close(platform):
|
72 |
+
"""Handle deployment and close modal in one action"""
|
73 |
+
if not platform:
|
74 |
+
# Return error message but keep modal open
|
75 |
+
return "Please select a platform.", gr.update(visible=True)
|
76 |
+
|
77 |
+
# Get current host URL (placeholder logic)
|
78 |
+
current_url = os.getenv("HOST_URL", "http://127.0.0.1:7860")
|
79 |
+
success_msg = f"β
Successfully deployed into {platform}!\nπ {current_url}"
|
80 |
+
# success_msg = f"β
Successfully deployed into {platform}!\nπ [{current_url}]({current_url})"
|
81 |
+
|
82 |
+
# Return success message and close modal
|
83 |
+
return success_msg, gr.update(visible=False)
|
84 |
+
|
85 |
+
def show_deploy_modal():
|
86 |
+
"""Show the deploy modal"""
|
87 |
+
return gr.update(visible=True)
|
88 |
+
|
89 |
+
def hide_deploy_modal():
|
90 |
+
"""Hide the deploy modal"""
|
91 |
+
return gr.update(visible=False)
|
92 |
+
|
93 |
+
# === Utility Functions ===
|
94 |
+
|
95 |
+
def toggle_visibility(current):
|
96 |
+
new_state = not current
|
97 |
+
return gr.update(visible=new_state), new_state
|
98 |
+
|
99 |
+
def handle_tool_selection(selected_tools):
|
100 |
+
# Always ensure "MCP Server Tool" is included
|
101 |
+
if "MCP Server Tool" not in selected_tools:
|
102 |
+
selected_tools = ["MCP Server Tool"] + selected_tools
|
103 |
+
return gr.update(value=selected_tools)
|
104 |
+
|
105 |
+
def save_agent(name, desc, instructions, tools, session_data):
|
106 |
+
if name:
|
107 |
+
agent = {
|
108 |
+
"name": name,
|
109 |
+
"desc": desc,
|
110 |
+
"instructions": instructions,
|
111 |
+
"tools": tools,
|
112 |
+
"kb_enabled": "KB Configuration" in tools # Track if KB is enabled
|
113 |
+
}
|
114 |
+
session_data["agents"].append(agent)
|
115 |
+
return (
|
116 |
+
gr.update(choices=[a["name"] for a in session_data["agents"]], value=name),
|
117 |
+
"Agent saved successfully!", # This is the missing return value for the textbox
|
118 |
+
session_data,
|
119 |
+
)
|
120 |
+
return gr.update(), "Agent name is required!", session_data
|
121 |
+
|
122 |
+
def store_files_in_session(uploaded_files, session_data):
|
123 |
+
"""Store uploaded files in session storage and return their paths."""
|
124 |
+
if not uploaded_files:
|
125 |
+
return []
|
126 |
+
|
127 |
+
# Initialize session_files if not exists
|
128 |
+
if "session_files" not in session_data:
|
129 |
+
session_data["session_files"] = {}
|
130 |
+
|
131 |
+
stored_file_paths = []
|
132 |
+
|
133 |
+
for file in uploaded_files:
|
134 |
+
# Generate unique filename to avoid conflicts
|
135 |
+
file_id = str(uuid.uuid4())
|
136 |
+
original_name = Path(file.name).name
|
137 |
+
file_extension = Path(file.name).suffix
|
138 |
+
stored_filename = f"{file_id}_{original_name.replace(' ', '_')}"
|
139 |
+
|
140 |
+
# Create session-specific directory
|
141 |
+
session_dir = SESSION_FILES_DIR / file_id[:8] # Use first 8 chars of UUID
|
142 |
+
session_dir.mkdir(exist_ok=True)
|
143 |
+
|
144 |
+
# Copy file to session storage
|
145 |
+
stored_file_path = session_dir / stored_filename
|
146 |
+
shutil.copy2(file.name, stored_file_path)
|
147 |
+
|
148 |
+
# Store file metadata in session
|
149 |
+
session_data["session_files"][file_id] = {
|
150 |
+
"original_name": original_name,
|
151 |
+
"stored_path": str(stored_file_path),
|
152 |
+
"file_extension": file_extension,
|
153 |
+
"file_size": os.path.getsize(stored_file_path)
|
154 |
+
}
|
155 |
+
|
156 |
+
stored_file_paths.append({
|
157 |
+
"file_id": file_id,
|
158 |
+
"original_name": original_name,
|
159 |
+
"stored_path": str(stored_file_path)
|
160 |
+
})
|
161 |
+
|
162 |
+
return stored_file_paths
|
163 |
+
|
164 |
+
def save_kb(uploaded_files, name, session_data):
|
165 |
+
if not name:
|
166 |
+
return (
|
167 |
+
gr.update(),
|
168 |
+
gr.update(),
|
169 |
+
gr.update(),
|
170 |
+
"Knowledge Base name required.",
|
171 |
+
session_data
|
172 |
+
)
|
173 |
+
|
174 |
+
if uploaded_files:
|
175 |
+
# Store files in session storage
|
176 |
+
stored_files = store_files_in_session(uploaded_files, session_data)
|
177 |
+
|
178 |
+
# Create knowledge base entry with stored file references
|
179 |
+
kb_entry = {
|
180 |
+
"name": name,
|
181 |
+
"files": stored_files, # Store file metadata instead of paths
|
182 |
+
"created_at": str(uuid.uuid4()) # Add timestamp/id for uniqueness
|
183 |
+
}
|
184 |
+
|
185 |
+
session_data["kb"].append(kb_entry)
|
186 |
+
|
187 |
+
file_names = ", ".join([f["original_name"] for f in stored_files])
|
188 |
+
message = f"""β
Knowledge Base Saved!\nName: {name}\nFiles: {file_names}"""
|
189 |
+
|
190 |
+
# Disable upload and save after saving
|
191 |
+
return (
|
192 |
+
gr.update(interactive=False),
|
193 |
+
gr.update(interactive=False),
|
194 |
+
gr.update(interactive=False),
|
195 |
+
message,
|
196 |
+
session_data
|
197 |
+
)
|
198 |
+
else:
|
199 |
+
return (
|
200 |
+
gr.update(),
|
201 |
+
gr.update(),
|
202 |
+
gr.update(),
|
203 |
+
"β No files uploaded. Please upload files before saving.",
|
204 |
+
session_data
|
205 |
+
)
|
206 |
+
|
207 |
+
def get_session_file_paths(session_data, kb_name):
|
208 |
+
"""Retrieve actual file paths for a given knowledge base from session storage."""
|
209 |
+
kb_entries = session_data.get("kb", [])
|
210 |
+
|
211 |
+
for kb in kb_entries:
|
212 |
+
if kb["name"] == kb_name:
|
213 |
+
file_paths = []
|
214 |
+
for file_info in kb.get("files", []):
|
215 |
+
if isinstance(file_info, dict) and "stored_path" in file_info:
|
216 |
+
# Verify file still exists
|
217 |
+
if os.path.exists(file_info["stored_path"]):
|
218 |
+
file_paths.append(file_info["stored_path"])
|
219 |
+
else:
|
220 |
+
print(f"Warning: File {file_info['original_name']} no longer exists at {file_info['stored_path']}")
|
221 |
+
elif isinstance(file_info, str):
|
222 |
+
# Handle legacy format
|
223 |
+
if os.path.exists(file_info):
|
224 |
+
file_paths.append(file_info)
|
225 |
+
return file_paths
|
226 |
+
|
227 |
+
return []
|
228 |
+
|
229 |
+
def build_configs_from_session(session_data):
|
230 |
+
kb_entries = session_data.get("kb", [])
|
231 |
+
agents = session_data.get("agents", [])
|
232 |
+
|
233 |
+
# Get the selected/active agent
|
234 |
+
selected_agent = agents[-1] if agents else {}
|
235 |
+
|
236 |
+
# Check if the selected agent has KB Configuration enabled
|
237 |
+
kb_enabled = selected_agent.get("kb_enabled", False)
|
238 |
+
|
239 |
+
if not kb_enabled or not kb_entries:
|
240 |
+
# No KB configuration or agent doesn't use KB
|
241 |
+
agno_kb_config = {
|
242 |
+
"knowledge_base": {
|
243 |
+
"collection_name": "default_collection",
|
244 |
+
"chunk_size": 1000,
|
245 |
+
"overlap": 200,
|
246 |
+
"num_documents": 6,
|
247 |
+
"chunking_strategy": "fixed",
|
248 |
+
"recreate": False,
|
249 |
+
"input_data": {
|
250 |
+
"type": "pdf",
|
251 |
+
"source": []
|
252 |
+
}
|
253 |
+
},
|
254 |
+
"instructions": {
|
255 |
+
"collections_to_search": "default_collection"
|
256 |
+
}
|
257 |
+
}
|
258 |
+
else:
|
259 |
+
# Use KB configuration since agent has KB enabled
|
260 |
+
selected_kb = kb_entries[0] # Use the first KB entry
|
261 |
+
kb_name = selected_kb["name"]
|
262 |
+
|
263 |
+
# Get actual file paths from session storage
|
264 |
+
file_paths = get_session_file_paths(session_data, kb_name)
|
265 |
+
|
266 |
+
agno_kb_config = {
|
267 |
+
"knowledge_base": {
|
268 |
+
"collection_name": kb_name,
|
269 |
+
"chunk_size": 1000,
|
270 |
+
"overlap": 200,
|
271 |
+
"num_documents": 6,
|
272 |
+
"chunking_strategy": "fixed",
|
273 |
+
"recreate": False,
|
274 |
+
"input_data": {
|
275 |
+
"type": "pdf",
|
276 |
+
"source": [{"path": file_paths}] if file_paths else []
|
277 |
+
}
|
278 |
+
},
|
279 |
+
"instructions": {
|
280 |
+
"collections_to_search": kb_name
|
281 |
+
}
|
282 |
+
}
|
283 |
+
|
284 |
+
# Base agent config from session
|
285 |
+
agent_description = selected_agent.get("desc", "Agent Description")
|
286 |
+
agent_instructions = selected_agent.get("instructions", [])
|
287 |
+
|
288 |
+
# Extended defaults
|
289 |
+
default_description = (
|
290 |
+
"This agent dynamically selects and uses tools based on the user's query. "
|
291 |
+
"It can access two main resources: an MCP tools with a SQL database and a knowledge base."
|
292 |
+
)
|
293 |
+
|
294 |
+
default_instructions = [
|
295 |
+
"Carefully analyze the user's question to determine the context.",
|
296 |
+
"If a query is related to the knowledge base, use the knowledge base to answer or perform the action.",
|
297 |
+
"If the question is related to SQL database operationsβsuch as retrieving dataβuse the MCP tools to answer or perform the action.",
|
298 |
+
"If the question is related to generation of graph, plot, chart, or visualization, use the MCP tools 'get_columns_info_from_database' tool to get the necessary columns information then use 'generate_python_code' tool to generate the python code and finally use 'visualization_tool' tool to execute the python code and generate the visualization. While generating python code for chart or plot or graph use different and attractive color combinations for visualization. It should be multi-color and attractive for better visualization.",
|
299 |
+
"If the query requires both, prioritize extracting structured data from the SQL database first, then supplement with information from the knowledge base as needed.",
|
300 |
+
"Always respond with accurate, concise, and relevant information based on the selected tool."
|
301 |
+
]
|
302 |
+
|
303 |
+
# Merge instructions
|
304 |
+
if isinstance(agent_instructions, str):
|
305 |
+
agent_instructions = [agent_instructions]
|
306 |
+
elif not isinstance(agent_instructions, list):
|
307 |
+
agent_instructions = []
|
308 |
+
|
309 |
+
agent_config = {
|
310 |
+
"name": selected_agent.get("name", "Default Agent"),
|
311 |
+
"description": f"{agent_description}\n\n{default_description}",
|
312 |
+
"instructions": agent_instructions + default_instructions,
|
313 |
+
"kb_enabled": kb_enabled
|
314 |
+
}
|
315 |
+
|
316 |
+
return agno_kb_config, agent_config
|
317 |
+
|
318 |
+
def cleanup_session_files(session_data):
|
319 |
+
"""Clean up session files when session ends (optional)."""
|
320 |
+
session_files = session_data.get("session_files", {})
|
321 |
+
|
322 |
+
for file_id, file_info in session_files.items():
|
323 |
+
file_path = file_info.get("stored_path")
|
324 |
+
if file_path and os.path.exists(file_path):
|
325 |
+
try:
|
326 |
+
os.remove(file_path)
|
327 |
+
# Also try to remove the directory if it's empty
|
328 |
+
parent_dir = Path(file_path).parent
|
329 |
+
if parent_dir.exists() and not any(parent_dir.iterdir()):
|
330 |
+
parent_dir.rmdir()
|
331 |
+
except OSError as e:
|
332 |
+
print(f"Error cleaning up file {file_path}: {e}")
|
333 |
+
|
334 |
+
# === Backend query handler with routing functionality ===
|
335 |
+
async def handle_query(query, chat_history, session_data):
|
336 |
+
try:
|
337 |
+
user_id = str(uuid.uuid4())
|
338 |
+
thread_id = str(uuid.uuid4())
|
339 |
+
|
340 |
+
agno_kb_config, agent_config = build_configs_from_session(session_data)
|
341 |
+
|
342 |
+
print("Knowledge base config:", json.dumps(agno_kb_config, indent=2))
|
343 |
+
print("Agent config:", json.dumps(agent_config, indent=2))
|
344 |
+
|
345 |
+
if agent_config.get("kb_enabled") and agno_kb_config["knowledge_base"]["input_data"]["source"]:
|
346 |
+
file_paths = agno_kb_config["knowledge_base"]["input_data"]["source"][0].get("path", [])
|
347 |
+
existing_files = [f for f in file_paths if os.path.exists(f)]
|
348 |
+
print(f"Files to process: {len(existing_files)} out of {len(file_paths)}")
|
349 |
+
if len(existing_files) != len(file_paths):
|
350 |
+
print("Warning: Some files are missing!")
|
351 |
+
|
352 |
+
agno_kb = None
|
353 |
+
if agent_config.get("kb_enabled"):
|
354 |
+
agno_kb_module = AgnoKnowledgeBase(
|
355 |
+
query=query,
|
356 |
+
user_id=user_id,
|
357 |
+
thread_id=thread_id,
|
358 |
+
agno_kb_config=agno_kb_config
|
359 |
+
)
|
360 |
+
agno_kb = agno_kb_module.setup_knowledge_base()
|
361 |
+
|
362 |
+
async with MCPTools(url="http://127.0.0.1:7863/gradio_api/mcp/sse", transport="sse") as mcp_tool:
|
363 |
+
agent_factory = AgentFactory(user_id, thread_id, agent_config, knowledge_base=agno_kb)
|
364 |
+
router = await agent_factory.routing_agent()
|
365 |
+
router_event = await router.arun(query, stream=False)
|
366 |
+
router_event_resp = await last_message(router_event)
|
367 |
+
|
368 |
+
if router_event_resp and router_event_resp.lower().strip() == "visualization":
|
369 |
+
model_name = "Qwen/Qwen3-235B-A22B"
|
370 |
+
else:
|
371 |
+
model_name = "meta-llama/Meta-Llama-3.1-405B-Instruct"
|
372 |
+
|
373 |
+
agent = await agent_factory.normal_and_reasoning_agent(
|
374 |
+
tools=[mcp_tool],
|
375 |
+
model_name=model_name
|
376 |
+
)
|
377 |
+
|
378 |
+
response = await agent.arun(query, stream=False)
|
379 |
+
|
380 |
+
last_msg = next(
|
381 |
+
(m.content for m in reversed(response.messages or []) if m.role == "assistant"),
|
382 |
+
"β οΈ No response from agent."
|
383 |
+
)
|
384 |
+
|
385 |
+
image_paths = get_images_from_folder()
|
386 |
+
|
387 |
+
updated_chat_history = chat_history + [{"role": "user", "content": query}]
|
388 |
+
|
389 |
+
if image_paths:
|
390 |
+
print(f"Found {len(image_paths)} images to process")
|
391 |
+
|
392 |
+
formatted_response = format_response_with_images(last_msg, len(image_paths))
|
393 |
+
updated_chat_history.append({
|
394 |
+
"role": "assistant",
|
395 |
+
"content": formatted_response
|
396 |
+
})
|
397 |
+
|
398 |
+
for img_path in image_paths:
|
399 |
+
try:
|
400 |
+
img_html = add_image_as_base64_html(img_path)
|
401 |
+
if img_html:
|
402 |
+
updated_chat_history.append({
|
403 |
+
"role": "assistant",
|
404 |
+
"content": img_html
|
405 |
+
})
|
406 |
+
print(f"Added image via base64 HTML: {os.path.basename(img_path)}")
|
407 |
+
else:
|
408 |
+
updated_chat_history.append({
|
409 |
+
"role": "assistant",
|
410 |
+
"content": f"πΌοΈ Generated visualization: {os.path.basename(img_path)} (Error displaying image)"
|
411 |
+
})
|
412 |
+
|
413 |
+
except Exception as e:
|
414 |
+
print(f"Error processing image {img_path}: {e}")
|
415 |
+
updated_chat_history.append({
|
416 |
+
"role": "assistant",
|
417 |
+
"content": f"β οΈ Error displaying image: {os.path.basename(img_path)}"
|
418 |
+
})
|
419 |
+
|
420 |
+
# Delete images after displaying them
|
421 |
+
delete_images_from_folder(image_paths)
|
422 |
+
|
423 |
+
else:
|
424 |
+
updated_chat_history.append({
|
425 |
+
"role": "assistant",
|
426 |
+
"content": last_msg
|
427 |
+
})
|
428 |
+
|
429 |
+
return updated_chat_history, session_data
|
430 |
+
|
431 |
+
except Exception as e:
|
432 |
+
traceback.print_exc()
|
433 |
+
error_msg = f"Error processing query: {str(e)}"
|
434 |
+
updated_chat_history = chat_history + [
|
435 |
+
{"role": "user", "content": query},
|
436 |
+
{"role": "assistant", "content": error_msg}
|
437 |
+
]
|
438 |
+
return updated_chat_history, session_data
|
439 |
+
|
440 |
+
|
441 |
+
# === Image Handling Functions ===
|
442 |
+
def get_images_from_folder():
|
443 |
+
"""Get all image files from the specified folder."""
|
444 |
+
if not IMAGES_FOLDER_PATH.exists():
|
445 |
+
print(f"Images folder does not exist: {IMAGES_FOLDER_PATH}")
|
446 |
+
return []
|
447 |
+
|
448 |
+
# Look for common image file extensions
|
449 |
+
image_extensions = {'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.svg', '.webp'}
|
450 |
+
image_files = []
|
451 |
+
|
452 |
+
try:
|
453 |
+
for file_path in IMAGES_FOLDER_PATH.iterdir():
|
454 |
+
if file_path.is_file() and file_path.suffix.lower() in image_extensions:
|
455 |
+
image_files.append(str(file_path))
|
456 |
+
|
457 |
+
print(f"Found {len(image_files)} images in {IMAGES_FOLDER_PATH}")
|
458 |
+
return image_files
|
459 |
+
|
460 |
+
except Exception as e:
|
461 |
+
print(f"Error scanning images folder: {e}")
|
462 |
+
return []
|
463 |
+
|
464 |
+
|
465 |
+
def format_response_with_images(text_response, image_count):
|
466 |
+
"""Format the response to include text and mention images."""
|
467 |
+
if not image_count:
|
468 |
+
return text_response
|
469 |
+
|
470 |
+
# Add a clear separator and mention of visualizations
|
471 |
+
separator = "\n\n" + "="*50 + "\n"
|
472 |
+
|
473 |
+
if image_count == 1:
|
474 |
+
return f"{text_response}{separator}π **Generated Visualization** (displayed below):"
|
475 |
+
else:
|
476 |
+
return f"{text_response}{separator}π **Generated {image_count} Visualizations** (displayed below):"
|
477 |
+
|
478 |
+
|
479 |
+
def add_image_as_base64_html(image_path):
|
480 |
+
"""Convert image to base64 HTML for direct embedding with larger size and better quality."""
|
481 |
+
try:
|
482 |
+
import io, base64, os
|
483 |
+
from PIL import Image
|
484 |
+
|
485 |
+
with Image.open(image_path) as img:
|
486 |
+
# Resize for web display while maintaining aspect ratio (larger size)
|
487 |
+
img.thumbnail((1200, 900), Image.Resampling.LANCZOS)
|
488 |
+
|
489 |
+
# Convert to RGB if necessary
|
490 |
+
if img.mode in ('RGBA', 'LA', 'P'):
|
491 |
+
background = Image.new('RGB', img.size, (255, 255, 255))
|
492 |
+
if img.mode == 'P':
|
493 |
+
img = img.convert('RGBA')
|
494 |
+
background.paste(img, mask=img.split()[-1] if img.mode == 'RGBA' else None)
|
495 |
+
img = background
|
496 |
+
|
497 |
+
# Convert to base64
|
498 |
+
buffer = io.BytesIO()
|
499 |
+
img.save(buffer, format='PNG')
|
500 |
+
img_bytes = buffer.getvalue()
|
501 |
+
img_base64 = base64.b64encode(img_bytes).decode('utf-8')
|
502 |
+
|
503 |
+
# Create styled HTML img tag with bigger max width and height
|
504 |
+
img_html = f'''
|
505 |
+
<div style="text-align: center; margin: 15px 0; padding: 10px; border: 1px solid #e0e0e0; border-radius: 10px; background-color: #fafafa;">
|
506 |
+
<img src="data:image/png;base64,{img_base64}"
|
507 |
+
alt="{os.path.basename(image_path)}"
|
508 |
+
style="max-width: 90%; max-height: 900px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.15); display: block; margin: 0 auto;">
|
509 |
+
<p style="margin-top: 10px; font-size: 16px; color: #555; text-align: center; font-weight: 600;">
|
510 |
+
π {os.path.basename(image_path)}
|
511 |
+
</p>
|
512 |
+
</div>
|
513 |
+
'''
|
514 |
+
return img_html
|
515 |
+
|
516 |
+
except Exception as e:
|
517 |
+
print(f"Error converting image {image_path} to base64: {e}")
|
518 |
+
return None
|
519 |
+
|
520 |
+
|
521 |
+
except Exception as e:
|
522 |
+
print(f"Error converting image {image_path} to base64: {e}")
|
523 |
+
return None
|
524 |
+
|
525 |
+
|
526 |
+
def delete_images_from_folder(image_paths):
|
527 |
+
"""Delete specified image files from the folder."""
|
528 |
+
deleted_count = 0
|
529 |
+
for image_path in image_paths:
|
530 |
+
try:
|
531 |
+
if os.path.exists(image_path):
|
532 |
+
os.remove(image_path)
|
533 |
+
deleted_count += 1
|
534 |
+
print(f"Deleted image: {os.path.basename(image_path)}")
|
535 |
+
except OSError as e:
|
536 |
+
print(f"Error deleting image {image_path}: {e}")
|
537 |
+
|
538 |
+
if deleted_count > 0:
|
539 |
+
print(f"Successfully deleted {deleted_count} images from folder")
|
540 |
+
|
541 |
+
|
542 |
+
with gr.Blocks(theme=gr.themes.Soft(),
|
543 |
+
css="""
|
544 |
+
#agent-popup, #kb-popup {
|
545 |
+
position: fixed !important;
|
546 |
+
top: 50% !important;
|
547 |
+
left: 50% !important;
|
548 |
+
transform: translate(-50%, -50%) !important;
|
549 |
+
background-color: #1e1e1e !important;
|
550 |
+
border: 2px solid #ccc !important;
|
551 |
+
border-radius: 10px !important;
|
552 |
+
padding: 20px !important;
|
553 |
+
z-index: 1000 !important;
|
554 |
+
box-shadow: 0 0 20px rgba(0,0,0,0.5);
|
555 |
+
}
|
556 |
+
|
557 |
+
#tool-popup {
|
558 |
+
position: fixed !important;
|
559 |
+
top: 0 !important;
|
560 |
+
left: 0 !important;
|
561 |
+
right: 0 !important;
|
562 |
+
bottom: 0 !important;
|
563 |
+
width: 100vw !important;
|
564 |
+
height: 100vh !important;
|
565 |
+
max-width: none !important;
|
566 |
+
max-height: none !important;
|
567 |
+
margin: 0 !important;
|
568 |
+
padding: 40px !important;
|
569 |
+
background-color: #1e1e1e !important;
|
570 |
+
border: none !important;
|
571 |
+
border-radius: 0 !important;
|
572 |
+
z-index: 9999 !important;
|
573 |
+
box-shadow: none !important;
|
574 |
+
overflow-y: auto !important;
|
575 |
+
transform: none !important;
|
576 |
+
}
|
577 |
+
|
578 |
+
#tool-popup > * {
|
579 |
+
max-width: none !important;
|
580 |
+
}
|
581 |
+
|
582 |
+
.deploy-modal {
|
583 |
+
background-color: #2d2d2d !important;
|
584 |
+
border: 2px solid #444 !important;
|
585 |
+
border-radius: 15px !important;
|
586 |
+
padding: 30px !important;
|
587 |
+
max-width: 450px !important;
|
588 |
+
width: 90% !important;
|
589 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.8) !important;
|
590 |
+
color: white !important;
|
591 |
+
}
|
592 |
+
|
593 |
+
.modal-overlay {
|
594 |
+
position: fixed !important;
|
595 |
+
top: 0 !important;
|
596 |
+
left: 0 !important;
|
597 |
+
right: 0 !important;
|
598 |
+
bottom: 0 !important;
|
599 |
+
background-color: rgba(0, 0, 0, 0.5) !important;
|
600 |
+
z-index: 999 !important;
|
601 |
+
display: flex !important;
|
602 |
+
align-items: center !important;
|
603 |
+
justify-content: center !important;
|
604 |
+
}
|
605 |
+
|
606 |
+
.modal {
|
607 |
+
background-color: #1e1e1e !important;
|
608 |
+
border: 2px solid #ccc !important;
|
609 |
+
border-radius: 10px !important;
|
610 |
+
padding: 20px !important;
|
611 |
+
max-width: 500px !important;
|
612 |
+
width: 90% !important;
|
613 |
+
box-shadow: 0 0 20px rgba(0,0,0,0.5) !important;
|
614 |
+
}
|
615 |
+
|
616 |
+
.small-upload .wrap-inner {
|
617 |
+
padding: 6px !important;
|
618 |
+
font-size: 12px !important;
|
619 |
+
}
|
620 |
+
.closing-btn {
|
621 |
+
font-size: 14px !important;
|
622 |
+
color: #e74c3c;
|
623 |
+
background: transparent;
|
624 |
+
border: none;
|
625 |
+
text-align: left;
|
626 |
+
cursor: pointer;
|
627 |
+
}
|
628 |
+
""")as app:
|
629 |
+
|
630 |
+
# Initialize session state
|
631 |
+
session_data = gr.State({
|
632 |
+
"agents": [],
|
633 |
+
"tools": [],
|
634 |
+
"kb": [],
|
635 |
+
"session_files": {} # Add session files storage
|
636 |
+
})
|
637 |
+
|
638 |
+
kb_visible = gr.State(False)
|
639 |
+
tool_visible = gr.State(False)
|
640 |
+
agent_visible = gr.State(False)
|
641 |
+
deploy_visible = gr.State(False)
|
642 |
+
|
643 |
+
with gr.Group(visible=True):
|
644 |
+
with gr.Row():
|
645 |
+
with gr.Column(scale=3):
|
646 |
+
gr.Markdown("### π Knowledge Base")
|
647 |
+
kb_btn = gr.Button("Add KB β")
|
648 |
+
|
649 |
+
# Display current KB status
|
650 |
+
with gr.Group():
|
651 |
+
kb_status_display = gr.Textbox(
|
652 |
+
label="Current KB Status",
|
653 |
+
value="No Knowledge Base loaded",
|
654 |
+
interactive=False
|
655 |
+
)
|
656 |
+
|
657 |
+
gr.Markdown("### π§° Tools")
|
658 |
+
tool_btn = gr.Button("Add Tool")
|
659 |
+
gr.Markdown("### π€ Agent")
|
660 |
+
agent_btn = gr.Button("Add Agent")
|
661 |
+
gr.Markdown("### π§ Active Agent")
|
662 |
+
active_agent = gr.Radio(choices=[], label="Selected Agent", interactive=True)
|
663 |
+
|
664 |
+
# Style for Send Button
|
665 |
+
gr.HTML("""
|
666 |
+
<style>
|
667 |
+
#send-btn {
|
668 |
+
height: 50px;
|
669 |
+
font-size: 16px;
|
670 |
+
padding: 10px 20px;
|
671 |
+
background-color: #6262E2;
|
672 |
+
color: white;
|
673 |
+
border: none;
|
674 |
+
border-radius: 8px;
|
675 |
+
cursor: pointer;
|
676 |
+
}
|
677 |
+
#send-btn:hover {
|
678 |
+
background-color: #0056b3;
|
679 |
+
}
|
680 |
+
</style>
|
681 |
+
""")
|
682 |
+
|
683 |
+
with gr.Column(scale=7):
|
684 |
+
# gr.Markdown("## Dynamic and Customizable AI Agent")
|
685 |
+
gr.Markdown("<h2 style='color:#4CAF50;'>NoCoMind - Dynamic & Customizable AI Agents</h2>")
|
686 |
+
deploy_output = gr.Textbox(label="Deployment Status", visible=False, lines=2)
|
687 |
+
|
688 |
+
chatbot = gr.Chatbot(
|
689 |
+
label="Chat",
|
690 |
+
height=400,
|
691 |
+
type="messages",
|
692 |
+
elem_classes=["chatbot-container"],
|
693 |
+
show_copy_button=True,
|
694 |
+
bubble_full_width=False,
|
695 |
+
render_markdown=True,
|
696 |
+
value=[]
|
697 |
+
)
|
698 |
+
|
699 |
+
with gr.Row():
|
700 |
+
user_input = gr.Textbox(
|
701 |
+
placeholder="Type your message...",
|
702 |
+
show_label=False,
|
703 |
+
scale=4,
|
704 |
+
lines=1
|
705 |
+
)
|
706 |
+
send_btn = gr.Button(
|
707 |
+
"Send",
|
708 |
+
scale=0.1,
|
709 |
+
elem_id="send-btn"
|
710 |
+
)
|
711 |
+
|
712 |
+
|
713 |
+
# === Modals ===
|
714 |
+
with gr.Group(visible=False, elem_id="kb-popup") as kb_popup:
|
715 |
+
gr.Markdown("### Knowledge Base")
|
716 |
+
kb_files = gr.File(
|
717 |
+
label="Upload Files",
|
718 |
+
file_types=[".pdf"],
|
719 |
+
file_count="multiple",
|
720 |
+
elem_classes=["small-upload"],
|
721 |
+
interactive=True
|
722 |
+
)
|
723 |
+
kb_name = gr.Textbox(label="Name:", interactive=True)
|
724 |
+
kb_save = gr.Button("Save", interactive=True)
|
725 |
+
kb_output = gr.Textbox(label="Status", lines=4)
|
726 |
+
kb_close = gr.Button("β Close", elem_classes=["closing-btn"])
|
727 |
+
|
728 |
+
with gr.Group(visible=False, elem_id="tool-popup") as tool_popup:
|
729 |
+
gr.Markdown("## Tool Configuration")
|
730 |
+
tool_description = gr.Textbox(
|
731 |
+
label="Description",
|
732 |
+
value='''
|
733 |
+
β¨ You're now using a Gradio MCP tools!
|
734 |
+
This feature helps our smart agents assist you better with your tasks.
|
735 |
+
Just sit back and let the Gradio MCP Agents do the heavy lifting for you!
|
736 |
+
''' ,
|
737 |
+
lines = 4
|
738 |
+
)
|
739 |
+
|
740 |
+
with gr.Blocks() as demo:
|
741 |
+
gr.Markdown(
|
742 |
+
'''
|
743 |
+
π **Integrated Tools in Gradio MCP Agents**
|
744 |
+
|
745 |
+
1. π **SQL Database Engine Tool** β Currenty, Flipkart sample database is already configured. We can add any database in the future.
|
746 |
+
Query and interact with structured data effortlessly. Whether you're retrieving, filtering, or analyzing records, this tool lets the agents access your database just like a data expert.
|
747 |
+
|
748 |
+
2. π **Python Code Generator Tool**
|
749 |
+
Instantly generate and run Python code for your needs. From data processing to simple logic, let the agents write and execute scripts without you touching a single line of code.
|
750 |
+
|
751 |
+
3. π **Visualization Tool**
|
752 |
+
Transform data into beautiful, interactive charts and plots. This tool helps agents present insights in a clear and visually appealing way, making it easier for you to understand and decide.
|
753 |
+
|
754 |
+
π **Use the buttons below to open or close the Flipkart database preview (`mobiles` table):**
|
755 |
+
'''
|
756 |
+
)
|
757 |
+
|
758 |
+
show_db_btn = gr.Button("Show Database Preview")
|
759 |
+
hide_db_btn = gr.Button("Hide Database Preview")
|
760 |
+
|
761 |
+
data_preview = gr.Dataframe(
|
762 |
+
value=None,
|
763 |
+
label="π Flipkart DB (Full View)",
|
764 |
+
interactive=False,
|
765 |
+
wrap=True
|
766 |
+
)
|
767 |
+
|
768 |
+
# Button clicks update the dataframe accordingly
|
769 |
+
show_db_btn.click(fn=load_entire_database, inputs=None, outputs=data_preview)
|
770 |
+
hide_db_btn.click(fn=clear_database_view, inputs=None, outputs=data_preview)
|
771 |
+
|
772 |
+
tool_close = gr.Button("β Close", elem_classes=["closing-btn"], variant="secondary")
|
773 |
+
|
774 |
+
with gr.Group(visible=False, elem_id="agent-popup") as agent_popup:
|
775 |
+
gr.HTML('<div style="max-height: 400px; overflow-y: auto; padding: 10px;">')
|
776 |
+
gr.Markdown("#### Agent Configuration")
|
777 |
+
agent_name = gr.Textbox(label="Name", placeholder="Enter agent name")
|
778 |
+
agent_desc = gr.Textbox(label="Description", placeholder="Enter description", lines=1)
|
779 |
+
agent_instructions = gr.Textbox(label="Instructions", placeholder="Enter specific instructions", lines=2)
|
780 |
+
tool_select = gr.CheckboxGroup(
|
781 |
+
choices=available_tools,
|
782 |
+
label="Tools",
|
783 |
+
value=["MCP Server Tool"], # Always start with MCP Server Tool selected
|
784 |
+
info="Note: MCP Server Tool is always required and will be automatically selected"
|
785 |
+
)
|
786 |
+
agent_save = gr.Button("πΎ Save Agent")
|
787 |
+
agent_status = gr.Textbox(label="Status", interactive=False,lines = 1)
|
788 |
+
agent_close = gr.Button("β Close")
|
789 |
+
gr.HTML('</div>')
|
790 |
+
|
791 |
+
# === Deploy Modal ===
|
792 |
+
|
793 |
+
with gr.Group(visible=False, elem_id="deploy-modal-overlay") as deploy_overlay:
|
794 |
+
with gr.Group(elem_id="deploy-modal"):
|
795 |
+
gr.Markdown("## π Choose Deployment Option")
|
796 |
+
deploy_choice = gr.Radio(
|
797 |
+
choices=["GCP", "AWS", "Azure", "Gradio Cloud"],
|
798 |
+
label="Select Platform",
|
799 |
+
value=None # No default selection
|
800 |
+
)
|
801 |
+
with gr.Row():
|
802 |
+
confirm_deploy = gr.Button("β
Confirm", variant="primary")
|
803 |
+
close_deploy = gr.Button("β Close", elem_id="closing-btn", variant="secondary")
|
804 |
+
|
805 |
+
trigger_btn = gr.Button("π Deploy AI Agent")
|
806 |
+
|
807 |
+
# === Event Bindings ===
|
808 |
+
|
809 |
+
kb_btn.click(toggle_visibility, inputs=kb_visible, outputs=[kb_popup, kb_visible])
|
810 |
+
kb_close.click(toggle_visibility, inputs=kb_visible, outputs=[kb_popup, kb_visible])
|
811 |
+
|
812 |
+
tool_btn.click(toggle_visibility, inputs=tool_visible, outputs=[tool_popup, tool_visible])
|
813 |
+
tool_close.click(toggle_visibility, inputs=tool_visible, outputs=[tool_popup, tool_visible])
|
814 |
+
|
815 |
+
agent_btn.click(toggle_visibility, inputs=agent_visible, outputs=[agent_popup, agent_visible])
|
816 |
+
agent_close.click(toggle_visibility, inputs=agent_visible, outputs=[agent_popup, agent_visible])
|
817 |
+
|
818 |
+
|
819 |
+
# Show modal on trigger
|
820 |
+
trigger_btn.click(toggle_visibility, inputs=deploy_visible, outputs=[deploy_overlay, deploy_visible])
|
821 |
+
confirm_deploy.click(toggle_visibility, inputs=deploy_visible, outputs=[deploy_overlay, deploy_visible])
|
822 |
+
close_deploy.click(toggle_visibility, inputs=deploy_visible, outputs=[deploy_overlay, deploy_visible])
|
823 |
+
|
824 |
+
close_deploy.click(
|
825 |
+
fn=lambda: gr.update(visible=False),
|
826 |
+
outputs=deploy_overlay
|
827 |
+
)
|
828 |
+
|
829 |
+
# FIXED: Confirm deploy now properly handles the modal state
|
830 |
+
confirm_deploy.click(
|
831 |
+
fn=handle_deploy_and_close,
|
832 |
+
inputs=deploy_choice,
|
833 |
+
outputs=[deploy_output, deploy_overlay]
|
834 |
+
).then(
|
835 |
+
fn=lambda: gr.update(visible=True), # Show the deploy_output after successful deployment
|
836 |
+
outputs=deploy_output
|
837 |
+
)
|
838 |
+
|
839 |
+
# Add event handler to ensure MCP Server Tool stays selected
|
840 |
+
tool_select.change(
|
841 |
+
fn=handle_tool_selection,
|
842 |
+
inputs=[tool_select],
|
843 |
+
outputs=[tool_select]
|
844 |
+
)
|
845 |
+
|
846 |
+
agent_save.click(
|
847 |
+
save_agent,
|
848 |
+
inputs=[agent_name, agent_desc, agent_instructions, tool_select, session_data],
|
849 |
+
outputs=[active_agent,agent_status, session_data]
|
850 |
+
)
|
851 |
+
|
852 |
+
# Updated KB save with session file handling and disabling functionality
|
853 |
+
def save_kb_and_update_status(uploaded_files, name, session_data):
|
854 |
+
kb_files_update, name_update, save_btn_update, result_msg, updated_session = save_kb(uploaded_files, name, session_data)
|
855 |
+
|
856 |
+
# Update KB status display
|
857 |
+
kb_list = updated_session.get("kb", [])
|
858 |
+
if kb_list:
|
859 |
+
latest_kb = kb_list[-1]
|
860 |
+
file_count = len(latest_kb.get("files", []))
|
861 |
+
status_msg = f"β
KB '{latest_kb['name']}' loaded with {file_count} files"
|
862 |
+
else:
|
863 |
+
status_msg = "No Knowledge Base loaded"
|
864 |
+
|
865 |
+
return kb_files_update, name_update, save_btn_update, result_msg, updated_session, status_msg
|
866 |
+
|
867 |
+
kb_save.click(
|
868 |
+
save_kb_and_update_status,
|
869 |
+
inputs=[kb_files, kb_name, session_data],
|
870 |
+
outputs=[kb_files, kb_name, kb_save, kb_output, session_data, kb_status_display]
|
871 |
+
)
|
872 |
+
|
873 |
+
# UPDATED: Send button with proper chat history management
|
874 |
+
def send_message_wrapper(query, current_chat, session_data):
|
875 |
+
if not query.strip():
|
876 |
+
return current_chat + [{"role": "assistant", "content": "Please enter a question."}], session_data, ""
|
877 |
+
|
878 |
+
# Check if agent has KB enabled and KB is available
|
879 |
+
agents = session_data.get("agents", [])
|
880 |
+
if agents:
|
881 |
+
selected_agent = agents[-1] # Get the last/active agent
|
882 |
+
if selected_agent.get("kb_enabled", False):
|
883 |
+
kb_list = session_data.get("kb", [])
|
884 |
+
if not kb_list:
|
885 |
+
error_response = current_chat + [
|
886 |
+
{"role": "user", "content": query},
|
887 |
+
{"role": "assistant", "content": "β οΈ Agent requires KB Configuration but no Knowledge Base is loaded. Please upload and save a KB first."}
|
888 |
+
]
|
889 |
+
return error_response, session_data, ""
|
890 |
+
|
891 |
+
# Call the async handler with current chat history
|
892 |
+
try:
|
893 |
+
updated_chat, updated_session = asyncio.run(handle_query(query, current_chat, session_data))
|
894 |
+
return updated_chat, updated_session, "" # Clear input
|
895 |
+
except Exception as e:
|
896 |
+
print(f"Error in send_message_wrapper: {e}")
|
897 |
+
error_response = current_chat + [
|
898 |
+
{"role": "user", "content": query},
|
899 |
+
{"role": "assistant", "content": f"Error: {str(e)}"}
|
900 |
+
]
|
901 |
+
return error_response, session_data, ""
|
902 |
+
|
903 |
+
# NEW: Clear chat function
|
904 |
+
def clear_chat_history(session_data):
|
905 |
+
"""Clear the chat history"""
|
906 |
+
session_data["chat_history"] = []
|
907 |
+
return [], session_data
|
908 |
+
|
909 |
+
# Updated event handlers for sending messages
|
910 |
+
send_btn.click(
|
911 |
+
fn=send_message_wrapper,
|
912 |
+
inputs=[user_input, chatbot, session_data],
|
913 |
+
outputs=[chatbot, session_data, user_input]
|
914 |
+
)
|
915 |
+
|
916 |
+
# Allow Enter key to send message
|
917 |
+
user_input.submit(
|
918 |
+
fn=send_message_wrapper,
|
919 |
+
inputs=[user_input, chatbot, session_data],
|
920 |
+
outputs=[chatbot, session_data, user_input]
|
921 |
+
)
|
922 |
+
|
923 |
+
if __name__ == "__main__":
|
924 |
+
mcp_process = multiprocessing.Process(target=run_mcp_server)
|
925 |
+
mcp_process.start()
|
926 |
+
|
927 |
+
# Give MCP server time to start
|
928 |
+
import time
|
929 |
+
time.sleep(3)
|
930 |
+
|
931 |
+
app.launch(server_port=7860, debug=True)
|