cryogenic22 commited on
Commit
6100379
·
verified ·
1 Parent(s): bf05d0e

Update selfapi_writer.py

Browse files
Files changed (1) hide show
  1. selfapi_writer.py +22 -15
selfapi_writer.py CHANGED
@@ -1,24 +1,16 @@
 
 
1
  from anthropic import Anthropic
2
  import streamlit as st
3
  import json
4
  import os
5
  import tiktoken
6
  from typing import Dict, Any, Optional, List, Tuple, Generator
7
-
8
- class ContentState:
9
- """Tracks the state and progression of content generation"""
10
- def __init__(self):
11
- self.current_summary = ""
12
- self.section_outlines = []
13
- self.generated_sections = []
14
- self.narrative_threads = []
15
- self.key_points_covered = set()
16
- self.transition_points = []
17
 
18
  class SelfApiWriter:
19
  def __init__(self):
20
  """Initialize the Self.api writer with enhanced content tracking"""
21
- # Try to get API key from environment variables first, then from secrets
22
  ANTHROPIC_API_KEY = os.getenv('api_key')
23
 
24
  if not ANTHROPIC_API_KEY:
@@ -34,7 +26,7 @@ class SelfApiWriter:
34
  # Configuration for generation
35
  self.pages_per_chapter = 25
36
  self.words_per_page = 250
37
- self.max_iterations = 10 # Reduced from 20 to 10
38
  self.max_tokens = 15000
39
 
40
  # Token encoding
@@ -113,7 +105,7 @@ class SelfApiWriter:
113
  return outline
114
 
115
  def process_blueprint(self, blueprint: str) -> Dict[str, Any]:
116
- """Process blueprint to extract complete writing guidelines and structure"""
117
  try:
118
  with st.spinner("Processing blueprint..."):
119
  truncated_blueprint, overview_summary = self._truncate_blueprint(blueprint)
@@ -161,7 +153,7 @@ class SelfApiWriter:
161
  temperature=0,
162
  system=system_prompt,
163
  messages=[{
164
- "role": "user",
165
  "content": f"""Analyze this book blueprint and extract ALL information:
166
 
167
  {truncated_blueprint}
@@ -187,6 +179,12 @@ class SelfApiWriter:
187
  st.error(f"Error processing blueprint: {str(e)}")
188
  return None
189
 
 
 
 
 
 
 
190
  def _generate_transition(self, content_id: str, prev_content: str, next_content: str) -> str:
191
  """Generate smooth transition between content sections"""
192
  state = self.content_states[content_id]
@@ -240,13 +238,22 @@ class SelfApiWriter:
240
  generate_func: callable,
241
  content_id: str,
242
  title: str,
243
- total_steps: int = 10) -> str: # Default steps reduced to 10
244
  """Enhanced generation with content continuity tracking"""
245
  progress_bar = st.progress(0, text=f"Generating {title}...")
246
  full_content = ""
247
  state = self.content_states[content_id]
248
 
249
  try:
 
 
 
 
 
 
 
 
 
250
  # Generate initial outline
251
  outline = self._generate_section_outline(content_id, "section", title)
252
  points_per_iteration = max(1, len(outline) // total_steps)
 
1
+ # selfapi_writer.py
2
+
3
  from anthropic import Anthropic
4
  import streamlit as st
5
  import json
6
  import os
7
  import tiktoken
8
  from typing import Dict, Any, Optional, List, Tuple, Generator
9
+ from content_state import ContentState
 
 
 
 
 
 
 
 
 
10
 
11
  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:
 
26
  # Configuration for generation
27
  self.pages_per_chapter = 25
28
  self.words_per_page = 250
29
+ self.max_iterations = 10
30
  self.max_tokens = 15000
31
 
32
  # Token encoding
 
105
  return outline
106
 
107
  def process_blueprint(self, blueprint: str) -> Dict[str, Any]:
108
+ """Process blueprint to extract structure and guidelines"""
109
  try:
110
  with st.spinner("Processing blueprint..."):
111
  truncated_blueprint, overview_summary = self._truncate_blueprint(blueprint)
 
153
  temperature=0,
154
  system=system_prompt,
155
  messages=[{
156
+ "role": "user",
157
  "content": f"""Analyze this book blueprint and extract ALL information:
158
 
159
  {truncated_blueprint}
 
179
  st.error(f"Error processing blueprint: {str(e)}")
180
  return None
181
 
182
+ def set_manual_content(self, content_id: str, content: str) -> None:
183
+ """Set pre-written content for a section"""
184
+ if content_id not in self.content_states:
185
+ self._initialize_content_state(content_id)
186
+ self.content_states[content_id].set_manual_content(content)
187
+
188
  def _generate_transition(self, content_id: str, prev_content: str, next_content: str) -> str:
189
  """Generate smooth transition between content sections"""
190
  state = self.content_states[content_id]
 
238
  generate_func: callable,
239
  content_id: str,
240
  title: str,
241
+ total_steps: int = 10) -> str:
242
  """Enhanced generation with content continuity tracking"""
243
  progress_bar = st.progress(0, text=f"Generating {title}...")
244
  full_content = ""
245
  state = self.content_states[content_id]
246
 
247
  try:
248
+ # If manual content exists, use it as a starting point
249
+ if state.manual_content:
250
+ full_content = state.manual_content + "\n\n"
251
+ # Generate initial summary from manual content
252
+ state.current_summary = self._generate_progressive_summary(
253
+ content_id,
254
+ full_content
255
+ )
256
+
257
  # Generate initial outline
258
  outline = self._generate_section_outline(content_id, "section", title)
259
  points_per_iteration = max(1, len(outline) // total_steps)