feat: teach mode + accumulator fixes
This commit is contained in:
parent
86a399d218
commit
7b3178e2fd
|
@ -329,5 +329,6 @@ class Device:
|
||||||
listener.start()
|
listener.start()
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
if os.getenv('TEACH_MODE') == "False":
|
||||||
asyncio.run(self.start_async())
|
asyncio.run(self.start_async())
|
||||||
p.terminate()
|
p.terminate()
|
|
@ -20,7 +20,7 @@ from .i import configure_interpreter
|
||||||
from interpreter import interpreter
|
from interpreter import interpreter
|
||||||
import ngrok
|
import ngrok
|
||||||
from ..utils.accumulator import Accumulator
|
from ..utils.accumulator import Accumulator
|
||||||
|
from .teach import teach
|
||||||
from .utils.logs import setup_logging
|
from .utils.logs import setup_logging
|
||||||
from .utils.logs import logger
|
from .utils.logs import logger
|
||||||
setup_logging()
|
setup_logging()
|
||||||
|
@ -101,8 +101,6 @@ async def websocket_endpoint(websocket: WebSocket):
|
||||||
send_task = asyncio.create_task(send_messages(websocket))
|
send_task = asyncio.create_task(send_messages(websocket))
|
||||||
try:
|
try:
|
||||||
await asyncio.gather(receive_task, send_task)
|
await asyncio.gather(receive_task, send_task)
|
||||||
except WebSocketDisconnect:
|
|
||||||
pass
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug(traceback.format_exc())
|
logger.debug(traceback.format_exc())
|
||||||
logger.info(f"Connection lost. Error: {e}")
|
logger.info(f"Connection lost. Error: {e}")
|
||||||
|
@ -290,6 +288,9 @@ from uvicorn import Config, Server
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
if os.getenv('TEACH_MODE') == "True":
|
||||||
|
teach()
|
||||||
|
else:
|
||||||
# Start listening
|
# Start listening
|
||||||
asyncio.create_task(listener())
|
asyncio.create_task(listener())
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,59 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from .utils.logs import setup_logging, logger
|
||||||
|
import tkinter as tk
|
||||||
|
import tkinter.simpledialog
|
||||||
|
from interpreter import interpreter
|
||||||
|
from tkinter import messagebox
|
||||||
|
from ..utils.accumulator import Accumulator
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
|
||||||
|
setup_logging()
|
||||||
|
accumulator = Accumulator()
|
||||||
class Skill:
|
class Skill:
|
||||||
def __init__(self, name: str):
|
def __init__(self, name: str):
|
||||||
self.skill_name = name
|
self.skill_name = name
|
||||||
self.steps = []
|
self.steps = []
|
||||||
|
|
||||||
def teach(self, code: str):
|
def to_camel_case(text):
|
||||||
self.steps.append(code)
|
words = text.split()
|
||||||
|
camel_case_string = words[0].lower() + ''.join(word.title() for word in words[1:])
|
||||||
|
return camel_case_string
|
||||||
|
|
||||||
|
def generate_python_code(function_name, steps):
|
||||||
|
code_string = f'def {to_camel_case(function_name)}():\n'
|
||||||
|
code_string += f' """{function_name}"""\n'
|
||||||
|
code_string += f' print({steps})\n'
|
||||||
|
return code_string
|
||||||
|
|
||||||
|
def teach():
|
||||||
|
root = tk.Tk()
|
||||||
|
root.withdraw()
|
||||||
|
|
||||||
|
skill_name = tkinter.simpledialog.askstring("Skill Name", "Please enter the name for the skill:")
|
||||||
|
skill = Skill(skill_name)
|
||||||
|
while True:
|
||||||
|
step = tkinter.simpledialog.askstring("Next Step", "Enter the next step (or 'end' to finish): ")
|
||||||
|
logger.info(f"Performing step: {step}")
|
||||||
|
if step == "end":
|
||||||
|
break
|
||||||
|
|
||||||
|
for chunk in interpreter.chat(step, stream=True, display=False):
|
||||||
|
if "format" in chunk and chunk["format"] == "execution":
|
||||||
|
content = chunk["content"]
|
||||||
|
language = content["format"]
|
||||||
|
code = content["content"]
|
||||||
|
interpreter.computer.run(code, language)
|
||||||
|
time.sleep(0.05)
|
||||||
|
accumulator.accumulate(chunk)
|
||||||
|
|
||||||
|
isCorrect = messagebox.askyesno("To Proceed?", "Did I do this step right?")
|
||||||
|
if isCorrect:
|
||||||
|
skill.steps.append(step)
|
||||||
|
|
||||||
|
print(skill.skill_name, skill.steps)
|
||||||
|
python_code = generate_python_code(skill.skill_name, skill.steps)
|
||||||
|
SKILLS_DIR = os.path.dirname(__file__) + "/skills"
|
||||||
|
filename = os.path.join(SKILLS_DIR, f"{skill.skill_name.replace(' ', '_')}.py")
|
||||||
|
with open(filename, "w") as file:
|
||||||
|
file.write(python_code)
|
||||||
|
|
|
@ -25,6 +25,10 @@ class Accumulator:
|
||||||
self.message = chunk
|
self.message = chunk
|
||||||
if "content" not in self.message:
|
if "content" not in self.message:
|
||||||
self.message["content"] = chunk["content"]
|
self.message["content"] = chunk["content"]
|
||||||
|
else:
|
||||||
|
if type(chunk["content"]) == dict:
|
||||||
|
# dict concatenation cannot happen, so we see if chunk is a dict
|
||||||
|
self.message["content"]["content"] += chunk["content"]["content"]
|
||||||
else:
|
else:
|
||||||
self.message["content"] += chunk["content"]
|
self.message["content"] += chunk["content"]
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in New Issue