|
|
@ -9,19 +9,21 @@ from starlette.websockets import WebSocket
|
|
|
|
from queue import Queue
|
|
|
|
from queue import Queue
|
|
|
|
from pynput import keyboard
|
|
|
|
from pynput import keyboard
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
|
|
|
|
import traceback
|
|
|
|
import websockets
|
|
|
|
import websockets
|
|
|
|
import queue
|
|
|
|
import queue
|
|
|
|
|
|
|
|
import pydub
|
|
|
|
import ast
|
|
|
|
import ast
|
|
|
|
from pydub import AudioSegment
|
|
|
|
from pydub import AudioSegment
|
|
|
|
from pydub.playback import play
|
|
|
|
from pydub.playback import play
|
|
|
|
import io
|
|
|
|
import io
|
|
|
|
|
|
|
|
import time
|
|
|
|
import wave
|
|
|
|
import wave
|
|
|
|
import tempfile
|
|
|
|
import tempfile
|
|
|
|
from datetime import datetime
|
|
|
|
from datetime import datetime
|
|
|
|
from interpreter import interpreter # Just for code execution. Maybe we should let people do from interpreter.computer import run?
|
|
|
|
from interpreter import interpreter # Just for code execution. Maybe we should let people do from interpreter.computer import run?
|
|
|
|
from utils.kernel import put_kernel_messages_into_queue
|
|
|
|
from utils.kernel import put_kernel_messages_into_queue
|
|
|
|
from stt import stt_wav
|
|
|
|
from stt import stt_wav
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from utils.logs import setup_logging
|
|
|
|
from utils.logs import setup_logging
|
|
|
|
from utils.logs import logger
|
|
|
|
from utils.logs import logger
|
|
|
@ -35,6 +37,10 @@ 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.")
|
|
|
|
|
|
|
|
|
|
|
|
# Initialize PyAudio
|
|
|
|
# Initialize PyAudio
|
|
|
|
p = pyaudio.PyAudio()
|
|
|
|
p = pyaudio.PyAudio()
|
|
|
@ -124,6 +130,7 @@ def on_release(key):
|
|
|
|
logger.info("Exiting...")
|
|
|
|
logger.info("Exiting...")
|
|
|
|
os._exit(0)
|
|
|
|
os._exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
|
|
|
|
send_queue = queue.Queue()
|
|
|
|
send_queue = queue.Queue()
|
|
|
|
|
|
|
|
|
|
|
@ -136,12 +143,8 @@ async def message_sender(websocket):
|
|
|
|
async def websocket_communication(WS_URL):
|
|
|
|
async def websocket_communication(WS_URL):
|
|
|
|
while True:
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
logger.info(f"Connecting to `{WS_URL}` ...")
|
|
|
|
async with websockets.connect(WS_URL) as websocket:
|
|
|
|
|
|
|
|
|
|
|
|
headers = {"ngrok-skip-browser-warning": str(80), "User-Agent": "project01"} if os.getenv('NGROK_AUTHTOKEN') else {}
|
|
|
|
|
|
|
|
async with websockets.connect(WS_URL, extra_headers=headers) as websocket:
|
|
|
|
|
|
|
|
logger.info("Press the spacebar to start/stop recording. Press ESC to exit.")
|
|
|
|
logger.info("Press the spacebar to start/stop recording. Press ESC to exit.")
|
|
|
|
|
|
|
|
|
|
|
|
asyncio.create_task(message_sender(websocket))
|
|
|
|
asyncio.create_task(message_sender(websocket))
|
|
|
|
|
|
|
|
|
|
|
|
initial_message = {"role": None, "type": None, "format": None, "content": None}
|
|
|
|
initial_message = {"role": None, "type": None, "format": None, "content": None}
|
|
|
@ -195,12 +198,7 @@ async def websocket_communication(WS_URL):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# Configuration for WebSocket
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
async def main():
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
|
|