Exposed the server with ngrok
This commit is contained in:
		
							parent
							
								
									ada6c5225c
								
							
						
					
					
						commit
						fbc8760990
					
				| 
						 | 
					@ -14,9 +14,14 @@ WHISPER_MODEL_NAME="ggml-tiny.en.bin"
 | 
				
			||||||
PIPER_VOICE_URL="https://huggingface.co/rhasspy/piper-voices/resolve/main/en/en_US/lessac/medium/"
 | 
					PIPER_VOICE_URL="https://huggingface.co/rhasspy/piper-voices/resolve/main/en/en_US/lessac/medium/"
 | 
				
			||||||
PIPER_VOICE_NAME="en_US-lessac-medium.onnx"
 | 
					PIPER_VOICE_NAME="en_US-lessac-medium.onnx"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Expose through Ngrok
 | 
				
			||||||
 | 
					# Uncomment following line with your Ngrok auth token (https://dashboard.ngrok.com/get-started/your-authtoken)
 | 
				
			||||||
 | 
					#NGROK_AUTHTOKEN=""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# If SERVER_START, this is where we'll serve the server.
 | 
					# If SERVER_START, this is where we'll serve the server.
 | 
				
			||||||
# If DEVICE_START, this is where the device expects the server to be.
 | 
					# If DEVICE_START, this is where the device expects the server to be.
 | 
				
			||||||
SERVER_URL=ws://localhost:8000/
 | 
					SERVER_URL=ws://localhost:8000/
 | 
				
			||||||
 | 
					SERVER_CONNECTION_URL=ws://localhost:8000/ # Comment if setting up through Ngrok
 | 
				
			||||||
SERVER_START=True
 | 
					SERVER_START=True
 | 
				
			||||||
DEVICE_START=True
 | 
					DEVICE_START=True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -38,11 +38,6 @@ RATE = 44100  # Sample rate
 | 
				
			||||||
RECORDING = False  # Flag to control recording state
 | 
					RECORDING = False  # Flag to control recording state
 | 
				
			||||||
SPACEBAR_PRESSED = False  # Flag to track spacebar press state
 | 
					SPACEBAR_PRESSED = False  # Flag to track spacebar press state
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Configuration for WebSocket
 | 
					 | 
				
			||||||
WS_URL = os.getenv('SERVER_URL')
 | 
					 | 
				
			||||||
if not WS_URL:
 | 
					 | 
				
			||||||
    raise ValueError("The environment variable SERVER_URL is not set. Please set it to proceed.")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Specify OS
 | 
					# Specify OS
 | 
				
			||||||
