forked from adamwlarson/ai-book-writer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
102 lines (82 loc) · 4.64 KB
/
main.py
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
"""Main script for running the book generation system"""
import os
from config import get_config
from agents import BookAgents
from book_generator import BookGenerator
from outline_generator import OutlineGenerator
from txt2pdf import save_book_to_pdf
def save_book_to_txt(book_content, output_dir="book_output"):
"""Save the generated book content to a text file for editing."""
os.makedirs(output_dir, exist_ok=True)
txt_file_path = os.path.join(output_dir, "full_book.txt")
with open(txt_file_path, "w", encoding="utf-8") as f:
for chapter in book_content:
f.write(f"\nChapter {chapter['chapter_number']}: {chapter['title']}\n")
f.write("-" * 50 + "\n")
f.write(chapter['content'] + "\n")
print(f"Book saved for editing: {txt_file_path}")
def main():
# Get configuration
agent_config = get_config()
# Initial prompt for the book
initial_prompt = """
Create a Ghanaian folklore story that embodies traditional storytelling elements with local flavor and wisdom. The story should feature well-known folklore characters like Kweku Ananse, the cunning trickster, alongside figures such as Tortoise (akyekyedeɛ), Owl (patuo), and the wise Ohene Amasa, set in a vibrant Ghanaian village.
The story should incorporate proverbs, riddles, and cultural values such as community unity, patience, honesty, and wit, weaving lessons into the narrative. The setting should reflect a typical Ghanaian village, complete with bustling market scenes, evening storytelling under the baobab tree, and traditional ceremonies featuring kente, adowa dance, and the sounds of the talking drum.
Story Structure:
Setup: Ananse hears of a magical talking drum hidden deep in the forest, said to grant great wisdom and fortune to whoever possesses it.
Initial Conflict: The village elders announce a challenge—whoever brings the drum back will earn the title of "The Wisest of All." Ananse, akyekyedeɛ, and patuo each set off on the quest.
Rising Action: Ananse uses his trickery to outwit his competitors but faces unexpected trials from spirits of the forest (Mmoatia).
Climax: Upon finding the drum, Ananse must solve a riddle posed by the ancestors to claim it.
Resolution: Ananse learns that true wisdom comes not from trickery but from humility and teamwork.
Characters:
Kweku Ananse: Cunning and always seeking an easy way out, yet lovable and relatable.
akyekyedeɛ (Tortoise): Slow but wise and persistent.
patuo (Owl): Keeper of ancient wisdom, mysterious and observant.
Ohene Amasa: The respected leader, fair and just.
Mmoatia (Forest Spirits): Mischievous and wise beings who test the challengers.
Themes:
The value of wisdom over cunning.
The importance of community and collaboration.
Respect for tradition and elders.
Cultural Elements:
Include descriptions of Ghanaian foods like fufu and light soup, the beauty of kente cloth, the significance of adinkra symbols, and interactions in Twi phrases such as:
"Ananse, wo ho te sɛn?" (Ananse, how are you?)
"Ɛyɛ, me da ase." (I’m fine, thank you.)
"""
num_chapters = 3
# Create agents
outline_agents = BookAgents(agent_config)
agents = outline_agents.create_agents(initial_prompt, num_chapters)
# Generate the outline
outline_gen = OutlineGenerator(agents, agent_config)
print("Generating book outline...")
outline = outline_gen.generate_outline(initial_prompt, num_chapters)
# Create new agents with outline context
book_agents = BookAgents(agent_config, outline)
agents_with_context = book_agents.create_agents(initial_prompt, num_chapters)
# Initialize book generator with contextual agents
book_gen = BookGenerator(agents_with_context, agent_config, outline)
# Print and save the generated outline
print("\nGenerated Outline:")
for chapter in outline:
print(f"\nChapter {chapter['chapter_number']}: {chapter['title']}")
print("-" * 50)
print(chapter['prompt'])
# Save the outline for reference
print("\nSaving outline to file...")
os.makedirs("book_output", exist_ok=True)
with open("book_output/outline.txt", "w", encoding="utf-8") as f:
for chapter in outline:
f.write(f"\nChapter {chapter['chapter_number']}: {chapter['title']}\n")
f.write("-" * 50 + "\n")
f.write(chapter['prompt'] + "\n")
# Generate the book using the outline
print("\nGenerating book chapters...")
full_book = book_gen.generate_book(outline)
if full_book:
save_book_to_txt(full_book)
save_book_to_pdf(full_book)
else:
print("Error: Book generation failed. No content to save.")
if __name__ == "__main__":
main()