| from __future__ import annotations |
|
|
| from typing import Annotated |
|
|
| import gradio as gr |
| from ._docstrings import autodoc |
| from ._core import sandboxed_exec |
|
|
| from app import _log_call_end, _log_call_start, _truncate_for_log |
|
|
|
|
| |
| TOOL_SUMMARY = ( |
| "Execute Python code from the tool root; returns captured stdout or the exception text." |
| ) |
|
|
|
|
| @autodoc( |
| summary=TOOL_SUMMARY, |
| ) |
| def Code_Interpreter(code: Annotated[str, "Python source code to run; stdout is captured and returned."]) -> str: |
| _log_call_start("Code_Interpreter", code=_truncate_for_log(code or "", 300)) |
| result = sandboxed_exec(code, ast_mode=False) |
| _log_call_end("Code_Interpreter", _truncate_for_log(result)) |
| return result |
|
|
|
|
| def build_interface() -> gr.Interface: |
| return gr.Interface( |
| fn=Code_Interpreter, |
| inputs=gr.Code(label="Python Code", language="python"), |
| outputs=gr.Textbox(label="Output", lines=5, max_lines=20), |
| title="Code Interpreter", |
| description="<div style=\"text-align:center\">Execute Python code and see the output.</div>", |
| api_description=TOOL_SUMMARY, |
| flagging_mode="never", |
| ) |
|
|
|
|
| __all__ = ["Code_Interpreter", "build_interface"] |