jandan138 commited on
Commit
c3f75eb
·
1 Parent(s): 5402fd3

copy yiyang

Browse files
Files changed (3) hide show
  1. README.md +5 -7
  2. app.py +279 -4
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,14 +1,12 @@
1
  ---
2
- title: Grutopia Nav
3
- emoji: 😻
4
- colorFrom: blue
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 5.36.2
8
  app_file: app.py
9
  pinned: false
10
- license: mit
11
- short_description: nav
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Grdemo
3
+ emoji: 🦀
4
+ colorFrom: red
5
+ colorTo: red
6
  sdk: gradio
7
+ sdk_version: 5.35.0
8
  app_file: app.py
9
  pinned: false
 
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,7 +1,282 @@
1
  import gradio as gr
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ # import json
4
+ import os
5
+ from typing import Optional
6
 
7
+ # 后端API配置(可配置化)
8
+ BACKEND_URL = os.getenv("BACKEND_URL", "http://your-backend-server:5000")
9
+ API_ENDPOINTS = {
10
+ "submit_task": f"{BACKEND_URL}/api/v1/submit",
11
+ "query_status": f"{BACKEND_URL}/api/v1/status",
12
+ "get_result": f"{BACKEND_URL}/api/v1/result"
13
+ }
14
 
15
+ # 模拟场景配置
16
+ SCENE_CONFIGS = {
17
+ "default_desk": {
18
+ "description": "标准实验桌",
19
+ "objects": ["番茄酱", "盐瓶", "餐刀", "杯子"]
20
+ },
21
+ "cluttered_desk": {
22
+ "description": "杂乱桌面场景",
23
+ "objects": ["书本", "笔", "手机", "水杯", "零食袋"]
24
+ },
25
+ "industrial_table": {
26
+ "description": "工业工作台",
27
+ "objects": ["扳手", "螺丝", "电路板", "润滑剂"]
28
+ }
29
+ }
30
+
31
+ # 可用模型列表
32
+ MODEL_CHOICES = [
33
+ "GRManipulation-v1.0",
34
+ "GR00T-N1",
35
+ "GR00T-1.5",
36
+ "Pi0",
37
+ "DP+CLIP",
38
+ "AcT+CLIP"
39
+ ]
40
+
41
+ def submit_to_backend(
42
+ scene: str,
43
+ prompt: str,
44
+ model: str,
45
+ max_steps: int = 100,
46
+ visualize: bool = True
47
+ ) -> dict:
48
+ """
49
+ 提交任务到后端API
50
+ """
51
+ payload = {
52
+ "scene_config": scene,
53
+ "prompt": prompt,
54
+ "model": model,
55
+ "params": {
56
+ "max_steps": max_steps,
57
+ "visualize": visualize
58
+ },
59
+ "metadata": {
60
+ "submit_from": "gradio_ui"
61
+ }
62
+ }
63
+
64
+ try:
65
+ response = requests.post(
66
+ API_ENDPOINTS["submit_task"],
67
+ json=payload,
68
+ timeout=10
69
+ )
70
+ return response.json()
71
+ except Exception as e:
72
+ return {"status": "error", "message": str(e)}
73
+
74
+ def get_task_status(task_id: str) -> dict:
75
+ """
76
+ 查询任务状态
77
+ """
78
+ try:
79
+ response = requests.get(
80
+ f"{API_ENDPOINTS['query_status']}/{task_id}",
81
+ timeout=5
82
+ )
83
+ return response.json()
84
+ except Exception as e:
85
+ return {"status": "error", "message": str(e)}
86
+
87
+ def get_task_result(task_id: str) -> Optional[dict]:
88
+ """
89
+ 获取任务结果
90
+ """
91
+ try:
92
+ response = requests.get(
93
+ f"{API_ENDPOINTS['get_result']}/{task_id}",
94
+ timeout=5
95
+ )
96
+ return response.json()
97
+ except Exception as e:
98
+ print(f"Error fetching result: {e}")
99
+ return None
100
+
101
+ def run_simulation(
102
+ scene: str,
103
+ prompt: str,
104
+ model: str,
105
+ progress=gr.Progress()
106
+ ) -> dict:
107
+ """
108
+ 运行仿真的主函数
109
+ """
110
+ # 提交任务到后端
111
+ progress(0.1, desc="提交任务到后端...")
112
+ submission = submit_to_backend(scene, prompt, model)
113
+
114
+ if submission.get("status") != "success":
115
+ raise gr.Error(f"提交失败: {submission.get('message', '未知错误')}")
116
+
117
+ task_id = submission["task_id"]
118
+ progress(0.3, desc="任务已提交,等待执行...")
119
+
120
+ # 轮询任务状态
121
+ max_checks = 20
122
+ for i in range(max_checks):
123
+ status = get_task_status(task_id)
124
+
125
+ if status.get("status") == "completed":
126
+ progress(0.9, desc="获取结果...")
127
+ result = get_task_result(task_id)
128
+ if result:
129
+ return {
130
+ "video": result.get("video_path"),
131
+ "metrics": result.get("metrics"),
132
+ "log": result.get("log")
133
+ }
134
+ else:
135
+ raise gr.Error("获取结果失败")
136
+
137
+ elif status.get("status") == "failed":
138
+ raise gr.Error(f"任务执行失败: {status.get('message')}")
139
+
140
+ progress(0.3 + 0.6 * (i/max_checks), desc=f"任务执行中...({status.get('progress', 0)}%)")
141
+
142
+ raise gr.Error("任务执行超时")
143
+
144
+ # 自定义CSS样式
145
+ custom_css = """
146
+ #simulation-panel {
147
+ border-radius: 8px;
148
+ padding: 20px;
149
+ background: #f9f9f9;
150
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
151
+ }
152
+ #result-panel {
153
+ border-radius: 8px;
154
+ padding: 20px;
155
+ background: #f0f8ff;
156
+ }
157
+ .dark #simulation-panel { background: #2a2a2a; }
158
+ .dark #result-panel { background: #1a2a3a; }
159
+ """
160
+
161
+ # 创建Gradio界面
162
+ with gr.Blocks(title="机械臂操作训练系统", css=custom_css) as demo:
163
+ # 标题和描述
164
+ gr.Markdown("""
165
+ # 🤖 IsaacSim 机械臂操作评测演示
166
+ ### 基于GRManipulation框架的仿真训练
167
+ """)
168
+
169
+ with gr.Row():
170
+ # 左侧控制面板
171
+ with gr.Column(elem_id="simulation-panel"):
172
+ gr.Markdown("### 仿真参数配置")
173
+
174
+ # 场景选择
175
+ scene_dropdown = gr.Dropdown(
176
+ label="选择场景配置",
177
+ choices=list(SCENE_CONFIGS.keys()),
178
+ value="default_desk",
179
+ interactive=True
180
+ )
181
+
182
+ # 场景描述预览
183
+ scene_description = gr.Markdown("")
184
+
185
+ # 动态更新场景描述
186
+ def update_scene_desc(scene):
187
+ config = SCENE_CONFIGS.get(scene, {})
188
+ desc = config.get("description", "无描述")
189
+ objects = "、".join(config.get("objects", []))
190
+ return f"**{desc}** \n包含物体: {objects}"
191
+
192
+ scene_dropdown.change(
193
+ update_scene_desc,
194
+ inputs=scene_dropdown,
195
+ outputs=scene_description
196
+ )
197
+
198
+ # 操作指令输入
199
+ prompt_input = gr.Textbox(
200
+ label="操作指令",
201
+ placeholder="例如: '请把桌子上的番茄酱拿起来'",
202
+ lines=2,
203
+ max_lines=4
204
+ )
205
+
206
+ # 模型选择
207
+ model_dropdown = gr.Dropdown(
208
+ label="选择训练模型",
209
+ choices=MODEL_CHOICES,
210
+ value=MODEL_CHOICES[0]
211
+ )
212
+
213
+ # 高级参数
214
+ with gr.Accordion("高级参数", open=False):
215
+ max_steps = gr.Slider(
216
+ minimum=50,
217
+ maximum=500,
218
+ value=100,
219
+ step=10,
220
+ label="最大步数"
221
+ )
222
+ visualize = gr.Checkbox(
223
+ value=True,
224
+ label="启用可视化"
225
+ )
226
+
227
+ # 提交按钮
228
+ submit_btn = gr.Button("开始仿真", variant="primary")
229
+
230
+ # 右侧结果面板
231
+ with gr.Column(elem_id="result-panel"):
232
+ gr.Markdown("### 仿真结果")
233
+
234
+ # 视频输出
235
+ video_output = gr.Video(
236
+ label="操作过程回放",
237
+ interactive=False,
238
+ format="mp4"
239
+ )
240
+
241
+ # 指标展示
242
+ metrics_output = gr.JSON(
243
+ label="性能指标",
244
+ visible=False
245
+ )
246
+
247
+ # 日志输出
248
+ log_output = gr.Textbox(
249
+ label="执行日志",
250
+ visible=False,
251
+ lines=10,
252
+ max_lines=20
253
+ )
254
+
255
+ # 示例
256
+ gr.Examples(
257
+ examples=[
258
+ ["default_desk", "请把番茄酱移动到桌子左侧", "GR00T-N1"],
259
+ ["cluttered_desk", "将手机放到书本旁边", "DP+CLIP"],
260
+ ["industrial_table", "拿起电路板并旋转90度", "AcT+CLIP"]
261
+ ],
262
+ inputs=[scene_dropdown, prompt_input, model_dropdown],
263
+ label="快速示例"
264
+ )
265
+
266
+ # 提交处理
267
+ submit_btn.click(
268
+ fn=run_simulation,
269
+ inputs=[scene_dropdown, prompt_input, model_dropdown],
270
+ outputs=[video_output, metrics_output, log_output],
271
+ api_name="run_simulation"
272
+ )
273
+
274
+ # 初始化场景描述
275
+ demo.load(
276
+ fn=lambda: update_scene_desc("default_desk"),
277
+ outputs=scene_description
278
+ )
279
+
280
+ # 启动应用
281
+ if __name__ == "__main__":
282
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ requests
2
+ typing-extensions