Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (69 loc) · 2.81 KB
/
Copy pathmain.py
File metadata and controls
80 lines (69 loc) · 2.81 KB
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
"""
Main entry point for the Book Writer System
"""
import argparse
import os
from pathlib import Path
# Handle the readline issue on Windows before importing other modules
try:
import readline
# On Windows, the readline module may not have a backend attribute
# which causes issues in Python's cmd module
if not hasattr(readline, 'backend'):
# If backend attribute doesn't exist, set a default value to avoid AttributeError
readline.backend = 'builtin' # or another appropriate default
except ImportError:
# readline may not be available on all systems
pass
from book_writer.app import BookWriterApp
from book_writer.ui import create_ui
def main():
"""Main function to run the Book Writer System."""
parser = argparse.ArgumentParser(description="Book Writer System")
parser.add_argument(
"--ui",
action="store_true",
help="Launch the web-based Gradio UI instead of the interactive CLI."
)
parser.add_argument(
"--project-path",
type=str,
default=os.getcwd(),
help="Path to the book project directory."
)
args = parser.parse_args()
project_path = Path(args.project_path)
if args.ui:
print("Launching Gradio Web UI...")
# The UI manages its own project loading, so we just launch it
web_ui = create_ui()
# Enable queue to handle async handlers and ensure stability
web_ui = web_ui.queue()
# Force blocking launch to prevent early process exit in some environments
web_ui.launch(
share=True,
server_name="127.0.0.1",
server_port=7860,
show_error=True,
prevent_thread_lock=False # ensure this call blocks until shutdown
)
else:
print(f"Starting interactive CLI for project at: {project_path}")
if not project_path.exists():
print(f"Project path not found. Creating a new project at {project_path}")
book_title = input("What is the title of your book? (default: 'My Book'): ").strip()
if not book_title:
book_title = "My Book"
# Prompt for target number of pages
target_pages_input = input("How many pages would you like to write for this book? (default: 100): ").strip()
try:
target_pages = int(target_pages_input) if target_pages_input else 100
except ValueError:
target_pages = 100
print(f"Invalid input. Using default value of {target_pages} pages.")
app = BookWriterApp.create_project(project_path, book_title=book_title, target_pages=target_pages)
else:
app = BookWriterApp(project_path)
app.start_interactive_mode()
if __name__ == "__main__":
main()