add meet flag and better error handling
This commit is contained in:
parent
f68f83c6ba
commit
3f6ba52a0e
138
software/main.py
138
software/main.py
|
@ -2,6 +2,8 @@ from yaspin import yaspin
|
||||||
spinner = yaspin()
|
spinner = yaspin()
|
||||||
spinner.start()
|
spinner.start()
|
||||||
|
|
||||||
|
|
||||||
|
import sys
|
||||||
import typer
|
import typer
|
||||||
import ngrok
|
import ngrok
|
||||||
import platform
|
import platform
|
||||||
|
@ -10,7 +12,6 @@ import os
|
||||||
import importlib
|
import importlib
|
||||||
from source.server.server import start_server
|
from source.server.server import start_server
|
||||||
import subprocess
|
import subprocess
|
||||||
import webview
|
|
||||||
import socket
|
import socket
|
||||||
import json
|
import json
|
||||||
import segno
|
import segno
|
||||||
|
@ -20,8 +21,11 @@ from dotenv import load_dotenv
|
||||||
import signal
|
import signal
|
||||||
from source.server.livekit.worker import main as worker_main
|
from source.server.livekit.worker import main as worker_main
|
||||||
from source.server.livekit.multimodal import main as multimodal_main
|
from source.server.livekit.multimodal import main as multimodal_main
|
||||||
import warnings
|
|
||||||
import requests
|
import requests
|
||||||
|
import webbrowser
|
||||||
|
from pathlib import Path
|
||||||
|
import shutil
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
@ -29,6 +33,17 @@ system_type = platform.system()
|
||||||
|
|
||||||
app = typer.Typer()
|
app = typer.Typer()
|
||||||
|
|
||||||
|
def check_pnpm():
|
||||||
|
"""Check if pnpm is installed."""
|
||||||
|
if not shutil.which("pnpm"):
|
||||||
|
raise typer.BadParameter(
|
||||||
|
"pnpm is required to run the meet interface. Please install it first: https://pnpm.io/installation"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
ROOM_NAME = "my-room"
|
||||||
|
AGENT_NAME = "light"
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def run(
|
def run(
|
||||||
server: str = typer.Option(
|
server: str = typer.Option(
|
||||||
|
@ -77,6 +92,11 @@ def run(
|
||||||
"--multimodal",
|
"--multimodal",
|
||||||
help="Run the multimodal agent",
|
help="Run the multimodal agent",
|
||||||
),
|
),
|
||||||
|
meet: bool = typer.Option(
|
||||||
|
False,
|
||||||
|
"--meet",
|
||||||
|
help="Run the web-based meeting interface locally",
|
||||||
|
),
|
||||||
):
|
):
|
||||||
|
|
||||||
threads = []
|
threads = []
|
||||||
|
@ -153,6 +173,7 @@ def run(
|
||||||
debug
|
debug
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
spinner.stop()
|
spinner.stop()
|
||||||
print("Starting server...")
|
print("Starting server...")
|
||||||
server_thread.start()
|
server_thread.start()
|
||||||
|
@ -162,7 +183,29 @@ def run(
|
||||||
|
|
||||||
### LIVEKIT SERVER
|
### LIVEKIT SERVER
|
||||||
def run_command(command):
|
def run_command(command):
|
||||||
subprocess.run(command, shell=True, check=True)
|
while True:
|
||||||
|
process = subprocess.run(command, shell=True, check=True, preexec_fn=os.setsid)
|
||||||
|
|
||||||
|
url = f"http://{server_host}:{server_port}"
|
||||||
|
while True:
|
||||||
|
time.sleep(5)
|
||||||
|
try:
|
||||||
|
response = requests.get(url)
|
||||||
|
if response.status_code == 200:
|
||||||
|
print("livekit server is running")
|
||||||
|
else:
|
||||||
|
print("request failed: ", response.status_code)
|
||||||
|
break
|
||||||
|
except requests.RequestException:
|
||||||
|
print("request exception")
|
||||||
|
break
|
||||||
|
|
||||||
|
print("Server failed to start, retrying...")
|
||||||
|
try:
|
||||||
|
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
|
||||||
|
except ProcessLookupError:
|
||||||
|
pass # Process group already terminated
|
||||||
|
|
||||||
|
|
||||||
# Start the livekit server
|
# Start the livekit server
|
||||||
if debug:
|
if debug:
|
||||||
|
@ -180,6 +223,7 @@ def run(
|
||||||
|
|
||||||
if expose:
|
if expose:
|
||||||
|
|
||||||
|
|
||||||
### EXPOSE OVER INTERNET
|
### EXPOSE OVER INTERNET
|
||||||
listener = ngrok.forward(f"{server_host}:{server_port}", authtoken_from_env=True, domain=domain)
|
listener = ngrok.forward(f"{server_host}:{server_port}", authtoken_from_env=True, domain=domain)
|
||||||
url = listener.url()
|
url = listener.url()
|
||||||
|
@ -218,11 +262,21 @@ def run(
|
||||||
# Signal handler for termination signals
|
# Signal handler for termination signals
|
||||||
def signal_handler(sig, frame):
|
def signal_handler(sig, frame):
|
||||||
print("Termination signal received. Shutting down...")
|
print("Termination signal received. Shutting down...")
|
||||||
for thread in threads:
|
try:
|
||||||
if thread.is_alive():
|
# Kill all processes in our process group
|
||||||
# Kill subprocess associated with thread
|
os.killpg(os.getpid(), signal.SIGTERM)
|
||||||
subprocess.run(f"pkill -P {os.getpid()}", shell=True)
|
|
||||||
os._exit(0)
|
# Additional cleanup for any remaining threads
|
||||||
|
for thread in threads:
|
||||||
|
if thread.is_alive():
|
||||||
|
try:
|
||||||
|
subprocess.run(f"pkill -P {os.getpid()}", shell=True)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
os._exit(0)
|
||||||
|
|
||||||
# Register signal handler for SIGINT and SIGTERM
|
# Register signal handler for SIGINT and SIGTERM
|
||||||
signal.signal(signal.SIGINT, signal_handler)
|
signal.signal(signal.SIGINT, signal_handler)
|
||||||
|
@ -235,6 +289,7 @@ def run(
|
||||||
response = requests.get(url)
|
response = requests.get(url)
|
||||||
status = "OK" if response.status_code == 200 else "Not OK"
|
status = "OK" if response.status_code == 200 else "Not OK"
|
||||||
if status == "OK":
|
if status == "OK":
|
||||||
|
print("livekit server is running")
|
||||||
break
|
break
|
||||||
except requests.RequestException:
|
except requests.RequestException:
|
||||||
pass
|
pass
|
||||||
|
@ -247,7 +302,7 @@ def run(
|
||||||
.with_name("You") \
|
.with_name("You") \
|
||||||
.with_grants(api.VideoGrants(
|
.with_grants(api.VideoGrants(
|
||||||
room_join=True,
|
room_join=True,
|
||||||
room="my-room",
|
room=ROOM_NAME,
|
||||||
)).to_jwt())
|
)).to_jwt())
|
||||||
|
|
||||||
### DISPLAY QR CODE
|
### DISPLAY QR CODE
|
||||||
|
@ -262,6 +317,28 @@ def run(
|
||||||
qr_thread.start()
|
qr_thread.start()
|
||||||
threads.append(qr_thread)
|
threads.append(qr_thread)
|
||||||
|
|
||||||
|
|
||||||
|
if meet:
|
||||||
|
check_pnpm()
|
||||||
|
# Get the path to the meet client directory
|
||||||
|
meet_client_path = Path(__file__).parent / "source" / "clients" / "meet"
|
||||||
|
|
||||||
|
# Install dependencies if needed
|
||||||
|
spinner.text = "Installing meet client dependencies..."
|
||||||
|
subprocess.run(["pnpm", "install"], cwd=meet_client_path, check=True)
|
||||||
|
|
||||||
|
# Start the Next.js dev server in a separate thread
|
||||||
|
def run_next_server():
|
||||||
|
subprocess.run(["pnpm", "dev"], cwd=meet_client_path, check=True)
|
||||||
|
|
||||||
|
next_server_thread = threading.Thread(target=run_next_server)
|
||||||
|
next_server_thread.daemon = True
|
||||||
|
next_server_thread.start()
|
||||||
|
threads.append(next_server_thread)
|
||||||
|
|
||||||
|
# Wait for Next.js server to start
|
||||||
|
time.sleep(3)
|
||||||
|
|
||||||
### START LIVEKIT WORKER
|
### START LIVEKIT WORKER
|
||||||
if server == "livekit":
|
if server == "livekit":
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
@ -271,25 +348,32 @@ def run(
|
||||||
os.environ['01_TTS'] = interpreter.tts
|
os.environ['01_TTS'] = interpreter.tts
|
||||||
os.environ['01_STT'] = interpreter.stt
|
os.environ['01_STT'] = interpreter.stt
|
||||||
|
|
||||||
# meet_url = f'http://localhost:3000/custom?liveKitUrl={url.replace("http", "ws")}&token={token}\n\n'
|
if debug and not meet:
|
||||||
meet_url = f'https://meet.livekit.io/custom?liveKitUrl={url.replace("http", "ws")}&token={participant_token}\n\n'
|
meet_url = f'http://localhost:3000/custom?liveKitUrl={url.replace("http", "ws")}&token={participant_token}\n\n'
|
||||||
print("\n")
|
print("\n")
|
||||||
print("For debugging, you can join a video call with your assistant. Click the link below, then send a chat message that says {CONTEXT_MODE_OFF}, then begin speaking:")
|
print("For debugging, you can join a video call with your assistant. Click the link below, then send a chat message that says {CONTEXT_MODE_OFF}, then begin speaking:")
|
||||||
print(meet_url)
|
print(meet_url)
|
||||||
|
|
||||||
for attempt in range(30):
|
# Open the browser with the pre-configured URL
|
||||||
try:
|
if meet:
|
||||||
if multimodal:
|
meet_url = f'http://localhost:3000/custom?liveKitUrl={url.replace("http", "ws")}&token={participant_token}'
|
||||||
multimodal_main(local_livekit_url)
|
spinner.stop()
|
||||||
else:
|
print(f"\nOpening meet interface at: {meet_url}")
|
||||||
worker_main(local_livekit_url)
|
webbrowser.open(meet_url)
|
||||||
except KeyboardInterrupt:
|
|
||||||
print("Exiting.")
|
print(f"multimodal flag is: {multimodal}")
|
||||||
raise
|
try:
|
||||||
except Exception as e:
|
if multimodal:
|
||||||
print(f"Error occurred: {e}")
|
multimodal_main(url)
|
||||||
print("Retrying...")
|
else:
|
||||||
time.sleep(1)
|
print("Starting worker...")
|
||||||
|
worker_main(url)
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("Exiting.")
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error occurred: {e}")
|
||||||
|
|
||||||
# Wait for all threads to complete
|
# Wait for all threads to complete
|
||||||
for thread in threads:
|
for thread in threads:
|
||||||
|
|
Loading…
Reference in New Issue