Njap-png commited on
Commit ·
c2f1f7e
1
Parent(s): 3ff2768
Switch self-learning to from-scratch reuse
Browse files
app.py
CHANGED
|
@@ -255,7 +255,7 @@ def generate_response(message, history):
|
|
| 255 |
if _web_ctx:
|
| 256 |
user_content = f"{_web_ctx}\n\n{user_content}\n\nAnswer using the live web results above."
|
| 257 |
_msgs = [
|
| 258 |
-
{"role": "system", "content": "You are Agron, a
|
| 259 |
{"role": "user", "content": user_content},
|
| 260 |
]
|
| 261 |
_prompt = VOICE_TOK.apply_chat_template(_msgs, tokenize=False, add_generation_prompt=True)
|
|
@@ -306,9 +306,9 @@ def generate_response(message, history):
|
|
| 306 |
})
|
| 307 |
# adaptive trigger: self-learn from every conversation (cooldown 30s)
|
| 308 |
global LAST_TRAIN_TIME
|
| 309 |
-
if len(interactions) >=
|
| 310 |
LAST_TRAIN_TIME = time.time()
|
| 311 |
-
threading.Thread(target=
|
| 312 |
return response
|
| 313 |
|
| 314 |
|
|
@@ -762,7 +762,13 @@ def self_update():
|
|
| 762 |
|
| 763 |
EVOLVE_TOPICS = [
|
| 764 |
"artificial intelligence self improvement",
|
| 765 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 766 |
"machine learning transformers",
|
| 767 |
"python programming best practices",
|
| 768 |
"data structures algorithms",
|
|
@@ -958,6 +964,55 @@ def create_tool_from_description(desc):
|
|
| 958 |
register_builtin_tools()
|
| 959 |
load_tools()
|
| 960 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 961 |
with gr.Blocks(title="Agron Neural Interface") as demo:
|
| 962 |
gr.HTML(PARTICLE_HTML, js_on_load=PARTICLE_JS)
|
| 963 |
gr.Markdown("<h1 class='agron-title'>AGRØN NEURAL INTERFACE</h1>", elem_classes="agron-card")
|
|
@@ -991,6 +1046,7 @@ with gr.Blocks(title="Agron Neural Interface") as demo:
|
|
| 991 |
create_btn = gr.Button("Create tool")
|
| 992 |
gr.Markdown("### Learning controls")
|
| 993 |
learn_btn = gr.Button("Train now on memory")
|
|
|
|
| 994 |
dict_btn = gr.Button("Learn dictionary words")
|
| 995 |
web_topic = gr.Textbox(label="Web topic / search query", placeholder="e.g. transformer architecture", lines=1)
|
| 996 |
web_btn = gr.Button("Search & learn web")
|
|
@@ -1046,6 +1102,14 @@ with gr.Blocks(title="Agron Neural Interface") as demo:
|
|
| 1046 |
tool_btn.click(run_tool_fn, [tool_dd, tool_in], tool_out)
|
| 1047 |
create_btn.click(create_tool_fn, tool_desc, status)
|
| 1048 |
learn_btn.click(do_learn, outputs=status)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1049 |
dict_btn.click(do_dict, outputs=status)
|
| 1050 |
web_btn.click(do_web, outputs=status)
|
| 1051 |
def do_evolve():
|
|
|
|
| 255 |
if _web_ctx:
|
| 256 |
user_content = f"{_web_ctx}\n\n{user_content}\n\nAnswer using the live web results above."
|
| 257 |
_msgs = [
|
| 258 |
+
{"role": "system", "content": "You are Agron, a self-improving AI assistant. Reason step-by-step (premises, plan, critique, refine), then answer concisely. Use live web search results when provided. Never say you cannot search or browse. If unsure, suggest 'Search & learn web'."},
|
| 259 |
{"role": "user", "content": user_content},
|
| 260 |
]
|
| 261 |
_prompt = VOICE_TOK.apply_chat_template(_msgs, tokenize=False, add_generation_prompt=True)
|
|
|
|
| 306 |
})
|
| 307 |
# adaptive trigger: self-learn from every conversation (cooldown 30s)
|
| 308 |
global LAST_TRAIN_TIME
|
| 309 |
+
if len(interactions) >= 8 and not training_state["running"] and (time.time() - LAST_TRAIN_TIME) >= 300:
|
| 310 |
LAST_TRAIN_TIME = time.time()
|
| 311 |
+
threading.Thread(target=learn_from_scratch_memory, daemon=True).start()
|
| 312 |
return response
|
| 313 |
|
| 314 |
|
|
|
|
| 762 |
|
| 763 |
EVOLVE_TOPICS = [
|
| 764 |
"artificial intelligence self improvement",
|
| 765 |
+
"chain of thought reasoning",
|
| 766 |
+
"tree of thought search",
|
| 767 |
+
"self-consistency decoding",
|
| 768 |
+
"recursive self improvement algorithms",
|
| 769 |
+
"react agent pattern",
|
| 770 |
+
"retrieval augmented generation",
|
| 771 |
+
"algorithm design and complexity",
|
| 772 |
"machine learning transformers",
|
| 773 |
"python programming best practices",
|
| 774 |
"data structures algorithms",
|
|
|
|
| 964 |
register_builtin_tools()
|
| 965 |
load_tools()
|
| 966 |
|
| 967 |
+
|
| 968 |
+
def learn_from_scratch_memory():
|
| 969 |
+
global MODEL, LAST_TRAIN_TIME
|
| 970 |
+
if not TRAIN_LOCK.acquire(blocking=False):
|
| 971 |
+
return
|
| 972 |
+
training_state["running"] = True
|
| 973 |
+
training_state["last"] = "building from-scratch corpus..."
|
| 974 |
+
try:
|
| 975 |
+
if len(interactions) < 6:
|
| 976 |
+
raise RuntimeError(f"need at least 6 interactions (have {len(interactions)})")
|
| 977 |
+
corpus = os.path.join(MEMORY_DIR, "scratch_corpus.txt")
|
| 978 |
+
lines = [f"User: {r['user']}\nAgron: {r['assistant']}" for r in interactions]
|
| 979 |
+
with open(corpus, "w", encoding="utf-8") as f:
|
| 980 |
+
f.write("\n\n".join(lines))
|
| 981 |
+
out_dir = "/content/agron-from-scratch-memory"
|
| 982 |
+
training_state["last"] = f"training from scratch on {len(interactions)} interactions..."
|
| 983 |
+
subprocess.run([
|
| 984 |
+
"python3", os.path.join(ROOT, "train_from_scratch.py"),
|
| 985 |
+
"--size", "current", "--steps", "300",
|
| 986 |
+
"--dataset", "file", "--file", corpus,
|
| 987 |
+
"--output", out_dir
|
| 988 |
+
], check=True, timeout=600)
|
| 989 |
+
import shutil
|
| 990 |
+
shutil.copyfile(os.path.join(out_dir, "final", "model.safetensors"), os.path.join(ROOT, "model.safetensors"))
|
| 991 |
+
# reload weights into the live model
|
| 992 |
+
try:
|
| 993 |
+
st = load_file(os.path.join(ROOT, "model.safetensors"))
|
| 994 |
+
with IO_LOCK:
|
| 995 |
+
MODEL.load_state_dict(st)
|
| 996 |
+
except Exception:
|
| 997 |
+
pass
|
| 998 |
+
learned_topics.append("from-scratch memory model")
|
| 999 |
+
persist_topics()
|
| 1000 |
+
training_state["last"] = "self-updating from scratch..."
|
| 1001 |
+
subprocess.run(["git", "-C", ROOT, "add", "model.safetensors", "memory/scratch_corpus.txt"], check=True)
|
| 1002 |
+
subprocess.run(["git", "-C", ROOT, "add", "memory/learned_topics.json"], check=False)
|
| 1003 |
+
status = subprocess.run(["git", "-C", ROOT, "status", "--porcelain"], capture_output=True, text=True, check=True).stdout.strip()
|
| 1004 |
+
if status:
|
| 1005 |
+
subprocess.run(["git", "-C", ROOT, "commit", "-m", f"Agron from-scratch learning ({len(interactions)} interactions)"], check=True)
|
| 1006 |
+
subprocess.run(["git", "-C", ROOT, "push", "origin", "main"], check=True)
|
| 1007 |
+
LAST_TRAIN_TIME = time.time()
|
| 1008 |
+
training_state["last"] = f"from-scratch learned & pushed ({len(interactions)} interactions)"
|
| 1009 |
+
except Exception as e:
|
| 1010 |
+
training_state["last"] = f"from-scratch error: {str(e)[:200]}"
|
| 1011 |
+
finally:
|
| 1012 |
+
training_state["running"] = False
|
| 1013 |
+
TRAIN_LOCK.release()
|
| 1014 |
+
|
| 1015 |
+
|
| 1016 |
with gr.Blocks(title="Agron Neural Interface") as demo:
|
| 1017 |
gr.HTML(PARTICLE_HTML, js_on_load=PARTICLE_JS)
|
| 1018 |
gr.Markdown("<h1 class='agron-title'>AGRØN NEURAL INTERFACE</h1>", elem_classes="agron-card")
|
|
|
|
| 1046 |
create_btn = gr.Button("Create tool")
|
| 1047 |
gr.Markdown("### Learning controls")
|
| 1048 |
learn_btn = gr.Button("Train now on memory")
|
| 1049 |
+
scratch_btn = gr.Button("Train from scratch (no fine-tune)")
|
| 1050 |
dict_btn = gr.Button("Learn dictionary words")
|
| 1051 |
web_topic = gr.Textbox(label="Web topic / search query", placeholder="e.g. transformer architecture", lines=1)
|
| 1052 |
web_btn = gr.Button("Search & learn web")
|
|
|
|
| 1102 |
tool_btn.click(run_tool_fn, [tool_dd, tool_in], tool_out)
|
| 1103 |
create_btn.click(create_tool_fn, tool_desc, status)
|
| 1104 |
learn_btn.click(do_learn, outputs=status)
|
| 1105 |
+
def do_scratch():
|
| 1106 |
+
if training_state["running"]:
|
| 1107 |
+
return "training already running"
|
| 1108 |
+
if len(interactions) < 6:
|
| 1109 |
+
return f"need at least 6 interactions (have {len(interactions)})"
|
| 1110 |
+
threading.Thread(target=learn_from_scratch_memory, daemon=True).start()
|
| 1111 |
+
return "from-scratch training started in background..."
|
| 1112 |
+
scratch_btn.click(do_scratch, outputs=status)
|
| 1113 |
dict_btn.click(do_dict, outputs=status)
|
| 1114 |
web_btn.click(do_web, outputs=status)
|
| 1115 |
def do_evolve():
|