Exposed the server with ngrok

pull/30/head
Zohaib Rauf 11 months ago
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_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 DEVICE_START, this is where the device expects the server to be.
SERVER_URL=ws://localhost:8000/
SERVER_CONNECTION_URL=ws://localhost:8000/ # Comment if setting up through Ngrok
SERVER_START=True
DEVICE_START=True

@ -38,11 +38,6 @@ RATE = 44100 # Sample rate
RECORDING = False # Flag to control recording 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
current_platform = get_system_info()
@ -203,6 +198,11 @@ async def websocket_communication(WS_URL):
if __name__ == "__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
asyncio.create_task(websocket_communication(WS_URL))

@ -9,4 +9,5 @@ playsound
python-dotenv
ffmpeg-python
textual
pydub
pydub
ngrok

@ -14,6 +14,7 @@ import threading
import uvicorn
import re
from fastapi import FastAPI
from fastapi.responses import PlainTextResponse
from threading import Thread
from starlette.websockets import WebSocket
from stt import stt_bytes
@ -24,6 +25,7 @@ import urllib.parse
from utils.kernel import put_kernel_messages_into_queue
from i import configure_interpreter
from interpreter import interpreter
import ngrok
from utils.logs import setup_logging
from utils.logs import logger
@ -93,6 +95,10 @@ if os.getenv('CODE_RUNNER') == "device":
# Configure interpreter
interpreter = configure_interpreter(interpreter)
app.get("/ping")
async def ping():
return PlainTextResponse("pong")
@app.websocket("/")
async def websocket_endpoint(websocket: WebSocket):
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", "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
@ -247,6 +264,12 @@ if __name__ == "__main__":
if not server_url:
raise ValueError("The environment variable SERVER_URL is not set. Please set it to proceed.")
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`...")
config = Config(app, host=parsed_url.hostname, port=parsed_url.port, lifespan='on')

@ -63,6 +63,13 @@ fi
start_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 &
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 stop_processes SIGINT SIGTERM
# DEVICE
# Start device if DEVICE_START is True
if [[ "$DEVICE_START" == "True" ]]; then
start_device
fi
# SERVER
# Start server if SERVER_START is True
if [[ "$SERVER_START" == "True" ]]; then
start_server
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 $DEVICE_PID
wait $SERVER_PID

Loading…
Cancel
Save