Spaces:
Build error
Build error
Update selfapi_writer.py
Browse files- selfapi_writer.py +39 -0
selfapi_writer.py
CHANGED
|
@@ -64,6 +64,45 @@ class SelfApiWriter:
|
|
| 64 |
]
|
| 65 |
}
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
def write_introduction(self) -> str:
|
| 68 |
"""Generate the book's introduction"""
|
| 69 |
try:
|
|
|
|
| 64 |
]
|
| 65 |
}
|
| 66 |
|
| 67 |
+
def process_blueprint(self, blueprint: str) -> Dict[str, Any]:
|
| 68 |
+
"""Process updated blueprint to generate new book structure"""
|
| 69 |
+
try:
|
| 70 |
+
system_prompt = """You are an expert book planner analyzing a blueprint for 'Self.api: A Modern Spiritual Operating System'.
|
| 71 |
+
Extract and organize the book structure from the provided blueprint.
|
| 72 |
+
Return a JSON structure with the following format:
|
| 73 |
+
{
|
| 74 |
+
"introduction": "Introduction title",
|
| 75 |
+
"parts": [
|
| 76 |
+
{
|
| 77 |
+
"title": "Part title",
|
| 78 |
+
"chapters": ["Chapter 1 title", "Chapter 2 title", ...]
|
| 79 |
+
}
|
| 80 |
+
]
|
| 81 |
+
}
|
| 82 |
+
Ensure all sections from the blueprint are included and properly organized."""
|
| 83 |
+
|
| 84 |
+
prompt = f"""Analyze this book blueprint and extract the structure:
|
| 85 |
+
|
| 86 |
+
{blueprint}
|
| 87 |
+
|
| 88 |
+
Return only the JSON structure with no additional text."""
|
| 89 |
+
|
| 90 |
+
response = self.client.messages.create(
|
| 91 |
+
model="claude-3-sonnet-20240229",
|
| 92 |
+
max_tokens=2000,
|
| 93 |
+
temperature=0,
|
| 94 |
+
system=system_prompt,
|
| 95 |
+
messages=[{"role": "user", "content": prompt}]
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
new_structure = json.loads(response.content[0].text)
|
| 99 |
+
self.book_structure = new_structure
|
| 100 |
+
return new_structure
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
st.error(f"Error processing blueprint: {str(e)}")
|
| 104 |
+
return self.book_structure # Return existing structure on error
|
| 105 |
+
|
| 106 |
def write_introduction(self) -> str:
|
| 107 |
"""Generate the book's introduction"""
|
| 108 |
try:
|