Spaces:
Running
Running
owenkaplinsky
commited on
Commit
·
ae5e83b
1
Parent(s):
938d259
Add cast_as block; fix bugs
Browse files- project/chat.py +10 -5
- project/src/blocks/text.js +25 -0
- project/src/generators/python.js +19 -0
- project/src/index.js +1 -1
- project/src/toolbox.js +4 -0
- project/test.py +5 -1
- requirements.txt +2 -1
project/chat.py
CHANGED
|
@@ -696,6 +696,8 @@ def create_gradio_interface():
|
|
| 696 |
|
| 697 |
Deployment is done only once; the deployed MCP may be run any number of times afterward.
|
| 698 |
|
|
|
|
|
|
|
| 699 |
---
|
| 700 |
|
| 701 |
## VALUE BLOCK CONSTRUCTION: ABSOLUTE RULE
|
|
@@ -1014,13 +1016,8 @@ def create_gradio_interface():
|
|
| 1014 |
"require_approval": "never"
|
| 1015 |
}
|
| 1016 |
dynamic_tools.append(mcp_tool)
|
| 1017 |
-
print(f"[MCP] Injected MCP tool for server: {current_mcp_server_url}")
|
| 1018 |
-
else:
|
| 1019 |
-
print(f"[MCP] Skipping MCP tool injection - space not running yet")
|
| 1020 |
except Exception as mcp_error:
|
| 1021 |
print(f"[MCP ERROR] Failed during MCP injection: {mcp_error}")
|
| 1022 |
-
print(f"[MCP] Continuing without MCP tools...")
|
| 1023 |
-
# Continue without MCP - don't crash
|
| 1024 |
|
| 1025 |
# Add deployment status message to instructions if deployment just happened and space is not running
|
| 1026 |
deployment_instructions = instructions
|
|
@@ -1084,6 +1081,14 @@ def create_gradio_interface():
|
|
| 1084 |
tool_result = None
|
| 1085 |
result_label = ""
|
| 1086 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1087 |
if function_name == "delete_block":
|
| 1088 |
block_id = function_args.get("id", "")
|
| 1089 |
print(Fore.YELLOW + f"Agent deleted block with ID `{block_id}`." + Style.RESET_ALL)
|
|
|
|
| 696 |
|
| 697 |
Deployment is done only once; the deployed MCP may be run any number of times afterward.
|
| 698 |
|
| 699 |
+
NEVER TRY DEPLOYING MORE THAN ONCE. YOU ONLY EVER NEED TO DEPLOY ONCE. AFTER THAT YOU CAN ONLY RUN IT.
|
| 700 |
+
|
| 701 |
---
|
| 702 |
|
| 703 |
## VALUE BLOCK CONSTRUCTION: ABSOLUTE RULE
|
|
|
|
| 1016 |
"require_approval": "never"
|
| 1017 |
}
|
| 1018 |
dynamic_tools.append(mcp_tool)
|
|
|
|
|
|
|
|
|
|
| 1019 |
except Exception as mcp_error:
|
| 1020 |
print(f"[MCP ERROR] Failed during MCP injection: {mcp_error}")
|
|
|
|
|
|
|
| 1021 |
|
| 1022 |
# Add deployment status message to instructions if deployment just happened and space is not running
|
| 1023 |
deployment_instructions = instructions
|
|
|
|
| 1081 |
tool_result = None
|
| 1082 |
result_label = ""
|
| 1083 |
|
| 1084 |
+
# Log MCP tool calls
|
| 1085 |
+
if function_name not in ("delete_block", "create_block", "create_variable", "edit_mcp", "deploy_to_huggingface"):
|
| 1086 |
+
# This is an MCP tool call
|
| 1087 |
+
print(Fore.GREEN + f"[MCP TOOL CALL] Running MCP with inputs:" + Style.RESET_ALL)
|
| 1088 |
+
print(Fore.GREEN + f" Tool name: {function_name}" + Style.RESET_ALL)
|
| 1089 |
+
for key, value in function_args.items():
|
| 1090 |
+
print(Fore.GREEN + f" {key}: {value}" + Style.RESET_ALL)
|
| 1091 |
+
|
| 1092 |
if function_name == "delete_block":
|
| 1093 |
block_id = function_args.get("id", "")
|
| 1094 |
print(Fore.YELLOW + f"Agent deleted block with ID `{block_id}`." + Style.RESET_ALL)
|
project/src/blocks/text.js
CHANGED
|
@@ -653,6 +653,30 @@ const lists_contains = {
|
|
| 653 |
helpUrl: "",
|
| 654 |
};
|
| 655 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
// Dynamic function call block
|
| 657 |
const func_call = {
|
| 658 |
type: "func_call",
|
|
@@ -1157,4 +1181,5 @@ export const blocks = Blockly.common.createBlockDefinitionsFromJsonArray([
|
|
| 1157 |
call_api,
|
| 1158 |
in_json,
|
| 1159 |
lists_contains,
|
|
|
|
| 1160 |
]);
|
|
|
|
| 653 |
helpUrl: "",
|
| 654 |
};
|
| 655 |
|
| 656 |
+
// Cast block for type conversion
|
| 657 |
+
const cast_as = {
|
| 658 |
+
type: "cast_as",
|
| 659 |
+
message0: "cast %1 as %2",
|
| 660 |
+
args0: [
|
| 661 |
+
{ type: "input_value", name: "VALUE", check: null },
|
| 662 |
+
{
|
| 663 |
+
type: "field_dropdown",
|
| 664 |
+
name: "TYPE",
|
| 665 |
+
options: [
|
| 666 |
+
["int", "int"],
|
| 667 |
+
["float", "float"],
|
| 668 |
+
["str", "str"],
|
| 669 |
+
["bool", "bool"],
|
| 670 |
+
],
|
| 671 |
+
},
|
| 672 |
+
],
|
| 673 |
+
output: null,
|
| 674 |
+
colour: 210,
|
| 675 |
+
inputsInline: true,
|
| 676 |
+
tooltip: "Convert a value to a different type",
|
| 677 |
+
helpUrl: "",
|
| 678 |
+
};
|
| 679 |
+
|
| 680 |
// Dynamic function call block
|
| 681 |
const func_call = {
|
| 682 |
type: "func_call",
|
|
|
|
| 1181 |
call_api,
|
| 1182 |
in_json,
|
| 1183 |
lists_contains,
|
| 1184 |
+
cast_as,
|
| 1185 |
]);
|
project/src/generators/python.js
CHANGED
|
@@ -17,6 +17,7 @@ forBlock['create_mcp'] = function (block, generator) {
|
|
| 17 |
}
|
| 18 |
|
| 19 |
const typedInputs = [];
|
|
|
|
| 20 |
let i = 0;
|
| 21 |
|
| 22 |
// Build list of typed input parameters with inferred Python types
|
|
@@ -39,6 +40,7 @@ forBlock['create_mcp'] = function (block, generator) {
|
|
| 39 |
break;
|
| 40 |
case 'list':
|
| 41 |
pyType = 'list';
|
|
|
|
| 42 |
break;
|
| 43 |
case 'boolean':
|
| 44 |
pyType = 'bool';
|
|
@@ -53,6 +55,14 @@ forBlock['create_mcp'] = function (block, generator) {
|
|
| 53 |
|
| 54 |
// Main function body and return value(s)
|
| 55 |
let body = generator.statementToCode(block, 'BODY');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
let returnStatement = '';
|
| 57 |
|
| 58 |
const returnValues = [];
|
|
@@ -381,4 +391,13 @@ forBlock['lists_contains'] = function (block, generator) {
|
|
| 381 |
// Generate code to check if item is in list
|
| 382 |
const code = `${item} in ${list}`;
|
| 383 |
return [code, Order.ATOMIC];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
};
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
const typedInputs = [];
|
| 20 |
+
const listParams = [];
|
| 21 |
let i = 0;
|
| 22 |
|
| 23 |
// Build list of typed input parameters with inferred Python types
|
|
|
|
| 40 |
break;
|
| 41 |
case 'list':
|
| 42 |
pyType = 'list';
|
| 43 |
+
listParams.push(paramName);
|
| 44 |
break;
|
| 45 |
case 'boolean':
|
| 46 |
pyType = 'bool';
|
|
|
|
| 55 |
|
| 56 |
// Main function body and return value(s)
|
| 57 |
let body = generator.statementToCode(block, 'BODY');
|
| 58 |
+
|
| 59 |
+
// Add list conversion code at the beginning of the body
|
| 60 |
+
let listConversionCode = '';
|
| 61 |
+
for (const param of listParams) {
|
| 62 |
+
listConversionCode += ` ${param} = ${param}.iloc[:, 0].tolist()\n`;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
body = listConversionCode + body;
|
| 66 |
let returnStatement = '';
|
| 67 |
|
| 68 |
const returnValues = [];
|
|
|
|
| 391 |
// Generate code to check if item is in list
|
| 392 |
const code = `${item} in ${list}`;
|
| 393 |
return [code, Order.ATOMIC];
|
| 394 |
+
};
|
| 395 |
+
|
| 396 |
+
forBlock['cast_as'] = function (block, generator) {
|
| 397 |
+
const value = generator.valueToCode(block, 'VALUE', Order.NONE) || "''";
|
| 398 |
+
const type = block.getFieldValue('TYPE');
|
| 399 |
+
|
| 400 |
+
// Generate code to cast value to the specified type
|
| 401 |
+
const code = `${type}(${value})`;
|
| 402 |
+
return [code, Order.FUNCTION_CALL];
|
| 403 |
};
|
project/src/index.js
CHANGED
|
@@ -1284,7 +1284,7 @@ const updateCode = () => {
|
|
| 1284 |
code = "from sympy import isprime\n\n" + code;
|
| 1285 |
}
|
| 1286 |
|
| 1287 |
-
code = "import gradio as gr" + code;
|
| 1288 |
|
| 1289 |
// Extract input and output counts from the create_mcp block to build Gradio interface
|
| 1290 |
const mcpBlocks = ws.getBlocksByType('create_mcp');
|
|
|
|
| 1284 |
code = "from sympy import isprime\n\n" + code;
|
| 1285 |
}
|
| 1286 |
|
| 1287 |
+
code = "import gradio as gr\n" + code;
|
| 1288 |
|
| 1289 |
// Extract input and output counts from the create_mcp block to build Gradio interface
|
| 1290 |
const mcpBlocks = ws.getBlocksByType('create_mcp');
|
project/src/toolbox.js
CHANGED
|
@@ -106,6 +106,10 @@ export const toolbox = {
|
|
| 106 |
kind: 'block',
|
| 107 |
type: 'logic_null',
|
| 108 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
],
|
| 110 |
},
|
| 111 |
{
|
|
|
|
| 106 |
kind: 'block',
|
| 107 |
type: 'logic_null',
|
| 108 |
},
|
| 109 |
+
{
|
| 110 |
+
kind: 'block',
|
| 111 |
+
type: 'cast_as',
|
| 112 |
+
},
|
| 113 |
],
|
| 114 |
},
|
| 115 |
{
|
project/test.py
CHANGED
|
@@ -3,6 +3,8 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
| 5 |
import ast
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
@@ -136,7 +138,6 @@ def execute_blockly_logic(user_inputs):
|
|
| 136 |
exec("import os", env)
|
| 137 |
exec(code_to_run, env)
|
| 138 |
if "create_mcp" in env:
|
| 139 |
-
import inspect
|
| 140 |
sig = inspect.signature(env["create_mcp"])
|
| 141 |
params = list(sig.parameters.values())
|
| 142 |
|
|
@@ -190,6 +191,9 @@ def execute_blockly_logic(user_inputs):
|
|
| 190 |
# If conversion fails, pass the raw input
|
| 191 |
typed_args.append(arg)
|
| 192 |
|
|
|
|
|
|
|
|
|
|
| 193 |
result = env["create_mcp"](*typed_args)
|
| 194 |
elif "process_input" in env:
|
| 195 |
env["process_input"](user_inputs)
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
| 5 |
import ast
|
| 6 |
+
import inspect
|
| 7 |
+
import pandas as pd
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
| 138 |
exec("import os", env)
|
| 139 |
exec(code_to_run, env)
|
| 140 |
if "create_mcp" in env:
|
|
|
|
| 141 |
sig = inspect.signature(env["create_mcp"])
|
| 142 |
params = list(sig.parameters.values())
|
| 143 |
|
|
|
|
| 191 |
# If conversion fails, pass the raw input
|
| 192 |
typed_args.append(arg)
|
| 193 |
|
| 194 |
+
if len(typed_args) > 0 and isinstance(typed_args[0], list):
|
| 195 |
+
typed_args[0] = pd.DataFrame(typed_args[0])
|
| 196 |
+
|
| 197 |
result = env["create_mcp"](*typed_args)
|
| 198 |
elif "process_input" in env:
|
| 199 |
env["process_input"](user_inputs)
|
requirements.txt
CHANGED
|
@@ -5,4 +5,5 @@ requests
|
|
| 5 |
colorama
|
| 6 |
huggingface_hub
|
| 7 |
gradio_client
|
| 8 |
-
mcp
|
|
|
|
|
|
| 5 |
colorama
|
| 6 |
huggingface_hub
|
| 7 |
gradio_client
|
| 8 |
+
mcp
|
| 9 |
+
pandas
|