@ -2,6 +2,7 @@ from dotenv import load_dotenv
load_dotenv ( ) # take environment variables from .env.
import requests
import subprocess
import os
import sys
@ -12,6 +13,7 @@ from pynput import keyboard
import json
import traceback
import websockets
import websockets . sync . client
import queue
from pydub import AudioSegment
from pydub . playback import play
@ -169,11 +171,11 @@ class Device:
elapsed_time = time . time ( ) - self . playback_latency
print ( f " Time from request to playback: { elapsed_time } seconds " )
self . playback_latency = None
"""
if audio is not None :
mpv_process . stdin . write ( audio ) # type: ignore
mpv_process . stdin . flush ( ) # type: ignore
"""
args = [ " ffplay " , " -autoexit " , " - " , " -nodisp " ]
proc = subprocess . Popen (
args = args ,
@ -183,9 +185,8 @@ class Device:
)
out , err = proc . communicate ( input = audio )
proc . poll ( )
play ( audio )
"""
play ( audio )
# self.audiosegments.remove(audio)
# await asyncio.sleep(0.1)
except asyncio . exceptions . CancelledError :
@ -361,7 +362,7 @@ class Device:
async def websocket_communication ( self , WS_URL ) :
print ( " websocket communication was called!!!! " )
show_connection_log = True
"""
async def exec_ws_communication ( websocket ) :
if CAMERA_ENABLED :
print (
@ -373,11 +374,11 @@ class Device:
asyncio . create_task ( self . message_sender ( websocket ) )
while True :
await asyncio . sleep ( 0.0001 )
await asyncio . sleep ( 0 )
chunk = await websocket . recv ( )
logger . debug ( f " Got this message from the server: { type ( chunk ) } { chunk } " )
# print((f"Got this message from the server: {type(chunk)} {chunk}") )
#logger.debug(f"Got this message from the server: {type(chunk)} {chunk}" )
print ( ( f " Got this message from the server: { type ( chunk ) } " ) )
if type ( chunk ) == str :
chunk = json . loads ( chunk )
@ -388,7 +389,7 @@ class Device:
continue
# At this point, we have our message
# print("checkpoint reached!", message )
print ( " checkpoint reached! " )
if isinstance ( message , bytes ) :
# if message["type"] == "audio" and message["format"].startswith("bytes"):
@ -398,23 +399,23 @@ class Device:
audio_bytes = message
# Create an AudioSegment instance with the raw data
"""
audio = AudioSegment (
# raw audio data (bytes)
data = audio_bytes ,
# signed 16-bit little-endian format
sample_width = 2 ,
# 24,000 Hz frame rate
frame_rate = 16 000,
frame_rate = 24 000,
# mono sound
channels = 1 ,
)
"""
# print("audio segment was created")
await self . audiosegments . put ( audio_bytes )
# await self.audiosegments.put(audio)
print ( " audio segment was created " )
#await self.audiosegments.put(audio_bytes)
await self . audiosegments . put ( audio )
# Run the code if that's the client's job
if os . getenv ( " CODE_RUNNER " ) == " client " :
@ -424,42 +425,65 @@ class Device:
result = interpreter . computer . run ( language , code )
send_queue . put ( result )
"""
if is_win10 ( ) :
logger . info ( " Windows 10 detected " )
# Workaround for Windows 10 not latching to the websocket server.
# See https://github.com/OpenInterpreter/01/issues/197
try :
ws = websockets . connect ( WS_URL )
await exec_ws_communication ( ws )
# await exec_ws_communication(ws )
except Exception as e :
logger . error ( f " Error while attempting to connect: { e } " )
else :
print ( " websocket url is " , WS_URL )
while True :
i = 0
# while True:
# try:
# i += 1
# print("i is", i)
# # Hit the /ping endpoint
# ping_url = f"http://{self.server_url}/ping"
# response = requests.get(ping_url)
# print(response.text)
# # async with aiohttp.ClientSession() as session:
# # async with session.get(ping_url) as response:
# # print(f"Ping response: {await response.text()}")
for i in range ( 3 ) :
print ( i )
try :
async with websockets . connect ( WS_URL ) as websocket :
print ( " awaiting exec_ws_communication " )
await exec_ws_communication ( websocket )
print ( " happi happi happi :DDDDDDDDDDDDD " )
# await exec_ws_communication(websocket)
# print("exiting exec_ws_communication")
except :
logger . info ( traceback . format_exc ( ) )
if show_connection_log :
logger . info ( f " Connecting to ` { WS_URL } `... " )
show_connection_log = False
await asyncio . sleep ( 2 )
print ( " exception in websocket communication!!!!!!!!!!!!!!!!! " )
traceback . print_exc ( )
# except:
# print("exception in websocket communication!!!!!!!!!!!!!!!!!")
# traceback.print_exc()
# if show_connection_log:
# logger.info(f"Connecting to `{WS_URL}`...")
# show_connection_log = False
# await asyncio.sleep(2)
async def start_async ( self ) :
print ( " start async was called!!!!! " )
# Configuration for WebSocket
WS_URL = f " ws:// { self . server_url } "
WS_URL = f " ws:// { self . server_url } / "
# Start the WebSocket communication
asyncio . create_task ( self . websocket_communication ( WS_URL ) )
await self . websocket_communication ( WS_URL )
"""
# Start watching the kernel if it's your job to do that
if os . getenv ( " CODE_RUNNER " ) == " client " :
asyncio . create_task ( put_kernel_messages_into_queue ( send_queue ) )
asyncio . create_task ( self . play_audiosegments ( ) )
#asyncio.create_task(self.play_audiosegments() )
# If Raspberry Pi, add the button listener, otherwise use the spacebar
if current_platform . startswith ( " raspberry-pi " ) :
@ -483,12 +507,11 @@ class Device:
else :
break
else :
# Keyboard listener for spacebar press/release
listener = keyboard . Listener (
on_press = self . on_press , on_release = self . on_release
)
listener . start ( )
print ( " listener for keyboard started!!!!! " )
"""
# Keyboard listener for spacebar press/release
# listener = keyboard.Listener(on_press=self.on_press, on_release=self.on_release)
# listener.start()
# print("listener for keyboard started!!!!!")
def start ( self ) :
print ( " device was started!!!!!! " )