File size: 2,860 Bytes
7858a1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Main entry point for the LoRASA agent server.
"""

import argparse
import os
import sys
from pathlib import Path

import torch

from agent import LoRASAAgent, RLAgent
from agent_server import start_server


def main():
    """Main entry point."""
    parser = argparse.ArgumentParser(description="Start the LoRASA agent server")
    parser.add_argument(
        "--host", type=str, default="0.0.0.0", help="Host to bind the server to"
    )
    parser.add_argument(
        "--port", type=int, default=8000, help="Port to bind the server to"
    )
    parser.add_argument(
        "--backbone-path",
        type=str,
        default=None,
        help="Path to backbone checkpoint (default: checkpoints/lorasa_shared_backbone.pt)",
    )
    parser.add_argument(
        "--adapter-dir",
        type=str,
        default=None,
        help="Path to adapter directory (default: checkpoints)",
    )
    parser.add_argument(
        "--device",
        type=str,
        default=None,
        help="Device to use (cuda/cpu, default: auto-detect)",
    )

    args = parser.parse_args()

    # Determine device
    if args.device is None:
        device = "cuda" if torch.cuda.is_available() else "cpu"
    else:
        device = args.device

    # Determine paths (use local checkpoints directory in agent folder)
    agent_dir = Path(__file__).parent
    
    if args.backbone_path is None:
        backbone_path = agent_dir / "checkpoints" / "lorasa_shared_backbone.pt"
    else:
        backbone_path = Path(args.backbone_path)

    if args.adapter_dir is None:
        adapter_dir = agent_dir / "checkpoints"
    else:
        adapter_dir = Path(args.adapter_dir)

    if not backbone_path.exists():
        print(f"Error: Backbone file not found: {backbone_path}")
        sys.exit(1)

    if not adapter_dir.exists():
        print(f"Error: Adapter directory not found: {adapter_dir}")
        sys.exit(1)

    # Create the LoRASA Agent
    try:
        agent = LoRASAAgent(
            backbone_path=str(backbone_path),
            adapter_dir=str(adapter_dir),
            device=device,
        )
        print(f"✓ LoRASA Agent initialized")
        print(f"  - Backbone: {backbone_path}")
        print(f"  - Adapter dir: {adapter_dir}")
        print(f"  - Device: {device}")
    except Exception as e:
        print(f"Error initializing agent: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)

    # Start the server
    print(f"Starting server on {args.host}:{args.port}...")
    try:
        start_server(agent, args.host, args.port)
    except KeyboardInterrupt:
        print("\nServer stopped by user")
    except Exception as e:
        print(f"Server error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    main()