Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -371,6 +371,7 @@ if _output_buffer:
|
|
| 371 |
executor = SecureCodeExecutor()
|
| 372 |
|
| 373 |
# MCP Functions
|
|
|
|
| 374 |
def execute_python_code(code: str, session_id: str = None) -> str:
|
| 375 |
"""
|
| 376 |
Execute Python code safely with visualization support.
|
|
@@ -380,9 +381,39 @@ def execute_python_code(code: str, session_id: str = None) -> str:
|
|
| 380 |
session_id (str, optional): Session ID for persistent context
|
| 381 |
|
| 382 |
Returns:
|
| 383 |
-
str: JSON string with execution results
|
| 384 |
"""
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
|
| 387 |
def execute_javascript_code(code: str, session_id: str = None) -> str:
|
| 388 |
"""
|
|
|
|
| 371 |
executor = SecureCodeExecutor()
|
| 372 |
|
| 373 |
# MCP Functions
|
| 374 |
+
# Replace the execute_python_code function with this enhanced version
|
| 375 |
def execute_python_code(code: str, session_id: str = None) -> str:
|
| 376 |
"""
|
| 377 |
Execute Python code safely with visualization support.
|
|
|
|
| 381 |
session_id (str, optional): Session ID for persistent context
|
| 382 |
|
| 383 |
Returns:
|
| 384 |
+
str: JSON string with execution results, including image data for LibreChat
|
| 385 |
"""
|
| 386 |
+
result_json = executor.execute_code(code, "python", session_id)
|
| 387 |
+
result = json.loads(result_json)
|
| 388 |
+
|
| 389 |
+
# If there's a plot, we need to format it specially for LibreChat
|
| 390 |
+
if result.get("plot"):
|
| 391 |
+
plot_base64 = result["plot"]
|
| 392 |
+
|
| 393 |
+
# Create a response that LibreChat can understand
|
| 394 |
+
# Include both the text output and indicate there's an image
|
| 395 |
+
response = {
|
| 396 |
+
"success": result["success"],
|
| 397 |
+
"stdout": result.get("stdout", ""),
|
| 398 |
+
"stderr": result.get("stderr", ""),
|
| 399 |
+
"session_id": result.get("session_id"),
|
| 400 |
+
"execution_time": result.get("execution_time"),
|
| 401 |
+
# Add image information
|
| 402 |
+
"images": [
|
| 403 |
+
{
|
| 404 |
+
"type": "image/png",
|
| 405 |
+
"data": plot_base64,
|
| 406 |
+
"description": "Generated matplotlib plot"
|
| 407 |
+
}
|
| 408 |
+
],
|
| 409 |
+
# Also include a message about the image
|
| 410 |
+
"message": f"{result.get('stdout', '')}\n\n📊 Generated visualization (matplotlib plot)"
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
return json.dumps(response, indent=2)
|
| 414 |
+
|
| 415 |
+
# If no plot, return the original result
|
| 416 |
+
return result_json
|
| 417 |
|
| 418 |
def execute_javascript_code(code: str, session_id: str = None) -> str:
|
| 419 |
"""
|