File size: 6,522 Bytes
9ee6f68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from src.vlm_interaction.interact_prompt_helper import FeasibilityPromptFormatter
from src.vlm_interaction.vlm_interactor import InteractStatusCode
from src.utils.config_manager import get_config_manager


class TaskFeasibilityEvaluator:
    """
    Main class for evaluating task feasibility using VLM.
    """

    def __init__(self, prompt_formatter: FeasibilityPromptFormatter = None):
        self.formatter = prompt_formatter or FeasibilityPromptFormatter()

    def evaluate_task_feasibility(
        self,
        vlminteractor,
        task,
        scene_graph,
        width: int = 512,
        height: int = 512,
        save_path: str = None,
    ):
        """
        Evaluate task feasibility by sending images and prompts to VLM.
        """
        if save_path is None:
            save_path = "./image4vote/"

        # Reset conversation
        vlminteractor.conversation = []

        # Get task information
        task_description = task.task_description
        # Add system prompt
        platform_name_list = [p.platform_name for p in task.platform_list]
        multilayer_object_name_list = [
            m.multilayer_object_name for m in task.multi_layer_object_list
        ]
        system_prompt = self.formatter.get_system_prompt(
            task_description=task_description,
            platform_name_list=platform_name_list,
            multilayer_object_name_list=multilayer_object_name_list,
        )
        vlminteractor.add_content(
            content=system_prompt, role="system", content_type="text"
        )

        for platform in task.platform_list:
            self._add_platform_images(
                vlminteractor,
                task,
                platform.platform_name,
                scene_graph,
                save_path,
                width,
                height,
            )

        for ml_object in task.multi_layer_object_list:
            self._add_multilayer_images(
                vlminteractor,
                task,
                ml_object.multilayer_object_name,
                scene_graph,
                save_path,
                width,
                height,
            )

        status_code, choice = vlminteractor.send_content_n_request()

        if status_code == InteractStatusCode.SUCCESS:
            print("Request successful:", choice)
            return choice
        else:
            print("Request failed with status code:", status_code)
            return "Request failed"

    def _add_platform_images(
        self, vlminteractor, task, platform_name, scene_graph, save_path, width, height
    ):
        """
        Add platform images and descriptions to the conversation.
        """
        platform = [
            p
            for p in scene_graph.platforms.values()
            if p.get_name_for_interaction() == platform_name
        ][0]

        # Take platform picturesta
        platform_img, platform_img_list = scene_graph.auto_take_platform_picture(
            platform_name=platform.name,
            view="human_full",
            save_path=f"{save_path}/{platform.name}.png",
        )

        # Add images to conversation
        n_platform_img_list = len(platform_img_list)
        if n_platform_img_list > 1:
            for i, platform_img in enumerate(platform_img_list):
                image_path = (
                    f"{save_path}/{platform.name}_{i+1}_out_of_{n_platform_img_list}.png"
                )
                vlminteractor.add_content(
                    content=image_path, role="user", content_type="image"
                )
        else:
            vlminteractor.add_content(
                content=f"{save_path}/{platform.name}.png",
                role="user",
                content_type="image",
            )

        # Add platform description
        child_name_list = [
            f"No. {i+1}: {child.name}" for i, child in enumerate(platform.children)
        ]
        explanation = self.formatter.get_platform_introduction(
            platform_name=platform.name,
            child_name_list=child_name_list,
            image_count=n_platform_img_list,
        )
        vlminteractor.add_content(content=explanation, role="user", content_type="text")

    def _add_multilayer_images(
        self,
        vlminteractor,
        task,
        multilayer_object_name,
        scene_graph,
        save_path,
        width,
        height,
    ):
        """
        Add multilayer object images and descriptions to the conversation.
        """
        # Add multilayer introduction
        multilayer_intro = self.formatter.get_multilayer_introduction()
        vlminteractor.add_content(
            content=multilayer_intro, role="user", content_type="text"
        )

        multilayer_object = scene_graph.nodes[multilayer_object_name]

        # Process each layer
        for layer_id, platform in enumerate(multilayer_object.own_platform):
            # Take platform picture
            platform_img, platform_img_list = scene_graph.auto_take_platform_picture(
                platform_name=platform.name,
                save_path=f"{save_path}/{platform.name}.png",
                view="human_full",
            )

            # Add images to conversation
            n_platform_img_list = len(platform_img_list)
            if n_platform_img_list > 1:
                for i, platform_img in enumerate(platform_img_list):
                    image_path = f"{save_path}/{platform.name}_{i+1}_out_of_{n_platform_img_list}.png"
                    vlminteractor.add_content(
                        content=image_path, role="user", content_type="image"
                    )
            else:
                vlminteractor.add_content(
                    content=f"{save_path}/{platform.name}.png",
                    role="user",
                    content_type="image",
                )

            # Add layer description
            child_name_list = [
                f"No. {i+1}: {child.get_name_for_interaction()}"
                for i, child in enumerate(platform.children)
            ]
            is_top_layer = layer_id == len(multilayer_object.own_platform) - 1

            explanation = self.formatter.get_multilayer_layer_description(
                platform_name=platform.name,
                child_name_list=child_name_list,
                is_top_layer=is_top_layer,
            )
            vlminteractor.add_content(
                content=explanation, role="user", content_type="text"
            )