|
|
@ -6,21 +6,19 @@ 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
|
|
|
@ -34,10 +32,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.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Initialize PyAudio
|
|
|
|
# Initialize PyAudio
|
|
|
|
p = pyaudio.PyAudio()
|
|
|
|
p = pyaudio.PyAudio()
|
|
|
@ -127,7 +121,6 @@ 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()
|
|
|
|
|
|
|
|
|
|
|
@ -140,8 +133,12 @@ async def message_sender(websocket):
|
|
|
|
async def websocket_communication(WS_URL):
|
|
|
|
async def websocket_communication(WS_URL):
|
|
|
|
while True:
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
async with websockets.connect(WS_URL) as websocket:
|
|
|
|
logger.info(f"Connecting to `{WS_URL}` ...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}
|
|
|
@ -187,14 +184,20 @@ async def websocket_communication(WS_URL):
|
|
|
|
result = interpreter.computer.run(language, code)
|
|
|
|
result = interpreter.computer.run(language, code)
|
|
|
|
send_queue.put(result)
|
|
|
|
send_queue.put(result)
|
|
|
|
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
|
|
|
# traceback.print_exc()
|
|
|
|
except Exception as e:
|
|
|
|
logger.info(f"Connecting to `{WS_URL}`...")
|
|
|
|
logging.exception(f"An error occurred during websocket communication. {e}")
|
|
|
|
|
|
|
|
logging.info(f"Connecting to `{WS_URL}`...")
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
|
|