You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

init_tasks.py 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #
  2. # Run this file to download the human_eval dataset, and create a corresponding testbed scenario:
  3. # (default: ../scenarios/human_eval_two_agents_gpt4.jsonl and ./scenarios/human_eval_two_agents_gpt35.jsonl)
  4. #
  5. import requests
  6. import tarfile
  7. import hashlib
  8. import io
  9. import json
  10. import os
  11. import re
  12. import sys
  13. URL = "https://raw.githubusercontent.com/web-arena-x/webarena/main/config_files/test.raw.json"
  14. SCRIPT_PATH = os.path.realpath(__file__)
  15. SCRIPT_NAME = os.path.basename(SCRIPT_PATH)
  16. SCRIPT_DIR = os.path.dirname(SCRIPT_PATH)
  17. SCENARIO_DIR = os.path.realpath(os.path.join(SCRIPT_DIR, os.path.pardir))
  18. TEMPLATES_DIR = os.path.join(SCENARIO_DIR, "Templates")
  19. TASKS_DIR = os.path.join(SCENARIO_DIR, "Tasks")
  20. DOWNLOADS_DIR = os.path.join(SCENARIO_DIR, "Downloads")
  21. def download():
  22. """Download the WebArena dataset (if not already downloaded).
  23. Return a JSON list of problem instances."""
  24. if not os.path.isdir(DOWNLOADS_DIR):
  25. os.mkdir(DOWNLOADS_DIR)
  26. json_file = os.path.join(DOWNLOADS_DIR, "test.raw.json")
  27. if not os.path.isfile(json_file):
  28. # Send a HTTP request to the URL
  29. response = requests.get(URL, stream=True)
  30. response.raise_for_status()
  31. # If the HTTP request returns a status code 200, proceed
  32. with open(json_file, "wb") as fh:
  33. for chunk in response.iter_content(chunk_size=512):
  34. fh.write(chunk)
  35. # Load the problems
  36. problems = None
  37. with open(json_file, "rb") as fh:
  38. problems = json.load(fh)
  39. return problems
  40. def create_jsonl(name, tasks, template):
  41. """Creates a JSONL scenario file with a given name, dictionary of MATH problems, and template path."""
  42. # Create a task directory if it doesn't exist
  43. if not os.path.isdir(TASKS_DIR):
  44. os.mkdir(TASKS_DIR)
  45. # Create the jsonl file
  46. prompt_fields = ["task_id", "intent_template_id", "sites", "require_login", "start_url", "geolocation", "intent"]
  47. with open(os.path.join(TASKS_DIR, name + ".jsonl"), "wt") as fh:
  48. for task in tasks:
  49. print(f"Converting: {name}, {task['task_id']}")
  50. task_prompt = {}
  51. for field in prompt_fields:
  52. task_prompt[field] = task[field]
  53. record = {
  54. "id": str(task["task_id"]),
  55. "template": [os.path.join(TEMPLATES_DIR, "Common"), template],
  56. "substitutions": {
  57. "task_prompt.json.txt": {"__TASK_PROMPT__": json.dumps(task_prompt, indent=4)},
  58. "full_task.json.txt": {"__FULL_TASK__": json.dumps(task, indent=4)},
  59. },
  60. }
  61. fh.write(json.dumps(record).strip() + "\n")
  62. ###############################################################################
  63. def main():
  64. tasks = download()
  65. # list all directories in the Templates directory
  66. # and populate a dictionary with the name and path
  67. templates = {}
  68. for entry in os.scandir(TEMPLATES_DIR):
  69. if entry.is_dir():
  70. if entry.name == "Common": # Skip the common template, which will be included in all
  71. continue
  72. templates[re.sub(r"\s", "", entry.name)] = entry.path
  73. # Divide the tasks by their websites and if they are validation or test
  74. page_groups = dict()
  75. for task in tasks:
  76. # We don't know how the intent ids are distributed, so hash them to get a uniform distribution
  77. template_hash = hashlib.md5(str(task["intent_template_id"]).encode("utf-8")).hexdigest()
  78. # The full hash will consist of 32 hexadecimal digits. We can get a 50/50 split by checking if the first digit is in the range (0-7) vs (8-F)
  79. task_set = "validation" if template_hash[0] in "01234567" else "test"
  80. key = task["sites"][0]
  81. if len(task["sites"]) > 1:
  82. key = "several_sites"
  83. key = task_set + "_" + key
  84. # key = "__".join(sorted([s for s in task["sites"]]))
  85. if key not in page_groups:
  86. page_groups[key] = list()
  87. page_groups[key].append(task)
  88. # Create the json files
  89. for t in templates.items():
  90. for pg in page_groups:
  91. create_jsonl(f"webarena__{pg}_{t[0]}", page_groups[pg], t[1])
  92. if __name__ == "__main__" and __package__ is None:
  93. main()