You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
247 lines
7.7 KiB
247 lines
7.7 KiB
5 months ago
|
"""
|
||
|
01 # Runs light server and light simulator
|
||
|
|
||
|
01 --server livekit # Runs livekit server only
|
||
|
01 --server light # Runs light server only
|
||
|
|
||
|
01 --client light-python
|
||
|
|
||
|
... --expose # Exposes the server with ngrok
|
||
|
... --expose --domain <domain> # Exposes the server on a specific ngrok domain
|
||
|
... --qr # Displays a qr code
|
||
|
"""
|
||
|
|
||
5 months ago
|
from yaspin import yaspin
|
||
|
spinner = yaspin()
|
||
|
spinner.start()
|
||
|
|
||
10 months ago
|
import typer
|
||
5 months ago
|
import ngrok
|
||
10 months ago
|
import platform
|
||
|
import threading
|
||
|
import os
|
||
|
import importlib
|
||
5 months ago
|
from source.server.server import start_server
|
||
6 months ago
|
import subprocess
|
||
5 months ago
|
import socket
|
||
|
import json
|
||
|
import segno
|
||
|
import time
|
||
5 months ago
|
from dotenv import load_dotenv
|
||
10 months ago
|
import signal
|
||
9 months ago
|
|
||
5 months ago
|
load_dotenv()
|
||
9 months ago
|
|
||
5 months ago
|
system_type = platform.system()
|
||
|
|
||
|
app = typer.Typer()
|
||
|
|
||
10 months ago
|
@app.command()
|
||
|
def run(
|
||
5 months ago
|
server: str = typer.Option(
|
||
|
None,
|
||
|
"--server",
|
||
|
help="Run server (accepts `livekit` or `light`)",
|
||
|
),
|
||
9 months ago
|
server_host: str = typer.Option(
|
||
|
"0.0.0.0",
|
||
|
"--server-host",
|
||
|
help="Specify the server host where the server will deploy",
|
||
|
),
|
||
|
server_port: int = typer.Option(
|
||
5 months ago
|
10101,
|
||
9 months ago
|
"--server-port",
|
||
|
help="Specify the server port where the server will deploy",
|
||
|
),
|
||
5 months ago
|
expose: bool = typer.Option(False, "--expose", help="Expose server over the internet"),
|
||
|
domain: str = typer.Option(None, "--domain", help="Use `--expose` with a custom ngrok domain"),
|
||
|
client: str = typer.Option(None, "--client", help="Run client of a particular type. Accepts `light-python`, defaults to `light-python`"),
|
||
9 months ago
|
server_url: str = typer.Option(
|
||
|
None,
|
||
|
"--server-url",
|
||
5 months ago
|
help="Specify the server URL that the --client should expect. Defaults to server-host and server-port",
|
||
9 months ago
|
),
|
||
7 months ago
|
qr: bool = typer.Option(
|
||
5 months ago
|
False, "--qr", help="Display QR code containing the server connection information (will be ngrok url if `--expose` is used)"
|
||
5 months ago
|
),
|
||
6 months ago
|
profiles: bool = typer.Option(
|
||
|
False,
|
||
|
"--profiles",
|
||
5 months ago
|
help="Opens the folder where profiles are contained",
|
||
6 months ago
|
),
|
||
|
profile: str = typer.Option(
|
||
5 months ago
|
"default.py",
|
||
6 months ago
|
"--profile",
|
||
|
help="Specify the path to the profile, or the name of the file if it's in the `profiles` directory (run `--profiles` to open the profiles directory)",
|
||
|
),
|
||
7 months ago
|
debug: bool = typer.Option(
|
||
|
False,
|
||
|
"--debug",
|
||
5 months ago
|
help="Print latency measurements and save microphone recordings locally for manual playback",
|
||
5 months ago
|
),
|
||
9 months ago
|
):
|
||
5 months ago
|
|
||
|
threads = []
|
||
|
|
||
|
# Handle `01` with no arguments, which should start server + client
|
||
|
if not server and not client:
|
||
|
server = "light"
|
||
|
client = "light-python"
|
||
|
|
||
|
### PROFILES
|
||
7 months ago
|
|
||
6 months ago
|
profiles_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "source", "server", "profiles")
|
||
|
|
||
|
if profiles:
|
||
|
if platform.system() == "Windows":
|
||
|
subprocess.Popen(['explorer', profiles_dir])
|
||
|
elif platform.system() == "Darwin":
|
||
|
subprocess.Popen(['open', profiles_dir])
|
||
|
elif platform.system() == "Linux":
|
||
|
subprocess.Popen(['xdg-open', profiles_dir])
|
||
|
else:
|
||
|
subprocess.Popen(['open', profiles_dir])
|
||
|
exit(0)
|
||
|
|
||
|
if profile:
|
||
|
if not os.path.isfile(profile):
|
||
|
profile = os.path.join(profiles_dir, profile)
|
||
|
if not os.path.isfile(profile):
|
||
|
profile += ".py"
|
||
|
if not os.path.isfile(profile):
|
||
|
print(f"Invalid profile path: {profile}")
|
||
|
exit(1)
|
||
|
|
||
5 months ago
|
|
||
|
### SERVER
|
||
|
|
||
9 months ago
|
if system_type == "Windows":
|
||
|
server_host = "localhost"
|
||
9 months ago
|
|
||
10 months ago
|
if not server_url:
|
||
|
server_url = f"{server_host}:{server_port}"
|
||
9 months ago
|
|
||
10 months ago
|
if server:
|
||
6 months ago
|
|
||
5 months ago
|
### LIGHT SERVER (required by livekit)
|
||
6 months ago
|
|
||
5 months ago
|
if server == "light":
|
||
|
light_server_port = server_port
|
||
5 months ago
|
voice = True # The light server will support voice
|
||
5 months ago
|
elif server == "livekit":
|
||
|
# The light server should run at a different port if we want to run a livekit server
|
||
5 months ago
|
spinner.stop()
|
||
5 months ago
|
print(f"Starting light server (required for livekit server) on the port before `--server-port` (port {server_port-1}), unless the `AN_OPEN_PORT` env var is set.")
|
||
|
print(f"The livekit server will be started on port {server_port}.")
|
||
|
light_server_port = os.getenv('AN_OPEN_PORT', server_port-1)
|
||
5 months ago
|
voice = False # The light server will NOT support voice. It will just run Open Interpreter. The Livekit server will handle voice
|
||
6 months ago
|
|
||
9 months ago
|
server_thread = threading.Thread(
|
||
6 months ago
|
target=start_server,
|
||
9 months ago
|
args=(
|
||
6 months ago
|
server_host,
|
||
5 months ago
|
light_server_port,
|
||
6 months ago
|
profile,
|
||
5 months ago
|
voice,
|
||
5 months ago
|
debug
|
||
9 months ago
|
),
|
||
|
)
|
||
5 months ago
|
spinner.stop()
|
||
|
print("Starting server...")
|
||
10 months ago
|
server_thread.start()
|
||
5 months ago
|
threads.append(server_thread)
|
||
10 months ago
|
|
||
5 months ago
|
if server == "livekit":
|
||
7 months ago
|
|
||
5 months ago
|
### LIVEKIT SERVER
|
||
6 months ago
|
|
||
5 months ago
|
def run_command(command):
|
||
|
subprocess.run(command, shell=True, check=True)
|
||
6 months ago
|
|
||
5 months ago
|
# Start the livekit server
|
||
|
livekit_thread = threading.Thread(
|
||
|
target=run_command, args=(f'livekit-server --dev --bind "{server_host}" --port {server_port}',)
|
||
|
)
|
||
|
time.sleep(7)
|
||
|
livekit_thread.start()
|
||
|
threads.append(livekit_thread)
|
||
|
|
||
|
# We communicate with the livekit worker via environment variables:
|
||
|
os.environ["INTERPRETER_SERVER_HOST"] = server_host
|
||
|
os.environ["INTERPRETER_LIGHT_SERVER_PORT"] = str(light_server_port)
|
||
|
os.environ["LIVEKIT_URL"] = f"ws://{server_host}:{server_port}"
|
||
|
|
||
|
# Start the livekit worker
|
||
|
worker_thread = threading.Thread(
|
||
5 months ago
|
target=run_command, args=("python source/server/livekit/worker.py dev",) # TODO: This should not be a CLI, it should just run the python file
|
||
5 months ago
|
)
|
||
|
time.sleep(7)
|
||
|
worker_thread.start()
|
||
|
threads.append(worker_thread)
|
||
10 months ago
|
|
||
5 months ago
|
if expose:
|
||
5 months ago
|
|
||
5 months ago
|
### EXPOSE OVER INTERNET
|
||
|
listener = ngrok.forward(f"{server_host}:{server_port}", authtoken_from_env=True, domain=domain)
|
||
|
url = listener.url()
|
||
5 months ago
|
|
||
5 months ago
|
else:
|
||
5 months ago
|
|
||
5 months ago
|
### GET LOCAL URL
|
||
5 months ago
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||
|
s.connect(("8.8.8.8", 80))
|
||
|
ip_address = s.getsockname()[0]
|
||
|
s.close()
|
||
5 months ago
|
url = f"http://{ip_address}:{server_port}"
|
||
|
|
||
|
|
||
|
if server == "livekit":
|
||
|
print("Livekit server will run at:", url)
|
||
|
|
||
|
|
||
|
### DISPLAY QR CODE
|
||
|
|
||
|
if qr:
|
||
|
time.sleep(7)
|
||
5 months ago
|
content = json.dumps({"livekit_server": url})
|
||
5 months ago
|
qr_code = segno.make(content)
|
||
|
qr_code.terminal(compact=True)
|
||
5 months ago
|
|
||
5 months ago
|
|
||
5 months ago
|
### CLIENT
|
||
|
|
||
|
if client:
|
||
|
|
||
|
module = importlib.import_module(
|
||
|
f".clients.{client}.client", package="source"
|
||
|
)
|
||
5 months ago
|
|
||
5 months ago
|
client_thread = threading.Thread(target=module.run, args=[server_url, debug])
|
||
5 months ago
|
spinner.stop()
|
||
|
print("Starting client...")
|
||
5 months ago
|
client_thread.start()
|
||
|
threads.append(client_thread)
|
||
5 months ago
|
|
||
|
|
||
5 months ago
|
### WAIT FOR THREADS TO FINISH, HANDLE CTRL-C
|
||
|
|
||
|
# Signal handler for termination signals
|
||
|
def signal_handler(sig, frame):
|
||
|
print("Termination signal received. Shutting down...")
|
||
5 months ago
|
for thread in threads:
|
||
5 months ago
|
if thread.is_alive():
|
||
|
# Kill subprocess associated with thread
|
||
|
subprocess.run(f"pkill -P {os.getpid()}", shell=True)
|
||
|
os._exit(0)
|
||
|
|
||
|
# Register signal handler for SIGINT and SIGTERM
|
||
|
signal.signal(signal.SIGINT, signal_handler)
|
||
|
signal.signal(signal.SIGTERM, signal_handler)
|
||
5 months ago
|
|
||
10 months ago
|
try:
|
||
5 months ago
|
# Wait for all threads to complete
|
||
|
for thread in threads:
|
||
|
thread.join()
|
||
10 months ago
|
except KeyboardInterrupt:
|
||
5 months ago
|
# On KeyboardInterrupt, send SIGINT to self
|
||
|
os.kill(os.getpid(), signal.SIGINT)
|