From e366f3825bce5eb8cb4731a1029987c2dc176ffe Mon Sep 17 00:00:00 2001 From: Shiven Mian Date: Sat, 17 Feb 2024 13:35:53 -0800 Subject: [PATCH] feat: add code in skill instead of steps --- 01OS/01OS/server/teach.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/01OS/01OS/server/teach.py b/01OS/01OS/server/teach.py index 1da969b..ea9aa8a 100644 --- a/01OS/01OS/server/teach.py +++ b/01OS/01OS/server/teach.py @@ -7,6 +7,7 @@ from tkinter import messagebox from ..utils.accumulator import Accumulator import time import os +import textwrap setup_logging() accumulator = Accumulator() @@ -14,16 +15,18 @@ class Skill: def __init__(self, name: str): self.skill_name = name self.steps = [] + self.code = "" def to_camel_case(text): 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): +def generate_python_code(function_name, steps, code): code_string = f'def {to_camel_case(function_name)}():\n' code_string += f' """{function_name}"""\n' - code_string += f' print({steps})\n' + indented_code = textwrap.indent(code, ' ') + code_string += indented_code + '\n' return code_string def teach(): @@ -38,11 +41,13 @@ def teach(): if step == "end": break + chunk_code = "" 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"] + chunk_code += code interpreter.computer.run(code, language) time.sleep(0.05) accumulator.accumulate(chunk) @@ -50,9 +55,9 @@ def teach(): isCorrect = messagebox.askyesno("To Proceed?", "Did I do this step right?") if isCorrect: skill.steps.append(step) + skill.code += chunk_code - print(skill.skill_name, skill.steps) - python_code = generate_python_code(skill.skill_name, skill.steps) + python_code = generate_python_code(skill.skill_name, skill.steps, skill.code) 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: