Optimize the code based on feedback from the MLLM
(self, section: Section, feedback: VideoFeedback)
| 459 | ) |
| 460 | |
| 461 | def optimize_with_feedback(self, section: Section, feedback: VideoFeedback) -> bool: |
| 462 | """Optimize the code based on feedback from the MLLM""" |
| 463 | if not feedback.has_issues or not feedback.suggested_improvements: |
| 464 | print(f"✅ {self.learning_topic} {section.id} no optimization needed") |
| 465 | return True |
| 466 | |
| 467 | # === Step 1: back up original code === |
| 468 | original_code_content = self.section_codes[section.id] |
| 469 | |
| 470 | for attempt in range(self.max_feedback_gen_code_tries): |
| 471 | print( |
| 472 | f"🎯 {self.learning_topic} MLLM feedback optimization {section.id} code, attempt {attempt + 1}/{self.max_feedback_gen_code_tries}" |
| 473 | ) |
| 474 | |
| 475 | # === Step 2: back up original code and apply improvements === |
| 476 | if attempt > 0: |
| 477 | self.section_codes[section.id] = original_code_content |
| 478 | |
| 479 | # === Step 3: re-generate code with feedback === |
| 480 | self.generate_section_code( |
| 481 | section=section, attempt=attempt + 1, feedback_improvements=feedback.suggested_improvements |
| 482 | ) |
| 483 | success = self.debug_and_fix_code(section.id, max_fix_attempts=self.max_mllm_fix_bugs_tries) |
| 484 | if success: |
| 485 | optimized_output_dir = self.output_dir / "optimized_videos" |
| 486 | optimized_output_dir.mkdir(exist_ok=True) |
| 487 | optimized_video_path = optimized_output_dir / f"{section.id}_optimized.mp4" |
| 488 | |
| 489 | if section.id in self.section_videos: |
| 490 | original_video_path = Path(self.section_videos[section.id]) |
| 491 | if original_video_path.exists(): |
| 492 | original_video_path.rename(optimized_video_path) |
| 493 | self.section_videos[section.id] = str(optimized_video_path) |
| 494 | print(f"✨ {self.learning_topic} {section.id} optimized video saved: {optimized_video_path}") |
| 495 | else: |
| 496 | print(f"⚠️ {self.learning_topic} {section.id} original video file not found: {original_video_path}") |
| 497 | else: |
| 498 | print(f"⚠️ {self.learning_topic} {section.id} no optimized video path found") |
| 499 | return True |
| 500 | else: |
| 501 | print( |
| 502 | f"❌ {self.learning_topic} {section.id} MLLM optimization failed, attempt {attempt + 1}/{self.max_feedback_gen_code_tries}" |
| 503 | ) |
| 504 | |
| 505 | return False |
| 506 | |
| 507 | def generate_codes(self) -> Dict[str, str]: |
| 508 | if not self.sections: |
no test coverage detected