Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from ai2thor.controller import Controller
|
3 |
+
|
4 |
+
from ai2thor.platform import CloudRendering
|
5 |
+
# 初始化全局模拟器对象,启用 headless 模式
|
6 |
+
controller = Controller(platform=CloudRendering)
|
7 |
+
|
8 |
+
# 用于记录所有动作的列表
|
9 |
+
action_history = []
|
10 |
+
|
11 |
+
def initialize_simulation():
|
12 |
+
"""
|
13 |
+
初始化 ai2thor 模拟器,并返回初始场景的截图。
|
14 |
+
"""
|
15 |
+
controller.reset('FloorPlan28') # 指定场景
|
16 |
+
event = controller.step(action='Pass') # 不执行任何动作,只获取初始图像
|
17 |
+
image = event.frame
|
18 |
+
action_history.clear() # 清空历史记录
|
19 |
+
action_history.append((image, "初始化"))
|
20 |
+
return image, "初始化", action_history
|
21 |
+
|
22 |
+
def move_forward():
|
23 |
+
"""
|
24 |
+
在 ai2thor 模拟器中执行"向前移动"操作,并返回当前环境的截图。
|
25 |
+
"""
|
26 |
+
action = "向前移动"
|
27 |
+
event = controller.step(action='MoveAhead')
|
28 |
+
image = event.frame
|
29 |
+
action_history.append((image, action))
|
30 |
+
return image, action, action_history
|
31 |
+
|
32 |
+
def move_backward():
|
33 |
+
"""
|
34 |
+
在 ai2thor 模拟器中执行"向后移动"操作,并返回当前环境的截图。
|
35 |
+
"""
|
36 |
+
action = "向后移动"
|
37 |
+
event = controller.step(action='MoveBack')
|
38 |
+
image = event.frame
|
39 |
+
action_history.append((image, action))
|
40 |
+
return image, action, action_history
|
41 |
+
|
42 |
+
def move_left():
|
43 |
+
"""
|
44 |
+
在 ai2thor 模拟器中执行"向左旋转"操作,并返回当前环境的截图。
|
45 |
+
"""
|
46 |
+
action = "向左旋转"
|
47 |
+
event = controller.step(action='RotateLeft')
|
48 |
+
image = event.frame
|
49 |
+
action_history.append((image, action))
|
50 |
+
return image, action, action_history
|
51 |
+
|
52 |
+
def move_right():
|
53 |
+
"""
|
54 |
+
在 ai2thor 模拟器中执行"向右旋转"操作,并返回当前环境的截图。
|
55 |
+
"""
|
56 |
+
action = "向右旋转"
|
57 |
+
event = controller.step(action='RotateRight')
|
58 |
+
image = event.frame
|
59 |
+
action_history.append((image, action))
|
60 |
+
return image, action, action_history
|
61 |
+
|
62 |
+
# 使用 Gradio 构建交互界面
|
63 |
+
with gr.Blocks() as iface:
|
64 |
+
gr.Markdown("## 具身模型演示")
|
65 |
+
init_button = gr.Button("初始化")
|
66 |
+
forward_button = gr.Button("向前移动")
|
67 |
+
backward_button = gr.Button("向后移动")
|
68 |
+
left_button = gr.Button("向左旋转")
|
69 |
+
right_button = gr.Button("向右旋转")
|
70 |
+
image_output = gr.Image()
|
71 |
+
current_action_output = gr.Textbox(label="当前动作")
|
72 |
+
action_history_output = gr.Gallery(label="动作历史")
|
73 |
+
|
74 |
+
init_button.click(fn=initialize_simulation, outputs=[image_output, current_action_output, action_history_output])
|
75 |
+
forward_button.click(fn=move_forward, outputs=[image_output, current_action_output, action_history_output])
|
76 |
+
backward_button.click(fn=move_backward, outputs=[image_output, current_action_output, action_history_output])
|
77 |
+
left_button.click(fn=move_left, outputs=[image_output, current_action_output, action_history_output])
|
78 |
+
right_button.click(fn=move_right, outputs=[image_output, current_action_output, action_history_output])
|
79 |
+
|
80 |
+
iface.launch()
|