Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,24 @@ from langgraph.graph import StateGraph, END
|
|
| 8 |
from typing import TypedDict
|
| 9 |
import string
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -124,28 +142,29 @@ class SuperSmartAgent:
|
|
| 124 |
|
| 125 |
def execute_python_code(self, state):
|
| 126 |
question = state["question"]
|
| 127 |
-
|
| 128 |
-
#
|
| 129 |
-
if "
|
| 130 |
-
# Placeholder for actual file handling and execution logic
|
| 131 |
-
# In a real implementation, you would read the attached file and execute it
|
| 132 |
-
python_code = """
|
| 133 |
-
# Example Python code
|
| 134 |
-
def calculate():
|
| 135 |
-
return 42
|
| 136 |
-
|
| 137 |
-
result = calculate()
|
| 138 |
-
print(result)
|
| 139 |
-
"""
|
| 140 |
try:
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
except Exception as e:
|
| 145 |
-
state["response"] = f"Error executing Python code: {str(e)}"
|
| 146 |
else:
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
return state
|
| 150 |
|
| 151 |
|
|
@@ -153,6 +172,7 @@ class SuperSmartAgent:
|
|
| 153 |
|
| 154 |
|
| 155 |
|
|
|
|
| 156 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 157 |
"""
|
| 158 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 8 |
from typing import TypedDict
|
| 9 |
import string
|
| 10 |
|
| 11 |
+
import sys
|
| 12 |
+
import io
|
| 13 |
+
import contextlib
|
| 14 |
+
|
| 15 |
+
def code_interpreter(code: str) -> str:
|
| 16 |
+
"""
|
| 17 |
+
Executes the given Python code string and captures its final printed output.
|
| 18 |
+
"""
|
| 19 |
+
buffer = io.StringIO()
|
| 20 |
+
try:
|
| 21 |
+
# Redirect stdout to buffer
|
| 22 |
+
with contextlib.redirect_stdout(buffer):
|
| 23 |
+
exec(code, {})
|
| 24 |
+
return buffer.getvalue().strip()
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Execution Error: {e}"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
|
| 30 |
# --- Constants ---
|
| 31 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 142 |
|
| 143 |
def execute_python_code(self, state):
|
| 144 |
question = state["question"]
|
| 145 |
+
|
| 146 |
+
# Extract code from the question (optional enhancement)
|
| 147 |
+
if "```python" in question:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
try:
|
| 149 |
+
code = question.split("```python")[1].split("```")[0].strip()
|
| 150 |
+
except IndexError:
|
| 151 |
+
code = ""
|
|
|
|
|
|
|
| 152 |
else:
|
| 153 |
+
# Fallback code
|
| 154 |
+
code = """
|
| 155 |
+
def calculate():
|
| 156 |
+
return 42
|
| 157 |
+
|
| 158 |
+
result = calculate()
|
| 159 |
+
print(result)
|
| 160 |
+
"""
|
| 161 |
+
|
| 162 |
+
try:
|
| 163 |
+
result = code_interpreter(code)
|
| 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 |
|
| 170 |
|
|
|
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
+
|
| 176 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 177 |
"""
|
| 178 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|