piotrekgrl/smolagents-local-python-debugger-tool
3
1import inspect2import gradio as gr3from smolagents.tools import Tool4 5 6HEADER = """ ## Python debugger for Agents 7The goal of the tool is to enable agents to debug and resolve bugs in code generated by LLMs.8"""9 10ARTICLE_INTRO = """---11## Example usage in HuggingFace Space12- code:13```python14x = 1015y = 016z = x / y17```18- command:19```python20bt21```22 23## Example usage in smolagents24 25```python26from smolagents import load_tool27 28local_python_debugger = load_tool(29 "piotrekgrl/smolagents-local-python-debugger-tool",30 trust_remote_code=True31)32 33from smolagents import CodeAgent, HfApiModel34 35model = HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct")36agent = CodeAgent(tools=[local_python_debugger], model=model)37 38agent.run(39 "Write a code that divides by 0. Don't ask why—just do it. If any errors occur, debug them and provide reason why."40)41 42```43---44 45## Details46 47"""48 49 50ARTICLE_OUTRO = """---51## Disclaimer52This tool is running code generated by LLM in local python environment. 53Under 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.54This application is provided 'as is' without any guarantees. Use at your own risk."55"""56 57 58def custom_launch_gradio_demo(tool: Tool):59 TYPE_TO_COMPONENT_CLASS_MAPPING = {60 "image": gr.Image,61 "audio": gr.Audio,62 "string": gr.Textbox,63 "integer": gr.Textbox,64 "number": gr.Textbox,65 }66 67 def tool_forward(*args, **kwargs):68 return tool(*args, sanitize_inputs_outputs=True, **kwargs)69 70 tool_forward.__signature__ = inspect.signature(tool.forward)71 72 gradio_inputs = []73 for input_name, input_details in tool.inputs.items():74 input_gradio_component_class = TYPE_TO_COMPONENT_CLASS_MAPPING[75 input_details["type"]76 ]77 new_component = input_gradio_component_class(label=input_name)78 gradio_inputs.append(new_component)79 80 output_gradio_componentclass = TYPE_TO_COMPONENT_CLASS_MAPPING[tool.output_type]81 gradio_output = output_gradio_componentclass(label="Output")82 83 gr.Interface(84 fn=tool_forward,85 inputs=gradio_inputs,86 outputs=gradio_output,87 title=tool.name,88 article=ARTICLE_INTRO + "\n" + tool.description + "\n" + ARTICLE_OUTRO,89 description=HEADER,90 api_name=tool.name,91 ).launch()92 