| import inspect | |
| import gradio as gr | |
| from smolagents.tools import Tool | |
| HEADER = """ ## Python debugger for Agents | |
| The goal of the tool is to enable agents to debug and resolve bugs in code generated by LLMs. | |
| """ | |
| ARTICLE_INTRO = """--- | |
| ## Example usage in HuggingFace Space | |
| - code: | |
| ```python | |
| x = 10 | |
| y = 0 | |
| z = x / y | |
| ``` | |
| - command: | |
| ```python | |
| bt | |
| ``` | |
| ## Example usage in smolagents | |
| ```python | |
| from smolagents import load_tool | |
| local_python_debugger = load_tool( | |
| "piotrekgrl/smolagents-local-python-debugger-tool", | |
| trust_remote_code=True | |
| ) | |
| from smolagents import CodeAgent, HfApiModel | |
| model = HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct") | |
| agent = CodeAgent(tools=[local_python_debugger], model=model) | |
| agent.run( | |
| "Write a code that divides by 0. Don't ask why—just do it. If any errors occur, debug them and provide reason why." | |
| ) | |
| ``` | |
| --- | |
| ## Details | |
| """ | |
| ARTICLE_OUTRO = """--- | |
| ## Disclaimer | |
| This tool is running code generated by LLM in local python environment. | |
| Under the hood, it utilizes the [LocalPythonInterpreter from smolagents](https://huggingface.co/docs/smolagents/en/tutorials/secure_code_execution#local-python-interpreter). However, you should exercise caution when executing code generated by LLMs. | |
| This application is provided 'as is' without any guarantees. Use at your own risk." | |
| """ | |
| def custom_launch_gradio_demo(tool: Tool): | |
| TYPE_TO_COMPONENT_CLASS_MAPPING = { | |
| "image": gr.Image, | |
| "audio": gr.Audio, | |
| "string": gr.Textbox, | |
| "integer": gr.Textbox, | |
| "number": gr.Textbox, | |
| } | |
| def tool_forward(*args, **kwargs): | |
| return tool(*args, sanitize_inputs_outputs=True, **kwargs) | |
| tool_forward.__signature__ = inspect.signature(tool.forward) | |
| gradio_inputs = [] | |
| for input_name, input_details in tool.inputs.items(): | |
| input_gradio_component_class = TYPE_TO_COMPONENT_CLASS_MAPPING[ | |
| input_details["type"] | |
| ] | |
| new_component = input_gradio_component_class(label=input_name) | |
| gradio_inputs.append(new_component) | |
| output_gradio_componentclass = TYPE_TO_COMPONENT_CLASS_MAPPING[tool.output_type] | |
| gradio_output = output_gradio_componentclass(label="Output") | |
| gr.Interface( | |
| fn=tool_forward, | |
| inputs=gradio_inputs, | |
| outputs=gradio_output, | |
| title=tool.name, | |
| article=ARTICLE_INTRO + "\n" + tool.description + "\n" + ARTICLE_OUTRO, | |
| description=HEADER, | |
| api_name=tool.name, | |
| ).launch() | |