Spaces:
Build error
Build error
Update selfapi_writer.py
Browse files- selfapi_writer.py +71 -59
selfapi_writer.py
CHANGED
|
@@ -360,71 +360,83 @@ class SelfApiWriter:
|
|
| 360 |
self.context['introduction'] = full_intro_content
|
| 361 |
return full_intro_content
|
| 362 |
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
content_id = f"part_{part_idx}_chapter_{chapter_idx}"
|
| 369 |
-
self._initialize_content_state(content_id)
|
| 370 |
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
Create content that:
|
| 391 |
-
1. Builds naturally on previous sections
|
| 392 |
-
2. Develops the specified points
|
| 393 |
-
3. Maintains consistent narrative threads
|
| 394 |
-
4. Creates smooth transitions
|
| 395 |
-
5. Follows all style and structure guidelines"""
|
| 396 |
-
|
| 397 |
-
response = self.client.messages.create(
|
| 398 |
-
model=self.model,
|
| 399 |
-
max_tokens=2000,
|
| 400 |
-
temperature=0.7,
|
| 401 |
-
system=system_prompt,
|
| 402 |
-
messages=[{"role": "user", "content": f"Write the next section of Chapter: {chapter_title}"}]
|
| 403 |
-
)
|
| 404 |
-
|
| 405 |
-
return response.content[0].text
|
| 406 |
-
|
| 407 |
-
full_chapter_content = self._generate_with_continuity(
|
| 408 |
-
generate_chapter_iteration,
|
| 409 |
-
content_id,
|
| 410 |
-
f"Chapter: {self.book_structure['parts'][part_idx]['chapters'][chapter_idx]}"
|
| 411 |
-
)
|
| 412 |
|
| 413 |
-
|
| 414 |
-
|
|
|
|
|
|
|
| 415 |
|
| 416 |
-
|
| 417 |
-
self.context['parts'].append({'chapters': []})
|
| 418 |
|
| 419 |
-
|
| 420 |
-
|
| 421 |
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
}
|
| 426 |
|
| 427 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
|
| 429 |
def _summarize_text(self, text: str) -> str:
|
| 430 |
"""Generate a concise summary of text"""
|
|
|
|
| 360 |
self.context['introduction'] = full_intro_content
|
| 361 |
return full_intro_content
|
| 362 |
|
| 363 |
+
def write_chapter(self, part_idx: int, chapter_idx: int, additional_prompt: str = "") -> str:
|
| 364 |
+
"""Generate a chapter using enhanced content continuity and additional prompts"""
|
| 365 |
+
if not self.initialized:
|
| 366 |
+
raise ValueError("Writer not initialized. Process blueprint first.")
|
|
|
|
|
|
|
|
|
|
| 367 |
|
| 368 |
+
content_id = f"part_{part_idx}_chapter_{ch_idx}"
|
| 369 |
+
self._initialize_content_state(content_id)
|
| 370 |
+
|
| 371 |
+
# Add any additional prompts to the content state
|
| 372 |
+
if additional_prompt:
|
| 373 |
+
self.content_states[content_id].add_custom_prompt(content_id, additional_prompt)
|
| 374 |
+
|
| 375 |
+
def generate_chapter_iteration(iteration: int,
|
| 376 |
+
previous_summary: str,
|
| 377 |
+
points_to_cover: List[str],
|
| 378 |
+
narrative_threads: List[str]) -> str:
|
| 379 |
+
"""Generate a single chapter iteration with enhanced context"""
|
| 380 |
+
part = self.book_structure["parts"][part_idx]
|
| 381 |
+
chapter_title = part["chapters"][chapter_idx]
|
| 382 |
+
part_title = part["title"]
|
| 383 |
+
|
| 384 |
+
# Get complete context including custom prompts
|
| 385 |
+
section_context = self.content_states[content_id].get_section_context(content_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
|
| 387 |
+
# Enhanced system prompt with additional context
|
| 388 |
+
system_prompt = f"""You are writing '{self.book_info.get('title', 'Untitled Book')}'
|
| 389 |
+
Chapter: {chapter_title}
|
| 390 |
+
Part: {part_title}
|
| 391 |
|
| 392 |
+
Blueprint Context: {self.context.get('full_original_blueprint', '')}
|
|
|
|
| 393 |
|
| 394 |
+
Additional Instructions: {section_context['additional_prompt']}
|
| 395 |
+
Custom Guidelines: {section_context['custom_instructions']}
|
| 396 |
|
| 397 |
+
Previous Content Summary: {previous_summary}
|
| 398 |
+
Points to Cover in This Section: {', '.join(points_to_cover)}
|
| 399 |
+
Active Narrative Threads: {', '.join(narrative_threads)}
|
|
|
|
| 400 |
|
| 401 |
+
Writing Guidelines: {json.dumps(self.writing_guidelines, indent=2)}
|
| 402 |
+
|
| 403 |
+
Create content that:
|
| 404 |
+
1. Builds naturally on previous sections
|
| 405 |
+
2. Incorporates the additional instructions and custom guidelines
|
| 406 |
+
3. Maintains consistent narrative threads
|
| 407 |
+
4. Creates smooth transitions
|
| 408 |
+
5. Follows all style and structure guidelines
|
| 409 |
+
|
| 410 |
+
If additional instructions are provided, ensure they are seamlessly integrated
|
| 411 |
+
into the content while maintaining the overall style and structure."""
|
| 412 |
+
|
| 413 |
+
response = self.client.messages.create(
|
| 414 |
+
model=self.model,
|
| 415 |
+
max_tokens=2000,
|
| 416 |
+
temperature=0.7,
|
| 417 |
+
system=system_prompt,
|
| 418 |
+
messages=[{
|
| 419 |
+
"role": "user",
|
| 420 |
+
"content": f"Write the next section of Chapter: {chapter_title}, incorporating any additional instructions provided."
|
| 421 |
+
}]
|
| 422 |
+
)
|
| 423 |
+
|
| 424 |
+
return response.content[0].text
|
| 425 |
+
|
| 426 |
+
# Update generation process to include additional context
|
| 427 |
+
full_chapter_content = self._generate_with_continuity(
|
| 428 |
+
generate_chapter_iteration,
|
| 429 |
+
content_id,
|
| 430 |
+
f"Chapter: {self.book_structure['parts'][part_idx]['chapters'][chapter_idx]}"
|
| 431 |
+
)
|
| 432 |
+
|
| 433 |
+
# Store context history
|
| 434 |
+
self.content_states[content_id].update_context_history(
|
| 435 |
+
content_id,
|
| 436 |
+
self.content_states[content_id].get_section_context(content_id)
|
| 437 |
+
)
|
| 438 |
+
|
| 439 |
+
return full_chapter_content
|
| 440 |
|
| 441 |
def _summarize_text(self, text: str) -> str:
|
| 442 |
"""Generate a concise summary of text"""
|