Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,19 +9,14 @@ import json
|
|
| 9 |
import uuid
|
| 10 |
|
| 11 |
from multi_agent_book_workflow import BookWritingOrchestrator
|
| 12 |
-
from persistence_manager import
|
| 13 |
-
HuggingFacePersistenceManager,
|
| 14 |
-
check_huggingface_storage,
|
| 15 |
-
APIConfigManager,
|
| 16 |
-
ResiliентAPIHandler
|
| 17 |
-
)
|
| 18 |
|
| 19 |
class BookWritingApp:
|
| 20 |
def __init__(self):
|
| 21 |
"""
|
| 22 |
Initialize the Streamlit application for book writing
|
| 23 |
|
| 24 |
-
Manages session state
|
| 25 |
"""
|
| 26 |
# Set page configuration
|
| 27 |
st.set_page_config(
|
|
@@ -30,47 +25,23 @@ class BookWritingApp:
|
|
| 30 |
layout="wide"
|
| 31 |
)
|
| 32 |
|
| 33 |
-
# Retrieve API credentials
|
| 34 |
-
self.api_credentials = APIConfigManager.get_api_credentials()
|
| 35 |
-
|
| 36 |
# Initialize persistence manager
|
| 37 |
-
self.persistence_manager =
|
| 38 |
-
|
| 39 |
-
# Verify storage capabilities
|
| 40 |
-
self._verify_storage()
|
| 41 |
|
| 42 |
# Initialize session state
|
| 43 |
self._initialize_session_state()
|
| 44 |
|
| 45 |
-
# Set up project orchestrator
|
| 46 |
self._setup_project_orchestrator()
|
| 47 |
|
| 48 |
-
def _verify_storage(self):
|
| 49 |
-
"""
|
| 50 |
-
Verify Hugging Face storage capabilities
|
| 51 |
-
"""
|
| 52 |
-
storage_check = check_huggingface_storage()
|
| 53 |
-
|
| 54 |
-
# Display warnings if storage is not fully accessible
|
| 55 |
-
if not storage_check['data_dir_exists']:
|
| 56 |
-
st.error("Persistent storage directory not found. Some features may be limited.")
|
| 57 |
-
|
| 58 |
-
if not storage_check['data_dir_writable']:
|
| 59 |
-
st.warning("Cannot write to persistent storage. Data may not be saved.")
|
| 60 |
-
|
| 61 |
-
# Optional: Display free space
|
| 62 |
-
free_space_mb = storage_check['free_space'] / (1024 * 1024)
|
| 63 |
-
st.sidebar.info(f"Available Storage: {free_space_mb:.2f} MB")
|
| 64 |
-
|
| 65 |
def _setup_project_orchestrator(self):
|
| 66 |
"""
|
| 67 |
-
Initialize project orchestrator
|
| 68 |
"""
|
| 69 |
-
# Create project orchestrator using API credentials
|
| 70 |
try:
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
)
|
| 74 |
except Exception as e:
|
| 75 |
st.error(f"Error setting up project orchestrator: {e}")
|
| 76 |
st.session_state.project_orchestrator = None
|
|
@@ -163,11 +134,6 @@ class BookWritingApp:
|
|
| 163 |
"""
|
| 164 |
st.sidebar.header("Project Management")
|
| 165 |
|
| 166 |
-
# API Configuration Display
|
| 167 |
-
st.sidebar.subheader("API Configuration")
|
| 168 |
-
for key, value in self.api_credentials.items():
|
| 169 |
-
st.sidebar.text(f"{key}: {'*' * 8 if value else 'Not Set'}")
|
| 170 |
-
|
| 171 |
# New Project Button
|
| 172 |
if st.sidebar.button("Start New Project"):
|
| 173 |
# Reset session state
|
|
@@ -206,191 +172,7 @@ class BookWritingApp:
|
|
| 206 |
if st.sidebar.button("Save Current Project"):
|
| 207 |
self._save_current_project()
|
| 208 |
|
| 209 |
-
|
| 210 |
-
"""
|
| 211 |
-
Render the main book generation interface
|
| 212 |
-
"""
|
| 213 |
-
# Tabs for different stages of book writing
|
| 214 |
-
tab1, tab2, tab3 = st.tabs([
|
| 215 |
-
"Book Concept",
|
| 216 |
-
"Chapter Generation",
|
| 217 |
-
"Project Progress"
|
| 218 |
-
])
|
| 219 |
-
|
| 220 |
-
with tab1:
|
| 221 |
-
self._render_concept_development()
|
| 222 |
-
|
| 223 |
-
with tab2:
|
| 224 |
-
self._render_chapter_generation()
|
| 225 |
-
|
| 226 |
-
with tab3:
|
| 227 |
-
self._render_project_progress()
|
| 228 |
-
|
| 229 |
-
def _render_concept_development(self):
|
| 230 |
-
"""
|
| 231 |
-
Render the book concept development interface
|
| 232 |
-
"""
|
| 233 |
-
st.header("📘 Book Concept Development")
|
| 234 |
-
|
| 235 |
-
# Initial concept input
|
| 236 |
-
initial_concept = st.text_area(
|
| 237 |
-
"Describe Your Book Idea",
|
| 238 |
-
height=200,
|
| 239 |
-
placeholder="Enter a comprehensive description of your book concept..."
|
| 240 |
-
)
|
| 241 |
-
|
| 242 |
-
if st.button("Generate Book Concept"):
|
| 243 |
-
# Ensure project orchestrator is available
|
| 244 |
-
if not st.session_state.project_orchestrator:
|
| 245 |
-
self._setup_project_orchestrator()
|
| 246 |
-
|
| 247 |
-
with st.spinner("Developing Book Concept..."):
|
| 248 |
-
try:
|
| 249 |
-
# Generate book concept using multi-agent approach
|
| 250 |
-
book_concept = st.session_state.project_orchestrator.generate_book_concept(
|
| 251 |
-
initial_concept
|
| 252 |
-
)
|
| 253 |
-
|
| 254 |
-
# Update session state
|
| 255 |
-
st.session_state.book_concept = book_concept
|
| 256 |
-
st.session_state.project_metadata.update({
|
| 257 |
-
'title': book_concept.get('title', 'Untitled Project'),
|
| 258 |
-
'genre': book_concept.get('genre', 'Unspecified')
|
| 259 |
-
})
|
| 260 |
-
|
| 261 |
-
# Display generated concept
|
| 262 |
-
st.subheader("Generated Book Concept")
|
| 263 |
-
st.json(book_concept)
|
| 264 |
-
|
| 265 |
-
except Exception as e:
|
| 266 |
-
st.error(f"Error generating book concept: {e}")
|
| 267 |
-
|
| 268 |
-
def _render_chapter_generation(self):
|
| 269 |
-
"""
|
| 270 |
-
Render the chapter generation interface
|
| 271 |
-
"""
|
| 272 |
-
st.header("✍️ Chapter Generation")
|
| 273 |
-
|
| 274 |
-
# Check if book concept exists
|
| 275 |
-
if not st.session_state.book_concept:
|
| 276 |
-
st.warning("Please generate a book concept first.")
|
| 277 |
-
return
|
| 278 |
-
|
| 279 |
-
# Chapter generation controls
|
| 280 |
-
col1, col2 = st.columns(2)
|
| 281 |
-
|
| 282 |
-
with col1:
|
| 283 |
-
# Chapter number selection
|
| 284 |
-
max_chapters = 20 # Can be adjusted
|
| 285 |
-
chapter_number = st.number_input(
|
| 286 |
-
"Select Chapter to Generate",
|
| 287 |
-
min_value=1,
|
| 288 |
-
max_value=max_chapters,
|
| 289 |
-
value=len(st.session_state.generated_chapters) + 1
|
| 290 |
-
)
|
| 291 |
-
|
| 292 |
-
with col2:
|
| 293 |
-
# Total chapters planning
|
| 294 |
-
total_chapters = st.number_input(
|
| 295 |
-
"Total Planned Chapters",
|
| 296 |
-
min_value=1,
|
| 297 |
-
max_value=max_chapters,
|
| 298 |
-
value=st.session_state.project_metadata.get('total_chapters', 10)
|
| 299 |
-
)
|
| 300 |
-
st.session_state.project_metadata['total_chapters'] = total_chapters
|
| 301 |
-
|
| 302 |
-
# Generate Chapter Button
|
| 303 |
-
if st.button("Generate Chapter"):
|
| 304 |
-
with st.spinner(f"Generating Chapter {chapter_number}..."):
|
| 305 |
-
try:
|
| 306 |
-
# Generate chapter content
|
| 307 |
-
chapter_content = st.session_state.project_orchestrator.generate_chapter_content(
|
| 308 |
-
st.session_state.book_concept,
|
| 309 |
-
chapter_number
|
| 310 |
-
)
|
| 311 |
-
|
| 312 |
-
# Store generated chapter
|
| 313 |
-
st.session_state.generated_chapters[chapter_number] = {
|
| 314 |
-
'content': chapter_content,
|
| 315 |
-
'status': 'Generated'
|
| 316 |
-
}
|
| 317 |
-
|
| 318 |
-
# Display Chapter Content
|
| 319 |
-
st.subheader(f"Chapter {chapter_number}")
|
| 320 |
-
st.write(chapter_content)
|
| 321 |
-
|
| 322 |
-
# Optional: Edit Chapter
|
| 323 |
-
edited_content = st.text_area(
|
| 324 |
-
"Edit Chapter Content",
|
| 325 |
-
value=chapter_content,
|
| 326 |
-
height=400
|
| 327 |
-
)
|
| 328 |
-
|
| 329 |
-
# Save edited content
|
| 330 |
-
if st.button(f"Save Chapter {chapter_number}"):
|
| 331 |
-
st.session_state.generated_chapters[chapter_number]['content'] = edited_content
|
| 332 |
-
st.success(f"Chapter {chapter_number} saved!")
|
| 333 |
-
|
| 334 |
-
except Exception as e:
|
| 335 |
-
st.error(f"Error generating chapter: {e}")
|
| 336 |
-
|
| 337 |
-
def _render_project_progress(self):
|
| 338 |
-
"""
|
| 339 |
-
Render project progress and tracking interface
|
| 340 |
-
"""
|
| 341 |
-
st.header("📊 Project Progress")
|
| 342 |
-
|
| 343 |
-
# Project Metadata Overview
|
| 344 |
-
st.subheader("Project Overview")
|
| 345 |
-
col1, col2 = st.columns(2)
|
| 346 |
-
|
| 347 |
-
with col1:
|
| 348 |
-
st.metric("Book Title", st.session_state.project_metadata.get('title', 'Untitled'))
|
| 349 |
-
st.metric("Genre", st.session_state.project_metadata.get('genre', 'Unspecified'))
|
| 350 |
-
|
| 351 |
-
with col2:
|
| 352 |
-
total_chapters = st.session_state.project_metadata.get('total_chapters', 0)
|
| 353 |
-
generated_chapters = len(st.session_state.generated_chapters)
|
| 354 |
-
|
| 355 |
-
st.metric("Total Planned Chapters", total_chapters)
|
| 356 |
-
st.metric("Chapters Generated", generated_chapters)
|
| 357 |
-
|
| 358 |
-
# Progress Bar
|
| 359 |
-
progress = generated_chapters / total_chapters if total_chapters > 0 else 0
|
| 360 |
-
st.progress(progress)
|
| 361 |
-
|
| 362 |
-
# Chapter Navigation and Details
|
| 363 |
-
st.subheader("Generated Chapters")
|
| 364 |
-
|
| 365 |
-
# Create tabs for each generated chapter
|
| 366 |
-
if st.session_state.generated_chapters:
|
| 367 |
-
chapter_tabs = st.tabs([
|
| 368 |
-
f"Chapter {ch_num}" for ch_num in sorted(st.session_state.generated_chapters.keys())
|
| 369 |
-
])
|
| 370 |
-
|
| 371 |
-
for i, ch_num in enumerate(sorted(st.session_state.generated_chapters.keys())):
|
| 372 |
-
with chapter_tabs[i]:
|
| 373 |
-
chapter_data = st.session_state.generated_chapters[ch_num]
|
| 374 |
-
st.write(chapter_data['content'])
|
| 375 |
-
|
| 376 |
-
# Chapter status and actions
|
| 377 |
-
col1, col2 = st.columns(2)
|
| 378 |
-
with col1:
|
| 379 |
-
status = st.selectbox(
|
| 380 |
-
"Chapter Status",
|
| 381 |
-
["Generated", "In Review", "Completed"],
|
| 382 |
-
key=f"status_{ch_num}"
|
| 383 |
-
)
|
| 384 |
-
|
| 385 |
-
with col2:
|
| 386 |
-
if st.button(f"Export Chapter {ch_num}"):
|
| 387 |
-
# Export chapter functionality
|
| 388 |
-
export_path = os.path.join('/data', f"chapter_{ch_num}.txt")
|
| 389 |
-
with open(export_path, "w") as f:
|
| 390 |
-
f.write(chapter_data['content'])
|
| 391 |
-
st.success(f"Chapter {ch_num} exported to {export_path}!")
|
| 392 |
-
else:
|
| 393 |
-
st.info("No chapters generated yet. Start writing your book!")
|
| 394 |
|
| 395 |
def run(self):
|
| 396 |
"""
|
|
|
|
| 9 |
import uuid
|
| 10 |
|
| 11 |
from multi_agent_book_workflow import BookWritingOrchestrator
|
| 12 |
+
from persistence_manager import ProjectPersistenceManager
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
class BookWritingApp:
|
| 15 |
def __init__(self):
|
| 16 |
"""
|
| 17 |
Initialize the Streamlit application for book writing
|
| 18 |
|
| 19 |
+
Manages session state and persistence
|
| 20 |
"""
|
| 21 |
# Set page configuration
|
| 22 |
st.set_page_config(
|
|
|
|
| 25 |
layout="wide"
|
| 26 |
)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
# Initialize persistence manager
|
| 29 |
+
self.persistence_manager = ProjectPersistenceManager()
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Initialize session state
|
| 32 |
self._initialize_session_state()
|
| 33 |
|
| 34 |
+
# Set up project orchestrator
|
| 35 |
self._setup_project_orchestrator()
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def _setup_project_orchestrator(self):
|
| 38 |
"""
|
| 39 |
+
Initialize project orchestrator
|
| 40 |
"""
|
|
|
|
| 41 |
try:
|
| 42 |
+
# Create project orchestrator
|
| 43 |
+
# Note: You may need to adjust this based on how you want to handle API keys
|
| 44 |
+
st.session_state.project_orchestrator = BookWritingOrchestrator()
|
| 45 |
except Exception as e:
|
| 46 |
st.error(f"Error setting up project orchestrator: {e}")
|
| 47 |
st.session_state.project_orchestrator = None
|
|
|
|
| 134 |
"""
|
| 135 |
st.sidebar.header("Project Management")
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
# New Project Button
|
| 138 |
if st.sidebar.button("Start New Project"):
|
| 139 |
# Reset session state
|
|
|
|
| 172 |
if st.sidebar.button("Save Current Project"):
|
| 173 |
self._save_current_project()
|
| 174 |
|
| 175 |
+
# ... (rest of the methods remain the same as in the previous implementation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
def run(self):
|
| 178 |
"""
|