-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathserver.py
More file actions
1290 lines (1025 loc) · 44.5 KB
/
Copy pathserver.py
File metadata and controls
1290 lines (1025 loc) · 44.5 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2025 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import contextlib
import glob
import json
import logging
import os
import queue
import subprocess
import sys
import threading
import time
from pathlib import Path
from typing import Any, Literal
from fastapi import FastAPI, HTTPException, Request, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from starlette.datastructures import Headers
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.responses import Response
from starlette.types import Scope
from . import datasets as dataset_browser
# Import our custom calibration functionality
from .calibrate import CalibrationRequest, calibration_manager
from .jobs import (
JobAlreadyRunningError,
JobNotFoundError,
JobNotRunningError,
JobTarget,
job_registry,
)
# Import our custom recording functionality
from .record import (
DatasetInfoRequest,
RecordingRequest,
UploadRequest,
handle_delete_dataset,
handle_exit_early,
handle_get_dataset_info,
handle_recording_status,
handle_rerecord_episode,
handle_start_recording,
handle_stop_recording,
handle_upload_dataset,
)
from .rollout import (
InferenceRequest,
handle_inference_status,
handle_start_inference,
handle_stop_inference,
)
# Import our custom teleoperation functionality
from .teleoperate import (
TeleoperateRequest,
handle_get_joint_positions,
handle_start_teleoperation,
handle_stop_teleoperation,
handle_teleoperation_status,
)
# Training is now job-based; see app/jobs.py.
from .train import TrainingRequest
from .update import handle_run_update, handle_update_check
from .utils import config
from .utils.config import (
FOLLOWER_CONFIG_PATH,
LEADER_CONFIG_PATH,
delete_robot_record,
detect_port_after_disconnect,
find_available_ports,
find_robot_port,
get_default_robot_port,
get_robot_record,
get_saved_robot_port,
is_robot_record_clean,
is_valid_robot_name,
list_robot_records,
save_robot_port,
save_robot_record,
)
from .utils.hf_auth import cached_whoami, handle_hf_auth_status, handle_hf_login, shared_hf_api
from .utils.system import (
handle_get_cuda_status,
handle_get_policy_extra,
handle_get_training_extra,
handle_get_wandb_extra,
handle_install_policy_extra,
handle_install_policy_extra_status,
handle_install_training_extra,
handle_install_training_extra_status,
handle_install_wandb_extra,
handle_install_wandb_extra_status,
warn_if_cuda_mismatch,
)
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class StartTrainingBody(BaseModel):
"""Wrapping body for POST /jobs/training. Adds optional target spec."""
config: TrainingRequest
target: JobTarget | None = None
@classmethod
def from_legacy(cls, raw: dict) -> "StartTrainingBody":
"""Accept the old request shape (TrainingRequest fields at top level)
as well as the new shape ({config: ..., target: ...}).
"""
if "config" in raw and isinstance(raw["config"], dict):
return cls.model_validate(raw)
# Legacy: top-level training fields, no target.
return cls(config=TrainingRequest.model_validate(raw))
# Cache for HF Jobs hardware flavors (5-minute TTL)
_flavors_cache: dict = {"data": None, "fetched_at": 0.0}
_FLAVOR_CACHE_TTL_SECONDS = 300.0
app = FastAPI()
# In dev mode the React app runs on :8080 while the API runs on :8000; in
# prod they share an origin and CORS is unnecessary. allow_credentials with
# a wildcard origin is rejected by browsers, so we drop it.
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
FRONTEND_DIST = Path(__file__).parent.parent / "frontend" / "dist"
# Get the path to the lerobot root directory (3 levels up from this script)
LEROBOT_PATH = str(Path(__file__).parent.parent.parent.parent)
logger.info(f"LeRobot path: {LEROBOT_PATH}")
class ConnectionManager:
def __init__(self):
self.active_connections: list[WebSocket] = []
self.broadcast_queue = queue.Queue()
self.broadcast_thread = None
self.is_running = False
# Guards `active_connections` since the broadcast worker thread also
# mutates it on send failure.
self._connections_lock = threading.Lock()
async def connect(self, websocket: WebSocket):
await websocket.accept()
with self._connections_lock:
self.active_connections.append(websocket)
count = len(self.active_connections)
logger.info(f"WebSocket connected. Total connections: {count}")
if not self.is_running:
self.start_broadcast_thread()
def disconnect(self, websocket: WebSocket):
with self._connections_lock:
if websocket in self.active_connections:
self.active_connections.remove(websocket)
count = len(self.active_connections)
logger.info(f"WebSocket disconnected. Total connections: {count}")
else:
count = len(self.active_connections)
if count == 0 and self.is_running:
self.stop_broadcast_thread()
def start_broadcast_thread(self):
"""Start the background thread for broadcasting data"""
if self.is_running:
return
self.is_running = True
self.broadcast_thread = threading.Thread(target=self._broadcast_worker, daemon=True)
self.broadcast_thread.start()
logger.info("📡 Broadcast thread started")
def stop_broadcast_thread(self):
"""Stop the background thread"""
self.is_running = False
if self.broadcast_thread:
self.broadcast_thread.join(timeout=1.0)
logger.info("📡 Broadcast thread stopped")
def _broadcast_worker(self):
"""Background worker thread for broadcasting WebSocket data"""
import asyncio
# Create a new event loop for this thread
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
while self.is_running:
try:
# Get data from queue with timeout
data = self.broadcast_queue.get(timeout=0.1)
if data is None: # Poison pill to stop
break
# Broadcast to all connections
if self.active_connections:
loop.run_until_complete(self._send_to_all_connections(data))
except queue.Empty:
continue
except Exception as e:
logger.error(f"Error in broadcast worker: {e}")
finally:
loop.close()
async def _send_to_all_connections(self, data: dict[str, Any]):
"""Send data to all active WebSocket connections"""
with self._connections_lock:
connections = list(self.active_connections)
if not connections:
return
disconnected = []
for connection in connections:
try:
await connection.send_json(data)
except Exception as e:
logger.error(f"Error sending data to WebSocket: {e}")
disconnected.append(connection)
for connection in disconnected:
self.disconnect(connection)
def broadcast_joint_data_sync(self, data: dict[str, Any]):
"""Thread-safe method to queue data for broadcasting"""
if self.is_running and self.active_connections:
try:
self.broadcast_queue.put_nowait(data)
except queue.Full:
logger.warning("Broadcast queue is full, dropping data")
def notify_jobs_changed(self) -> None:
"""Push a 'jobs_changed' event to all WS clients so they refetch.
Called from JobRegistry on submit / watchdog finalisation / delete.
Skipped silently if no clients are connected — the frontend does an
initial fetch on mount, so a missed broadcast is self-healing.
"""
if self.is_running and self.active_connections:
with contextlib.suppress(queue.Full):
self.broadcast_queue.put_nowait({"type": "jobs_changed", "timestamp": time.time()})
def notify_job_progress(self, snapshots: list[dict]) -> None:
"""Push a 'job_progress' event with per-running-job snapshots.
Fired from the JobRegistry watchdog (~1Hz) while jobs are running so
the dashboard's progress bar updates live without refetching /jobs
(let alone /jobs/hub, which hits the HF API on every call).
"""
if self.is_running and self.active_connections:
with contextlib.suppress(queue.Full):
self.broadcast_queue.put_nowait(
{"type": "job_progress", "jobs": snapshots, "timestamp": time.time()}
)
manager = ConnectionManager()
job_registry.set_on_change(manager.notify_jobs_changed)
job_registry.set_on_progress(manager.notify_job_progress)
@app.get("/get-configs")
def get_configs():
# Get all available calibration configs
leader_configs = [os.path.basename(f) for f in glob.glob(os.path.join(LEADER_CONFIG_PATH, "*.json"))]
follower_configs = [os.path.basename(f) for f in glob.glob(os.path.join(FOLLOWER_CONFIG_PATH, "*.json"))]
return {"leader_configs": leader_configs, "follower_configs": follower_configs}
@app.post("/move-arm")
def teleoperate_arm(request: TeleoperateRequest):
"""Start teleoperation of the robot arm"""
return handle_start_teleoperation(request, manager)
@app.post("/stop-teleoperation")
def stop_teleoperation():
"""Stop the current teleoperation session"""
return handle_stop_teleoperation()
@app.get("/teleoperation-status")
def teleoperation_status():
"""Get the current teleoperation status"""
return handle_teleoperation_status()
@app.get("/joint-positions")
def get_joint_positions():
"""Get current robot joint positions"""
return handle_get_joint_positions()
@app.post("/start-inference")
def start_inference(request: InferenceRequest):
result = handle_start_inference(request)
if not result.get("success"):
raise HTTPException(
status_code=result.get("status_code", 500),
detail=result.get("message", "Failed to start inference"),
)
return result
@app.post("/stop-inference")
def stop_inference():
result = handle_stop_inference()
if not result.get("success"):
raise HTTPException(
status_code=result.get("status_code", 500),
detail=result.get("message", "Failed to stop inference"),
)
return result
@app.get("/inference-status")
def inference_status():
return handle_inference_status()
@app.get("/health")
def health_check():
"""Simple health check endpoint to verify server is running"""
return {"status": "ok", "message": "FastAPI server is running"}
@app.get("/hf-auth-status")
def hf_auth_status():
"""Check whether the local HF CLI is authenticated and return user info."""
return handle_hf_auth_status()
class HfLoginBody(BaseModel):
token: str
@app.post("/hf-auth/login")
def hf_auth_login(body: HfLoginBody):
"""Persist a pasted HF token (validated against whoami) for this user."""
try:
return handle_hf_login(body.token)
except ValueError as exc:
raise HTTPException(status_code=401, detail=str(exc)) from exc
@app.get("/datasets")
def datasets_list():
"""List datasets available to the user — Hub-owned + local cache.
Each entry carries a `source` field: "local", "hub", or "both".
"""
return dataset_browser.list_all_datasets()
@app.get("/ws-test")
def websocket_test():
"""Test endpoint to verify WebSocket support"""
return {"websocket_endpoint": "/ws/joint-data", "status": "available"}
@app.websocket("/ws/joint-data")
async def websocket_endpoint(websocket: WebSocket):
logger.info("🔗 New WebSocket connection attempt")
try:
await manager.connect(websocket)
logger.info("✅ WebSocket connection established")
while True:
# Keep the connection alive and wait for messages
try:
data = await asyncio.wait_for(websocket.receive_text(), timeout=1.0)
# Handle any incoming messages if needed
logger.debug(f"Received WebSocket message: {data}")
except TimeoutError:
# No message received, continue
pass
except WebSocketDisconnect:
logger.info("🔌 WebSocket client disconnected")
break
# Small delay to prevent excessive CPU usage
await asyncio.sleep(0.01)
except WebSocketDisconnect:
logger.info("🔌 WebSocket disconnected normally")
except Exception as e:
logger.error(f"❌ WebSocket error: {e}")
finally:
manager.disconnect(websocket)
logger.info("🧹 WebSocket connection cleaned up")
@app.post("/start-recording")
def start_recording(request: RecordingRequest):
"""Start a dataset recording session"""
return handle_start_recording(request)
@app.post("/stop-recording")
def stop_recording():
"""Stop the current recording session"""
return handle_stop_recording()
@app.get("/recording-status")
def recording_status():
"""Get the current recording status"""
return handle_recording_status()
@app.post("/recording-exit-early")
def recording_exit_early():
"""Skip to next episode (replaces right arrow key)"""
return handle_exit_early()
@app.post("/recording-rerecord-episode")
def recording_rerecord_episode():
"""Re-record current episode (replaces left arrow key)"""
return handle_rerecord_episode()
@app.post("/upload-dataset")
def upload_dataset(request: UploadRequest):
"""Upload dataset to HuggingFace Hub"""
return handle_upload_dataset(request)
@app.post("/dataset-info")
def get_dataset_info(request: DatasetInfoRequest):
"""Get information about a saved dataset"""
return handle_get_dataset_info(request)
@app.post("/delete-dataset")
def delete_dataset(request: DatasetInfoRequest):
"""Remove a recorded dataset directory from local disk."""
return handle_delete_dataset(request)
# ============================================================================
# JOB ENDPOINTS
# ============================================================================
@app.post("/jobs/training", status_code=201)
async def create_training_job(req: Request):
raw = await req.json()
body = StartTrainingBody.from_legacy(raw)
try:
record = job_registry.start(body.config, body.target)
except JobAlreadyRunningError as exc:
raise HTTPException(status_code=409, detail=f"Job already running: {exc}") from exc
except ValueError as exc:
# e.g. "flavor is required when runner is hf_cloud"
raise HTTPException(status_code=400, detail=str(exc)) from exc
return record
class ImportModelRequest(BaseModel):
source: str
name: str | None = None
@app.post("/jobs/import", status_code=201)
def import_model(body: ImportModelRequest):
"""Register an external model (local dir or HF repo) as a pseudo-job."""
try:
return job_registry.register_imported(body.source, body.name)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
@app.get("/jobs")
def list_jobs(limit: int = 10):
return {"jobs": job_registry.list(limit=limit)}
@app.get("/jobs/hub")
def list_hub_jobs():
"""List the user's HF Cloud compute Jobs and their uploaded LeRobot model
repos on huggingface.co.
Returns 200 with empty lists when no token is configured so the frontend
can render an unauthenticated empty state without surfacing an error.
Declared before `/jobs/{job_id}` so FastAPI's first-match routing doesn't
treat "hub" as a job id.
"""
info = cached_whoami()
if info is None:
return {"authenticated": False, "jobs": [], "models": []}
api = shared_hf_api()
authors: list[str] = []
if info.get("name"):
authors.append(info["name"])
for o in info.get("orgs", []) or []:
if isinstance(o, dict) and o.get("name"):
authors.append(o["name"])
try:
jobs = api.list_jobs()
except Exception as exc:
logger.warning("list_jobs failed: %s", exc)
jobs = []
seen_models: set[str] = set()
models: list[dict] = []
for author in authors:
try:
for m in api.list_models(author=author, filter="LeRobot", limit=200):
if m.id in seen_models:
continue
seen_models.add(m.id)
models.append(
{
"repo_id": m.id,
"last_modified": m.last_modified.isoformat() if m.last_modified else None,
"private": bool(getattr(m, "private", False)),
}
)
except Exception as exc:
logger.warning("list_models(%s) failed: %s", author, exc)
models.sort(key=lambda m: m["last_modified"] or "", reverse=True)
return {
"authenticated": True,
"jobs": [
{
"id": ji.id,
"created_at": ji.created_at.isoformat() if ji.created_at else None,
"docker_image": ji.docker_image,
"space_id": ji.space_id,
"flavor": ji.flavor,
"status": ({"stage": ji.status.stage, "message": ji.status.message} if ji.status else None),
"owner": ji.owner.name if ji.owner else None,
"url": ji.url,
}
for ji in jobs
],
"models": models,
}
@app.get("/jobs/{job_id}")
def get_job(job_id: str):
try:
return job_registry.get(job_id)
except JobNotFoundError as exc:
raise HTTPException(status_code=404, detail=f"Job {job_id!r} not found") from exc
@app.get("/jobs/{job_id}/logs")
def get_job_logs(job_id: str):
try:
logs = job_registry.drain_logs(job_id)
except JobNotFoundError as exc:
raise HTTPException(status_code=404, detail=f"Job {job_id!r} not found") from exc
return {"logs": logs}
@app.get("/jobs/{job_id}/log-file")
def get_job_log_file(job_id: str):
"""Return the entire on-disk log file for a job. Drains the live queue too
so the next /logs poll returns only lines that arrived after this call."""
try:
logs = job_registry.read_persisted_logs(job_id)
except JobNotFoundError as exc:
raise HTTPException(status_code=404, detail=f"Job {job_id!r} not found") from exc
# Best-effort drain so the frontend doesn't double-display.
with contextlib.suppress(JobNotFoundError):
job_registry.drain_logs(job_id)
return {"logs": logs}
@app.get("/jobs/{job_id}/metrics-history")
def get_job_metrics_history(job_id: str):
"""Return the per-step loss/lr/grad-norm series reconstructed from the
job's log.jsonl. Used to seed the monitoring charts so curves persist
across page reloads, navigation, and lelab restarts."""
try:
points = job_registry.read_metrics_history(job_id)
except JobNotFoundError as exc:
raise HTTPException(status_code=404, detail=f"Job {job_id!r} not found") from exc
return {"points": points}
@app.get("/jobs/{job_id}/checkpoints")
def get_job_checkpoints(job_id: str):
"""List the checkpoints saved for this job, ascending by step."""
try:
return {"checkpoints": job_registry.list_checkpoints(job_id)}
except JobNotFoundError as exc:
raise HTTPException(status_code=404, detail=f"Job {job_id!r} not found") from exc
@app.get("/jobs/{job_id}/checkpoints/{step}/policy-config")
def get_checkpoint_policy_config(job_id: str, step: int):
"""Return the UX-relevant slice of a checkpoint's pretrained_model config:
policy_type, image_features (per-camera height/width), and requires_task."""
try:
return job_registry.get_policy_config_summary(job_id, step)
except JobNotFoundError as exc:
raise HTTPException(status_code=404, detail=f"Job {job_id!r} not found") from exc
except FileNotFoundError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
@app.post("/jobs/{job_id}/stop")
def stop_job(job_id: str):
try:
return job_registry.stop(job_id)
except JobNotFoundError as exc:
raise HTTPException(status_code=404, detail=f"Job {job_id!r} not found") from exc
except JobNotRunningError as exc:
raise HTTPException(status_code=409, detail=f"Job {job_id!r} is not running") from exc
@app.delete("/jobs/{job_id}", status_code=204)
def delete_job(job_id: str):
try:
job_registry.delete(job_id)
except JobNotFoundError as exc:
raise HTTPException(status_code=404, detail=f"Job {job_id!r} not found") from exc
except JobNotRunningError as exc:
raise HTTPException(status_code=409, detail=f"Job {job_id!r} is running; stop it first") from exc
@app.get("/jobs/runners/hardware")
def get_runners_hardware():
"""Return HF Jobs flavor catalog + auth state for the TargetCard.
Both the flavors list and the whoami result are cached in-process to
keep this endpoint cheap (it can be re-fetched whenever auth state
changes). The whoami cache is invalidated on login.
"""
info = cached_whoami()
if info is None or not info.get("name"):
return {"authenticated": False, "username": None, "flavors": []}
username: str = info["name"]
api = shared_hf_api()
now = time.time()
if _flavors_cache["data"] is None or now - _flavors_cache["fetched_at"] > _FLAVOR_CACHE_TTL_SECONDS:
try:
hw_list = api.list_jobs_hardware()
except Exception as exc:
logger.warning("list_jobs_hardware failed: %s", exc)
return {"authenticated": True, "username": username, "flavors": []}
_flavors_cache["data"] = [
{
"name": h.name,
"pretty_name": h.pretty_name,
"cpu": h.cpu,
"ram": h.ram,
"accelerator": h.accelerator,
"unit_cost_usd": h.unit_cost_usd,
"unit_label": h.unit_label,
}
for h in hw_list
]
_flavors_cache["fetched_at"] = now
return {
"authenticated": True,
"username": username,
"flavors": _flavors_cache["data"],
}
# ============================================================================
# SYSTEM ENDPOINTS
# ============================================================================
@app.get("/system/cuda-status")
def get_cuda_status():
"""Report whether an NVIDIA GPU is present but PyTorch is CPU-only (issue #30)."""
return handle_get_cuda_status()
@app.get("/system/training-extra")
def get_training_extra():
"""Return whether the LeRobot training extra (accelerate) is importable."""
return handle_get_training_extra()
@app.post("/system/training-extra/install")
def install_training_extra():
"""Spawn `pip install accelerate` as a background subprocess. No-op if already running."""
return handle_install_training_extra()
@app.get("/system/training-extra/install-status")
def install_training_extra_status():
"""Return current install state plus any pending log lines (drained on read)."""
return handle_install_training_extra_status()
@app.get("/system/wandb-extra")
def get_wandb_extra():
"""Return whether the `wandb` package is importable in this lelab process."""
return handle_get_wandb_extra()
@app.post("/system/wandb-extra/install")
def install_wandb_extra():
"""Spawn `pip install wandb` as a background subprocess. No-op if already running."""
return handle_install_wandb_extra()
@app.get("/system/wandb-extra/install-status")
def install_wandb_extra_status():
"""Return current wandb install state plus any pending log lines (drained on read)."""
return handle_install_wandb_extra_status()
@app.get("/system/policy-extra/{policy_type}")
def get_policy_extra(policy_type: str):
"""Whether the optional LeRobot extra a policy needs (e.g. transformers for
smolvla/pi0, diffusers for diffusion) is importable. Core policies report available."""
return handle_get_policy_extra(policy_type)
@app.post("/system/policy-extra/{policy_type}/install")
def install_policy_extra(policy_type: str):
"""Spawn `pip install lerobot[<extra>]` for the policy's extra in the background."""
return handle_install_policy_extra(policy_type)
@app.get("/system/policy-extra/{policy_type}/install-status")
def install_policy_extra_status(policy_type: str):
"""Return the policy extra's install state plus any pending log lines (drained on read)."""
return handle_install_policy_extra_status(policy_type)
@app.get("/system/update-check")
def update_check():
"""Report whether a newer LeLab commit exists on GitHub (cached, silent on failure)."""
return handle_update_check()
@app.post("/system/update")
def run_update():
"""Run the pip upgrade in-process; the user must restart lelab afterwards."""
return handle_run_update()
# Replay is rendered by the embedded lerobot/visualize_dataset Space; no backend routes needed.
# ============================================================================
# Calibration endpoints
@app.post("/start-calibration")
def start_calibration(request: CalibrationRequest):
"""Start calibration process"""
return calibration_manager.start_calibration(request)
@app.post("/stop-calibration")
def stop_calibration():
"""Stop calibration process"""
return calibration_manager.stop_calibration_process()
@app.get("/calibration-status")
def calibration_status():
"""Get current calibration status"""
from dataclasses import asdict
status = calibration_manager.get_status()
return asdict(status)
@app.post("/complete-calibration-step")
def complete_calibration_step():
"""Complete the current calibration step"""
return calibration_manager.complete_step()
@app.get("/calibration-configs/{device_type}")
def get_calibration_configs(device_type: str):
"""Get all calibration config files for a specific device type"""
try:
if device_type == "robot":
config_path = FOLLOWER_CONFIG_PATH
elif device_type == "teleop":
config_path = LEADER_CONFIG_PATH
else:
return {"success": False, "message": "Invalid device type"}
# Get all JSON files in the config directory
configs = []
if os.path.exists(config_path):
for file in os.listdir(config_path):
if file.endswith(".json"):
config_name = os.path.splitext(file)[0]
file_path = os.path.join(config_path, file)
file_size = os.path.getsize(file_path)
modified_time = os.path.getmtime(file_path)
configs.append(
{
"name": config_name,
"filename": file,
"size": file_size,
"modified": modified_time,
}
)
return {"success": True, "configs": configs, "device_type": device_type}
except Exception as e:
logger.error(f"Error getting calibration configs: {e}")
return {"success": False, "message": str(e)}
@app.delete("/calibration-configs/{device_type}/{config_name}")
def delete_calibration_config(device_type: str, config_name: str):
"""Delete a calibration config file"""
try:
if device_type == "robot":
config_path = FOLLOWER_CONFIG_PATH
elif device_type == "teleop":
config_path = LEADER_CONFIG_PATH
else:
return {"success": False, "message": "Invalid device type"}
# config_name is interpolated into a filename, so reject path-traversal
# characters (/, \, ..) before touching the filesystem. Defense-in-depth:
# FastAPI path params already block a literal "/", but not "\" or "..".
# Reuses the same guard already applied to robot-record deletes.
if not is_valid_robot_name(config_name):
return {"success": False, "message": "Invalid configuration name"}
# Construct the file path
filename = f"{config_name}.json"
file_path = os.path.join(config_path, filename)
# Check if file exists
if not os.path.exists(file_path):
return {"success": False, "message": "Configuration file not found"}
# Delete the file
os.remove(file_path)
logger.info(f"Deleted calibration config: {file_path}")
return {
"success": True,
"message": f"Configuration '{config_name}' deleted successfully",
}
except Exception as e:
logger.error(f"Error deleting calibration config: {e}")
return {"success": False, "message": str(e)}
# ============================================================================
# PORT DETECTION ENDPOINTS
# ============================================================================
@app.get("/available-ports")
def get_available_ports():
"""Get all available serial ports"""
try:
ports = find_available_ports()
return {"status": "success", "ports": ports}
except Exception as e:
logger.error(f"Error getting available ports: {e}")
return {"status": "error", "message": str(e)}
# Runs in a fresh Python — see _avfoundation_cameras_in_cv2_order for why.
# Mirrors OpenCV's macOS enumeration: video + muxed devices sorted by
# uniqueID (cap_avfoundation_mac.mm), so the returned index matches what
# cv2.VideoCapture will open.
_AVF_ENUM_SCRIPT = """
import json, objc
from Foundation import NSBundle
bundle = NSBundle.bundleWithPath_("/System/Library/Frameworks/AVFoundation.framework")
bundle.load()
types = []
for name in (
"AVCaptureDeviceTypeBuiltInWideAngleCamera",
"AVCaptureDeviceTypeExternalUnknown", # macOS < 14
"AVCaptureDeviceTypeExternal", # macOS >= 14
"AVCaptureDeviceTypeContinuityCamera", # macOS >= 14
"AVCaptureDeviceTypeDeskViewCamera", # macOS >= 13
):
loaded = {}
try:
objc.loadBundleVariables(bundle, loaded, [(name, b"@")])
except objc.error:
continue
if loaded.get(name) is not None:
types.append(loaded[name])
cls = objc.lookUpClass("AVCaptureDeviceDiscoverySession")
devs = []
for mt in ("vide", "muxx"):
devs.extend(cls.discoverySessionWithDeviceTypes_mediaType_position_(types, mt, 0).devices() or [])
devs.sort(key=lambda d: d.uniqueID())
print(json.dumps([
{"index": i, "name": str(d.localizedName()), "unique_id": str(d.uniqueID())}
for i, d in enumerate(devs)
]))
"""
def _avfoundation_cameras_in_cv2_order() -> list[dict[str, Any]]:
"""Enumerate macOS cameras in a fresh Python subprocess.
AVFoundation's in-process device cache doesn't refresh on USB
hotplug. Both the deprecated ``+devicesWithMediaType:`` and a
long-lived ``AVCaptureDeviceDiscoverySession`` go stale, because
device-connection notifications are delivered via
``NSNotificationCenter`` on a thread that needs an active
``NSRunLoop`` — uvicorn workers don't run one. A fresh subprocess
re-initializes AVFoundation, which reads IOKit's live device state
at startup.
"""
try:
result = subprocess.run(
[sys.executable, "-c", _AVF_ENUM_SCRIPT],
capture_output=True,
text=True,
timeout=10,
check=True,
)
except (subprocess.SubprocessError, OSError) as e:
logger.warning("AVFoundation enumeration subprocess failed: %s", e)
return []
try:
return json.loads(result.stdout)
except json.JSONDecodeError as e:
logger.warning("AVFoundation enumeration returned invalid JSON: %s", e)
return []
def _generic_cv2_cameras(backend) -> list[dict[str, Any]]:
"""Last-resort enumeration: probe cv2 indices with placeholder names."""
import cv2
cameras: list[dict[str, Any]] = []
for i in range(10):
cap = cv2.VideoCapture(i, backend)
opened = cap.isOpened()
cap.release()
if opened:
cameras.append({"index": i, "name": f"Camera {i}", "available": True})
return cameras
def _windows_cameras() -> list[dict[str, Any]]:
"""Enumerate Windows cameras with their real DirectShow names.
pygrabber lists DirectShow video devices in the same order cv2's DSHOW
backend indexes them (which recording is pinned to), so the returned index
matches what ``cv2.VideoCapture(i, CAP_DSHOW)`` opens. The real names let the
frontend match each index to the browser's ``MediaDeviceInfo.label`` for the
live preview. Falls back to generic names if pygrabber is unavailable.
"""