Skip to content

Commit

Permalink
Add metadata field to UI element that can store arbitrary information.
Browse files Browse the repository at this point in the history
Update json extraction to try conversion with json module as well.

PiperOrigin-RevId: 717534522
  • Loading branch information
The android_world Authors committed Jan 20, 2025
1 parent 6fbf15b commit ca16d3d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
19 changes: 15 additions & 4 deletions android_world/agents/agent_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
"""Utilities for agents."""

import ast
import json
import re
from typing import Any, Optional
from typing import Any


def extract_json(s: str) -> Optional[dict[str, Any]]:
def extract_json(s: str) -> dict[str, Any] | None:
"""Extracts JSON from string.
Tries conversion with ast and json modules.
Args:
s: A string with a JSON in it. E.g., "{'hello': 'world'}" or from CoT:
"let's think step-by-step, ..., {'hello': 'world'}".
Expand All @@ -35,7 +38,15 @@ def extract_json(s: str) -> Optional[dict[str, Any]]:
try:
return ast.literal_eval(match.group())
except (SyntaxError, ValueError) as error:
print('Cannot extract JSON, skipping due to error %s', error)
return None
try:
# Try conversion with json module.
return json.loads(match.group())
except (SyntaxError, ValueError) as error2:
print(
'Cannot extract JSON, skipping due to errors %s and %s',
error,
error2,
)
return None
else:
return None
1 change: 1 addition & 0 deletions android_world/env/representation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class UIElement:
resource_name: Optional[str] = None
tooltip: Optional[str] = None
resource_id: Optional[str] = None
metadata: Optional[dict[str, Any]] = None


def accessibility_node_to_ui_element(
Expand Down

0 comments on commit ca16d3d

Please sign in to comment.