File size: 21,326 Bytes
fe66e1b
8c719f8
 
d5349e1
b308c3d
8db68e1
6100379
bf05d0e
8c719f8
 
bf05d0e
86171af
5a01605
73b4945
b308c3d
 
73b4945
4e4633b
8c719f8
5177798
 
 
8c719f8
b308c3d
bf05d0e
2dc3347
6100379
2dc3347
b308c3d
 
 
8db68e1
bf05d0e
 
 
 
5a01605
 
 
5d34baa
5a01605
 
 
 
 
 
 
 
 
 
 
8db68e1
b308c3d
bf05d0e
b308c3d
 
 
 
 
 
 
 
 
 
 
 
2996e1f
bf05d0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b308c3d
 
 
 
 
 
 
bf05d0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9aa28bf
6100379
9aa28bf
8db68e1
 
b308c3d
8db68e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf05d0e
8db68e1
5177798
8db68e1
 
 
 
 
bf05d0e
5a01605
bf05d0e
 
 
 
 
 
 
 
8db68e1
 
 
 
 
 
 
 
 
 
 
 
9aa28bf
 
 
5177798
9aa28bf
5a01605
5feebe5
bf05d0e
5177798
 
 
bf05d0e
 
 
 
5a01605
 
 
8db68e1
b308c3d
8c719f8
8db68e1
bf05d0e
 
 
 
8c719f8
8db68e1
 
 
5feebe5
 
 
 
 
8db68e1
 
 
 
 
 
bf05d0e
 
 
 
 
 
8db68e1
 
 
 
bf05d0e
 
 
8db68e1
 
 
 
 
8c719f8
af10f17
 
 
 
 
 
 
5feebe5
af10f17
 
 
5feebe5
af10f17
5a01605
 
 
af10f17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5feebe5
 
af10f17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b462c2f
af10f17
 
 
 
 
 
 
 
 
 
 
 
5177798
af10f17
 
 
 
bf05d0e
5feebe5
af10f17
 
 
 
bf05d0e
5feebe5
 
 
5a01605
5feebe5
 
5a01605
5feebe5
 
5a01605
5feebe5
 
 
 
 
af10f17
bf05d0e
5feebe5
5a01605
 
 
 
5feebe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a01605
5feebe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
from anthropic import Anthropic
import streamlit as st
import json
import os
import tiktoken
from typing import Dict, Any, Optional, List, Tuple, Generator
from content_state import ContentState

