File size: 11,497 Bytes
bf56316 43e9024 136da4e bf56316 172738c 43e9024 bf56316 43e9024 |
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
---
title: Gradio Agent Inspector
emoji: 🕵️
colorFrom: blue
colorTo: yellow
sdk: gradio
sdk_version: 5.33.0
app_file: app.py
pinned: false
license: apache-2.0
short_description: Gradio component to help debug your agent
tags:
- gradio-custom-component
- custom-component-track
- adk
- agent
---
# `gradio_agent_inspector`
<a href="https://pypi.org/project/gradio_agent_inspector/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_agent_inspector"></a>
Agent Inspector for ADK
## Installation
```bash
pip install gradio_agent_inspector
```
## Usage
```python
import json
from pathlib import Path
import gradio as gr
from gradio_agent_inspector import AgentInspector
import os
def simulate_conversation():
dir_path = Path(os.path.dirname(os.path.realpath(__file__)))
initial_state = {
"state": {},
"events": [],
}
states = []
for i in range(2):
session_value_p = dir_path / "session-sample" / f"value-{i}.json"
with session_value_p.open("r", encoding="utf-8") as f:
session_value = json.load(f)
# attach event trace and graph info to the event
for e in session_value["events"]:
event_trace_p = dir_path / "event-trace" / f"trace-{e['id']}.json"
if event_trace_p.exists():
with event_trace_p.open("r", encoding="utf-8") as trace_f:
event_trace = json.load(trace_f)
if "gcp.vertex.agent.llm_request" in event_trace:
event_trace["gcp.vertex.agent.llm_request"] = json.loads(
event_trace["gcp.vertex.agent.llm_request"]
)
if "gcp.vertex.agent.llm_response" in event_trace:
event_trace["gcp.vertex.agent.llm_response"] = json.loads(
event_trace["gcp.vertex.agent.llm_response"]
)
e["trace"] = event_trace
event_graph_p = dir_path / "event-trace" / f"graph-{e['id']}.json"
if event_graph_p.exists():
with event_graph_p.open("r", encoding="utf-8") as graph_f:
event_graph = json.load(graph_f)
e["graph"] = event_graph
states.append(session_value)
return initial_state, states
def update_conversation_state(state_index, states):
if (state_index + 1) >= len(states):
return states[state_index], state_index
else:
new_index = state_index + 1
return states[new_index], new_index
initial_state, conversation_states = simulate_conversation()
with gr.Blocks() as demo:
gr.Markdown("# Agent Inspector")
state_counter = gr.State(-1)
agent_inspector = AgentInspector(json.dumps(initial_state))
with gr.Row():
next_btn = gr.Button(
f"▶️ Next ({0} / {len(conversation_states)})", variant="primary"
)
reset_btn = gr.Button("🔄 Reset", variant="secondary")
def next_state(current_counter):
new_state, new_counter = update_conversation_state(
current_counter, conversation_states
)
json_state = json.dumps(new_state)
next_button_label = f"▶️ Next ({new_counter+1} / {len(conversation_states)})"
return json_state, new_counter, next_button_label
def reset_conversation():
json_state = json.dumps(initial_state)
next_button_label = f"▶️ Next ({0} / {len(conversation_states)})"
return json_state, -1, next_button_label
next_btn.click(
next_state,
inputs=[state_counter],
outputs=[agent_inspector, state_counter, next_btn],
)
reset_btn.click(
reset_conversation, outputs=[agent_inspector, state_counter, next_btn]
)
# examples = gr.Examples(
# examples=[
# s for s in conversation_states
# ],
# inputs=[initial_state],
# )
if __name__ == "__main__":
demo.launch()
```
## `AgentInspector`
### Initialization
<table>
<thead>
<tr>
<th align="left">name</th>
<th align="left" style="width: 25%;">type</th>
<th align="left">default</th>
<th align="left">description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>value</code></td>
<td align="left" style="width: 25%;">
```python
str | Callable | None
```
</td>
<td align="left"><code>None</code></td>
<td align="left">default text to provide in textbox. If a function is provided, the function will be called each time the app loads to set the initial value of this component.</td>
</tr>
<tr>
<td align="left"><code>placeholder</code></td>
<td align="left" style="width: 25%;">
```python
str | None
```
</td>
<td align="left"><code>None</code></td>
<td align="left">placeholder hint to provide behind textbox.</td>
</tr>
<tr>
<td align="left"><code>label</code></td>
<td align="left" style="width: 25%;">
```python
str | I18nData | None
```
</td>
<td align="left"><code>None</code></td>
<td align="left">the label for this component, displayed above the component if `show_label` is `True` and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component corresponds to.</td>
</tr>
<tr>
<td align="left"><code>every</code></td>
<td align="left" style="width: 25%;">
```python
Timer | float | None
```
</td>
<td align="left"><code>None</code></td>
<td align="left">Continously calls `value` to recalculate it if `value` is a function (has no effect otherwise). Can provide a Timer whose tick resets `value`, or a float that provides the regular interval for the reset Timer.</td>
</tr>
<tr>
<td align="left"><code>inputs</code></td>
<td align="left" style="width: 25%;">
```python
Component | Sequence[Component] | set[Component] | None
```
</td>
<td align="left"><code>None</code></td>
<td align="left">Components that are used as inputs to calculate `value` if `value` is a function (has no effect otherwise). `value` is recalculated any time the inputs change.</td>
</tr>
<tr>
<td align="left"><code>show_label</code></td>
<td align="left" style="width: 25%;">
```python
bool | None
```
</td>
<td align="left"><code>None</code></td>
<td align="left">if True, will display label.</td>
</tr>
<tr>
<td align="left"><code>scale</code></td>
<td align="left" style="width: 25%;">
```python
int | None
```
</td>
<td align="left"><code>None</code></td>
<td align="left">relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.</td>
</tr>
<tr>
<td align="left"><code>min_width</code></td>
<td align="left" style="width: 25%;">
```python
int
```
</td>
<td align="left"><code>160</code></td>
<td align="left">minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.</td>
</tr>
<tr>
<td align="left"><code>interactive</code></td>
<td align="left" style="width: 25%;">
```python
bool | None
```
</td>
<td align="left"><code>None</code></td>
<td align="left">if True, will be rendered as an editable textbox; if False, editing will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.</td>
</tr>
<tr>
<td align="left"><code>visible</code></td>
<td align="left" style="width: 25%;">
```python
bool
```
</td>
<td align="left"><code>True</code></td>
<td align="left">If False, component will be hidden.</td>
</tr>
<tr>
<td align="left"><code>rtl</code></td>
<td align="left" style="width: 25%;">
```python
bool
```
</td>
<td align="left"><code>False</code></td>
<td align="left">If True and `type` is "text", sets the direction of the text to right-to-left (cursor appears on the left of the text). Default is False, which renders cursor on the right.</td>
</tr>
<tr>
<td align="left"><code>elem_id</code></td>
<td align="left" style="width: 25%;">
```python
str | None
```
</td>
<td align="left"><code>None</code></td>
<td align="left">An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.</td>
</tr>
<tr>
<td align="left"><code>elem_classes</code></td>
<td align="left" style="width: 25%;">
```python
list[str] | str | None
```
</td>
<td align="left"><code>None</code></td>
<td align="left">An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.</td>
</tr>
<tr>
<td align="left"><code>render</code></td>
<td align="left" style="width: 25%;">
```python
bool
```
</td>
<td align="left"><code>True</code></td>
<td align="left">If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.</td>
</tr>
<tr>
<td align="left"><code>key</code></td>
<td align="left" style="width: 25%;">
```python
int | str | tuple[int | str, ...] | None
```
</td>
<td align="left"><code>None</code></td>
<td align="left">in a gr.render, Components with the same key across re-renders are treated as the same component, not a new component. Properties set in 'preserved_by_key' are not reset across a re-render.</td>
</tr>
<tr>
<td align="left"><code>preserved_by_key</code></td>
<td align="left" style="width: 25%;">
```python
list[str] | str | None
```
</td>
<td align="left"><code>"value"</code></td>
<td align="left">A list of parameters from this component's constructor. Inside a gr.render() function, if a component is re-rendered with the same key, these (and only these) parameters will be preserved in the UI (if they have been changed by the user or an event listener) instead of re-rendered based on the values provided during constructor.</td>
</tr>
</tbody></table>
### Events
| name | description |
|:-----|:------------|
| `change` | Triggered when the value of the AgentInspector changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input. |
| `input` | This listener is triggered when the user changes the value of the AgentInspector. |
| `submit` | This listener is triggered when the user presses the Enter key while the AgentInspector is focused. |
### User function
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
- When used as an Input, the component only impacts the input signature of the user function.
- When used as an output, the component only impacts the return signature of the user function.
The code snippet below is accurate in cases where the component is used as both an input and an output.
- **As output:** Is passed, passes text value as a {str} into the function.
- **As input:** Should return, expects a {str} returned from function and sets textarea value to it.
```python
def predict(
value: str | None
) -> str | None:
return value
```
|