File size: 1,385 Bytes
adf2e22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
#
# @File:   instruction_generator.py
# @Author: Haozhe Xie
# @Date:   2025-05-31 19:42:51
# @Last Modified by: Haozhe Xie
# @Last Modified at: 2026-01-11 20:40:43
# @Email:  root@haozhexie.com

import json
import random


class InstructionGenerator:
    @staticmethod
    def generate_instruction(inst_metadata):
        if isinstance(inst_metadata, str):
            inst_metadata = json.loads(inst_metadata)

        tmpl = InstructionGenerator._get_instruction_template(inst_metadata["task"])
        object_desc = random.choice(inst_metadata.get("objects", [""]))
        container_desc = random.choice(inst_metadata.get("containers", [""]))
        return tmpl.format_map({"object": object_desc, "container": container_desc})

    @staticmethod
    def _get_instruction_template(task):
        pick_action = random.choice(
            ["pick up", "grasp", "catch", "grab", "get hold of"]
        )
        place_action = random.choice(
            ["place on", "put on", "set on", "position on", "return to", "deposit in"]
        )
        if task == "pick":
            return f"{pick_action} the {{object}}."
        elif task in ["place", "long-horizon"]:
            return f"{pick_action} the {{object}} and {place_action} the {{container}}."
        else:
            raise ValueError(f"Unknown task: {task}")