Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata field to UI element that can store arbitrary information. #213

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading