Merge remote-tracking branch 'upstream/main' into u/shivenmian/local

pull/19/head
Shiven Mian 12 months ago
commit 19502db6c6

3
.gitignore vendored

@ -159,4 +159,5 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear # and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
OS/01/conversations/user.json

@ -1,12 +0,0 @@
[
{
"role": "user",
"type": "message",
"content": " Hello, how are you doing?\n"
},
{
"role": "assistant",
"type": "message",
"content": "I'm an artificial intelligence, so I don't have feelings, but thank you for asking. How may I assist you today?"
}
]

@ -22,6 +22,10 @@ from interpreter import interpreter # Just for code execution. Maybe we should l
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
from utils.logs import setup_logging
from utils.logs import logger
setup_logging()
# Configuration for Audio Recording # Configuration for Audio Recording
CHUNK = 1024 # Record in chunks of 1024 samples CHUNK = 1024 # Record in chunks of 1024 samples
FORMAT = pyaudio.paInt16 # 16 bits per sample FORMAT = pyaudio.paInt16 # 16 bits per sample
@ -51,7 +55,7 @@ def record_audio():
"""Record audio from the microphone and add it to the queue.""" """Record audio from the microphone and add it to the queue."""
stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
print("Recording started...") logger.info("Recording started...")
global RECORDING global RECORDING
# Create a temporary WAV file to store the audio data # Create a temporary WAV file to store the audio data
@ -69,7 +73,7 @@ def record_audio():
wav_file.close() wav_file.close()
stream.stop_stream() stream.stop_stream()
stream.close() stream.close()
print("Recording stopped.") logger.info("Recording stopped.")
duration = wav_file.getnframes() / RATE duration = wav_file.getnframes() / RATE
if duration < 0.3: if duration < 0.3:
@ -120,7 +124,7 @@ def on_release(key):
if key == keyboard.Key.space: if key == keyboard.Key.space:
toggle_recording(False) toggle_recording(False)
elif key == keyboard.Key.esc: elif key == keyboard.Key.esc:
print("Exiting...") logger.info("Exiting...")
os._exit(0) os._exit(0)
import asyncio import asyncio
@ -137,7 +141,7 @@ async def websocket_communication(WS_URL):
while True: while True:
try: try:
async with websockets.connect(WS_URL) as websocket: async with websockets.connect(WS_URL) as websocket:
print("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}
@ -146,16 +150,18 @@ async def websocket_communication(WS_URL):
while True: while True:
message = await websocket.recv() message = await websocket.recv()
print("Got this message from the server:", type(message), message) logger.debug(f"Got this message from the server: {type(message)} {message}")
if type(message) == str: if type(message) == str:
message = json.loads(message) message = json.loads(message)
if message.get("end"): if message.get("end"):
print(f"Complete message from the server: {message_so_far}") logger.debug(f"Complete message from the server: {message_so_far}")
logger.info("\n")
message_so_far = initial_message message_so_far = initial_message
if "content" in message: if "content" in message:
print(message['content'], end="", flush=True)
if any(message_so_far[key] != message[key] for key in message_so_far if key != "content"): if any(message_so_far[key] != message[key] for key in message_so_far if key != "content"):
message_so_far = message message_so_far = message
else: else:
@ -183,7 +189,7 @@ async def websocket_communication(WS_URL):
except: except:
# traceback.print_exc() # traceback.print_exc()
print(f"Connecting to `{WS_URL}`...") logger.info(f"Connecting to `{WS_URL}`...")
await asyncio.sleep(2) await asyncio.sleep(2)

@ -22,6 +22,11 @@ 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
from utils.logs import setup_logging
from utils.logs import logger
setup_logging()
app = FastAPI() app = FastAPI()
conversation_history_path = Path(__file__).parent / 'conversations' / 'user.json' conversation_history_path = Path(__file__).parent / 'conversations' / 'user.json'
@ -64,10 +69,10 @@ if os.getenv('CODE_RUNNER') == "device":
to_device.put({"role": "assistant", "type": "code", "format": "python", "end": True}) to_device.put({"role": "assistant", "type": "code", "format": "python", "end": True})
# Stream the response # Stream the response
print("Waiting for the device to respond...") logger.info("Waiting for the device to respond...")
while True: while True:
chunk = from_computer.get() chunk = from_computer.get()
print("Server recieved from device:", chunk) logger.info(f"Server received from device: {chunk}")
if "end" in chunk: if "end" in chunk:
break break
yield chunk yield chunk
@ -94,7 +99,7 @@ async def websocket_endpoint(websocket: WebSocket):
await asyncio.gather(receive_task, send_task) await asyncio.gather(receive_task, send_task)
except Exception as e: except Exception as e:
traceback.print_exc() traceback.print_exc()
print(f"Connection lost. Error: {e}") logger.info(f"Connection lost. Error: {e}")
async def receive_messages(websocket: WebSocket): async def receive_messages(websocket: WebSocket):
while True: while True:
@ -109,7 +114,7 @@ async def receive_messages(websocket: WebSocket):
async def send_messages(websocket: WebSocket): async def send_messages(websocket: WebSocket):
while True: while True:
message = await to_device.get() message = await to_device.get()
print("Sending to the device:", type(message), message) logger.debug(f"Sending to the device: {type(message)} {message}")
await websocket.send_json(message) await websocket.send_json(message)
async def listener(): async def listener():
@ -159,7 +164,7 @@ async def listener():
for chunk in interpreter.chat(messages, stream=True, display=False): for chunk in interpreter.chat(messages, stream=True, display=False):
print("Got chunk:", chunk) logger.debug("Got chunk:", chunk)
# Send it to the user # Send it to the user
await to_device.put(chunk) await to_device.put(chunk)
@ -195,7 +200,7 @@ async def listener():
with open(conversation_history_path, 'w') as file: with open(conversation_history_path, 'w') as file:
json.dump(interpreter.messages, file, indent=4) json.dump(interpreter.messages, file, indent=4)
print("New user message recieved. Breaking.") logger.info("New user message recieved. Breaking.")
break break
# Also check if there's any new computer messages # Also check if there's any new computer messages
@ -204,7 +209,7 @@ async def listener():
with open(conversation_history_path, 'w') as file: with open(conversation_history_path, 'w') as file:
json.dump(interpreter.messages, file, indent=4) json.dump(interpreter.messages, file, indent=4)
print("New computer message recieved. Breaking.") logger.info("New computer message recieved. Breaking.")
break break
else: else:
with open(conversation_history_path, 'w') as file: with open(conversation_history_path, 'w') as file:
@ -239,7 +244,7 @@ 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)
print("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')
server = Server(config) server = Server(config)

@ -25,6 +25,10 @@ export STT_RUNNER=device # If server, audio will be sent over websocket.
# Will expose the server publically and display that URL. # Will expose the server publically and display that URL.
export SERVER_EXPOSE_PUBLICALLY=False export SERVER_EXPOSE_PUBLICALLY=False
# Debug level
# export LOG_LEVEL=DEBUG
export LOG_LEVEL="INFO"
### SETUP ### SETUP
# if using local models, install the models / executables # if using local models, install the models / executables

@ -11,6 +11,10 @@ import subprocess
import openai import openai
from openai import OpenAI from openai import OpenAI
from utils.logs import setup_logging
from utils.logs import logger
setup_logging()
client = OpenAI() client = OpenAI()
def convert_mime_type_to_format(mime_type: str) -> str: def convert_mime_type_to_format(mime_type: str) -> str:
@ -81,10 +85,10 @@ def stt_wav(wav_file_path: str):
response_format="text" response_format="text"
) )
except openai.BadRequestError as e: except openai.BadRequestError as e:
print("openai.BadRequestError:", e) logger.info(f"openai.BadRequestError: {e}")
return None return None
print("Transcription result:", transcript) logger.info(f"Transcription result: {transcript}")
return transcript return transcript
else: else:
temp_dir = tempfile.gettempdir() temp_dir = tempfile.gettempdir()

