selim-ba commited on
Commit
c7ba91d
·
verified ·
1 Parent(s): 412f41f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -145,25 +145,36 @@ class SuperSmartAgent:
145
  return state
146
 
147
  def execute_python_code(self, state):
148
- file_path = state.get("file_path")
149
- if file_path and os.path.isfile(file_path):
 
 
 
 
 
 
 
 
150
  try:
151
- with open(file_path, "r", encoding="utf-8") as f:
152
  code = f.read()
153
- code = textwrap.dedent(code).strip()
154
  except Exception as e:
155
- state["response"] = f"Error reading file: {str(e)}"
156
  return state
157
  else:
158
- # Fallback or error if no file is attached
159
- state["response"] = "No valid Python file provided for execution."
160
- return state
 
 
 
 
161
 
162
  try:
163
- result = code_interpreter(code) # Your interpreter logic here
164
- state["response"] = f"Python execution result:\n{result}"
165
  except Exception as e:
166
- state["response"] = f"Error executing Python code: {str(e)}"
167
 
168
  return state
169
 
 
145
  return state
146
 
147
  def execute_python_code(self, state):
148
+ question = state["question"]
149
+ file_name = state.get("file_name")
150
+
151
+ # Case 1: Embedded code block
152
+ code_blocks = re.findall(r"```(?:python)?(.*?)```", question, re.DOTALL)
153
+ if code_blocks:
154
+ code = textwrap.dedent(code_blocks[0]).strip()
155
+ elif file_name and file_name.endswith(".py"):
156
+ # Case 2: Load code from attached Python file
157
+ file_path = os.path.join("data", file_name) # Make sure this path matches the actual mount
158
  try:
159
+ with open(file_path, "r") as f:
160
  code = f.read()
 
161
  except Exception as e:
162
+ state["response"] = f"Error loading Python file: {e}"
163
  return state
164
  else:
165
+ # Default fallback
166
+ code = """
167
+ def calculate():
168
+ return 5
169
+ result = calculate()
170
+ print(result)
171
+ """
172
 
173
  try:
174
+ result = code_interpreter(code)
175
+ state["response"] = str(result)
176
  except Exception as e:
177
+ state["response"] = f"Error executing Python code: {e}"
178
 
179
  return state
180