-
-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathcli.py
More file actions
567 lines (451 loc) · 17 KB
/
Copy pathcli.py
File metadata and controls
567 lines (451 loc) · 17 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
"""Unified CLI for OpenAdapt ecosystem.
Usage:
openadapt capture start --name my-task
openadapt capture stop
openadapt capture list
openadapt capture view <name>
openadapt train --capture my-task --model qwen3vl-2b
openadapt train status
openadapt train stop
openadapt eval --checkpoint model.pt --benchmark waa
openadapt eval --agent api-claude --benchmark waa
openadapt serve --port 8080
openadapt version
openadapt doctor
"""
import sys
from typing import Optional
import click
@click.group()
@click.version_option(version="1.0.0", prog_name="openadapt")
def main():
"""OpenAdapt - GUI automation with ML.
Record demonstrations, train models, and evaluate agents.
\b
Quick Start:
openadapt capture start --name my-task # Record a demonstration
openadapt train --capture my-task # Train a model
openadapt eval --checkpoint model.pt # Evaluate the model
"""
pass
# =============================================================================
# Capture Commands
# =============================================================================
@main.group()
def capture():
"""Record GUI demonstrations.
\b
Examples:
openadapt capture start --name login-flow
openadapt capture stop
openadapt capture list
openadapt capture view login-flow
"""
pass
@capture.command("start")
@click.option("--name", "-n", required=True, help="Name for the capture session")
@click.option("--video/--no-video", default=True, help="Record video")
@click.option("--audio/--no-audio", default=False, help="Record audio")
def capture_start(name: str, video: bool, audio: bool):
"""Start a new capture session."""
try:
from openadapt_capture import Recorder
click.echo(f"Starting capture session: {name}")
click.echo("Press Ctrl+C (or Ctrl x3) to stop recording...")
with Recorder(
f"./{name}",
task_description=name,
capture_video=video,
capture_audio=audio,
) as recorder:
recorder.wait_for_ready()
click.echo("Recording...")
try:
while recorder.is_recording:
import time
time.sleep(1)
except KeyboardInterrupt:
pass
click.echo(f"\nCapture saved: ./{name}/ ({recorder.event_count} events)")
except ImportError:
click.echo("Error: openadapt-capture not installed.", err=True)
click.echo("Install with: pip install openadapt-capture", err=True)
sys.exit(1)
@capture.command("stop")
def capture_stop():
"""Stop the current capture session."""
click.echo("Stopping active capture session...")
# TODO: Implement stop via signal/file
click.echo("Note: Use Ctrl+C in the capture terminal to stop")
@capture.command("list")
@click.option("--path", "-p", default=".", help="Path to search for captures")
def capture_list(path: str):
"""List available captures."""
try:
from pathlib import Path
from openadapt_capture import Capture
captures_found = 0
for capture_dir in sorted(Path(path).iterdir()):
if capture_dir.is_dir() and (capture_dir / "recording.db").exists():
try:
cap = Capture.load(str(capture_dir))
desc = cap.task_description or ""
n_actions = sum(1 for _ in cap.actions())
cap.close()
click.echo(f" {capture_dir.name} ({n_actions} actions) {desc}")
captures_found += 1
except Exception:
continue
if captures_found == 0:
click.echo("No captures found.")
else:
click.echo(f"\nTotal: {captures_found} capture(s)")
except ImportError:
click.echo("Error: openadapt-capture not installed.", err=True)
sys.exit(1)
@capture.command("view")
@click.argument("name")
@click.option("--open/--no-open", default=True, help="Open in browser")
def capture_view(name: str, open: bool):
"""View a capture recording."""
try:
from pathlib import Path
from openadapt_capture import create_html
capture_path = Path(name)
if not capture_path.exists():
click.echo(f"Error: Capture not found: {name}", err=True)
sys.exit(1)
output_path = capture_path / "viewer.html"
create_html(str(capture_path), str(output_path))
click.echo(f"Viewer generated: {output_path}")
if open:
import webbrowser
webbrowser.open(f"file://{output_path.absolute()}")
except ImportError:
click.echo("Error: openadapt-capture not installed.", err=True)
sys.exit(1)
# =============================================================================
# Train Commands
# =============================================================================
@main.group()
def train():
"""Train ML models on captured demonstrations.
\b
Examples:
openadapt train start --capture login-flow --model qwen3vl-2b
openadapt train status
openadapt train stop
"""
pass
@train.command("start")
@click.option("--capture", "-c", required=True, help="Path to capture directory")
@click.option("--model", "-m", default="qwen3vl-2b", help="Model to train")
@click.option("--config", help="Path to training config YAML")
@click.option("--output", "-o", default="training_output", help="Output directory")
@click.option("--open/--no-open", default=True, help="Open dashboard in browser")
def train_start(
capture: str, model: str, config: Optional[str], output: str, open: bool
):
"""Start model training."""
try:
click.echo("Starting training...")
click.echo(f" Capture: {capture}")
click.echo(f" Output: {output}")
# Import and run training. The model is determined by the config
# file; --model is kept for backward compatibility only.
from openadapt_ml.scripts.train import main as train_main
if not config:
from openadapt_ml.cloud.local import detect_device
if "cuda" in detect_device():
config = "configs/qwen3vl_capture.yaml"
else:
config = "configs/qwen3vl_capture_4bit.yaml"
try:
from openadapt_ml.cloud.local import resolve_config_path
config = str(resolve_config_path(config))
except ImportError:
# Older openadapt-ml without resolve_config_path; use as-is.
pass
from pathlib import Path
if not Path(config).exists():
click.echo(f"Error: config not found: {config}", err=True)
click.echo(
"Upgrade openadapt-ml so bundled configs resolve, or pass "
"--config with a path to a training config YAML.",
err=True,
)
sys.exit(1)
click.echo(f" Config: {config}")
train_main(
config_path=config,
capture_path=capture,
output_dir=output,
open_dashboard=open,
)
except ImportError as e:
click.echo(f"Error: failed to import openadapt-ml ({e}).", err=True)
click.echo('Install with: pip install "openadapt-ml[training]"', err=True)
sys.exit(1)
@train.command("status")
@click.option("--output", "-o", default="training_output", help="Output directory")
def train_status(output: str):
"""Check training status."""
try:
import json
from pathlib import Path
log_path = Path(output) / "training_log.json"
if not log_path.exists():
click.echo("No active training found.")
return
with open(log_path) as f:
log = json.load(f)
status = log.get("status", "unknown")
epoch = log.get("epoch", 0)
loss = log.get("loss", 0)
elapsed = log.get("elapsed_time", 0)
click.echo(f"Status: {status}")
click.echo(f"Epoch: {epoch}")
click.echo(f"Loss: {loss:.4f}")
click.echo(f"Elapsed: {elapsed / 60:.1f} minutes")
except Exception as e:
click.echo(f"Error reading training status: {e}", err=True)
sys.exit(1)
@train.command("stop")
@click.option("--output", "-o", default="training_output", help="Output directory")
def train_stop(output: str):
"""Stop active training."""
try:
from pathlib import Path
stop_file = Path(output) / "STOP_TRAINING"
stop_file.touch()
click.echo("Stop signal sent. Training will stop after current step.")
except Exception as e:
click.echo(f"Error: {e}", err=True)
sys.exit(1)
# =============================================================================
# Eval Commands
# =============================================================================
@main.group()
def eval():
"""Evaluate models on benchmarks.
\b
Examples:
openadapt eval run --checkpoint model.pt --benchmark waa
openadapt eval run --agent api-claude --benchmark waa
openadapt eval mock --tasks 10
"""
pass
@eval.command("run")
@click.option("--checkpoint", "-c", help="Path to model checkpoint")
@click.option(
"--agent",
"-a",
type=click.Choice(["api-claude", "api-openai"]),
help="API agent to use",
)
@click.option("--benchmark", "-b", default="waa", help="Benchmark name")
@click.option("--tasks", "-t", default=10, help="Number of tasks")
@click.option("--server", "-s", help="WAA server URL for live eval")
@click.option("--demo", help="Demo file for agent")
def eval_run(
checkpoint: Optional[str],
agent: Optional[str],
benchmark: str,
tasks: int,
server: Optional[str],
demo: Optional[str],
):
"""Run benchmark evaluation."""
try:
from openadapt_evals import (
ApiAgent,
WAALiveAdapter,
WAAMockAdapter,
compute_metrics,
evaluate_agent_on_benchmark,
)
# Create agent
if checkpoint:
click.echo(f"Loading model from: {checkpoint}")
from openadapt_evals import PolicyAgent
eval_agent = PolicyAgent(checkpoint_path=checkpoint)
elif agent:
provider = "anthropic" if "claude" in agent else "openai"
click.echo(f"Using API agent: {provider}")
demo_text = None
if demo:
with open(demo) as f:
demo_text = f.read()
eval_agent = ApiAgent(provider=provider, demo=demo_text)
else:
click.echo("Error: Specify --checkpoint or --agent", err=True)
sys.exit(1)
# Create adapter
if server:
click.echo(f"Connecting to: {server}")
adapter = WAALiveAdapter(server_url=server)
else:
click.echo(f"Using mock adapter with {tasks} tasks")
adapter = WAAMockAdapter(num_tasks=tasks)
# Run evaluation
click.echo("Running evaluation...")
results = evaluate_agent_on_benchmark(eval_agent, adapter, max_steps=15)
# Compute and display metrics
metrics = compute_metrics(results)
click.echo("\nResults:")
click.echo(f" Success rate: {metrics['success_rate']:.1%}")
click.echo(f" Avg steps: {metrics['avg_steps']:.1f}")
click.echo(f" Total tasks: {metrics['total_tasks']}")
except ImportError as e:
click.echo(f"Error: Missing dependency: {e}", err=True)
sys.exit(1)
@eval.command("mock")
@click.option("--tasks", "-t", default=10, help="Number of mock tasks")
@click.option("--output", "-o", default="benchmark_results", help="Output directory")
def eval_mock(tasks: int, output: str):
"""Run mock evaluation for testing."""
try:
from openadapt_evals import (
SmartMockAgent,
WAAMockAdapter,
compute_metrics,
evaluate_agent_on_benchmark,
)
click.echo(f"Running mock evaluation with {tasks} tasks...")
agent = SmartMockAgent()
adapter = WAAMockAdapter(num_tasks=tasks)
results = evaluate_agent_on_benchmark(agent, adapter, max_steps=15)
metrics = compute_metrics(results)
click.echo("\nResults:")
click.echo(f" Success rate: {metrics['success_rate']:.1%}")
click.echo(f" Avg steps: {metrics['avg_steps']:.1f}")
except ImportError:
click.echo("Error: openadapt-evals not installed.", err=True)
sys.exit(1)
# =============================================================================
# Serve Command
# =============================================================================
@main.command()
@click.option("--port", "-p", default=8080, help="Port to serve on")
@click.option("--output", "-o", default="training_output", help="Output directory")
@click.option("--open/--no-open", default=True, help="Open in browser")
def serve(port: int, output: str, open: bool):
"""Serve the training dashboard and viewer.
\b
Examples:
openadapt serve --port 8080
openadapt serve --output training_output --open
"""
try:
click.echo(f"Starting server on port {port}...")
click.echo(f"Serving from: {output}")
import argparse
from pathlib import Path
from openadapt_ml.cloud import local as oa_local
# cmd_serve resolves the 'current' run against the module-level
# TRAINING_OUTPUT constant; point it at the requested directory
# so --output is honored.
oa_local.TRAINING_OUTPUT = Path(output)
if open:
import threading
import webbrowser
threading.Timer(
1.0, webbrowser.open, args=(f"http://localhost:{port}",)
).start()
sys.exit(
oa_local.cmd_serve(
argparse.Namespace(
port=port,
benchmark=None,
no_regenerate=False,
start_page=None,
quiet=False,
)
)
)
except ImportError as e:
click.echo(f"Error: failed to import openadapt-ml ({e}).", err=True)
click.echo('Install with: pip install "openadapt-ml[training]"', err=True)
sys.exit(1)
# =============================================================================
# Utility Commands
# =============================================================================
@main.command()
def version():
"""Show version information for all packages."""
click.echo("OpenAdapt Ecosystem Versions:")
click.echo("=" * 40)
packages = [
("openadapt", "openadapt"),
("openadapt-capture", "openadapt_capture"),
("openadapt-ml", "openadapt_ml"),
("openadapt-evals", "openadapt_evals"),
("openadapt-viewer", "openadapt_viewer"),
("openadapt-grounding", "openadapt_grounding"),
("openadapt-retrieval", "openadapt_retrieval"),
]
for name, module in packages:
try:
mod = __import__(module)
ver = getattr(mod, "__version__", "unknown")
click.echo(f" {name}: {ver}")
except ImportError:
click.echo(f" {name}: not installed")
@main.command()
def doctor():
"""Check system requirements and dependencies."""
click.echo("OpenAdapt System Check")
click.echo("=" * 40)
# Check Python version
import platform
click.echo(f"\nPython: {platform.python_version()}")
click.echo(f"Platform: {platform.system()} {platform.release()}")
# Check required packages
click.echo("\nCore packages:")
required = [
"openadapt_capture",
"openadapt_ml",
"openadapt_evals",
"openadapt_viewer",
]
for pkg in required:
try:
__import__(pkg)
click.echo(f" [OK] {pkg}")
except ImportError:
click.echo(f" [MISSING] {pkg}")
# Check optional packages
click.echo("\nOptional packages:")
optional = [
"openadapt_grounding",
"openadapt_retrieval",
]
for pkg in optional:
try:
__import__(pkg)
click.echo(f" [OK] {pkg}")
except ImportError:
click.echo(f" [--] {pkg} (not installed)")
# Check GPU
click.echo("\nGPU:")
try:
import torch
if torch.cuda.is_available():
click.echo(f" [OK] CUDA available: {torch.cuda.get_device_name(0)}")
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
click.echo(" [OK] MPS available (Apple Silicon)")
else:
click.echo(" [--] No GPU detected (CPU mode)")
except ImportError:
click.echo(" [--] PyTorch not installed")
# Check API keys
click.echo("\nAPI Keys:")
import os
keys = ["ANTHROPIC_API_KEY", "OPENAI_API_KEY"]
for key in keys:
if os.environ.get(key):
click.echo(f" [OK] {key} is set")
else:
click.echo(f" [--] {key} not set")
if __name__ == "__main__":
main()