Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,14 +6,17 @@
|
|
| 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
|
| 17 |
"""
|
| 18 |
# Set page configuration
|
| 19 |
st.set_page_config(
|
|
@@ -22,217 +25,194 @@ class BookWritingApp:
|
|
| 22 |
layout="wide"
|
| 23 |
)
|
| 24 |
|
| 25 |
-
# Initialize
|
| 26 |
-
|
| 27 |
-
st.session_state.project_orchestrator = None
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
st.session_state.generated_chapters = {}
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
st.session_state.project_metadata = {
|
| 39 |
-
'title': 'Untitled Project',
|
| 40 |
-
'genre': 'Unspecified',
|
| 41 |
-
'total_chapters': 0
|
| 42 |
-
}
|
| 43 |
|
| 44 |
-
def
|
| 45 |
"""
|
| 46 |
-
|
| 47 |
"""
|
| 48 |
-
|
| 49 |
-
tab1, tab2, tab3 = st.tabs([
|
| 50 |
-
"Book Concept",
|
| 51 |
-
"Chapter Generation",
|
| 52 |
-
"Book Progress"
|
| 53 |
-
])
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
-
def
|
| 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 |
-
'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 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
def
|
| 102 |
"""
|
| 103 |
-
|
| 104 |
"""
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 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
|
| 171 |
"""
|
| 172 |
-
|
| 173 |
"""
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
|
| 188 |
-
|
| 189 |
-
st.
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 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 |
-
#
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
-
#
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
def main():
|
| 238 |
# Initialize and run the application
|
|
|
|
| 6 |
import streamlit as st
|
| 7 |
import os
|
| 8 |
import json
|
| 9 |
+
import uuid
|
| 10 |
+
|
| 11 |
from multi_agent_book_workflow import BookWritingOrchestrator
|
| 12 |
+
from persistence_manager import HuggingFacePersistenceManager, check_huggingface_storage
|
| 13 |
|
| 14 |
class BookWritingApp:
|
| 15 |
def __init__(self):
|
| 16 |
"""
|
| 17 |
Initialize the Streamlit application for book writing
|
| 18 |
|
| 19 |
+
Manages session state, persistence, and API resilience
|
| 20 |
"""
|
| 21 |
# Set page configuration
|
| 22 |
st.set_page_config(
|
|
|
|
| 25 |
layout="wide"
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Initialize persistence manager
|
| 29 |
+
self.persistence_manager = HuggingFacePersistenceManager()
|
|
|
|
| 30 |
|
| 31 |
+
# Verify storage capabilities
|
| 32 |
+
self._verify_storage()
|
| 33 |
|
| 34 |
+
# Initialize session state
|
| 35 |
+
self._initialize_session_state()
|
|
|
|
| 36 |
|
| 37 |
+
# Set up resilient API handling
|
| 38 |
+
self._setup_resilient_api_handler()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
def _verify_storage(self):
|
| 41 |
"""
|
| 42 |
+
Verify Hugging Face storage capabilities
|
| 43 |
"""
|
| 44 |
+
storage_check = check_huggingface_storage()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Display warnings if storage is not fully accessible
|
| 47 |
+
if not storage_check['data_dir_exists']:
|
| 48 |
+
st.error("Persistent storage directory not found. Some features may be limited.")
|
| 49 |
|
| 50 |
+
if not storage_check['data_dir_writable']:
|
| 51 |
+
st.warning("Cannot write to persistent storage. Data may not be saved.")
|
| 52 |
|
| 53 |
+
# Optional: Display free space
|
| 54 |
+
free_space_mb = storage_check['free_space'] / (1024 * 1024)
|
| 55 |
+
st.sidebar.info(f"Available Storage: {free_space_mb:.2f} MB")
|
| 56 |
|
| 57 |
+
def _initialize_session_state(self):
|
| 58 |
"""
|
| 59 |
+
Initialize and manage Streamlit session state
|
| 60 |
"""
|
| 61 |
+
# Project tracking
|
| 62 |
+
if 'active_project_id' not in st.session_state:
|
| 63 |
+
st.session_state.active_project_id = None
|
| 64 |
+
|
| 65 |
+
# Restore last active project if exists
|
| 66 |
+
if st.session_state.active_project_id:
|
| 67 |
+
self._load_project(st.session_state.active_project_id)
|
| 68 |
+
|
| 69 |
+
# Ensure core session state variables exist
|
| 70 |
+
session_defaults = {
|
| 71 |
+
'project_orchestrator': None,
|
| 72 |
+
'book_concept': None,
|
| 73 |
+
'generated_chapters': {},
|
| 74 |
+
'project_metadata': {
|
| 75 |
+
'title': 'Untitled Project',
|
| 76 |
+
'genre': 'Unspecified',
|
| 77 |
+
'total_chapters': 0
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
|
| 81 |
+
for key, default in session_defaults.items():
|
| 82 |
+
if key not in st.session_state:
|
| 83 |
+
st.session_state[key] = default
|
| 84 |
+
|
| 85 |
+
def _load_project(self, project_id):
|
| 86 |
+
"""
|
| 87 |
+
Load a project from persistent storage
|
| 88 |
|
| 89 |
+
Args:
|
| 90 |
+
project_id (str): Unique identifier for the project
|
| 91 |
+
"""
|
| 92 |
+
try:
|
| 93 |
+
loaded_project = self.persistence_manager.load_project(project_id)
|
| 94 |
|
| 95 |
+
if loaded_project:
|
| 96 |
+
# Update session state with loaded project
|
| 97 |
+
st.session_state.active_project_id = project_id
|
| 98 |
+
st.session_state.book_concept = loaded_project.get('book_concept')
|
| 99 |
+
st.session_state.generated_chapters = loaded_project.get('chapters', {})
|
| 100 |
+
st.session_state.project_metadata = {
|
| 101 |
+
'title': loaded_project.get('title', 'Untitled Project'),
|
| 102 |
+
'genre': loaded_project.get('genre', 'Unspecified'),
|
| 103 |
+
'total_chapters': loaded_project.get('total_chapters', 0)
|
| 104 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
st.success(f"Loaded project: {loaded_project.get('title', 'Untitled')}")
|
| 107 |
+
else:
|
| 108 |
+
st.warning("Could not load project.")
|
| 109 |
+
|
| 110 |
+
except Exception as e:
|
| 111 |
+
st.error(f"Error loading project: {e}")
|
| 112 |
|
| 113 |
+
def _setup_resilient_api_handler(self):
|
| 114 |
"""
|
| 115 |
+
Set up resilient API handling for different language models
|
| 116 |
"""
|
| 117 |
+
# Initialize with primary and fallback models from workflow
|
| 118 |
+
if not st.session_state.project_orchestrator:
|
| 119 |
+
st.session_state.project_orchestrator = BookWritingOrchestrator()
|
| 120 |
+
|
| 121 |
+
openai_model = st.session_state.project_orchestrator.openai_llm
|
| 122 |
+
anthropic_model = st.session_state.project_orchestrator.anthropic_llm
|
| 123 |
+
|
| 124 |
+
# Create resilient handler (complete implementation needed)
|
| 125 |
+
from persistence_manager import ResiliентAPIHandler
|
| 126 |
+
self.resilient_handler = ResiliентAPIHandler(
|
| 127 |
+
primary_model=openai_model,
|
| 128 |
+
fallback_model=anthropic_model
|
| 129 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
def _save_current_project(self):
|
| 132 |
"""
|
| 133 |
+
Save the current project to persistent storage
|
| 134 |
"""
|
| 135 |
+
try:
|
| 136 |
+
# Prepare project data for saving
|
| 137 |
+
project_data = {
|
| 138 |
+
'project_id': st.session_state.active_project_id or str(uuid.uuid4()),
|
| 139 |
+
'title': st.session_state.project_metadata.get('title', 'Untitled Project'),
|
| 140 |
+
'genre': st.session_state.project_metadata.get('genre', 'Unspecified'),
|
| 141 |
+
'book_concept': st.session_state.book_concept,
|
| 142 |
+
'chapters': st.session_state.generated_chapters,
|
| 143 |
+
'total_chapters': st.session_state.project_metadata.get('total_chapters', 0)
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
# Save project
|
| 147 |
+
project_id = self.persistence_manager.save_project(project_data)
|
| 148 |
|
| 149 |
+
# Update active project ID
|
| 150 |
+
st.session_state.active_project_id = project_id
|
| 151 |
|
| 152 |
+
st.success(f"Project saved with ID: {project_id}")
|
| 153 |
+
|
| 154 |
+
except Exception as e:
|
| 155 |
+
st.error(f"Error saving project: {e}")
|
| 156 |
+
|
| 157 |
+
def render_project_management(self):
|
| 158 |
+
"""
|
| 159 |
+
Render project management interface
|
| 160 |
+
"""
|
| 161 |
+
st.sidebar.header("Project Management")
|
| 162 |
+
|
| 163 |
+
# New Project Button
|
| 164 |
+
if st.sidebar.button("Start New Project"):
|
| 165 |
+
# Reset session state
|
| 166 |
+
st.session_state.active_project_id = None
|
| 167 |
+
st.session_state.book_concept = None
|
| 168 |
+
st.session_state.generated_chapters = {}
|
| 169 |
+
st.session_state.project_metadata = {
|
| 170 |
+
'title': 'Untitled Project',
|
| 171 |
+
'genre': 'Unspecified',
|
| 172 |
+
'total_chapters': 0
|
| 173 |
+
}
|
| 174 |
+
st.experimental_rerun()
|
| 175 |
+
|
| 176 |
+
# Load Project
|
| 177 |
+
project_list = self.persistence_manager.list_projects()
|
| 178 |
+
if project_list:
|
| 179 |
+
selected_project = st.sidebar.selectbox(
|
| 180 |
+
"Load Existing Project",
|
| 181 |
+
[p['title'] for p in project_list],
|
| 182 |
+
index=None,
|
| 183 |
+
placeholder="Select a project..."
|
| 184 |
+
)
|
| 185 |
|
| 186 |
+
if selected_project:
|
| 187 |
+
# Find project ID
|
| 188 |
+
project_id = next(
|
| 189 |
+
p['project_id'] for p in project_list
|
| 190 |
+
if p['title'] == selected_project
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
# Load project
|
| 194 |
+
if st.sidebar.button("Load Selected Project"):
|
| 195 |
+
self._load_project(project_id)
|
| 196 |
+
|
| 197 |
+
# Save Current Project
|
| 198 |
+
if st.sidebar.button("Save Current Project"):
|
| 199 |
+
self._save_current_project()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
def run(self):
|
| 202 |
"""
|
| 203 |
+
Run the main Streamlit application
|
| 204 |
"""
|
| 205 |
+
# Render project management sidebar
|
| 206 |
+
self.render_project_management()
|
| 207 |
+
|
| 208 |
+
# Main application content (placeholder)
|
| 209 |
+
st.title("🚀 AI Book Writing Assistant")
|
| 210 |
|
| 211 |
+
# TODO: Add main application workflow
|
| 212 |
+
# This would include:
|
| 213 |
+
# 1. Book concept development
|
| 214 |
+
# 2. Chapter generation
|
| 215 |
+
# 3. Project progress tracking
|
| 216 |
|
| 217 |
def main():
|
| 218 |
# Initialize and run the application
|