Spaces:
Sleeping
Sleeping
Update multi_agent_book_workflow.py
Browse files- multi_agent_book_workflow.py +67 -63
multi_agent_book_workflow.py
CHANGED
|
@@ -233,68 +233,70 @@ class BookWritingOrchestrator:
|
|
| 233 |
"""
|
| 234 |
|
| 235 |
def generate_book_concept(self, initial_prompt: str) -> Dict[str, Any]:
|
| 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 |
def generate_chapter_content(self, book_concept: Dict[str, Any], chapter_number: int) -> str:
|
| 297 |
-
|
| 298 |
try:
|
| 299 |
# Get previous context
|
| 300 |
previous_context = self._retrieve_context(chapter_number - 1) if chapter_number > 1 else ""
|
|
@@ -316,6 +318,7 @@ class BookWritingOrchestrator:
|
|
| 316 |
|
| 317 |
Generate a complete, polished chapter.
|
| 318 |
""",
|
|
|
|
| 319 |
agent=self.writing_agent
|
| 320 |
)
|
| 321 |
|
|
@@ -330,6 +333,7 @@ class BookWritingOrchestrator:
|
|
| 330 |
|
| 331 |
Provide a final, edited version.
|
| 332 |
""",
|
|
|
|
| 333 |
agent=self.editing_agent
|
| 334 |
)
|
| 335 |
|
|
@@ -353,9 +357,9 @@ class BookWritingOrchestrator:
|
|
| 353 |
|
| 354 |
return chapter_content
|
| 355 |
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
|
| 360 |
def _parse_concept(self, raw_concept: str) -> Dict[str, Any]:
|
| 361 |
"""Parse the raw concept output into a structured format"""
|
|
|
|
| 233 |
"""
|
| 234 |
|
| 235 |
def generate_book_concept(self, initial_prompt: str) -> Dict[str, Any]:
|
| 236 |
+
"""
|
| 237 |
+
Generate a comprehensive book concept using multi-agent collaboration
|
| 238 |
+
"""
|
| 239 |
+
try:
|
| 240 |
+
# Concept Development Task
|
| 241 |
+
concept_task = Task(
|
| 242 |
+
description=f"""
|
| 243 |
+
Develop a comprehensive book concept based on this initial idea:
|
| 244 |
+
{initial_prompt}
|
| 245 |
+
|
| 246 |
+
Provide detailed outputs including:
|
| 247 |
+
1. Title: A unique and compelling book title
|
| 248 |
+
2. Genre: Primary genre and any relevant subgenres
|
| 249 |
+
3. Target Audience: Specific demographic and reader profile
|
| 250 |
+
4. Core Premise: The central concept or hook
|
| 251 |
+
5. Chapter Outline: Brief outline of proposed chapters
|
| 252 |
+
6. Narrative Approach: Point of view and stylistic elements
|
| 253 |
+
7. Key Themes: Major themes to be explored
|
| 254 |
+
|
| 255 |
+
Format the output as a structured JSON object.
|
| 256 |
+
""",
|
| 257 |
+
expected_output="A detailed JSON-formatted book concept with title, genre, target audience, and other key elements",
|
| 258 |
+
agent=self.concept_agent
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
# Research Validation Task
|
| 262 |
+
research_task = Task(
|
| 263 |
+
description="""
|
| 264 |
+
Review and enhance the generated book concept with:
|
| 265 |
+
1. Market analysis and genre conventions
|
| 266 |
+
2. Comparable successful titles
|
| 267 |
+
3. Unique selling points
|
| 268 |
+
4. Potential areas for deeper exploration
|
| 269 |
+
|
| 270 |
+
Add these insights to the concept structure.
|
| 271 |
+
""",
|
| 272 |
+
expected_output="A detailed analysis and enhancement of the book concept with market insights and comparative analysis",
|
| 273 |
+
agent=self.research_agent
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
# Create Crew for Collaborative Processing
|
| 277 |
+
book_concept_crew = Crew(
|
| 278 |
+
agents=[self.concept_agent, self.research_agent],
|
| 279 |
+
tasks=[concept_task, research_task],
|
| 280 |
+
verbose=True
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
# Execute Collaborative Workflow
|
| 284 |
+
result = book_concept_crew.kickoff()
|
| 285 |
+
|
| 286 |
+
# Parse and structure the result
|
| 287 |
+
parsed_concept = self._parse_concept(result)
|
| 288 |
+
|
| 289 |
+
# Store Context
|
| 290 |
+
self._store_context('book_concept', str(parsed_concept))
|
| 291 |
+
|
| 292 |
+
return parsed_concept
|
| 293 |
+
|
| 294 |
+
except Exception as e:
|
| 295 |
+
st.error(f"Book concept generation failed: {e}")
|
| 296 |
+
return self._fallback_book_concept(initial_prompt)
|
| 297 |
|
| 298 |
def generate_chapter_content(self, book_concept: Dict[str, Any], chapter_number: int) -> str:
|
| 299 |
+
"""Generate content for a specific chapter"""
|
| 300 |
try:
|
| 301 |
# Get previous context
|
| 302 |
previous_context = self._retrieve_context(chapter_number - 1) if chapter_number > 1 else ""
|
|
|
|
| 318 |
|
| 319 |
Generate a complete, polished chapter.
|
| 320 |
""",
|
| 321 |
+
expected_output="A complete, polished chapter with narrative continuity and character development",
|
| 322 |
agent=self.writing_agent
|
| 323 |
)
|
| 324 |
|
|
|
|
| 333 |
|
| 334 |
Provide a final, edited version.
|
| 335 |
""",
|
| 336 |
+
expected_output="A refined and polished version of the chapter with improved narrative quality",
|
| 337 |
agent=self.editing_agent
|
| 338 |
)
|
| 339 |
|
|
|
|
| 357 |
|
| 358 |
return chapter_content
|
| 359 |
|
| 360 |
+
except Exception as e:
|
| 361 |
+
st.error(f"Chapter generation failed: {e}")
|
| 362 |
+
return self._fallback_chapter_content(book_concept, chapter_number)
|
| 363 |
|
| 364 |
def _parse_concept(self, raw_concept: str) -> Dict[str, Any]:
|
| 365 |
"""Parse the raw concept output into a structured format"""
|