File size: 2,474 Bytes
4438281
 
 
 
 
 
3accd89
4438281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe8007f
 
 
 
 
 
 
 
4438281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe8007f
4438281
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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()