current_platform = get_system_info()
 | 
					current_platform = get_system_info()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -203,6 +198,11 @@ async def websocket_communication(WS_URL):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    async def main():
 | 
					    async def main():
 | 
				
			||||||
 | 
					        # Configuration for WebSocket
 | 
				
			||||||
 | 
					        WS_URL = os.getenv('SERVER_CONNECTION_URL')
 | 
				
			||||||
 | 
					        if not WS_URL:
 | 
				
			||||||
 | 
					            raise ValueError("The environment variable SERVER_URL is not set. Please set it to proceed.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Start the WebSocket communication
 | 
					        # Start the WebSocket communication
 | 
				
			||||||
        asyncio.create_task(websocket_communication(WS_URL))
 | 
					        asyncio.create_task(websocket_communication(WS_URL))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,3 +10,4 @@ python-dotenv
 | 
				
			||||||
ffmpeg-python
 | 
					ffmpeg-python
 | 
				
			||||||
textual
 | 
					textual
 | 
				
			||||||
pydub
 | 
					pydub
 | 
				
			||||||
 | 
					ngrok
 | 
				
			||||||
| 
						 | 
					@ -14,6 +14,7 @@ import threading
 | 
				
			||||||
import uvicorn
 | 
					import uvicorn
 | 
				
			||||||
import re
 | 
					import re
 | 
				
			||||||
from fastapi import FastAPI
 | 
					from fastapi import FastAPI
 | 
				
			||||||
 | 
					from fastapi.responses import PlainTextResponse
 | 
				
			||||||
from threading import Thread
 | 
					from threading import Thread
 | 
				
			||||||
from starlette.websockets import WebSocket
 | 
					from starlette.websockets import WebSocket
 | 
				
			||||||
from stt import stt_bytes
 | 
					from stt import stt_bytes
 | 
				
			||||||
| 
						 | 
					@ -24,6 +25,7 @@ import urllib.parse
 | 
				
			||||||
from utils.kernel import put_kernel_messages_into_queue
 | 
					from utils.kernel import put_kernel_messages_into_queue
 | 
				
			||||||
from i import configure_interpreter
 | 
					from i import configure_interpreter
 | 
				
			||||||
from interpreter import interpreter
 | 
					from interpreter import interpreter
 | 
				
			||||||
 | 
					import ngrok
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from utils.logs import setup_logging
 | 
					from utils.logs import setup_logging
 | 
				
			||||||
from utils.logs import logger
 | 
					from utils.logs import logger
 | 
				
			||||||
| 
						 | 
					@ -93,6 +95,10 @@ if os.getenv('CODE_RUNNER') == "device":
 | 
				
			||||||
# Configure interpreter
 | 
					# Configure interpreter
 | 
				
			||||||
interpreter = configure_interpreter(interpreter)
 | 
					interpreter = configure_interpreter(interpreter)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					app.get("/ping")
 | 
				
			||||||
 | 
					async def ping():
 | 
				
			||||||
 | 
					    return PlainTextResponse("pong")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@app.websocket("/")
 | 
					@app.websocket("/")
 | 
				
			||||||
async def websocket_endpoint(websocket: WebSocket):
 | 
					async def websocket_endpoint(websocket: WebSocket):
 | 
				
			||||||
    await websocket.accept()
 | 
					    await websocket.accept()
 | 
				
			||||||
| 
						 | 
					@ -229,6 +235,17 @@ async def stream_or_play_tts(sentence):
 | 
				
			||||||
        await to_device.put({"role": "assistant", "type": "audio", "format": "audio/mp3", "content": str(audio_bytes)})
 | 
					        await to_device.put({"role": "assistant", "type": "audio", "format": "audio/mp3", "content": str(audio_bytes)})
 | 
				
			||||||
        await to_device.put({"role": "assistant", "type": "audio", "format": "audio/mp3", "end": True})
 | 
					        await to_device.put({"role": "assistant", "type": "audio", "format": "audio/mp3", "end": True})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async def setup_ngrok(ngrok_auth_token, parsed_url):
 | 
				
			||||||
 | 
					    # Set up Ngrok
 | 
				
			||||||
 | 
					    logger.info("Setting up Ngrok")
 | 
				
			||||||
 | 
					    ngrok_listener = await ngrok.forward(f"{parsed_url.hostname}:{parsed_url.port}", authtoken=ngrok_auth_token, domain="project01.ngrok.dev")
 | 
				
			||||||
 | 
					    ngrok_parsed_url = urllib.parse.urlparse(ngrok_listener.url())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Setup SERVER_URL environment variable for device to use
 | 
				
			||||||
 | 
					    connection_url = f"wss://{ngrok_parsed_url.hostname}/"
 | 
				
			||||||
 | 
					    logger.info(f"Ngrok established at {ngrok_parsed_url.geturl()}")
 | 
				
			||||||
 | 
					    logger.info(f"\033[1mSERVER_CONNECTION_URL should be set to \"{connection_url}\"\033[0m")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from uvicorn import Config, Server
 | 
					from uvicorn import Config, Server
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -247,6 +264,12 @@ if __name__ == "__main__":
 | 
				
			||||||
        if not server_url:
 | 
					        if not server_url:
 | 
				
			||||||
            raise ValueError("The environment variable SERVER_URL is not set. Please set it to proceed.")
 | 
					            raise ValueError("The environment variable SERVER_URL is not set. Please set it to proceed.")
 | 
				
			||||||
        parsed_url = urllib.parse.urlparse(server_url)
 | 
					        parsed_url = urllib.parse.urlparse(server_url)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Set up Ngrok
 | 
				
			||||||
 | 
					        ngrok_auth_token = os.getenv('NGROK_AUTHTOKEN')
 | 
				
			||||||
 | 
					        if ngrok_auth_token is not None:
 | 
				
			||||||
 | 
					            await setup_ngrok(ngrok_auth_token, parsed_url)
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
        logger.info("Starting `server.py`...")
 | 
					        logger.info("Starting `server.py`...")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        config = Config(app, host=parsed_url.hostname, port=parsed_url.port, lifespan='on')
 | 
					        config = Config(app, host=parsed_url.hostname, port=parsed_url.port, lifespan='on')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -63,6 +63,13 @@ fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
start_device() {
 | 
					start_device() {
 | 
				
			||||||
    echo "Starting device..."
 | 
					    echo "Starting device..."
 | 
				
			||||||
 | 
					    if [[ -n $NGROK_AUTHTOKEN ]]; then
 | 
				
			||||||
 | 
					        echo "Waiting for Ngrok to setup"
 | 
				
			||||||
 | 
					        sleep 7
 | 
				
			||||||
 | 
					        read -p "Enter the Ngrok URL: " ngrok_url
 | 
				
			||||||
 | 
					        export SERVER_CONNECTION_URL=$ngrok_url
 | 
				
			||||||
 | 
					        echo "SERVER_CONNECTION_URL set to $SERVER_CONNECTION_URL"
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
    python device.py &
 | 
					    python device.py &
 | 
				
			||||||
    DEVICE_PID=$!
 | 
					    DEVICE_PID=$!
 | 
				
			||||||
    echo "Device started as process $DEVICE_PID"
 | 
					    echo "Device started as process $DEVICE_PID"
 | 
				
			||||||
| 
						 | 
					@ -90,18 +97,18 @@ stop_processes() {
 | 
				
			||||||
# Trap SIGINT and SIGTERM to stop processes when the script is terminated
 | 
					# Trap SIGINT and SIGTERM to stop processes when the script is terminated
 | 
				
			||||||
trap stop_processes SIGINT SIGTERM
 | 
					trap stop_processes SIGINT SIGTERM
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# DEVICE
 | 
					 | 
				
			||||||
# Start device if DEVICE_START is True
 | 
					 | 
				
			||||||
if [[ "$DEVICE_START" == "True" ]]; then
 | 
					 | 
				
			||||||
    start_device
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# SERVER
 | 
					# SERVER
 | 
				
			||||||
# Start server if SERVER_START is True
 | 
					# Start server if SERVER_START is True
 | 
				
			||||||
if [[ "$SERVER_START" == "True" ]]; then
 | 
					if [[ "$SERVER_START" == "True" ]]; then
 | 
				
			||||||
    start_server
 | 
					    start_server
 | 
				
			||||||
fi
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# DEVICE
 | 
				
			||||||
 | 
					# Start device if DEVICE_START is True
 | 
				
			||||||
 | 
					if [[ "$DEVICE_START" == "True" ]]; then
 | 
				
			||||||
 | 
					    start_device
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Wait for device and server processes to exit
 | 
					# Wait for device and server processes to exit
 | 
				
			||||||
wait $DEVICE_PID
 | 
					wait $DEVICE_PID
 | 
				
			||||||
wait $SERVER_PID
 | 
					wait $SERVER_PID
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue