|
| 1 | +import argparse |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import psutil |
| 5 | + |
| 6 | + |
| 7 | +def serve(args): |
| 8 | + """ |
| 9 | + Execute the serve command |
| 10 | + """ |
| 11 | + |
| 12 | + # Check if Lemonade Server is already running |
| 13 | + running_on_port = get_server_port() |
| 14 | + if running_on_port is not None: |
| 15 | + print( |
| 16 | + ( |
| 17 | + f"Lemonade Server is already running on port {running_on_port}\n" |
| 18 | + "Please stop the existing server before starting a new instance." |
| 19 | + ), |
| 20 | + ) |
| 21 | + return |
| 22 | + |
| 23 | + # Otherwise, start the server |
| 24 | + print("Starting Lemonade Server...") |
| 25 | + from lemonade.tools.serve import Server |
| 26 | + |
| 27 | + server = Server() |
| 28 | + server.run() |
| 29 | + |
| 30 | + |
| 31 | +def version(): |
| 32 | + """ |
| 33 | + Print the version number |
| 34 | + """ |
| 35 | + from turnkeyml import __version__ as version_number |
| 36 | + |
| 37 | + print(f"Lemonade Server version is {version_number}") |
| 38 | + |
| 39 | + |
| 40 | +def status(): |
| 41 | + """ |
| 42 | + Print the status of the server |
| 43 | + """ |
| 44 | + port = get_server_port() |
| 45 | + if port is None: |
| 46 | + print("Server is not running") |
| 47 | + else: |
| 48 | + print(f"Server is running on port {port}") |
| 49 | + |
| 50 | + |
| 51 | +def is_lemonade_server(pid): |
| 52 | + """ |
| 53 | + Check wether or not a given PID corresponds to a Lemonade server |
| 54 | + """ |
| 55 | + try: |
| 56 | + process = psutil.Process(pid) |
| 57 | + while True: |
| 58 | + if process.name() in [ # Windows |
| 59 | + "lemonade-server-dev.exe", |
| 60 | + "lemonade-server.exe", |
| 61 | + "lemonade.exe", |
| 62 | + ] or process.name() in [ # Linux |
| 63 | + "lemonade-server-dev", |
| 64 | + "lemonade-server", |
| 65 | + "lemonade", |
| 66 | + ]: |
| 67 | + return True |
| 68 | + if not process.parent(): |
| 69 | + return False |
| 70 | + process = process.parent() |
| 71 | + except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): |
| 72 | + return False |
| 73 | + return False |
| 74 | + |
| 75 | + |
| 76 | +def get_server_port() -> int | None: |
| 77 | + """ |
| 78 | + Get the port that Lemonade Server is running on |
| 79 | + """ |
| 80 | + # Go over all python processes that have a port open |
| 81 | + for process in psutil.process_iter(["pid", "name"]): |
| 82 | + try: |
| 83 | + connections = process.net_connections() |
| 84 | + for conn in connections: |
| 85 | + if conn.status == "LISTEN": |
| 86 | + if is_lemonade_server(process.info["pid"]): |
| 87 | + return conn.laddr.port |
| 88 | + except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): |
| 89 | + continue |
| 90 | + |
| 91 | + return None |
| 92 | + |
| 93 | + |
| 94 | +def main(): |
| 95 | + parser = argparse.ArgumentParser( |
| 96 | + description="Serve LLMs on CPU, GPU, and NPU.", |
| 97 | + usage=argparse.SUPPRESS, |
| 98 | + ) |
| 99 | + |
| 100 | + # Add version flag |
| 101 | + parser.add_argument( |
| 102 | + "-v", "--version", action="store_true", help="Show version number" |
| 103 | + ) |
| 104 | + |
| 105 | + # Create subparsers for commands |
| 106 | + subparsers = parser.add_subparsers( |
| 107 | + title="Available Commands", dest="command", metavar="" |
| 108 | + ) |
| 109 | + |
| 110 | + # Serve commands |
| 111 | + serve_parser = subparsers.add_parser("serve", help="Start server") |
| 112 | + status_parser = subparsers.add_parser("status", help="Check if server is running") |
| 113 | + |
| 114 | + args = parser.parse_args() |
| 115 | + |
| 116 | + if args.version: |
| 117 | + version() |
| 118 | + elif args.command == "serve": |
| 119 | + serve(args) |
| 120 | + elif args.command == "status": |
| 121 | + status() |
| 122 | + elif args.command == "help" or not args.command: |
| 123 | + parser.print_help() |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + main() |
0 commit comments