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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# File: app.py
|
| 2 |
+
# Location: /app.py (root directory for Hugging Face Spaces)
|
| 3 |
+
# Description: Main Streamlit application for AI Book Writing Assistant
|
| 4 |
+
# Deployment: Directly used in Hugging Face Spaces
|
| 5 |
+
|
| 6 |
+
import streamlit as st
|
| 7 |
+
import os
|
| 8 |
+
import json
|
| 9 |
+
from multi_agent_book_workflow import BookWritingOrchestrator
|
| 10 |
+
|
| 11 |
+
class BookWritingApp:
|
| 12 |
+
def __init__(self):
|
| 13 |
+
"""
|
| 14 |
+
Initialize the Streamlit application for book writing
|
| 15 |
+
|
| 16 |
+
Manages session state for the entire book writing project
|
| 17 |
+
"""
|
| 18 |
+
# Set page configuration
|
| 19 |
+
st.set_page_config(
|
| 20 |
+
page_title="AI Book Writing Assistant",
|
| 21 |
+
page_icon="๐",
|
| 22 |
+
layout="wide"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Initialize session state
|
| 26 |
+
if 'project_orchestrator' not in st.session_state:
|
| 27 |
+
st.session_state.project_orchestrator = None
|
| 28 |
+
|
| 29 |
+
if 'book_concept' not in st.session_state:
|
| 30 |
+
st.session_state.book_concept = None
|
| 31 |
+
|
| 32 |
+
# Track generated chapters
|
| 33 |
+
if 'generated_chapters' not in st.session_state:
|
| 34 |
+
st.session_state.generated_chapters = {}
|
| 35 |
+
|
| 36 |
+
# Track project metadata
|
| 37 |
+
if 'project_metadata' not in st.session_state:
|
| 38 |
+
st.session_state.project_metadata = {
|
| 39 |
+
'title': 'Untitled Project',
|
| 40 |
+
'genre': 'Unspecified',
|
| 41 |
+
'total_chapters': 0
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
def render_book_generation_interface(self):
|
| 45 |
+
"""
|
| 46 |
+
Comprehensive interface for book generation with progress tracking
|
| 47 |
+
"""
|
| 48 |
+
# Tabs for different aspects of book writing
|
| 49 |
+
tab1, tab2, tab3 = st.tabs([
|
| 50 |
+
"Book Concept",
|
| 51 |
+
"Chapter Generation",
|
| 52 |
+
"Book Progress"
|
| 53 |
+
])
|
| 54 |
+
|
| 55 |
+
with tab1:
|
| 56 |
+
self._render_concept_development()
|
| 57 |
+
|
| 58 |
+
with tab2:
|
| 59 |
+
self._render_chapter_generation()
|
| 60 |
+
|
| 61 |
+
with tab3:
|
| 62 |
+
self._render_book_progress()
|
| 63 |
+
|
| 64 |
+
def _render_concept_development(self):
|
| 65 |
+
"""
|
| 66 |
+
Render the book concept development interface
|
| 67 |
+
"""
|
| 68 |
+
st.header("๐ Book Concept Development")
|
| 69 |
+
|
| 70 |
+
# Initial concept input
|
| 71 |
+
initial_concept = st.text_area(
|
| 72 |
+
"Describe Your Book Idea",
|
| 73 |
+
height=200,
|
| 74 |
+
placeholder="Enter a comprehensive description of your book concept..."
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
if st.button("Generate Book Concept"):
|
| 78 |
+
if not st.session_state.project_orchestrator:
|
| 79 |
+
st.session_state.project_orchestrator = BookWritingOrchestrator()
|
| 80 |
+
|
| 81 |
+
with st.spinner("Developing Book Concept..."):
|
| 82 |
+
try:
|
| 83 |
+
book_concept = st.session_state.project_orchestrator.generate_book_concept(
|
| 84 |
+
initial_concept
|
| 85 |
+
)
|
| 86 |
+
st.session_state.book_concept = book_concept
|
| 87 |
+
|
| 88 |
+
# Update project metadata
|
| 89 |
+
st.session_state.project_metadata.update({
|
| 90 |
+
'title': book_concept.get('title', 'Untitled Project'),
|
| 91 |
+
'genre': book_concept.get('genre', 'Unspecified')
|
| 92 |
+
})
|
| 93 |
+
|
| 94 |
+
# Display Concept Details
|
| 95 |
+
st.subheader("Generated Book Concept")
|
| 96 |
+
st.json(book_concept)
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
st.error(f"Error generating book concept: {e}")
|
| 100 |
+
|
| 101 |
+
def _render_chapter_generation(self):
|
| 102 |
+
"""
|
| 103 |
+
Render the chapter generation interface with progress tracking
|
| 104 |
+
"""
|
| 105 |
+
st.header("โ๏ธ Chapter Generation")
|
| 106 |
+
|
| 107 |
+
# Check if book concept exists
|
| 108 |
+
if not st.session_state.book_concept:
|
| 109 |
+
st.warning("Please generate a book concept first.")
|
| 110 |
+
return
|
| 111 |
+
|
| 112 |
+
# Chapter generation controls
|
| 113 |
+
col1, col2 = st.columns(2)
|
| 114 |
+
|
| 115 |
+
with col1:
|
| 116 |
+
# Chapter number selection
|
| 117 |
+
max_chapters = 20 # Can be adjusted
|
| 118 |
+
chapter_number = st.number_input(
|
| 119 |
+
"Select Chapter to Generate",
|
| 120 |
+
min_value=1,
|
| 121 |
+
max_value=max_chapters,
|
| 122 |
+
value=len(st.session_state.generated_chapters) + 1
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
with col2:
|
| 126 |
+
# Total chapters planning
|
| 127 |
+
total_chapters = st.number_input(
|
| 128 |
+
"Total Planned Chapters",
|
| 129 |
+
min_value=1,
|
| 130 |
+
max_value=max_chapters,
|
| 131 |
+
value=st.session_state.project_metadata.get('total_chapters', 10)
|
| 132 |
+
)
|
| 133 |
+
st.session_state.project_metadata['total_chapters'] = total_chapters
|
| 134 |
+
|
| 135 |
+
# Generate Chapter Button
|
| 136 |
+
if st.button("Generate Chapter"):
|
| 137 |
+
with st.spinner(f"Generating Chapter {chapter_number}..."):
|
| 138 |
+
try:
|
| 139 |
+
# Generate chapter content
|
| 140 |
+
chapter_content = st.session_state.project_orchestrator.generate_chapter_content(
|
| 141 |
+
st.session_state.book_concept,
|
| 142 |
+
chapter_number
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# Store generated chapter
|
| 146 |
+
st.session_state.generated_chapters[chapter_number] = {
|
| 147 |
+
'content': chapter_content,
|
| 148 |
+
'status': 'Generated'
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
# Display Chapter Content
|
| 152 |
+
st.subheader(f"Chapter {chapter_number}")
|
| 153 |
+
st.write(chapter_content)
|
| 154 |
+
|
| 155 |
+
# Optional: Edit Chapter
|
| 156 |
+
edited_content = st.text_area(
|
| 157 |
+
"Edit Chapter Content",
|
| 158 |
+
value=chapter_content,
|
| 159 |
+
height=400
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
# Save edited content
|
| 163 |
+
if st.button(f"Save Chapter {chapter_number}"):
|
| 164 |
+
st.session_state.generated_chapters[chapter_number]['content'] = edited_content
|
| 165 |
+
st.success(f"Chapter {chapter_number} saved!")
|
| 166 |
+
|
| 167 |
+
except Exception as e:
|
| 168 |
+
st.error(f"Error generating chapter: {e}")
|
| 169 |
+
|
| 170 |
+
def _render_book_progress(self):
|
| 171 |
+
"""
|
| 172 |
+
Render a comprehensive view of book writing progress
|
| 173 |
+
"""
|
| 174 |
+
st.header("๐ Book Writing Progress")
|
| 175 |
+
|
| 176 |
+
# Project Metadata
|
| 177 |
+
st.subheader("Project Overview")
|
| 178 |
+
col1, col2 = st.columns(2)
|
| 179 |
+
|
| 180 |
+
with col1:
|
| 181 |
+
st.metric("Book Title", st.session_state.project_metadata.get('title', 'Untitled'))
|
| 182 |
+
st.metric("Genre", st.session_state.project_metadata.get('genre', 'Unspecified'))
|
| 183 |
+
|
| 184 |
+
with col2:
|
| 185 |
+
total_chapters = st.session_state.project_metadata.get('total_chapters', 0)
|
| 186 |
+
generated_chapters = len(st.session_state.generated_chapters)
|
| 187 |
+
|
| 188 |
+
st.metric("Total Planned Chapters", total_chapters)
|
| 189 |
+
st.metric("Chapters Generated", generated_chapters)
|
| 190 |
+
|
| 191 |
+
# Progress Bar
|
| 192 |
+
progress = generated_chapters / total_chapters if total_chapters > 0 else 0
|
| 193 |
+
st.progress(progress)
|
| 194 |
+
|
| 195 |
+
# Chapter Navigation and Details
|
| 196 |
+
st.subheader("Generated Chapters")
|
| 197 |
+
|
| 198 |
+
# Create tabs for each generated chapter
|
| 199 |
+
if st.session_state.generated_chapters:
|
| 200 |
+
chapter_tabs = st.tabs([
|
| 201 |
+
f"Chapter {ch_num}" for ch_num in sorted(st.session_state.generated_chapters.keys())
|
| 202 |
+
])
|
| 203 |
+
|
| 204 |
+
for i, ch_num in enumerate(sorted(st.session_state.generated_chapters.keys())):
|
| 205 |
+
with chapter_tabs[i]:
|
| 206 |
+
chapter_data = st.session_state.generated_chapters[ch_num]
|
| 207 |
+
st.write(chapter_data['content'])
|
| 208 |
+
|
| 209 |
+
# Chapter status and actions
|
| 210 |
+
col1, col2 = st.columns(2)
|
| 211 |
+
with col1:
|
| 212 |
+
status = st.selectbox(
|
| 213 |
+
"Chapter Status",
|
| 214 |
+
["Generated", "In Review", "Completed"],
|
| 215 |
+
key=f"status_{ch_num}"
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
with col2:
|
| 219 |
+
if st.button(f"Export Chapter {ch_num}"):
|
| 220 |
+
# Export chapter functionality
|
| 221 |
+
with open(f"chapter_{ch_num}.txt", "w") as f:
|
| 222 |
+
f.write(chapter_data['content'])
|
| 223 |
+
st.success(f"Chapter {ch_num} exported!")
|
| 224 |
+
else:
|
| 225 |
+
st.info("No chapters generated yet. Start writing your book!")
|
| 226 |
+
|
| 227 |
+
def run(self):
|
| 228 |
+
"""
|
| 229 |
+
Run the Streamlit application
|
| 230 |
+
"""
|
| 231 |
+
# Main title
|
| 232 |
+
st.title("๐ AI-Powered Book Writing Assistant")
|
| 233 |
+
|
| 234 |
+
# Render the comprehensive book generation interface
|
| 235 |
+
self.render_book_generation_interface()
|
| 236 |
+
|
| 237 |
+
def main():
|
| 238 |
+
# Initialize and run the application
|
| 239 |
+
app = BookWritingApp()
|
| 240 |
+
app.run()
|
| 241 |
+
|
| 242 |
+
if __name__ == "__main__":
|
| 243 |
+
main()
|