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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
def run(self):
|
| 178 |
"""
|
|
|
|
| 172 |
if st.sidebar.button("Save Current Project"):
|
| 173 |
self._save_current_project()
|
| 174 |
|
| 175 |
+
def render_book_generation_interface(self):
|
| 176 |
+
"""
|
| 177 |
+
Render the main book generation interface
|
| 178 |
+
"""
|
| 179 |
+
# Tabs for different stages of book writing
|
| 180 |
+
tab1, tab2, tab3 = st.tabs([
|
| 181 |
+
"Book Concept",
|
| 182 |
+
"Chapter Generation",
|
| 183 |
+
"Project Progress"
|
| 184 |
+
])
|
| 185 |
+
|
| 186 |
+
with tab1:
|
| 187 |
+
self._render_concept_development()
|
| 188 |
+
|
| 189 |
+
with tab2:
|
| 190 |
+
self._render_chapter_generation()
|
| 191 |
+
|
| 192 |
+
with tab3:
|
| 193 |
+
self._render_project_progress()
|
| 194 |
+
|
| 195 |
+
def _render_concept_development(self):
|
| 196 |
+
"""
|
| 197 |
+
Render the book concept development interface
|
| 198 |
+
"""
|
| 199 |
+
st.header("📘 Book Concept Development")
|
| 200 |
+
|
| 201 |
+
# Initial concept input
|
| 202 |
+
initial_concept = st.text_area(
|
| 203 |
+
"Describe Your Book Idea",
|
| 204 |
+
height=200,
|
| 205 |
+
placeholder="Enter a comprehensive description of your book concept..."
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
if st.button("Generate Book Concept"):
|
| 209 |
+
# Ensure project orchestrator is available
|
| 210 |
+
if not st.session_state.project_orchestrator:
|
| 211 |
+
self._setup_project_orchestrator()
|
| 212 |
+
|
| 213 |
+
with st.spinner("Developing Book Concept..."):
|
| 214 |
+
try:
|
| 215 |
+
# Generate book concept using multi-agent approach
|
| 216 |
+
book_concept = st.session_state.project_orchestrator.generate_book_concept(
|
| 217 |
+
initial_concept
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
# Update session state
|
| 221 |
+
st.session_state.book_concept = book_concept
|
| 222 |
+
st.session_state.project_metadata.update({
|
| 223 |
+
'title': book_concept.get('title', 'Untitled Project'),
|
| 224 |
+
'genre': book_concept.get('genre', 'Unspecified')
|
| 225 |
+
})
|
| 226 |
+
|
| 227 |
+
# Display generated concept
|
| 228 |
+
st.subheader("Generated Book Concept")
|
| 229 |
+
st.json(book_concept)
|
| 230 |
+
|
| 231 |
+
except Exception as e:
|
| 232 |
+
st.error(f"Error generating book concept: {e}")
|
| 233 |
+
|
| 234 |
+
def _render_chapter_generation(self):
|
| 235 |
+
"""
|
| 236 |
+
Render the chapter generation interface
|
| 237 |
+
"""
|
| 238 |
+
st.header("✍️ Chapter Generation")
|
| 239 |
+
|
| 240 |
+
# Check if book concept exists
|
| 241 |
+
if not st.session_state.book_concept:
|
| 242 |
+
st.warning("Please generate a book concept first.")
|
| 243 |
+
return
|
| 244 |
+
|
| 245 |
+
# Chapter generation controls
|
| 246 |
+
col1, col2 = st.columns(2)
|
| 247 |
+
|
| 248 |
+
with col1:
|
| 249 |
+
# Chapter number selection
|
| 250 |
+
max_chapters = 20 # Can be adjusted
|
| 251 |
+
chapter_number = st.number_input(
|
| 252 |
+
"Select Chapter to Generate",
|
| 253 |
+
min_value=1,
|
| 254 |
+
max_value=max_chapters,
|
| 255 |
+
value=len(st.session_state.generated_chapters) + 1
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
with col2:
|
| 259 |
+
# Total chapters planning
|
| 260 |
+
total_chapters = st.number_input(
|
| 261 |
+
"Total Planned Chapters",
|
| 262 |
+
min_value=1,
|
| 263 |
+
max_value=max_chapters,
|
| 264 |
+
value=st.session_state.project_metadata.get('total_chapters', 10)
|
| 265 |
+
)
|
| 266 |
+
st.session_state.project_metadata['total_chapters'] = total_chapters
|
| 267 |
+
|
| 268 |
+
# Generate Chapter Button
|
| 269 |
+
if st.button("Generate Chapter"):
|
| 270 |
+
with st.spinner(f"Generating Chapter {chapter_number}..."):
|
| 271 |
+
try:
|
| 272 |
+
# Generate chapter content
|
| 273 |
+
chapter_content = st.session_state.project_orchestrator.generate_chapter_content(
|
| 274 |
+
st.session_state.book_concept,
|
| 275 |
+
chapter_number
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
# Store generated chapter
|
| 279 |
+
st.session_state.generated_chapters[chapter_number] = {
|
| 280 |
+
'content': chapter_content,
|
| 281 |
+
'status': 'Generated'
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
# Display Chapter Content
|
| 285 |
+
st.subheader(f"Chapter {chapter_number}")
|
| 286 |
+
st.write(chapter_content)
|
| 287 |
+
|
| 288 |
+
# Optional: Edit Chapter
|
| 289 |
+
edited_content = st.text_area(
|
| 290 |
+
"Edit Chapter Content",
|
| 291 |
+
value=chapter_content,
|
| 292 |
+
height=400
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
# Save edited content
|
| 296 |
+
if st.button(f"Save Chapter {chapter_number}"):
|
| 297 |
+
st.session_state.generated_chapters[chapter_number]['content'] = edited_content
|
| 298 |
+
st.success(f"Chapter {chapter_number} saved!")
|
| 299 |
+
|
| 300 |
+
except Exception as e:
|
| 301 |
+
st.error(f"Error generating chapter: {e}")
|
| 302 |
+
|
| 303 |
+
def _render_project_progress(self):
|
| 304 |
+
"""
|
| 305 |
+
Render project progress and tracking interface
|
| 306 |
+
"""
|
| 307 |
+
st.header("📊 Project Progress")
|
| 308 |
+
|
| 309 |
+
# Project Metadata Overview
|
| 310 |
+
st.subheader("Project Overview")
|
| 311 |
+
col1, col2 = st.columns(2)
|
| 312 |
+
|
| 313 |
+
with col1:
|
| 314 |
+
st.metric("Book Title", st.session_state.project_metadata.get('title', 'Untitled'))
|
| 315 |
+
st.metric("Genre", st.session_state.project_metadata.get('genre', 'Unspecified'))
|
| 316 |
+
|
| 317 |
+
with col2:
|
| 318 |
+
total_chapters = st.session_state.project_metadata.get('total_chapters', 0)
|
| 319 |
+
generated_chapters = len(st.session_state.generated_chapters)
|
| 320 |
+
|
| 321 |
+
st.metric("Total Planned Chapters", total_chapters)
|
| 322 |
+
st.metric("Chapters Generated", generated_chapters)
|
| 323 |
+
|
| 324 |
+
# Progress Bar
|
| 325 |
+
progress = generated_chapters / total_chapters if total_chapters > 0 else 0
|
| 326 |
+
st.progress(progress)
|
| 327 |
+
|
| 328 |
+
# Chapter Navigation and Details
|
| 329 |
+
st.subheader("Generated Chapters")
|
| 330 |
+
|
| 331 |
+
# Create tabs for each generated chapter
|
| 332 |
+
if st.session_state.generated_chapters:
|
| 333 |
+
chapter_tabs = st.tabs([
|
| 334 |
+
f"Chapter {ch_num}" for ch_num in sorted(st.session_state.generated_chapters.keys())
|
| 335 |
+
])
|
| 336 |
+
|
| 337 |
+
for i, ch_num in enumerate(sorted(st.session_state.generated_chapters.keys())):
|
| 338 |
+
with chapter_tabs[i]:
|
| 339 |
+
chapter_data = st.session_state.generated_chapters[ch_num]
|
| 340 |
+
st.write(chapter_data['content'])
|
| 341 |
+
|
| 342 |
+
# Chapter status and actions
|
| 343 |
+
col1, col2 = st.columns(2)
|
| 344 |
+
with col1:
|
| 345 |
+
status = st.selectbox(
|
| 346 |
+
"Chapter Status",
|
| 347 |
+
["Generated", "In Review", "Completed"],
|
| 348 |
+
key=f"status_{ch_num}"
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
with col2:
|
| 352 |
+
if st.button(f"Export Chapter {ch_num}"):
|
| 353 |
+
# Export chapter functionality
|
| 354 |
+
export_path = os.path.join('/data', f"chapter_{ch_num}.txt")
|
| 355 |
+
with open(export_path, "w") as f:
|
| 356 |
+
f.write(chapter_data['content'])
|
| 357 |
+
st.success(f"Chapter {ch_num} exported to {export_path}!")
|
| 358 |
+
else:
|
| 359 |
+
st.info("No chapters generated yet. Start writing your book!")
|
| 360 |
|
| 361 |
def run(self):
|
| 362 |
"""
|