import os import asyncio from datetime import datetime class LessonModule: def __init__(self, trainer, lesson_path="data/lessons.txt", log_path="logs/lesson.log", state_path="lessonstate.txt"): self.trainer = trainer self.lesson_path = lesson_path self.log_path = log_path self.state_path = state_path self.current_index = 0 self.lessons = [] os.makedirs(os.path.dirname(self.log_path), exist_ok=True) if os.path.exists(self.lesson_path): with open(self.lesson_path, "r", encoding="utf-8") as f: self.lessons = [line.strip() for line in f if line.strip()] if os.path.exists(self.state_path): try: with open(self.state_path, "r", encoding="utf-8") as f: self.current_index = int(f.read().strip()) except Exception: self.current_index = 0 def _save_state(self): with open(self.state_path, "w", encoding="utf-8") as f: f.write(str(self.current_index)) def _log_lesson(self, text: str, score: float): with open(self.log_path, "a", encoding="utf-8") as f: f.write(f"[{datetime.utcnow().isoformat()}] {score:.2f} | {text.strip()}\n") async def start_lessons(self, interval=10): print("[LESSON] Starting lesson loop...") while self.current_index < len(self.lessons): line = self.lessons[self.current_index] if len(line.split()) >= 3 and self._is_valid(line): score = self.trainer.score_sentence(line) if self.trainer.is_reinforceable(line) and score >= 2.0: self.trainer.train_on_tokens_from_text(line) self._log_lesson(line, score) self.current_index += 1 self._save_state() await asyncio.sleep(interval) print("[LESSON] All lessons completed.") def _is_valid(self, text: str) -> bool: return all(c.isprintable() or c.isspace() for c in text) def reset(self): self.current_index = 0 self._save_state() def add_lesson(self, text: str): self.lessons.append(text) with open(self.lesson_path, "a", encoding="utf-8") as f: f.write(text.strip() + "\n") def progress(self): total = len(self.lessons) return { "current": self.current_index, "total": total, "percent": round(100 * self.current_index / total, 2) if total else 0.0 }