cryogenic22 commited on
Commit
4607424
·
verified ·
1 Parent(s): b3c6451

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +42 -81
utils.py CHANGED
@@ -1,18 +1,51 @@
1
  import streamlit as st
 
 
 
 
 
 
 
2
 
3
  def initialize_session_state():
4
  """Initialize all the session state variables needed for the application"""
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  # Main book data
7
  if "book_data" not in st.session_state:
8
- st.session_state.book_data = {
9
- "title": "self.api",
10
- "subtitle": "Accessing the Interface to Your Consciousness",
11
- "author": "",
12
- "chapters": {},
13
- "outline": {},
14
- "current_chapter": None,
15
- }
 
 
 
16
 
17
  # Persona library with system prompts
18
  if "persona_library" not in st.session_state:
@@ -371,76 +404,4 @@ Provide your contribution with these elements:
371
  3. Main content contribution (500-800 words)
372
  4. Suggested system visualizations or models (described in detail)
373
  5. Integration points with other perspectives"""
374
- }
375
- }
376
-
377
- # Organize personas by category
378
- if "persona_categories" not in st.session_state:
379
- st.session_state.persona_categories = {
380
- "Meta": ["meta_agent", "selector_agent"],
381
- "Technical Personas": ["tech_architect", "systems_thinker"],
382
- "Philosophical Personas": ["eastern_philosopher", "western_philosopher"],
383
- "Scientific Personas": ["neuroscientist"],
384
- "Practical Personas": ["meditation_teacher", "pragmatic_implementer"],
385
- "Creative Personas": ["creative_writer"]
386
- }
387
-
388
- # Chapter progress tracking
389
- if "chapter_progress" not in st.session_state:
390
- st.session_state.chapter_progress = {}
391
-
392
- # Agent thinking logs
393
- if "thinking_logs" not in st.session_state:
394
- st.session_state.thinking_logs = []
395
-
396
- def convert_markdown_to_html(markdown_text):
397
- """Convert markdown text to simple HTML for export"""
398
- html_text = markdown_text
399
-
400
- # Headers
401
- html_text = html_text.replace('# ', '<h1>').replace('\n# ', '</h1>\n')
402
- html_text = html_text.replace('## ', '<h2>').replace('\n## ', '</h2>\n')
403
- html_text = html_text.replace('### ', '<h3>').replace('\n### ', '</h3>\n')
404
-
405
- # Emphasis
406
- html_text = html_text.replace('**', '<strong>').replace('**', '</strong>')
407
- html_text = html_text.replace('*', '<em>').replace('*', '</em>')
408
-
409
- # Line breaks
410
- html_text = html_text.replace('\n\n', '<br><br>')
411
-
412
- return html_text
413
-
414
- def get_word_count(text):
415
- """Calculate word count in a text"""
416
- if not text:
417
- return 0
418
- return len(text.split())
419
-
420
- def estimate_reading_time(word_count, wpm=250):
421
- """Estimate reading time in minutes based on word count"""
422
- return max(1, word_count // wpm)
423
-
424
- def calculate_section_word_counts(content):
425
- """Calculate word counts for each section in the content"""
426
- section_counts = {}
427
- current_section = "Introduction"
428
- section_text = ""
429
-
430
- for line in content.split("\n"):
431
- if line.startswith("# ") or line.startswith("## "):
432
- # Save the previous section count
433
- if section_text:
434
- section_counts[current_section] = len(section_text.split())
435
-
436
- # Start a new section
437
- current_section = line.strip("# ")
438
- section_text = ""
439
- else:
440
- section_text += line + " "
441
-
442
- # Add the last section
443
- if section_text:
444
- section_counts[current_section] = len(section_text.split())
445
-
446
- return section_counts
 
1
  import streamlit as st
2
+ import os
3
+ import json
4
+
5
+ # Constants for persistent storage
6
+ PERSISTENT_DIR = "persistent_data"
7
+ BOOK_DATA_FILE = os.path.join(PERSISTENT_DIR, "book_data.json")
8
+ LOGS_FILE = os.path.join(PERSISTENT_DIR, "thinking_logs.json")
9
 
10
  def initialize_session_state():
11
  """Initialize all the session state variables needed for the application"""
12
 
13
+ # Create persistent directory if it doesn't exist
14
+ os.makedirs(PERSISTENT_DIR, exist_ok=True)
15
+
16
+ # Load book data if available
17
+ saved_book_data = None
18
+ try:
19
+ if os.path.exists(BOOK_DATA_FILE):
20
+ with open(BOOK_DATA_FILE, 'r') as f:
21
+ saved_book_data = json.load(f)
22
+ print("Loaded book data from persistent storage")
23
+ except Exception as e:
24
+ print(f"Error loading book data: {str(e)}")
25
+
26
+ # Load thinking logs if available
27
+ saved_thinking_logs = None
28
+ try:
29
+ if os.path.exists(LOGS_FILE):
30
+ with open(LOGS_FILE, 'r') as f:
31
+ saved_thinking_logs = json.load(f)
32
+ print("Loaded thinking logs from persistent storage")
33
+ except Exception as e:
34
+ print(f"Error loading thinking logs: {str(e)}")
35
+
36
  # Main book data
37
  if "book_data" not in st.session_state:
38
+ if saved_book_data:
39
+ st.session_state.book_data = saved_book_data
40
+ else:
41
+ st.session_state.book_data = {
42
+ "title": "self.api",
43
+ "subtitle": "Accessing the Interface to Your Consciousness",
44
+ "author": "",
45
+ "chapters": {},
46
+ "outline": "",
47
+ "current_chapter": None,
48
+ }
49
 
50
  # Persona library with system prompts
51
  if "persona_library" not in st.session_state:
 
404
  3. Main content contribution (500-800 words)
405
  4. Suggested system visualizations or models (described in detail)
406
  5. Integration points with other perspectives"""
407
+ }