| 3 | import traceback |
| 4 | |
| 5 | def formulate_sample(data, dependency_type): |
| 6 | try: |
| 7 | user_request = data["result"]["user_request"] |
| 8 | invoking_graph = data["result"]["invoking_graph"] |
| 9 | task_steps = data["result"]["task_steps"] |
| 10 | nodes = invoking_graph["nodes"] |
| 11 | links = invoking_graph["links"] |
| 12 | user_request = data["result"]["user_request"] |
| 13 | if "check_by_teacher" in data["result"]: |
| 14 | check_by_teacher = data["result"]["check_by_teacher"] |
| 15 | else: |
| 16 | check_by_teacher = invoking_graph["check_by_teacher"] |
| 17 | for node in nodes: |
| 18 | node["task"] = node["id"] |
| 19 | node.pop("id") |
| 20 | if dependency_type == "resource": |
| 21 | node["task"] = node["task"].replace("_", " ") |
| 22 | node["arguments"] = node["input"] |
| 23 | node.pop("input") |
| 24 | |
| 25 | for node in nodes: |
| 26 | assert isinstance(node, dict) |
| 27 | assert "task" in node |
| 28 | assert "arguments" in node |
| 29 | if isinstance(node["arguments"], str) and node["arguments"].startswith("<node-"): |
| 30 | node["arguments"] = [node["arguments"]] |
| 31 | assert isinstance(node["arguments"], list), node["arguments"] |
| 32 | if dependency_type == "resource": |
| 33 | assert len(node["arguments"]) <= 2 |
| 34 | for i, argument in enumerate(node["arguments"]): |
| 35 | for j, n in enumerate(nodes): |
| 36 | if n["task"] in argument: |
| 37 | node["arguments"][i] = f"<node-{j}>" |
| 38 | break |
| 39 | for link in links: |
| 40 | assert isinstance(link, dict) |
| 41 | assert "source" in link |
| 42 | assert "target" in link |
| 43 | if dependency_type == "resource": |
| 44 | link["source"] = link["source"].replace("_", " ") |
| 45 | link["target"] = link["target"].replace("_", " ") |
| 46 | assert isinstance(task_steps, list) |
| 47 | assert isinstance(nodes, list) |
| 48 | assert len(nodes) == len(task_steps) |
| 49 | assert isinstance(user_request, str) |
| 50 | assert isinstance(check_by_teacher, str) |
| 51 | except Exception as e: |
| 52 | print(e) |
| 53 | traceback.print_exc() |
| 54 | return None, None, None, None, None |
| 55 | return user_request, task_steps, links, nodes, check_by_teacher |
| 56 | |
| 57 | @click.command() |
| 58 | @click.option('--data_dir', default='data_huggingface', help='Path to the data directory') |