Step 2: Generate teaching storyboard from outline (optionally with asset enhancement)
(self)
| 188 | return self.outline |
| 189 | |
| 190 | def generate_storyboard(self) -> List[Section]: |
| 191 | """Step 2: Generate teaching storyboard from outline (optionally with asset enhancement)""" |
| 192 | if not self.outline: |
| 193 | raise ValueError("Outline not generated, please generate outline first") |
| 194 | |
| 195 | storyboard_file = self.output_dir / "storyboard.json" |
| 196 | enhanced_storyboard_file = self.output_dir / "storyboard_with_assets.json" |
| 197 | |
| 198 | if enhanced_storyboard_file.exists(): |
| 199 | print("📂 Found enhanced storyboard, loading...") |
| 200 | with open(enhanced_storyboard_file, "r", encoding="utf-8") as f: |
| 201 | self.enhanced_storyboard = json.load(f) |
| 202 | elif storyboard_file.exists(): |
| 203 | print("📂 Found storyboard, loading...") |
| 204 | with open(storyboard_file, "r", encoding="utf-8") as f: |
| 205 | storyboard_data = json.load(f) |
| 206 | if self.use_assets: |
| 207 | self.enhanced_storyboard = self._enhance_storyboard_with_assets(storyboard_data) |
| 208 | else: |
| 209 | self.enhanced_storyboard = storyboard_data |
| 210 | else: |
| 211 | print("🎬 Generating storyboard...") |
| 212 | refer_img_path = ( |
| 213 | self.knowledge_ref_img_folder / img_name |
| 214 | if (img_name := self.KNOWLEDGE2PATH.get(self.learning_topic)) is not None |
| 215 | else None |
| 216 | ) |
| 217 | |
| 218 | prompt2 = get_prompt2_storyboard( |
| 219 | outline=json.dumps(self.outline.__dict__, ensure_ascii=False, indent=2), |
| 220 | reference_image_path=refer_img_path, |
| 221 | ) |
| 222 | |
| 223 | for attempt in range(1, self.max_regenerate_tries + 1): |
| 224 | api_func = self._request_api_and_track_tokens |
| 225 | response = api_func(prompt2, max_tokens=self.max_code_token_length) |
| 226 | if response is None: |
| 227 | print(f"⚠️ Outline format invalid on attempt {attempt}, retrying...") |
| 228 | if attempt == self.max_regenerate_tries: |
| 229 | raise ValueError("API requests failed multiple times") |
| 230 | continue |
| 231 | |
| 232 | try: |
| 233 | content = response.candidates[0].content.parts[0].text |
| 234 | except Exception: |
| 235 | try: |
| 236 | content = response.choices[0].message.content |
| 237 | except Exception: |
| 238 | content = str(response) |
| 239 | |
| 240 | try: |
| 241 | json_str = extract_json_from_markdown(content) |
| 242 | storyboard_data = json.loads(json_str) |
| 243 | |
| 244 | # Save original storyboard |
| 245 | with open(storyboard_file, "w", encoding="utf-8") as f: |
| 246 | json.dump(storyboard_data, f, ensure_ascii=False, indent=2) |
| 247 |
no test coverage detected