Spaces:
Build error
Build error
Create selfapi_writer.py
Browse files- selfapi_writer.py +134 -0
selfapi_writer.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# File: selfapi_writer.py
|
| 2 |
+
|
| 3 |
+
import anthropic
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import json
|
| 6 |
+
from typing import Dict, Any
|
| 7 |
+
|
| 8 |
+
class SelfApiWriter:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
"""Initialize the Self.api writer"""
|
| 11 |
+
self.client = anthropic.Anthropic(
|
| 12 |
+
api_key=st.secrets["ANTHROPIC_API_KEY"]
|
| 13 |
+
)
|
| 14 |
+
self.context = {}
|
| 15 |
+
self.book_structure = {
|
| 16 |
+
"introduction": "System Requirements: A Human's Guide to Being Human",
|
| 17 |
+
"parts": [
|
| 18 |
+
{
|
| 19 |
+
"title": "The Human Input/Output System",
|
| 20 |
+
"chapters": [
|
| 21 |
+
"Rate Limiting Your Reality",
|
| 22 |
+
"Debugging Your Attention Span",
|
| 23 |
+
"The Mindfulness Microservice"
|
| 24 |
+
]
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"title": "Your Internal Neural Network",
|
| 28 |
+
"chapters": [
|
| 29 |
+
"Training Your Gut Algorithm",
|
| 30 |
+
"The Wisdom Cache",
|
| 31 |
+
"Pattern Recognition Beyond Logic"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"title": "The Source Code of Being",
|
| 36 |
+
"chapters": [
|
| 37 |
+
"Quantum Mechanics of Consciousness",
|
| 38 |
+
"The Purpose Protocol",
|
| 39 |
+
"Refactoring Your Reality"
|
| 40 |
+
]
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
"title": "The Universal Runtime Environment",
|
| 44 |
+
"chapters": [
|
| 45 |
+
"Distributed Consciousness Systems",
|
| 46 |
+
"The Empathy Protocol",
|
| 47 |
+
"Scaling Your Impact"
|
| 48 |
+
]
|
| 49 |
+
}
|
| 50 |
+
]
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
def write_introduction(self) -> str:
|
| 54 |
+
"""Generate the book's introduction"""
|
| 55 |
+
try:
|
| 56 |
+
system_prompt = """You are writing the introduction to 'Self.api: A Modern Spiritual Operating System'.
|
| 57 |
+
The introduction should hook tech professionals by speaking their language while introducing the book's core premise.
|
| 58 |
+
Include the author's journey from debugging code to debugging consciousness."""
|
| 59 |
+
|
| 60 |
+
intro_prompt = """Write the introduction "System Requirements: A Human's Guide to Being Human"
|
| 61 |
+
|
| 62 |
+
Key elements to include:
|
| 63 |
+
1. Opening hook: "In a world where we can Google anything except our own purpose..."
|
| 64 |
+
2. Author's personal journey
|
| 65 |
+
3. The core premise of viewing spirituality through an API/systems lens
|
| 66 |
+
4. Overview of the book's structure and approach
|
| 67 |
+
5. What readers will gain from this "operating manual for the human spirit"
|
| 68 |
+
|
| 69 |
+
Make it compelling, technical yet accessible, and set the tone for the entire book."""
|
| 70 |
+
|
| 71 |
+
response = self.client.messages.create(
|
| 72 |
+
model="claude-3-sonnet-20240229",
|
| 73 |
+
max_tokens=4000,
|
| 74 |
+
temperature=0.7,
|
| 75 |
+
system=system_prompt,
|
| 76 |
+
messages=[{"role": "user", "content": intro_prompt}]
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
intro_content = response.content[0].text
|
| 80 |
+
self.context['introduction'] = intro_content
|
| 81 |
+
return intro_content
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
st.error(f"Error generating introduction: {str(e)}")
|
| 85 |
+
return f"Error generating introduction: {str(e)}"
|
| 86 |
+
|
| 87 |
+
def write_chapter(self, part_idx: int, chapter_idx: int) -> str:
|
| 88 |
+
"""Generate a specific chapter"""
|
| 89 |
+
try:
|
| 90 |
+
part = self.book_structure["parts"][part_idx]
|
| 91 |
+
chapter_title = part["chapters"][chapter_idx]
|
| 92 |
+
part_title = part["title"]
|
| 93 |
+
previous_chapter = self.context.get(f'part_{part_idx}_chapter_{chapter_idx-1}', '')
|
| 94 |
+
|
| 95 |
+
system_prompt = """You are writing 'Self.api: A Modern Spiritual Operating System', a book that bridges technology and spirituality for tech professionals.
|
| 96 |
+
Follow these guidelines:
|
| 97 |
+
- Use API and technical metaphors naturally and accurately
|
| 98 |
+
- Maintain a voice that's witty but wise (Douglas Adams meets Deepak Chopra)
|
| 99 |
+
- Structure each chapter with: System Log (personal story), Documentation (teaching), Implementation Guide (practical steps)
|
| 100 |
+
- Balance is 60% practical, 40% philosophical
|
| 101 |
+
- Include 3 "aha" moments and 2-3 quotable passages
|
| 102 |
+
- Ensure every concept links to both ancient wisdom and modern science
|
| 103 |
+
- Write for analytical minds seeking spiritual depth without woo-woo
|
| 104 |
+
- Include practical exercises every few pages"""
|
| 105 |
+
|
| 106 |
+
chapter_prompt = f"""
|
| 107 |
+
Write Chapter: "{chapter_title}" in Part {part_idx + 1}: "{part_title}"
|
| 108 |
+
|
| 109 |
+
Previous Chapter Context: {previous_chapter[:1000] if previous_chapter else 'Starting new part'}
|
| 110 |
+
|
| 111 |
+
Requirements:
|
| 112 |
+
1. Open with a compelling "System Log" that relates to {chapter_title}
|
| 113 |
+
2. Use technically accurate API/programming metaphors
|
| 114 |
+
3. Include at least two practical exercises
|
| 115 |
+
4. End with clear implementation steps
|
| 116 |
+
5. Maintain the tech-spiritual bridge throughout
|
| 117 |
+
|
| 118 |
+
Begin writing the complete chapter now."""
|
| 119 |
+
|
| 120 |
+
response = self.client.messages.create(
|
| 121 |
+
model="claude-3-sonnet-20240229",
|
| 122 |
+
max_tokens=4000,
|
| 123 |
+
temperature=0.7,
|
| 124 |
+
system=system_prompt,
|
| 125 |
+
messages=[{"role": "user", "content": chapter_prompt}]
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
chapter_content = response.content[0].text
|
| 129 |
+
self.context[f'part_{part_idx}_chapter_{chapter_idx}'] = chapter_content
|
| 130 |
+
return chapter_content
|
| 131 |
+
|
| 132 |
+
except Exception as e:
|
| 133 |
+
st.error(f"Error generating chapter: {str(e)}")
|
| 134 |
+
return f"Error generating chapter {chapter_title}: {str(e)}"
|