Njap-png commited on
Commit
ee702c4
·
1 Parent(s): b00be6a

Structured 'what did you learn' answers + base training topic

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py CHANGED
@@ -39,6 +39,27 @@ def load_interactions():
39
  pass
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def persist_interaction(rec):
43
  global interactions
44
  with IO_LOCK:
@@ -83,6 +104,10 @@ def build_model():
83
  TOKENIZER = build_tokenizer()
84
  MODEL, DEVICE = build_model()
85
  load_interactions()
 
 
 
 
86
 
87
 
88
  def _normalize_history(history):
@@ -133,6 +158,17 @@ def generate_response(message, history):
133
  "knowledge. I learn from our conversations—ask me anything.")
134
  persist_interaction({"ts": __import__("time").time(), "user": message, "assistant": response})
135
  return response
 
 
 
 
 
 
 
 
 
 
 
136
  prompt = _build_prompt(message, history)
137
  with IO_LOCK:
138
  inputs = TOKENIZER(prompt, return_tensors="pt").to(DEVICE)
@@ -221,6 +257,9 @@ def learn_from_interactions():
221
  if status:
222
  subprocess.run(["git", "-C", ROOT, "commit", "-m", f"Agron auto-learn ({len(interactions)} interactions)"], check=True)
223
  subprocess.run(["git", "-C", ROOT, "push", "origin", "main"], check=True)
 
 
 
224
  training_state["last"] = f"learned & pushed @ {len(interactions)} interactions"
225
  except Exception as pe:
226
  training_state["last"] = f"push error: {str(pe)[:150]}"
@@ -404,6 +443,8 @@ def learn_dictionary():
404
  if status:
405
  subprocess.run(["git", "-C", ROOT, "commit", "-m", f"Agron dictionary learning ({len(lines)} words)"], check=True)
406
  subprocess.run(["git", "-C", ROOT, "push", "origin", "main"], check=True)
 
 
407
  training_state["last"] = f"dictionary learned & pushed ({len(lines)} words)"
408
  except Exception as e:
409
  training_state["last"] = f"dict error: {str(e)[:150]}"
@@ -527,6 +568,8 @@ def learn_web(topic):
527
  if status:
528
  subprocess.run(["git", "-C", ROOT, "commit", "-m", f"Agron web learning: {topic}"], check=True)
529
  subprocess.run(["git", "-C", ROOT, "push", "origin", "main"], check=True)
 
 
530
  training_state["last"] = f"web learned & pushed: {topic}"
531
  except Exception as e:
532
  training_state["last"] = f"web error: {str(e)[:150]}"
 
39
  pass
40
 
41
 
42
+ TOPICS_FILE = os.path.join(MEMORY_DIR, "learned_topics.json")
43
+ learned_topics = []
44
+
45
+
46
+ def load_topics():
47
+ global learned_topics
48
+ learned_topics = []
49
+ if os.path.exists(TOPICS_FILE):
50
+ try:
51
+ with open(TOPICS_FILE, "r") as f:
52
+ learned_topics = json.load(f)
53
+ except Exception:
54
+ learned_topics = []
55
+
56
+
57
+ def persist_topics():
58
+ with IO_LOCK:
59
+ with open(TOPICS_FILE, "w") as f:
60
+ json.dump(learned_topics, f)
61
+
62
+
63
  def persist_interaction(rec):
64
  global interactions
65
  with IO_LOCK:
 
104
  TOKENIZER = build_tokenizer()
105
  MODEL, DEVICE = build_model()
106
  load_interactions()
107
+ load_topics()
108
+ if not learned_topics:
109
+ learned_topics.append("base text model (WikiText-2)")
110
+ persist_topics()
111
 
112
 
113
  def _normalize_history(history):
 
158
  "knowledge. I learn from our conversations—ask me anything.")
159
  persist_interaction({"ts": __import__("time").time(), "user": message, "assistant": response})
160
  return response
161
+ if low in {"what did you learn", "what have you learned", "what did you learn about", "what do you know"}:
162
+ parts = []
163
+ if interactions:
164
+ parts.append(f"{len(interactions)} conversation exchanges (latest: '{interactions[-1]['user'][:80]}')")
165
+ if learned_topics:
166
+ parts.append("learned topics: " + ", ".join(learned_topics[-12:]))
167
+ else:
168
+ parts.append("no specialized training runs yet, but I store every conversation")
169
+ response = "I've learned from you and my training sources: " + "; ".join(parts) + "."
170
+ persist_interaction({"ts": __import__("time").time(), "user": message, "assistant": response})
171
+ return response
172
  prompt = _build_prompt(message, history)
173
  with IO_LOCK:
174
  inputs = TOKENIZER(prompt, return_tensors="pt").to(DEVICE)
 
257
  if status:
258
  subprocess.run(["git", "-C", ROOT, "commit", "-m", f"Agron auto-learn ({len(interactions)} interactions)"], check=True)
259
  subprocess.run(["git", "-C", ROOT, "push", "origin", "main"], check=True)
260
+ if "conversation memory" not in learned_topics:
261
+ learned_topics.append("conversation memory")
262
+ persist_topics()
263
  training_state["last"] = f"learned & pushed @ {len(interactions)} interactions"
264
  except Exception as pe:
265
  training_state["last"] = f"push error: {str(pe)[:150]}"
 
443
  if status:
444
  subprocess.run(["git", "-C", ROOT, "commit", "-m", f"Agron dictionary learning ({len(lines)} words)"], check=True)
445
  subprocess.run(["git", "-C", ROOT, "push", "origin", "main"], check=True)
446
+ learned_topics.append(f"dictionary words ({len(lines)})")
447
+ persist_topics()
448
  training_state["last"] = f"dictionary learned & pushed ({len(lines)} words)"
449
  except Exception as e:
450
  training_state["last"] = f"dict error: {str(e)[:150]}"
 
568
  if status:
569
  subprocess.run(["git", "-C", ROOT, "commit", "-m", f"Agron web learning: {topic}"], check=True)
570
  subprocess.run(["git", "-C", ROOT, "push", "origin", "main"], check=True)
571
+ learned_topics.append(f"web: {topic}")
572
+ persist_topics()
573
  training_state["last"] = f"web learned & pushed: {topic}"
574
  except Exception as e:
575
  training_state["last"] = f"web error: {str(e)[:150]}"