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 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 io
  8. import json
  9. import os
  10. import re
  11. import sys
  12. URL = "https://raw.githubusercontent.com/web-arena-x/webarena/main/config_files/test.raw.json"
  13. SCRIPT_PATH = os.path.realpath(__file__)
  14. SCRIPT_NAME = os.path.basename(SCRIPT_PATH)
  15. SCRIPT_DIR = os.path.dirname(SCRIPT_PATH)
  16. SCENARIO_DIR = os.path.realpath(os.path.join(SCRIPT_DIR, os.path.pardir))
  17. TEMPLATES_DIR = os.path.join(SCENARIO_DIR, "Templates")
  18. TASKS_DIR = os.path.join(SCENARIO_DIR, "Tasks")
  19. DOWNLOADS_DIR = os.path.join(SCENARIO_DIR, "Downloads")
  20. def download():
  21. """Download the WebArena dataset (if not already downloaded).
  22. Return a JSON list of problem instances."""
  23. if not os.path.isdir(DOWNLOADS_DIR):
  24. os.mkdir(DOWNLOADS_DIR)
  25. json_file = os.path.join(DOWNLOADS_DIR, "test.raw.json")
  26. if not os.path.isfile(json_file):
  27. # Send a HTTP request to the URL
  28. response = requests.get(URL, stream=True)
  29. response.raise_for_status()
  30. # If the HTTP request returns a status code 200, proceed
  31. with open(json_file, "wb") as fh:
  32. for chunk in response.iter_content(chunk_size=512):
  33. fh.write(chunk)
  34. # Load the problems
  35. problems = None
  36. with open(json_file, "rb") as fh:
  37. problems = json.load(fh)
  38. return problems
  39. def create_jsonl(name, tasks, template):
  40. """Creates a JSONL scenario file with a given name, dictionary of MATH problems, and template path."""
  41. # Create a task directory if it doesn't exist
  42. if not os.path.isdir(TASKS_DIR):
  43. os.mkdir(TASKS_DIR)
  44. # Create the jsonl file
  45. prompt_fields = ["task_id", "intent_template_id", "sites", "require_login", "start_url", "geolocation", "intent"]
  46. with open(os.path.join(TASKS_DIR, name + ".jsonl"), "wt") as fh:
  47. for task in tasks:
  48. print(f"Converting: {name}, {task['task_id']}")
  49. task_prompt = {}
  50. for field in prompt_fields:
  51. task_prompt[field] = task[field]
  52. record = {
  53. "id": str(task["task_id"]),
  54. "template": template,
  55. "substitutions": {
  56. "task_prompt.json.txt": {"__TASK_PROMPT__": json.dumps(task_prompt, indent=4)},
  57. "full_task.json.txt": {"__FULL_TASK__": json.dumps(task, indent=4)},
  58. },
  59. }
  60. fh.write(json.dumps(record).strip() + "\n")
  61. ###############################################################################
  62. def main():
  63. tasks = download()
  64. # list all directories in the Templates directory
  65. # and populate a dictionary with the name and path
  66. templates = {}
  67. for entry in os.scandir(TEMPLATES_DIR):
  68. if entry.is_dir():
  69. templates[re.sub(r"\s", "", entry.name)] = entry.path
  70. # Divide the tasks by their websites
  71. page_groups = dict()
  72. for task in tasks:
  73. key = task["sites"][0]
  74. if len(task["sites"]) > 1:
  75. key = "several_sites"
  76. # key = "__".join(sorted([s for s in task["sites"]]))
  77. if key not in page_groups:
  78. page_groups[key] = list()
  79. page_groups[key].append(task)
  80. # Create the json files
  81. for t in templates.items():
  82. for pg in page_groups:
  83. create_jsonl(f"webarena__{pg}_{t[0]}", page_groups[pg], t[1])
  84. if __name__ == "__main__" and __package__ is None:
  85. main()