Spaces:
Runtime error
Runtime error
Terry Zhuo
commited on
Commit
·
158d594
1
Parent(s):
380859d
update
Browse files
app.py
CHANGED
|
@@ -26,35 +26,28 @@ HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
|
| 26 |
API = HfApi(token=HF_TOKEN)
|
| 27 |
Result = Tuple[str, List[bool]]
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
# Create string buffers to capture output
|
| 32 |
stdout_buffer = io.StringIO()
|
| 33 |
stderr_buffer = io.StringIO()
|
| 34 |
-
|
| 35 |
-
# Create a dictionary for local variables
|
| 36 |
-
local_dict = {}
|
| 37 |
-
|
| 38 |
# Capture both stdout and stderr
|
| 39 |
with redirect_stdout(stdout_buffer), redirect_stderr(stderr_buffer):
|
| 40 |
try:
|
| 41 |
# Execute the code
|
| 42 |
-
exec(
|
| 43 |
|
| 44 |
# Get the output
|
| 45 |
output = stdout_buffer.getvalue()
|
| 46 |
errors = stderr_buffer.getvalue()
|
| 47 |
|
| 48 |
-
# If there's a return value in the last expression, capture it
|
| 49 |
-
last_line = code.strip().split('\n')[-1]
|
| 50 |
-
if not (last_line.startswith('print') or last_line.strip() == ''):
|
| 51 |
-
try:
|
| 52 |
-
result = eval(last_line, globals(), local_dict)
|
| 53 |
-
if result is not None:
|
| 54 |
-
output += f"\n>>> {result}"
|
| 55 |
-
except:
|
| 56 |
-
pass
|
| 57 |
-
|
| 58 |
# Combine stdout and stderr
|
| 59 |
result = output
|
| 60 |
if errors:
|
|
@@ -64,13 +57,14 @@ def run_code(code: str) -> str:
|
|
| 64 |
# Capture any execution errors
|
| 65 |
result = f"Error: {str(e)}"
|
| 66 |
|
| 67 |
-
return result
|
| 68 |
|
| 69 |
# Create the Gradio interface with better styling
|
| 70 |
interface = gr.Interface(
|
| 71 |
fn=run_code,
|
| 72 |
inputs=[
|
| 73 |
gr.Code(label="Python Code", language="python"),
|
|
|
|
| 74 |
],
|
| 75 |
outputs=[
|
| 76 |
gr.Textbox(label="Output")
|
|
|
|
| 26 |
API = HfApi(token=HF_TOKEN)
|
| 27 |
Result = Tuple[str, List[bool]]
|
| 28 |
|
| 29 |
+
dataset = load_dataset("bigcode/bigcodebench-tool")
|
| 30 |
+
tasks = {
|
| 31 |
+
_id: task["mixed_tool_implementation"]
|
| 32 |
+
for _id, task in dataset.items()
|
| 33 |
+
}
|
| 34 |
|
| 35 |
+
|
| 36 |
+
def run_code(code: str, _id: str) -> str:
|
| 37 |
# Create string buffers to capture output
|
| 38 |
stdout_buffer = io.StringIO()
|
| 39 |
stderr_buffer = io.StringIO()
|
| 40 |
+
pre_code = tasks[_id]
|
|
|
|
|
|
|
|
|
|
| 41 |
# Capture both stdout and stderr
|
| 42 |
with redirect_stdout(stdout_buffer), redirect_stderr(stderr_buffer):
|
| 43 |
try:
|
| 44 |
# Execute the code
|
| 45 |
+
exec(pre_code + code)
|
| 46 |
|
| 47 |
# Get the output
|
| 48 |
output = stdout_buffer.getvalue()
|
| 49 |
errors = stderr_buffer.getvalue()
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# Combine stdout and stderr
|
| 52 |
result = output
|
| 53 |
if errors:
|
|
|
|
| 57 |
# Capture any execution errors
|
| 58 |
result = f"Error: {str(e)}"
|
| 59 |
|
| 60 |
+
return result
|
| 61 |
|
| 62 |
# Create the Gradio interface with better styling
|
| 63 |
interface = gr.Interface(
|
| 64 |
fn=run_code,
|
| 65 |
inputs=[
|
| 66 |
gr.Code(label="Python Code", language="python"),
|
| 67 |
+
gr.Dropdown(label="Task", choices=list(tasks.keys())),
|
| 68 |
],
|
| 69 |
outputs=[
|
| 70 |
gr.Textbox(label="Output")
|