class SelfApiWriter:
    def __init__(self):
        """Initialize the Self.api writer with enhanced content tracking"""
        ANTHROPIC_API_KEY = os.getenv('api_key')
            
        if not ANTHROPIC_API_KEY:
            raise ValueError("Anthropic API key not found. Please ensure ANTHROPIC_API_KEY is set.")
        
        self.client = Anthropic(api_key=ANTHROPIC_API_KEY)
        self.model = "claude-3-opus-20240229"
        self.context = {}
        self.book_structure = None
        self.writing_guidelines = None
        self.initialized = False

        # Configuration for generation
        self.pages_per_chapter = 25
        self.words_per_page = 250
        self.max_iterations = 10
        self.max_tokens = 15000

        # Token encoding
        self.tokenizer = tiktoken.encoding_for_model("gpt-4")
        
        # Add content state tracking
        self.content_states = {}

    def _initialize_content_state(self, content_id: str) -> None:
        """Initialize a new content state tracker"""
        if content_id not in self.content_states:
            self.content_states[content_id] = ContentState()
    
    def set_manual_content(self, content_id: str, content: str) -> None:
        """Set manual content for a specific section"""
        if content_id not in self.content_states:
            self._initialize_content_state(content_id)
        self.content_states[content_id].set_manual_content(content)
        
        # If the content is for introduction, also store in context
        if content_id == 'introduction':
            if 'manual_content' not in self.context:
                self.context['manual_content'] = {}
            self.context['manual_content']['introduction'] = content

    def _truncate_blueprint(self, blueprint: str, max_tokens: int = 15000) -> Tuple[str, str]:
        """Intelligently truncate the blueprint to fit within token limits"""
        tokens = self.tokenizer.encode(blueprint)
        
        if len(tokens) <= max_tokens:
            return blueprint, ""
        
        truncated_tokens = tokens[:max_tokens]
        truncated_blueprint = self.tokenizer.decode(truncated_tokens)
        
        try:
            overview_response = self.client.messages.create(
                model=self.model,
                max_tokens=1000,
                system="You are an expert at creating concise summaries of book blueprints.",
                messages=[{
                    "role": "user",
                    "content": f"""The following blueprint was truncated due to length constraints. 
                    Please create a comprehensive overview that captures the essence of the 
                    truncated sections:

                    Truncated Blueprint Ending:
                    {blueprint[len(truncated_blueprint):]}

                    Provide a summary that:
                    1. Captures key themes and intentions
                    2. Highlights main sections that were cut off
                    3. Ensures no critical information is lost
                    4. Is concise but comprehensive"""
                }]
            )
            overview_summary = overview_response.content[0].text
        except Exception as e:
            overview_summary = f"Note: Some blueprint content was truncated. Original blueprint exceeded {max_tokens} tokens."
        
        return truncated_blueprint, overview_summary

    def _generate_section_outline(self, content_id: str, section_type: str, title: str) -> List[str]:
        """Generate detailed outline for a section before writing"""
        state = self.content_states[content_id]
        
        outline_prompt = f"""Based on the current progress:
        Previous Summary: {state.current_summary}
        Key Points Covered: {', '.join(state.key_points_covered)}
        
        Create a detailed outline for {section_type}: "{title}" that:
        1. Builds on previously covered material
        2. Introduces new concepts progressively
        3. Maintains narrative continuity
        4. Plans clear transitions between subsections
        
        Return the outline as a list of specific points to cover."""

        response = self.client.messages.create(
            model=self.model,
            max_tokens=1000,
            temperature=0.5,
            messages=[{"role": "user", "content": outline_prompt}]
        )
        
        outline = [point.strip() for point in response.content[0].text.split('\n') if point.strip()]
        state.section_outlines = outline
        return outline

    def process_blueprint(self, blueprint: str) -> Dict[str, Any]:
        """Process blueprint to extract structure and guidelines"""
        try:
            with st.spinner("Processing blueprint..."):
                truncated_blueprint, overview_summary = self._truncate_blueprint(blueprint)

                system_prompt = """You are an expert book planner analyzing a blueprint.
                Extract ALL relevant information and return it in a structured format.
                Include:
                1. Book title and high-level information
                2. Complete structure (introduction, parts, chapters)
                3. All writing style guidelines
                4. Content requirements and constraints
                5. Target audience details
                6. Chapter structure requirements
                7. Tone and voice requirements
                8. Any other relevant guidelines or requirements
                
                Return a JSON structure with the following format:
                {
                    "book_info": {
                        "title": "Book title",
                        "vision": "Core vision/purpose",
                        "target_audience": "Detailed audience description"
                    },
                    "structure": {
                        "introduction": "Introduction title",
                        "parts": [
                            {
                                "title": "Part title",
                                "chapters": ["Chapter 1 title", "Chapter 2 title", ...]
                            }
                        ]
                    },
                    "guidelines": {
                        "style": "Writing style description",
                        "tone": "Tone requirements",
                        "chapter_structure": ["Required chapter components"],
                        "content_requirements": ["Specific content requirements"],
                        "practical_elements": ["Required practical elements"]
                    }
                }"""

                response = self.client.messages.create(
                    model=self.model,
                    max_tokens=4000,
                    temperature=0,
                    system=system_prompt,
                    messages=[{
                        "role": "user", 
                        "content": f"""Analyze this book blueprint and extract ALL information:

                        {truncated_blueprint}

                        {overview_summary}

                        Return only the JSON structure without any additional text."""
                    }]
                )
                
                extracted_info = json.loads(response.content[0].text)
                extracted_info['full_original_blueprint'] = blueprint
                
                self.book_info = extracted_info["book_info"]
                self.book_structure = extracted_info["structure"]
                self.writing_guidelines = extracted_info["guidelines"]
                self.initialized = True
                self.context['full_original_blueprint'] = blueprint
                
                return extracted_info
            
        except Exception as e:
            st.error(f"Error processing blueprint: {str(e)}")
            return None


    def write_introduction(self, additional_prompt: str = "") -> str:
        """Generate the book's introduction with enhanced continuity"""
        if not self.initialized:
            raise ValueError("Writer not initialized. Process blueprint first.")
            
        content_id = "introduction"
        self._initialize_content_state(content_id)
            
        def generate_intro_iteration(iteration: int,
                                        previous_summary: str,
                                        points_to_cover: List[str],
                                        narrative_threads: List[str]) -> str:
            """Generate a single iteration of the introduction"""
            full_blueprint = self.context.get('full_original_blueprint', '')

            system_prompt = f"""You are writing the introduction for '{self.book_info.get('title', 'Untitled Book')}'
            
            Previous Content Summary: {previous_summary}
            Points to Cover in This Section: {', '.join(points_to_cover)}
            Active Narrative Threads: {', '.join(narrative_threads)}
            
            Core Vision: {self.book_info.get('vision', '')}
            Target Audience: {self.book_info.get('target_audience', '')}
            Writing Style: {self.writing_guidelines.get('style', 'Academic and clear')}
            Tone: {self.writing_guidelines.get('tone', 'Professional')}
            
            Additional Instructions: {additional_prompt}
            
            Write the introduction following these guidelines."""

            response = self.client.messages.create(
                model=self.model,
                max_tokens=2000,
                temperature=0.7,
                system=system_prompt,
                messages=[{
                    "role": "user",
                    "content": f"""Write the next section of the introduction, building on:
                    Previous Summary: {previous_summary}
                    Points to Cover: {', '.join(points_to_cover)}"""
                }]
            )
            
            return response.content[0].text

        full_intro_content = self._generate_with_continuity(
            generate_intro_iteration,
            content_id,
            "Introduction"
        )
        
        self.context['introduction'] = full_intro_content
        return full_intro_content

    def write_chapter(self, part_idx: int, chapter_idx: int, additional_prompt: str = "") -> str:
        """Generate a chapter using enhanced content continuity and additional prompts"""
        if not self.initialized:
            raise ValueError("Writer not initialized. Process blueprint first.")
            
        content_id = f"part_{part_idx}_chapter_{chapter_idx}"
        self._initialize_content_state(content_id)
        
        # Add any additional prompts to the content state
        if additional_prompt:
            self.content_states[content_id].add_custom_prompt(content_id, additional_prompt)
        
        def generate_chapter_iteration(iteration: int,
                                        previous_summary: str,
                                        points_to_cover: List[str],
                                        narrative_threads: List[str]) -> str:
            """Generate a single chapter iteration with enhanced context"""
            part = self.book_structure["parts"][part_idx]
            chapter_title = part["chapters"][chapter_idx]
            part_title = part["title"]
            
            # Get complete context including custom prompts
            section_context = self.content_states[content_id].get_section_context(content_id)
            
            # Enhanced system prompt with additional context
            system_prompt = f"""You are writing '{self.book_info.get('title', 'Untitled Book')}'
            Chapter: {chapter_title}
            Part: {part_title}
            
            Blueprint Context: {self.context.get('full_original_blueprint', '')}
            
            Additional Instructions: {additional_prompt}
            Custom Guidelines: {section_context.get('custom_instructions', '')}
            
            Previous Content Summary: {previous_summary}
            Points to Cover in This Section: {', '.join(points_to_cover)}
            Active Narrative Threads: {', '.join(narrative_threads)}
            
            Writing Guidelines: {json.dumps(self.writing_guidelines, indent=2)}
            
            Create content that:
            1. Builds naturally on previous sections
            2. Incorporates the additional instructions and custom guidelines
            3. Maintains consistent narrative threads
            4. Creates smooth transitions
            5. Follows all style and structure guidelines
            
            If additional instructions are provided, ensure they are seamlessly integrated
            into the content while maintaining the overall style and structure."""

            response = self.client.messages.create(
                model=self.model,
                max_tokens=2000,
                temperature=0.7,
                system=system_prompt,
                messages=[{
                    "role": "user", 
                    "content": f"Write the next section of Chapter: {chapter_title}, incorporating any additional instructions provided."
                }]
            )
            
            return response.content[0].text

        full_chapter_content = self._generate_with_continuity(
            generate_chapter_iteration,
            content_id,
            f"Chapter: {self.book_structure['parts'][part_idx]['chapters'][chapter_idx]}"
        )
        
        # Store context history
        self.content_states[content_id].update_context_history(
            content_id,
            self.content_states[content_id].get_section_context(content_id)
        )
        
        if 'parts' not in self.context:
            self.context['parts'] = []

        while len(self.context['parts']) <= part_idx:
            self.context['parts'].append({'chapters': []})

        while len(self.context['parts'][part_idx]['chapters']) <= chapter_idx:
            self.context['parts'][part_idx]['chapters'].append({})

        self.context['parts'][part_idx]['chapters'][chapter_idx] = {
            'title': self.book_structure['parts'][part_idx]['chapters'][chapter_idx],
            'content': full_chapter_content
        }
        
        return full_chapter_content

    def _generate_with_continuity(self, 
                                    generate_func: callable,
                                    content_id: str,
                                    title: str,
                                    total_steps: int = 10) -> str:
        """Enhanced generation with content continuity tracking"""
        progress_bar = st.progress(0, text=f"Generating {title}...")
        full_content = ""
        state = self.content_states[content_id]
        
        try:
            # If manual content exists, use it as a starting point
            if state.manual_content:
                full_content = state.manual_content + "\n\n"
                # Generate initial summary from manual content
                state.current_summary = self._generate_progressive_summary(
                    content_id,
                    full_content
                )
            
            # Generate initial outline
            outline = self._generate_section_outline(content_id, "section", title)
            points_per_iteration = max(1, len(outline) // total_steps)
            
            for iteration in range(1, total_steps + 1):
                progress = iteration / total_steps
                progress_bar.progress(
                    min(int(progress * 100), 100),
                    text=f"Generating {title}... (Iteration {iteration}/{total_steps})"
                )
                
                start_idx = (iteration - 1) * points_per_iteration
                end_idx = min(start_idx + points_per_iteration, len(outline))
                current_points = outline[start_idx:end_idx]
                
                new_content = generate_func(
                    iteration=iteration,
                    previous_summary=state.current_summary,
                    points_to_cover=current_points,
                    narrative_threads=state.narrative_threads
                )
                
                state.generated_sections.append(new_content)
                
                if iteration > 1:
                    transition = self._generate_transition(
                        content_id,
                        state.generated_sections[-2],
                        new_content
                    )
                    full_content += transition
                
                full_content += new_content
                
                state.current_summary = self._generate_progressive_summary(
                    content_id,
                    full_content
                )
                state.key_points_covered.update(current_points)
                
                if len(full_content.split()) > self.pages_per_chapter * self.words_per_page:
                    break
            
            conclusion = self._generate_conclusion(content_id, full_content)
            full_content += conclusion
            
            progress_bar.progress(100, text=f"Finished generating {title}")
            return full_content
            
        except Exception as e:
            st.error(f"Error generating {title}: {e}")
            progress_bar.empty()
            return f"Error generating {title}: {e}"
        finally:
            progress_bar.empty()

    def _generate_transition(self, content_id: str, prev_content: str, next_content: str) -> str:
        """Generate smooth transition between sections"""
        state = self.content_states[content_id]
        
        transition_prompt = f"""Create a smooth transition between these sections:
        
        Previous Section Summary: {self._summarize_text(prev_content)}
        Next Section Key Points: {self._summarize_text(next_content)}
        
        Create a natural bridge that:
        1. References relevant previous points
        2. Introduces upcoming concepts
        3. Maintains narrative flow
        4. Feels organic and not forced"""

        response = self.client.messages.create(
            model=self.model,
            max_tokens=300,
        temperature=0.7,
            messages=[{"role": "user", "content": transition_prompt}]
        )
        
        transition = response.content[0].text
        state.transition_points.append(transition)
        return transition

    def _generate_progressive_summary(self, content_id: str, content: str) -> str:
        """Generate a running summary of content progress"""
        summary_prompt = f"""Summarize the key points and narrative progression of:

        {content}

        Focus on:
        1. Main concepts introduced
        2. Key arguments developed
        3. Narrative threads established
        4. Important conclusions reached
        
        Keep the summary concise but comprehensive."""

        response = self.client.messages.create(
            model=self.model,
            max_tokens=500,
            temperature=0.3,
            messages=[{"role": "user", "content": summary_prompt}]
        )
        
        return response.content[0].text

    def _summarize_text(self, text: str) -> str:
        """Generate a concise summary of text"""
        response = self.client.messages.create(
            model=self.model,
            max_tokens=300,
            temperature=0.3,
            messages=[{
                "role": "user",
                "content": f"Summarize the key points from this text:\n\n{text}"
            }]
        )
        return response.content[0].text

    def _generate_conclusion(self, content_id: str, full_content: str) -> str:
        """Generate a conclusion that ties everything together"""
        state = self.content_states[content_id]
        
        conclusion_prompt = f"""Create a conclusion that ties together:

        Content Summary: {state.current_summary}
        Key Points Covered: {', '.join(state.key_points_covered)}
        Narrative Threads: {', '.join(state.narrative_threads)}
        
        The conclusion should:
        1. Summarize main arguments
        2. Connect key themes
        3. Reinforce core messages
        4. Provide closure while maintaining interest"""

        response = self.client.messages.create(
            model=self.model,
            max_tokens=500,
            temperature=0.7,
            messages=[{"role": "user", "content": conclusion_prompt}]
        )
        
        return response.content[0].text

    def get_current_structure(self) -> Optional[Dict[str, Any]]:
        """Get current book structure and guidelines"""
        if not self.initialized:
            return None
            
        return {
            "book_info": self.book_info,
            "structure": self.book_structure,
            "guidelines": self.writing_guidelines
        }