Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -48,14 +48,9 @@ repl = PythonREPL()
|
|
48 |
def python_repl(code: Annotated[str, "The python code to execute to generate your chart."]):
|
49 |
"""Executes Python code to generate a chart and returns the chart as a base64-encoded image."""
|
50 |
try:
|
51 |
-
#
|
52 |
-
st.write("Executing Code:", code)
|
53 |
-
|
54 |
-
# Prepare execution environment
|
55 |
exec_globals = {"plt": plt}
|
56 |
exec_locals = {}
|
57 |
-
|
58 |
-
# Execute the provided code
|
59 |
exec(code, exec_globals, exec_locals)
|
60 |
|
61 |
# Save the generated plot to a buffer
|
@@ -67,11 +62,9 @@ def python_repl(code: Annotated[str, "The python code to execute to generate you
|
|
67 |
plt.clf()
|
68 |
plt.close()
|
69 |
|
70 |
-
# Encode
|
71 |
encoded_image = base64.b64encode(buf.getvalue()).decode("utf-8")
|
72 |
return {"status": "success", "image": encoded_image}
|
73 |
-
except FileNotFoundError as e:
|
74 |
-
return {"status": "failed", "error": f"FileNotFoundError: {e}"}
|
75 |
except Exception as e:
|
76 |
return {"status": "failed", "error": repr(e)}
|
77 |
|
@@ -91,27 +84,21 @@ def tool_node(state):
|
|
91 |
tool_name = last_message.additional_kwargs["function_call"]["name"]
|
92 |
action = ToolInvocation(tool=tool_name, tool_input=tool_input)
|
93 |
response = tool_executor.invoke(action)
|
94 |
-
if isinstance(response, dict):
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
{
|
110 |
-
"role": "assistant",
|
111 |
-
"content": f"Error in {tool_name}: {response.get('error', 'Unknown error')}"
|
112 |
-
}
|
113 |
-
]
|
114 |
-
}
|
115 |
|
116 |
# Define router
|
117 |
def router(state):
|
|
|
48 |
def python_repl(code: Annotated[str, "The python code to execute to generate your chart."]):
|
49 |
"""Executes Python code to generate a chart and returns the chart as a base64-encoded image."""
|
50 |
try:
|
51 |
+
# Execute the code
|
|
|
|
|
|
|
52 |
exec_globals = {"plt": plt}
|
53 |
exec_locals = {}
|
|
|
|
|
54 |
exec(code, exec_globals, exec_locals)
|
55 |
|
56 |
# Save the generated plot to a buffer
|
|
|
62 |
plt.clf()
|
63 |
plt.close()
|
64 |
|
65 |
+
# Encode image as base64
|
66 |
encoded_image = base64.b64encode(buf.getvalue()).decode("utf-8")
|
67 |
return {"status": "success", "image": encoded_image}
|
|
|
|
|
68 |
except Exception as e:
|
69 |
return {"status": "failed", "error": repr(e)}
|
70 |
|
|
|
84 |
tool_name = last_message.additional_kwargs["function_call"]["name"]
|
85 |
action = ToolInvocation(tool=tool_name, tool_input=tool_input)
|
86 |
response = tool_executor.invoke(action)
|
87 |
+
if isinstance(response, dict) and response.get("status") == "success" and "image" in response:
|
88 |
+
return {
|
89 |
+
"messages": [
|
90 |
+
{
|
91 |
+
"role": "assistant",
|
92 |
+
"content": "Image generated successfully.",
|
93 |
+
"image": response["image"],
|
94 |
+
}
|
95 |
+
]
|
96 |
+
}
|
97 |
+
else:
|
98 |
+
function_message = FunctionMessage(
|
99 |
+
content=f"{tool_name} response: {str(response)}", name=action.tool
|
100 |
+
)
|
101 |
+
return {"messages": [function_message]}
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
# Define router
|
104 |
def router(state):
|