Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -145,25 +145,36 @@ class SuperSmartAgent:
|
|
| 145 |
return state
|
| 146 |
|
| 147 |
def execute_python_code(self, state):
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
try:
|
| 151 |
-
with open(file_path, "r"
|
| 152 |
code = f.read()
|
| 153 |
-
code = textwrap.dedent(code).strip()
|
| 154 |
except Exception as e:
|
| 155 |
-
state["response"] = f"Error
|
| 156 |
return state
|
| 157 |
else:
|
| 158 |
-
#
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
try:
|
| 163 |
-
result = code_interpreter(code)
|
| 164 |
-
state["response"] =
|
| 165 |
except Exception as e:
|
| 166 |
-
state["response"] = f"Error executing Python code: {
|
| 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 |
|