@ -2,6 +2,10 @@ import asyncio
import subprocess import subprocess
import platform import platform
from utils.logs import setup_logging
from utils.logs import logger
setup_logging()
def get_kernel_messages(): def get_kernel_messages():
""" """
Is this the way to do this? Is this the way to do this?
@ -16,7 +20,7 @@ def get_kernel_messages():
with open('/var/log/dmesg', 'r') as file: with open('/var/log/dmesg', 'r') as file:
return file.read() return file.read()
else: else:
print("Unsupported platform.") logger.info("Unsupported platform.")
def custom_filter(message): def custom_filter(message):
# Check for {TO_INTERPRETER{ message here }TO_INTERPRETER} pattern # Check for {TO_INTERPRETER{ message here }TO_INTERPRETER} pattern
@ -28,7 +32,7 @@ def custom_filter(message):
elif 'USB' in message: elif 'USB' in message:
return message return message
# Check for network related keywords # Check for network related keywords
elif any(keyword in message for keyword in ['network', 'IP', 'internet', 'LAN', 'WAN', 'router', 'switch']): elif any(keyword in message for keyword in ['network', 'IP', 'internet', 'LAN', 'WAN', 'router', 'switch']) and "networkStatusForFlags" not in message:
return message return message
else: else:
return None return None

