Nanthasit commited on
Commit
87fb54d
·
verified ·
1 Parent(s): 74f726c

Upload scripts/generate-browser-data.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. scripts/generate-browser-data.py +176 -0
scripts/generate-browser-data.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Generate browser training data from MiniWoB++ task specs + BrowserGym action space."""
3
+ import json, random, os
4
+ from datasets import Dataset
5
+ from huggingface_hub import login
6
+
7
+ TOKEN = os.environ.get("HF_TOKEN", "")
8
+ if TOKEN:
9
+ login(token=TOKEN)
10
+
11
+ TOOLS = [
12
+ {"type": "function", "function": {"name": "navigate", "description": "Go to a URL", "parameters": {"type": "object", "properties": {"url": {"type": "string"}}, "required": ["url"]}}},
13
+ {"type": "function", "function": {"name": "click", "description": "Click an element by selector or text", "parameters": {"type": "object", "properties": {"target": {"type": "string", "description": "CSS selector, text label, or coordinate"}}, "required": ["target"]}}},
14
+ {"type": "function", "function": {"name": "type", "description": "Type text into a field", "parameters": {"type": "object", "properties": {"selector": {"type": "string"}, "text": {"type": "string"}, "clear_first": {"type": "boolean"}}, "required": ["selector", "text"]}}},
15
+ {"type": "function", "function": {"name": "select", "description": "Select from dropdown", "parameters": {"type": "object", "properties": {"selector": {"type": "string"}, "value": {"type": "string"}}, "required": ["selector", "value"]}}},
16
+ {"type": "function", "function": {"name": "extract", "description": "Extract text content", "parameters": {"type": "object", "properties": {"selector": {"type": "string"}}}}},
17
+ {"type": "function", "function": {"name": "scroll", "description": "Scroll the page", "parameters": {"type": "object", "properties": {"direction": {"type": "string", "enum": ["up", "down", "top", "bottom"]}}, "required": ["direction"]}}},
18
+ {"type": "function", "function": {"name": "wait", "description": "Wait for milliseconds", "parameters": {"type": "object", "properties": {"ms": {"type": "integer"}}, "required": ["ms"]}}},
19
+ {"type": "function", "function": {"name": "press_key", "description": "Press a keyboard key", "parameters": {"type": "object", "properties": {"key": {"type": "string"}}, "required": ["key"]}}},
20
+ {"type": "function", "function": {"name": "screenshot", "description": "Take screenshot", "parameters": {"type": "object", "properties": {}}}},
21
+ {"type": "function", "function": {"name": "go_back", "description": "Go back", "parameters": {"type": "object", "properties": {}}}},
22
+ {"type": "function", "function": {"name": "done", "description": "Mark task as complete", "parameters": {"type": "object", "properties": {"answer": {"type": "string"}}}}},
23
+ ]
24
+
25
+ # MiniWoB++ task templates — each generates multiple randomized examples
26
+ TASKS = [
27
+ # ── CLICK TASKS ──
28
+ {"type": "click-single", "goal": "Click the button {label}", "steps": [("click", "{label}"), ("done", {"answer": "clicked"})], "variants": [("Submit",), ("OK",), ("Cancel",), ("Next",), ("Yes",), ("No",), ("Save",), ("Delete",), ("Search",), ("Continue",)]},
29
+ {"type": "click-target", "goal": "Click the button with id {id}", "steps": [("click", "#{id}"), ("done", {"answer": "done"})], "variants": [("btn-1",), ("target",), ("submit-btn",), ("main-cta",), ("action",)]},
30
+ {"type": "click-color", "goal": "Click the {color} button", "steps": [("click", ".{color}-btn"), ("done", {"answer": "clicked"})], "variants": [("red",), ("blue",), ("green",), ("yellow",), ("orange",), ("purple",)]},
31
+ {"type": "click-shape", "goal": "Click the {shape} shape", "steps": [("click", ".shape-{shape}"), ("done", {"answer": "clicked"})], "variants": [("circle",), ("square",), ("triangle",), ("star",), ("hexagon",)]},
32
+ {"type": "click-position", "goal": "Click the {pos} button", "steps": [("click", ".btn-{pos}"), ("done", {"answer": "clicked"})], "variants": [("first",), ("second",), ("third",), ("last",), ("middle",)]},
33
+ {"type": "click-popup", "goal": "Click {btn} in the dialog", "steps": [("wait", {"ms": 1000}), ("click", ".dialog .{btn}"), ("done", {"answer": "dismissed"})], "variants": [("confirm",), ("cancel",), ("close",), ("accept",), ("dismiss",)]},
34
+
35
+ # ── CHECKBOX TASKS ──
36
+ {"type": "checkbox-single", "goal": "Select the checkbox labeled {label}", "steps": [("click", "input[type=checkbox][aria-label=\"{label}\"]"), ("done", {"answer": "selected"})], "variants": [("Option A",), ("Option B",), ("Subscribe",), ("Agree",), ("Remember me",)]},
37
+ {"type": "checkbox-multiple", "goal": "Select {labels}", "steps": [("click", "#{id1}"), ("click", "#{id2}"), ("done", {"answer": "selected"})], "variants": [("opt1", "opt2"), ("a", "b"), ("x", "y"), ("foo", "bar"), ("yes", "no")]},
38
+
39
+ # ── TEXT ENTRY ──
40
+ {"type": "enter-text", "goal": "Type {text} into the text field", "steps": [("type", {"selector": "input[type=text]", "text": "{text}"}), ("press_key", {"key": "Enter"}), ("done", {"answer": "{text}"})], "variants": [("Hello World",), ("test@example.com",), ("John Doe",), ("123 Main St",), ("password123",), ("admin",), ("search query",)]},
41
+ {"type": "enter-number", "goal": "Enter the number {num}", "steps": [("type", {"selector": "input[type=number]", "text": "{num}"}), ("done", {"answer": "{num}"})], "variants": [(42,), (100,), (3.14,), (0,), (-5,), (1000,)]},
42
+ {"type": "enter-date", "goal": "Enter the date {date}", "steps": [("type", {"selector": "input[type=date]", "text": "{date}"}), ("done", {"answer": "{date}"})], "variants": [("2026-01-15",), ("2026-07-30",), ("2025-12-25",), ("2026-03-20",), ("2027-01-01",)]},
43
+ {"type": "enter-email", "goal": "Fill in the email field with {email}", "steps": [("type", {"selector": "input[type=email]", "text": "{email}"}), ("done", {"answer": "{email}"})], "variants": [("user@gmail.com",), ("john@company.com",), ("test@example.org",), ("admin@site.net",), ("info@business.io",)]},
44
+ {"type": "enter-password", "goal": "Type the password into the password field", "steps": [("type", {"selector": "input[type=password]", "text": "mypassword"}), ("done", {"answer": "entered"})], "variants": [(), ()]},
45
+
46
+ # ── FORM FILLING ──
47
+ {"type": "login-form", "goal": "Log in with username {user} and password {pass}", "steps": [("type", {"selector": "#username", "text": "{user}"}), ("type", {"selector": "#password", "text": "{pass}"}), ("click", "#login-btn"), ("done", {"answer": "logged in"})], "variants": [("admin", "admin123"), ("user1", "pass1"), ("john", "secret!"), ("test", "letmein"), ("demo", "demo123")]},
48
+ {"type": "signup-form", "goal": "Sign up with name {name}, email {email}", "steps": [("type", {"selector": "#name", "text": "{name}"}), ("type", {"selector": "#email", "text": "{email}"}), ("click", "#signup-btn"), ("done", {"answer": "signed up"})], "variants": [("Alice", "alice@test.com"), ("Bob", "bob@test.com"), ("Carol", "carol@test.com"), ("Dave", "dave@test.com"), ("Eve", "eve@test.com")]},
49
+ {"type": "search-form", "goal": "Search for {query}", "steps": [("type", {"selector": "input[type=search]", "text": "{query}"}), ("click", "button[type=submit]"), ("done", {"answer": "{query}"})], "variants": [("machine learning",), ("React tutorial",), ("best restaurants",), ("weather today",), ("Python jobs",), ("used cars",), ("flight deals",), ("movie times",)]},
50
+
51
+ # ── SELECT/DROPDOWN ──
52
+ {"type": "select-option", "goal": "Select {option} from the dropdown", "steps": [("select", {"selector": "select", "value": "{option}"}), ("done", {"answer": "{option}"})], "variants": [("option1",), ("option2",), ("A",), ("B",), ("C",), ("high",), ("medium",), ("low",), ("1",), ("2",)]},
53
+ {"type": "select-country", "goal": "Select country {country}", "steps": [("select", {"selector": "#country", "value": "{country}"}), ("done", {"answer": "{country}"})], "variants": [("US",), ("UK",), ("CA",), ("DE",), ("FR",), ("JP",), ("AU",), ("BR",), ("IN",), ("TH",)]},
54
+ {"type": "select-category", "goal": "Choose category {cat}", "steps": [("select", {"selector": "#category", "value": "{cat}"}), ("done", {"answer": "{cat}"})], "variants": [("electronics",), ("clothing",), ("books",), ("sports",), ("music",), ("software",), ("services",), ("automotive",)]},
55
+
56
+ # ── NAVIGATION ──
57
+ {"type": "navigate-tabs", "goal": "Switch to the {tab} tab", "steps": [("click", ".tab-{tab}"), ("done", {"answer": "{tab}"})], "variants": [("home",), ("profile",), ("settings",), ("billing",), ("notifications",), ("security",), ("help",)]},
58
+ {"type": "pagination", "goal": "Go to page {num}", "steps": [("click", ".page-{num}"), ("done", {"answer": "{num}"})], "variants": [(2,), (3,), (4,), (5,), ("next",), ("prev",), ("last",)]},
59
+ {"type": "breadcrumb", "goal": "Navigate to {section} via breadcrumb", "steps": [("click", ".breadcrumb .{section}"), ("done", {"answer": "{section}"})], "variants": [("home",), ("products",), ("category",), ("account",), ("dashboard",)]},
60
+
61
+ # ── MULTI-STEP ──
62
+ {"type": "multi-search-select", "goal": "Search for {query} and select result {num}", "steps": [("type", {"selector": "input[type=search]", "text": "{query}"}), ("click", "button[type=submit]"), ("click", ".result:nth-child({num}) a"), ("done", {"answer": "{query}"})], "variants": [("laptops", 1), ("phones", 2), ("books", 3), ("shoes", 1), ("headphones", 2)]},
63
+ {"type": "multi-login-dashboard", "goal": "Login and navigate to dashboard", "steps": [("type", {"selector": "#username", "text": "{user}"}), ("type", {"selector": "#password", "text": "{pass}"}), ("click", "#login-btn"), ("wait", {"ms": 2000}), ("click", ".nav-dashboard"), ("done", {"answer": "dashboard"})], "variants": [("admin", "admin123"), ("user", "pass123")]},
64
+ {"type": "multi-filter-sort", "goal": "Filter by {filter} and sort by {sort}", "steps": [("select", {"selector": "#filter", "value": "{filter}"}), ("select", {"selector": "#sort", "value": "{sort}"}), ("click", "#apply"), ("done", {"answer": "filtered"})], "variants": [("category", "price"), ("brand", "rating"), ("size", "name"), ("color", "newest"), ("price", "popular")]},
65
+
66
+ # ── EXTRACT/VERIFY ──
67
+ {"type": "read-text", "goal": "Read the text from the {element}", "steps": [("extract", {"selector": ".{element}"}), ("done", {"answer": "extracted"})], "variants": [("title",), ("description",), ("price",), ("status",), ("message",)]},
68
+ {"type": "read-table", "goal": "Get the value from row {row} column {col}", "steps": [("extract", {"selector": "table tr:nth-child({row}) td:nth-child({col})"}), ("done", {"answer": "read"})], "variants": [(1, 1), (1, 2), (2, 1), (2, 3), (3, 2)]},
69
+
70
+ # ── EMAIL TASKS ──
71
+ {"type": "email-inbox", "goal": "Open the email from {sender}", "steps": [("click", ".email-from-{sender}"), ("done", {"answer": "opened"})], "variants": [("alice@c.com",), ("bob@c.com",), ("admin@site.com",), ("support@co.com",), ("newsletter@co.com",)]},
72
+ {"type": "email-reply", "goal": "Reply to the email saying {message}", "steps": [("click", "#reply-btn"), ("type", {"selector": "#reply-body", "text": "{message}"}), ("click", "#send-reply"), ("done", {"answer": "replied"})], "variants": [("Thanks!",), ("Got it!",), ("Will do.",), ("Sounds good.",), ("On it.")]},
73
+ {"type": "email-star", "goal": "Star the email about {topic}", "steps": [("click", ".email-star-{topic}"), ("done", {"answer": "starred"})], "variants": [("meeting",), ("invoice",), ("report",), ("update",), ("alert",)]},
74
+
75
+ # ── SOCIAL MEDIA ──
76
+ {"type": "social-like", "goal": "Like the post by {author}", "steps": [("click", ".like-{author}"), ("done", {"answer": "liked"})], "variants": [("user1",), ("user2",), ("author",), ("friend",), ("colleague",)]},
77
+ {"type": "social-comment", "goal": "Comment {text} on the post", "steps": [("click", ".comment-btn"), ("type", {"selector": ".comment-input", "text": "{text}"}), ("click", ".submit-comment"), ("done", {"answer": "commented"})], "variants": [("Great post!",), ("Interesting!",), ("Thanks for sharing!",), ("I agree!",), ("Nice work!")]},
78
+ {"type": "social-follow", "goal": "Follow the user {user}", "steps": [("click", ".follow-{user}"), ("done", {"answer": "followed"})], "variants": [("user1",), ("influencer",), ("brand",), ("friend",), ("expert",)]},
79
+
80
+ # ── SORTING/FILTERING ──
81
+ {"type": "sort-asc", "goal": "Sort results in ascending order", "steps": [("click", "#sort-asc"), ("done", {"answer": "sorted"})], "variants": [(), ()]},
82
+ {"type": "sort-desc", "goal": "Sort results in descending order", "steps": [("click", "#sort-desc"), ("done", {"answer": "sorted"})], "variants": [(), ()]},
83
+ {"type": "filter-active", "goal": "Show only active items", "steps": [("click", "#filter-active"), ("done", {"answer": "filtered"})], "variants": [(), ()]},
84
+
85
+ # ── TIMING ──
86
+ {"type": "wait-load", "goal": "Wait for the page to load", "steps": [("wait", {"ms": 3000}), ("extract", {"selector": ".loaded"}), ("done", {"answer": "loaded"})], "variants": [(), ()]},
87
+ {"type": "wait-element", "goal": "Wait for element {el} to appear", "steps": [("wait", {"ms": 2000}), ("click", ".{el}"), ("done", {"answer": "seen"})], "variants": [("dynamic-content",), ("result",), ("popup",), ("confirmation",), ("status",)]},
88
+
89
+ # ── DYNAMIC CONTENT ──
90
+ {"type": "autocomplete", "goal": "Search {query} and select the autocomplete suggestion", "steps": [("type", {"selector": "input[type=search]", "text": "{query}"}), ("wait", {"ms": 1000}), ("click", ".autocomplete-item:first-child"), ("done", {"answer": "{query}"})], "variants": [("ja",), ("py",), ("rea",), ("doc",), ("hel",)]},
91
+ {"type": "typeahead", "goal": "Type {text} and pick suggestion {suggestion}", "steps": [("type", {"selector": ".typeahead", "text": "{text}"}), ("wait", {"ms": 500}), ("click", ".suggestion-{suggestion}"), ("done", {"answer": "{suggestion}"})], "variants": [("New", "New York"), ("San", "San Francisco"), ("Los", "Los Angeles"), ("Chi", "Chicago"), ("Hou", "Houston")]},
92
+
93
+ # ── DRAG & DROP ──
94
+ {"type": "drag-item", "goal": "Drag item {id} to position {pos}", "steps": [("click", "#item-{id}"), ("click", ".drop-zone-{pos}"), ("done", {"answer": "dragged"})], "variants": [(1, 2), (2, 3), (3, 1), (4, 2), (5, 1)]},
95
+
96
+ # ── SCROLL ──
97
+ {"type": "scroll-down", "goal": "Scroll down to see more content", "steps": [("scroll", {"direction": "down"}), ("wait", {"ms": 500}), ("extract", {"selector": ".new-content"}), ("done", {"answer": "scrolled"})], "variants": [(), ()]},
98
+ {"type": "scroll-find", "goal": "Scroll to find element {el}", "steps": [("scroll", {"direction": "down"}), ("scroll", {"direction": "down"}), ("click", ".{el}"), ("done", {"answer": "found"})], "variants": [("target",), ("result",), ("item",), ("link",), ("image",)]},
99
+ ]
100
+
101
+ def tc(name, args):
102
+ return {"type": "function", "function": {"name": name, "arguments": json.dumps(args)}}
103
+
104
+ def tr(name, content):
105
+ return {"role": "tool", "content": content, "name": name}
106
+
107
+ random.seed(42)
108
+ examples = []
109
+
110
+ for task in TASKS:
111
+ for variant in task["variants"]:
112
+ goal = task["goal"]
113
+ # Fill template
114
+ for i, v in enumerate(variant):
115
+ key = ["label", "id", "color", "shape", "pos", "btn", "text", "num", "date", "email", "name", "user", "pass", "query", "option", "country", "cat", "tab", "section", "filter", "sort", "row", "col", "sender", "message", "topic", "author", "el", "suggestion", "pos_num"][i % 30]
116
+ goal = goal.replace("{" + key + "}", str(v))
117
+
118
+ msgs = [
119
+ {"role": "system", "content": "You are a browser automation agent. Complete web tasks using the provided browser tools."},
120
+ {"role": "user", "content": goal},
121
+ ]
122
+
123
+ steps = []
124
+ for action_name, action_args in task["steps"]:
125
+ filled_args = {}
126
+ if isinstance(action_args, dict):
127
+ for k, v in action_args.items():
128
+ if isinstance(v, str) and "{" in v:
129
+ for i, vv in enumerate(variant):
130
+ key = ["label", "id", "color", "shape", "pos", "btn", "text", "num", "date", "email", "name", "user", "pass", "query", "option", "country", "cat", "tab", "section", "filter", "sort", "row", "col", "sender", "message", "topic", "author", "el", "suggestion", "pos_num"][i % 30]
131
+ v = v.replace("{" + key + "}", str(vv))
132
+ filled_args[k] = v
133
+ elif isinstance(action_args, str):
134
+ filled = action_args
135
+ for i, vv in enumerate(variant):
136
+ key = ["label", "id", "color", "shape", "pos", "btn", "text", "num", "date", "email", "name", "user", "pass", "query", "option", "country", "cat", "tab", "section", "filter", "sort", "row", "col", "sender", "message", "topic", "author", "el", "suggestion", "pos_num"][i % 30]
137
+ filled = filled.replace("{" + key + "}", str(vv))
138
+ filled_args = {"target": filled} if action_name == "click" else {"text": filled}
139
+
140
+ steps.append((action_name, filled_args))
141
+
142
+ for action_name, filled_args in steps:
143
+ msgs.append({"role": "assistant", "content": None, "tool_calls": [tc(action_name, filled_args)]})
144
+ resp_data = filled_args
145
+ msgs.append(tr(action_name, json.dumps(resp_data)))
146
+
147
+ examples.append({"messages": msgs, "tools": TOOLS})
148
+
149
+ # Add irrelevance examples
150
+ irrelevance_questions = [
151
+ "What is the capital of France?", "Write a poem about AI.",
152
+ "Explain quantum computing.", "What's the meaning of life?",
153
+ "Tell me a joke.", "How do I bake a cake?",
154
+ "What is 2+2?", "Translate 'hello' to Thai.",
155
+ "Who invented the telephone?", "What's your favorite color?",
156
+ "How does photosynthesis work?", "Define recursion.",
157
+ "What is the speed of light?", "Who painted the Mona Lisa?",
158
+ "What is the largest ocean?", "How tall is Mount Everest?",
159
+ "When was the first iPhone released?", "What causes rainbows?",
160
+ "What is machine learning?", "Tell me about the solar system.",
161
+ "What is blockchain?", "How do vaccines work?",
162
+ "What are black holes?", "What is the stock market?",
163
+ ]
164
+
165
+ for q in irrelevance_questions:
166
+ examples.append({"messages": [
167
+ {"role": "system", "content": "You are a browser automation agent. Only perform web browsing tasks using browser tools."},
168
+ {"role": "user", "content": q},
169
+ {"role": "assistant", "content": "I'm a browser agent. I can navigate pages, click buttons, fill forms, extract content, and handle browser interactions. Please give me a web task to do using my browser tools."}],
170
+ })
171
+
172
+ random.shuffle(examples)
173
+ ds = Dataset.from_list(examples)
174
+ ds.push_to_hub("Nanthasit/sakthai-coder-browser", split="train")
175
+ print(f"Pushed {len(examples)} examples ({len(examples)-len(irrelevance_questions)} browser + {len(irrelevance_questions)} irrelevance)")
176
+ print(f"Dataset: Nanthasit/sakthai-coder-browser")