DrishtiSharma commited on
Commit
b954d1b
·
verified ·
1 Parent(s): 693beb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -18
app.py CHANGED
@@ -48,9 +48,14 @@ 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
- # 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,9 +67,11 @@ def python_repl(code: Annotated[str, "The python code to execute to generate you
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,21 +91,27 @@ def tool_node(state):
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):
@@ -192,4 +205,4 @@ if st.button("Run Workflow"):
192
  # Display any text content
193
  st.write(message["content"])
194
  except Exception as e:
195
- st.error(f"An error occurred: {e}")
 
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
+ # Log the executed code for debugging
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
  plt.clf()
68
  plt.close()
69
 
70
+ # Encode the image as base64
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
  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
+ if response.get("status") == "success" and "image" in response:
96
+ return {
97
+ "messages": [
98
+ {
99
+ "role": "assistant",
100
+ "content": "Image generated successfully.",
101
+ "image": response["image"],
102
+ }
103
+ ]
104
+ }
105
+ else:
106
+ # Handle errors more informatively
107
+ return {
108
+ "messages": [
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):
 
205
  # Display any text content
206
  st.write(message["content"])
207
  except Exception as e:
208
+ st.error(f"An error occurred: {e}")