@ -0,0 +1,22 @@
import os
import logging
logger: logging.Logger = logging.getLogger("01")
root_logger: logging.Logger = logging.getLogger()
def _basic_config() -> None:
logging.basicConfig(
format="%(message)s"
)
def setup_logging() -> None:
env = os.environ.get("LOG_LEVEL", "").upper()
if env == "DEBUG":
_basic_config()
logger.setLevel(logging.DEBUG)
root_logger.setLevel(logging.DEBUG)
elif env == "INFO":
_basic_config()
logger.setLevel(logging.INFO)

@ -21,6 +21,7 @@ sudo apt-get install portaudio19-dev libav-tools
```bash ```bash
python -m pip install -r requirements.txt python -m pip install -r requirements.txt
``` ```
NB: Depending on your local Python version, you may run into [this issue↗](https://github.com/TaylorSMarks/playsound/issues/150) installing playsound. Workarounds are provided in the issue.
If you want to run local speech-to-text from whisper, download the GGML Whisper model from [Huggingface](https://huggingface.co/ggerganov/whisper.cpp). Then in `OS/01/start.sh`, set `ALL_LOCAL=TRUE` and set `WHISPER_MODEL_PATH` to the path of the model. If you want to run local speech-to-text from whisper, download the GGML Whisper model from [Huggingface](https://huggingface.co/ggerganov/whisper.cpp). Then in `OS/01/start.sh`, set `ALL_LOCAL=TRUE` and set `WHISPER_MODEL_PATH` to the path of the model.

@ -0,0 +1,22 @@
# Development Setup for Jetson Nano
1. Go through the tutorial here: https://developer.nvidia.com/embedded/learn/get-started-jetson-nano-devkit#intro
2. At the end of that guide, you should have a Jetson running off a power supply or micro USB.
3. Get network connectivity. The Jetson does not have a WiFi module so you will need to plug in ethernet.
If you have a laptop, you can share internet access over Ethernet.
To do this with Mac, do the following:
a. Plug a cable from the Jetson Ethernet port to your Mac (you can use a Ethernet -> USB converter for your Mac).
b. Go to General->Sharing, then click the little `(i)` icon next to "Internet Sharing", and check all the options.
![](mac-share-internet.png)
c. Go back to General->Sharing, and turn on "Internet Sharing".
![](mac-share-internet-v2.png)
d. Now the Jetson should have connectivity!

Binary file not shown.

After

Width:  |  Height:  |  Size: 470 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 702 KiB

Loading…
Cancel
Save