Spaces:
Build error
Build error
Update selfapi_writer.py
Browse files- selfapi_writer.py +82 -131
selfapi_writer.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
# selfapi_writer.py
|
| 2 |
-
|
| 3 |
from anthropic import Anthropic
|
| 4 |
import streamlit as st
|
| 5 |
import json
|
|
@@ -12,7 +10,7 @@ class SelfApiWriter:
|
|
| 12 |
def __init__(self):
|
| 13 |
"""Initialize the Self.api writer with enhanced content tracking"""
|
| 14 |
ANTHROPIC_API_KEY = os.getenv('api_key')
|
| 15 |
-
|
| 16 |
if not ANTHROPIC_API_KEY:
|
| 17 |
raise ValueError("Anthropic API key not found. Please ensure ANTHROPIC_API_KEY is set.")
|
| 18 |
|
|
@@ -235,10 +233,10 @@ class SelfApiWriter:
|
|
| 235 |
return response.content[0].text
|
| 236 |
|
| 237 |
def _generate_with_continuity(self,
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
"""Enhanced generation with content continuity tracking"""
|
| 243 |
progress_bar = st.progress(0, text=f"Generating {title}...")
|
| 244 |
full_content = ""
|
|
@@ -319,9 +317,9 @@ class SelfApiWriter:
|
|
| 319 |
self._initialize_content_state(content_id)
|
| 320 |
|
| 321 |
def generate_intro_iteration(iteration: int,
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
"""Generate a single iteration of the introduction"""
|
| 326 |
full_blueprint = self.context.get('full_original_blueprint', '')
|
| 327 |
|
|
@@ -360,129 +358,82 @@ class SelfApiWriter:
|
|
| 360 |
self.context['introduction'] = full_intro_content
|
| 361 |
return full_intro_content
|
| 362 |
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 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 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
return full_chapter_content
|
| 440 |
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
temperature=0.3,
|
| 447 |
-
messages=[{
|
| 448 |
-
"role": "user",
|
| 449 |
-
"content": f"Summarize the key points from this text:\n\n{text}"
|
| 450 |
-
}]
|
| 451 |
)
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
conclusion_prompt = f"""Create a conclusion that ties together:
|
| 459 |
-
|
| 460 |
-
Content Summary: {state.current_summary}
|
| 461 |
-
Key Points Covered: {', '.join(state.key_points_covered)}
|
| 462 |
-
Narrative Threads: {', '.join(state.narrative_threads)}
|
| 463 |
-
|
| 464 |
-
The conclusion should:
|
| 465 |
-
1. Summarize main arguments
|
| 466 |
-
2. Connect key themes
|
| 467 |
-
3. Reinforce core messages
|
| 468 |
-
4. Provide closure while maintaining interest"""
|
| 469 |
-
|
| 470 |
-
response = self.client.messages.create(
|
| 471 |
-
model=self.model,
|
| 472 |
-
max_tokens=500,
|
| 473 |
-
temperature=0.7,
|
| 474 |
-
messages=[{"role": "user", "content": conclusion_prompt}]
|
| 475 |
)
|
| 476 |
-
|
| 477 |
-
return
|
| 478 |
|
| 479 |
-
def
|
| 480 |
-
"""Get current book structure and guidelines"""
|
| 481 |
-
if not self.initialized:
|
| 482 |
-
return None
|
| 483 |
-
|
| 484 |
-
return {
|
| 485 |
-
"book_info": self.book_info,
|
| 486 |
-
"structure": self.book_structure,
|
| 487 |
-
"guidelines": self.writing_guidelines
|
| 488 |
-
}
|
|
|
|
|
|
|
|
|
|
| 1 |
from anthropic import Anthropic
|
| 2 |
import streamlit as st
|
| 3 |
import json
|
|
|
|
| 10 |
def __init__(self):
|
| 11 |
"""Initialize the Self.api writer with enhanced content tracking"""
|
| 12 |
ANTHROPIC_API_KEY = os.getenv('api_key')
|
| 13 |
+
|
| 14 |
if not ANTHROPIC_API_KEY:
|
| 15 |
raise ValueError("Anthropic API key not found. Please ensure ANTHROPIC_API_KEY is set.")
|
| 16 |
|
|
|
|
| 233 |
return response.content[0].text
|
| 234 |
|
| 235 |
def _generate_with_continuity(self,
|
| 236 |
+
generate_func: callable,
|
| 237 |
+
content_id: str,
|
| 238 |
+
title: str,
|
| 239 |
+
total_steps: int = 10) -> str:
|
| 240 |
"""Enhanced generation with content continuity tracking"""
|
| 241 |
progress_bar = st.progress(0, text=f"Generating {title}...")
|
| 242 |
full_content = ""
|
|
|
|
| 317 |
self._initialize_content_state(content_id)
|
| 318 |
|
| 319 |
def generate_intro_iteration(iteration: int,
|
| 320 |
+
previous_summary: str,
|
| 321 |
+
points_to_cover: List[str],
|
| 322 |
+
narrative_threads: List[str]) -> str:
|
| 323 |
"""Generate a single iteration of the introduction"""
|
| 324 |
full_blueprint = self.context.get('full_original_blueprint', '')
|
| 325 |
|
|
|
|
| 358 |
self.context['introduction'] = full_intro_content
|
| 359 |
return full_intro_content
|
| 360 |
|
| 361 |
+
def write_chapter(self, part_idx: int, chapter_idx: int, additional_prompt: str = "") -> str:
|
| 362 |
+
"""Generate a chapter using enhanced content continuity and additional prompts"""
|
| 363 |
+
if not self.initialized:
|
| 364 |
+
raise ValueError("Writer not initialized. Process blueprint first.")
|
| 365 |
+
|
| 366 |
+
content_id = f"part_{part_idx}_chapter_{chapter_idx}"
|
| 367 |
+
self._initialize_content_state(content_id)
|
| 368 |
+
|
| 369 |
+
# Add any additional prompts to the content state
|
| 370 |
+
if additional_prompt:
|
| 371 |
+
self.content_states[content_id].add_custom_prompt(content_id, additional_prompt)
|
| 372 |
+
|
| 373 |
+
def generate_chapter_iteration(iteration: int,
|
| 374 |
+
previous_summary: str,
|
| 375 |
+
points_to_cover: List[str],
|
| 376 |
+
narrative_threads: List[str]) -> str:
|
| 377 |
+
"""Generate a single chapter iteration with enhanced context"""
|
| 378 |
+
part = self.book_structure["parts"][part_idx]
|
| 379 |
+
chapter_title = part["chapters"][chapter_idx]
|
| 380 |
+
part_title = part["title"]
|
| 381 |
+
|
| 382 |
+
# Get complete context including custom prompts
|
| 383 |
+
section_context = self.content_states[content_id].get_section_context(content_id)
|
| 384 |
+
|
| 385 |
+
# Enhanced system prompt with additional context
|
| 386 |
+
system_prompt = f"""You are writing '{self.book_info.get('title', 'Untitled Book')}'
|
| 387 |
+
Chapter: {chapter_title}
|
| 388 |
+
Part: {part_title}
|
| 389 |
+
|
| 390 |
+
Blueprint Context: {self.context.get('full_original_blueprint', '')}
|
| 391 |
+
|
| 392 |
+
Additional Instructions: {section_context['additional_prompt']}
|
| 393 |
+
Custom Guidelines: {section_context['custom_instructions']}
|
| 394 |
+
|
| 395 |
+
Previous Content Summary: {previous_summary}
|
| 396 |
+
Points to Cover in This Section: {', '.join(points_to_cover)}
|
| 397 |
+
Active Narrative Threads: {', '.join(narrative_threads)}
|
| 398 |
+
|
| 399 |
+
Writing Guidelines: {json.dumps(self.writing_guidelines, indent=2)}
|
| 400 |
+
|
| 401 |
+
Create content that:
|
| 402 |
+
1. Builds naturally on previous sections
|
| 403 |
+
2. Incorporates the additional instructions and custom guidelines
|
| 404 |
+
3. Maintains consistent narrative threads
|
| 405 |
+
4. Creates smooth transitions
|
| 406 |
+
5. Follows all style and structure guidelines
|
| 407 |
+
|
| 408 |
+
If additional instructions are provided, ensure they are seamlessly integrated
|
| 409 |
+
into the content while maintaining the overall style and structure."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
|
| 411 |
+
response = self.client.messages.create(
|
| 412 |
+
model=self.model,
|
| 413 |
+
max_tokens=2000,
|
| 414 |
+
temperature=0.7,
|
| 415 |
+
system=system_prompt,
|
| 416 |
+
messages=[{
|
| 417 |
+
"role": "user",
|
| 418 |
+
"content": f"Write the next section of Chapter: {chapter_title}, incorporating any additional instructions provided."
|
| 419 |
+
}]
|
| 420 |
+
)
|
| 421 |
+
|
| 422 |
+
return response.content[0].text
|
|
|
|
|
|
|
| 423 |
|
| 424 |
+
# Update generation process to include additional context
|
| 425 |
+
full_chapter_content = self._generate_with_continuity(
|
| 426 |
+
generate_chapter_iteration,
|
| 427 |
+
content_id,
|
| 428 |
+
f"Chapter: {self.book_structure['parts'][part_idx]['chapters'][chapter_idx]}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
)
|
| 430 |
+
|
| 431 |
+
# Store context history
|
| 432 |
+
self.content_states[content_id].update_context_history(
|
| 433 |
+
content_id,
|
| 434 |
+
self.content_states[content_id].get_section_context(content_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
)
|
| 436 |
+
|
| 437 |
+
return full_chapter_content
|
| 438 |
|
| 439 |
+
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|