feat: Add Remote Action Replay Call
一些检查失败了
Quality / Style (push) Failing after 2s
Quality / Typos (push) Failing after 2s
Tests / Pytest (push) Failing after 2s
Tests / Pytest (minimal install) (push) Failing after 2s
Tests / End-to-end (push) Failing after 3s
Secret Leaks / trufflehog (push) Failing after 3s
Builds / CPU (push) Has been cancelled
Builds / GPU (push) Has been cancelled
Builds / GPU Dev (push) Has been cancelled
Nightly / CPU (push) Has been cancelled
Nightly / GPU (push) Has been cancelled

这个提交包含在:
2026-05-17 01:13:10 +08:00
父节点 94244cdc92
当前提交 ad9e4147df

查看文件

@ -48,7 +48,9 @@ python -m lerobot.so101_follower_ws_server \
import asyncio
import json
import logging
from dataclasses import asdict, dataclass
import time
from dataclasses import asdict, dataclass, replace
from pathlib import Path
from typing import Any, Literal
import draccus
@ -223,6 +225,8 @@ def _build_init_response(
def _apply_units_to_robot_config(robot_cfg: SO101FollowerConfig, units: Units) -> SO101FollowerConfig:
# Work on a copy to avoid mutating the shared draccus config object.
robot_cfg = replace(robot_cfg)
# Normalize UI expects degrees by default.
if units == "degrees":
if not robot_cfg.use_degrees:
@ -233,6 +237,56 @@ def _apply_units_to_robot_config(robot_cfg: SO101FollowerConfig, units: Units) -
return robot_cfg
def _replay_dataset_episode_sync(
robot_cfg: SO101FollowerConfig,
*,
repo_id: str,
episode: int,
root: str | Path | None,
) -> dict[str, Any]:
from lerobot.datasets.lerobot_dataset import LeRobotDataset # noqa: WPS433
from lerobot.utils.robot_utils import busy_wait # noqa: WPS433
robot = SO101Follower(robot_cfg)
try:
robot.connect(False)
dataset = LeRobotDataset(repo_id, root=root, episodes=[episode])
# Basic compatibility check when metadata is available.
ds_robot_type = getattr(dataset, "meta", None)
ds_robot_type = getattr(ds_robot_type, "robot_type", None)
if ds_robot_type and ds_robot_type != robot.name:
raise ValueError(
f"Dataset robot_type='{ds_robot_type}' does not match server robot='{robot.name}'."
)
actions = dataset.hf_dataset.select_columns("action")
action_names = dataset.features["action"]["names"]
for idx in range(dataset.num_frames):
t0 = time.perf_counter()
action_array = actions[idx]["action"]
action = {name: float(action_array[i]) for i, name in enumerate(action_names)}
robot.send_action(action)
busy_wait(1.0 / dataset.fps - (time.perf_counter() - t0))
return {
"status": "completed",
"repo_id": repo_id,
"episode": episode,
"num_frames": dataset.num_frames,
"fps": dataset.fps,
}
finally:
try:
robot.disconnect()
except Exception:
pass
def _has_required_calibration(robot: SO101Follower, units: Units) -> bool:
if units == "raw":
return True
@ -243,6 +297,8 @@ def _has_required_calibration(robot: SO101Follower, units: Units) -> bool:
def _build_app(cfg: SO101FollowerWsServerConfig):
FastAPI, WebSocket, WebSocketDisconnect, CORSMiddleware = _require_fastapi()
from fastapi import HTTPException, Request # noqa: WPS433
app = FastAPI(title="SO101 Follower WS Server")
app.add_middleware(
CORSMiddleware,
@ -258,6 +314,76 @@ def _build_app(cfg: SO101FollowerWsServerConfig):
async def health() -> dict[str, str]:
return {"status": "ok"}
@app.post("/api/replay")
async def api_replay(request: Request) -> dict[str, Any]: # type: ignore[valid-type]
"""Replay a dataset episode on the configured robot.
Request JSON supports either of:
- {"repo_id": "user/dataset", "episode": 0, "root": "/path/optional"}
- {"dataset": {"repo_id": "...", "episode": 0, "root": "..."}}
"""
try:
payload = await request.json()
except Exception:
raise HTTPException(status_code=400, detail="Invalid JSON body")
dataset_payload = payload.get("dataset") if isinstance(payload, dict) else None
if isinstance(dataset_payload, dict):
repo_id = dataset_payload.get("repo_id")
episode = dataset_payload.get("episode")
root = dataset_payload.get("root")
elif isinstance(payload, dict):
repo_id = payload.get("repo_id")
episode = payload.get("episode")
root = payload.get("root")
else:
repo_id = episode = root = None
if not repo_id or episode is None:
raise HTTPException(
status_code=400,
detail="Missing 'repo_id' or 'episode' in request body",
)
try:
episode_i = int(episode)
except Exception:
raise HTTPException(status_code=400, detail="'episode' must be an integer")
root_path: str | Path | None
if root is None or root == "":
root_path = None
else:
root_path = Path(str(root)).expanduser()
if cfg.single_client and client_lock.locked():
raise HTTPException(
status_code=423,
detail="Robot is busy (single_client=true). Disconnect the WS client or wait.",
)
lock_cm = client_lock if cfg.single_client else _NullAsyncLock()
async with lock_cm:
robot_cfg = cfg.robot
assert isinstance(robot_cfg, SO101FollowerConfig)
robot_cfg = _apply_units_to_robot_config(robot_cfg, cfg.units)
try:
result = await _to_thread(
_replay_dataset_episode_sync,
robot_cfg,
repo_id=str(repo_id),
episode=episode_i,
root=root_path,
)
return result
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
except Exception as exc:
logger.exception(f"Replay failed: {exc}")
raise HTTPException(status_code=500, detail="Replay failed")
@app.websocket("/ws/{robot_type}")
async def websocket_endpoint(websocket: WebSocket, robot_type: str = "six_axis"): # type: ignore[valid-type]
await websocket.accept()