File size: 3,874 Bytes
58af945
 
 
 
 
 
 
 
 
 
 
 
 
 
978d7ae
 
 
 
 
 
 
 
 
58af945
 
978d7ae
 
 
 
58af945
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
978d7ae
 
 
58af945
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
92
93
94
95
96
97
98
99
100
import streamlit as st
from src.educational_books.main import BookFlow
import threading
import time

def reset_state():
  st.session_state.book_outline = None
  st.session_state.book = None
  st.session_state.title = None
  st.session_state.book_flow = None
  st.session_state.section_finished = 0
  st.session_state.total_sections = 0

def start_generation(topic):
    book_flow = BookFlow()
    st.session_state.book_flow = book_flow
    st.session_state.error = None
    def run_kickoff():
      try:
        book_flow.kickoff(inputs={"topic": topic})
      except Exception as e:
        st.session_state.error = str(e)
    threading.Thread(target=run_kickoff, daemon=True).start()

def update_state():
  if st.session_state.get("error"):
    st.error(f"Generation failed: {st.session_state.error}")
    st.session_state.generation_started = False
    return
  book_flow = st.session_state.get("book_flow", None)
  if book_flow is not None:
    if hasattr(book_flow.state, "book_outline") and book_flow.state.book_outline and st.session_state.book_outline is None:
      st.session_state.book_outline = book_flow.state.book_outline_md
      st.session_state.title = book_flow.state.title
      st.session_state.total_sections = len(book_flow.state.book_outline)
    if hasattr(book_flow.state, "section_finished") and book_flow.state.section_finished != st.session_state.section_finished:
      st.session_state.section_finished = book_flow.state.section_finished
    if hasattr(book_flow.state, "book") and book_flow.state.book and st.session_state.book is None:
      st.session_state.book = book_flow.state.book_md
      st.session_state.generation_started = False

st.title("Educational Book Generator")
topic = st.text_input("Enter the topic for the book:", "Bubble Sort")

if "generation_started" not in st.session_state:
  st.session_state.generation_started = False
if "book_outline" not in st.session_state:
  st.session_state.book_outline = None
if "book" not in st.session_state:
  st.session_state.book = None
if "title" not in st.session_state:
  st.session_state.title = None
if "book_flow" not in st.session_state:
  st.session_state.book_flow = None
if "section_finished" not in st.session_state:
  st.session_state.section_finished = 0

# Button to start generation
if st.button("Generate Book"):
  st.session_state.generation_started = True
  reset_state()
  start_generation(topic)

# Show "Generating book..." message if the process has started
if st.session_state.generation_started:
  if st.session_state.book_outline is None:
    st.write("Generating book outline... Please wait.")
  elif st.session_state.book is None:
    st.write("Book Outline is ready")
    outline_filename = f"{st.session_state.title.replace(' ', '_')}_outline.md"
    st.download_button(
      label="Download Book Outline",
      data=st.session_state.book_outline,
      file_name=outline_filename,
      mime="text/markdown"
    )
    total = max(1, st.session_state.total_sections or 0)
    st.progress(st.session_state.section_finished / total,
                text=f"Generating book content... {st.session_state.section_finished}/{st.session_state.total_sections}")
elif st.session_state.book_outline is not None and st.session_state.book is not None:
  st.write("Book Outline is ready")
  outline_filename = f"{st.session_state.title.replace(' ', '_')}_outline.md"
  st.download_button(
    label="Download Book Outline",
    data=st.session_state.book_outline,
    file_name=outline_filename,
    mime="text/markdown"
  )
  st.write("Book content is ready")
  content_filename = f"{st.session_state.title.replace(' ', '_')}.md"
  st.download_button(
    label="Download Generated Book",
    data=st.session_state.book,
    file_name=content_filename,
    mime="text/markdown"
  )
if st.session_state.generation_started:
  time.sleep(5)
  update_state()
  st.rerun()