(self)
| 136 | return {"idx": self.idx, "knowledge_point": self.learning_topic, "folder": self.folder, "cfg": self.cfg} |
| 137 | |
| 138 | def generate_outline(self) -> TeachingOutline: |
| 139 | outline_file = self.output_dir / "outline.json" |
| 140 | |
| 141 | if outline_file.exists(): |
| 142 | print("📂 ...") |
| 143 | with open(outline_file, "r", encoding="utf-8") as f: |
| 144 | outline_data = json.load(f) |
| 145 | else: |
| 146 | """Step 1: Generate teaching outline from topic""" |
| 147 | refer_img_path = ( |
| 148 | self.knowledge_ref_img_folder / img_name |
| 149 | if (img_name := self.KNOWLEDGE2PATH.get(self.learning_topic)) is not None |
| 150 | else None |
| 151 | ) |
| 152 | prompt1 = get_prompt1_outline(knowledge_point=self.learning_topic, reference_image_path=refer_img_path) |
| 153 | |
| 154 | print(f"📝 Generating Outline...") |
| 155 | |
| 156 | for attempt in range(1, self.max_regenerate_tries + 1): |
| 157 | api_func = self._request_api_and_track_tokens if refer_img_path else self._request_api_and_track_tokens |
| 158 | response = api_func(prompt1, max_tokens=self.max_code_token_length) |
| 159 | if response is None: |
| 160 | print(f"⚠️ Attempt {attempt} failed, retrying...") |
| 161 | if attempt == self.max_regenerate_tries: |
| 162 | raise ValueError("API requests failed multiple times") |
| 163 | continue |
| 164 | try: |
| 165 | content = response.candidates[0].content.parts[0].text |
| 166 | except Exception: |
| 167 | try: |
| 168 | content = response.choices[0].message.content |
| 169 | except Exception: |
| 170 | content = str(response) |
| 171 | content = extract_json_from_markdown(content) |
| 172 | try: |
| 173 | outline_data = json.loads(content) |
| 174 | with open(self.output_dir / "outline.json", "w", encoding="utf-8") as f: |
| 175 | json.dump(outline_data, f, ensure_ascii=False, indent=2) |
| 176 | break |
| 177 | except json.JSONDecodeError: |
| 178 | print(f"⚠️ Outline format invalid on attempt {attempt}, retrying...") |
| 179 | if attempt == self.max_regenerate_tries: |
| 180 | raise ValueError("Outline format invalid multiple times, check prompt or API response") |
| 181 | |
| 182 | self.outline = TeachingOutline( |
| 183 | topic=outline_data["topic"], |
| 184 | target_audience=outline_data["target_audience"], |
| 185 | sections=outline_data["sections"], |
| 186 | ) |
| 187 | print(f"== Outline generated: {self.outline.topic}") |
| 188 | return self.outline |
| 189 | |
| 190 | def generate_storyboard(self) -> List[Section]: |
| 191 | """Step 2: Generate teaching storyboard from outline (optionally with asset enhancement)""" |
no test coverage detected